7a5a87971204255aa1c945af36b5b972fc826a04
[utils] / system / general / src / test / java / org / wamblee / system / graph / GraphTest.java
1 /*
2  * Copyright 2008 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.system.graph;
17
18 import java.util.Arrays;
19 import java.util.List;
20
21 import org.easymock.classextension.EasyMock;
22 import org.wamblee.test.AssertionUtils;
23
24 import junit.framework.TestCase;
25
26 public class GraphTest extends TestCase {
27
28     public void testNodeMgt() {
29         final Graph graph = new Graph();
30         assertTrue(graph.getNodes().isEmpty());
31         assertTrue(graph.getEdges().isEmpty());
32
33         final Node x = new DefaultNode("x");
34         graph.addNode(x);
35         assertEquals(Arrays.asList(new Node[] { x }), graph.getNodes());
36         assertSame(x, graph.findNode("x"));
37         assertNull(graph.findNode("y"));
38
39         assertTrue(graph.removeNode(x));
40         assertTrue(graph.getNodes().isEmpty());
41
42         // Empty node set. 
43         assertFalse(graph.removeNode(x));
44         
45         Node y = new DefaultNode("y"); 
46         graph.addNodes(Arrays.asList(new Node[] { x, y} ));
47         assertEquals(2, graph.getNodes().size());
48         
49         // duplicate node
50         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() { 
51             @Override
52             public void run() throws Exception {
53                 graph.addNode(new DefaultNode("x"));   
54             }
55         }, IllegalArgumentException.class);
56     }
57
58     public void testEdgeMgt() { 
59         final Graph graph = new Graph();
60         final Node x = new DefaultNode("x"); 
61         final Node y = new DefaultNode("y"); 
62         graph.addNode(x);
63         graph.addNode(y);
64         final Edge e = new DefaultEdge(x, y); 
65         graph.addEdge(e);
66         assertEquals(Arrays.asList(new Edge[] { e }), graph.getEdges());
67         
68         // duplicate edge.
69         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() { 
70             @Override
71             public void run() throws Exception {
72                 graph.addEdge(e);   
73             }
74         }, IllegalArgumentException.class);
75         
76         // Remove node when edge is still present
77         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() { 
78             @Override
79             public void run() throws Exception {
80                 graph.removeNode(x);
81             }
82         }, IllegalArgumentException.class);
83         
84         
85         Node a = new DefaultNode("a");
86         graph.addNode(a);
87         graph.addEdges(Arrays.asList(new Edge[] { new DefaultEdge(x, a), new DefaultEdge(y, a) }));
88         assertEquals(3, graph.getEdges().size());
89     }
90     
91     public void testExtend() { 
92         Graph graph = new Graph(); 
93         graph.addNode(new MyNode("x", new String[] { "a", "b"}));
94         graph.addNode(new MyNode("y", new String[] { "b", "c"}));
95         graph.addNode(new MyNode("z", new String[] { "a", "c"}));
96         graph.extend(new MyEdgeFactory());
97         
98         List<Edge> edges = graph.getEdges(); 
99         assertEquals(12, edges.size()); // 2 outgoing and 2 incoming nodes.
100     }
101     
102     public void testApplyFilter() { 
103         Graph graph = new Graph(); 
104         graph.addNode(new DefaultNode("x"));
105         graph.addNode(new DefaultNode("y"));
106         graph.addNode(new DefaultNode("z"));
107         graph.addEdge(new DefaultEdge(graph.findNode("x"), graph.findNode("y")));
108         graph.addEdge(new DefaultEdge(graph.findNode("y"), graph.findNode("z")));
109         graph.addEdge(new DefaultEdge(graph.findNode("z"), graph.findNode("x")));
110         
111         assertEquals(3, graph.getEdges().size());
112         
113         graph.applyFilter(new EdgeFilter() { 
114             @Override
115             public boolean isViolated(Edge aEdge) {
116                 if (aEdge.getFrom().getName().equals("x")) { 
117                     return false; 
118                 }
119                 return true; 
120             }
121         }); 
122         
123         assertEquals(1, graph.getEdges().size());
124         assertEquals("x", graph.getEdges().get(0).getFrom().getName());
125            
126     }
127     
128     public void testFindIncomingOutgoing() { 
129         Graph graph = new Graph();
130         Node x = new DefaultNode("x");
131         Node y = new DefaultNode("y"); 
132         graph.addNode(x);
133         graph.addNode(y);
134         Edge e = new DefaultEdge(x,y);
135         graph.addEdge(e);
136         
137         List<Edge> incoming = graph.findIncoming(x);
138         assertTrue(incoming.isEmpty()); 
139         List<Edge> outgoing = graph.findOutgoing(x);
140         assertEquals(1, outgoing.size());
141         assertSame(e, outgoing.get(0));
142         
143         incoming = graph.findIncoming(y);
144         assertEquals(1, incoming.size());
145         assertSame(e, incoming.get(0));
146            
147         outgoing = graph.findOutgoing(y);
148         assertTrue(outgoing.isEmpty()); 
149     }
150     
151     public void testAccept() {
152         Graph graph = new Graph(); 
153         Node x = new DefaultNode("x"); 
154         Node y = new DefaultNode("y"); 
155         Edge e = new DefaultEdge(x, y);
156         graph.addNode(x);
157         graph.addNode(y);
158         graph.addEdge(e);
159         Visitor visitor = EasyMock.createMock(Visitor.class);
160         visitor.visitNode(x);
161         visitor.visitNode(y);
162         visitor.visitEdge(e);
163         EasyMock.replay(visitor);
164         graph.accept(visitor);
165         EasyMock.verify(visitor);
166     }
167 }