675a06bd6c1c7247c713a9b1af79fc85b40afb27
[utils] /
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 static org.mockito.Mockito.*;
22 import org.wamblee.test.AssertionUtils;
23
24
25 import junit.framework.TestCase;
26
27 public class GraphTest extends TestCase {
28
29     public void testNodeMgt() {
30         final Graph graph = new Graph();
31         assertTrue(graph.getNodes().isEmpty());
32         assertTrue(graph.getEdges().isEmpty());
33
34         final Node x = new DefaultNode("x");
35         graph.addNode(x);
36         assertEquals(Arrays.asList(new Node[] { x }), graph.getNodes());
37         assertSame(x, graph.findNode("x"));
38         assertNull(graph.findNode("y"));
39
40         assertTrue(graph.removeNode(x));
41         assertTrue(graph.getNodes().isEmpty());
42
43         // Empty node set. 
44         assertFalse(graph.removeNode(x));
45         
46         Node y = new DefaultNode("y"); 
47         graph.addNodes(Arrays.asList(new Node[] { x, y} ));
48         assertEquals(2, graph.getNodes().size());
49         
50         // duplicate node
51         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() { 
52             @Override
53             public void run() throws Exception {
54                 graph.addNode(new DefaultNode("x"));   
55             }
56         }, IllegalArgumentException.class);
57     }
58
59     public void testEdgeMgt() { 
60         final Graph graph = new Graph();
61         final Node x = new DefaultNode("x"); 
62         final Node y = new DefaultNode("y"); 
63         graph.addNode(x);
64         graph.addNode(y);
65         final Edge e = new DefaultEdge(x, y); 
66         graph.addEdge(e);
67         assertEquals(Arrays.asList(new Edge[] { e }), graph.getEdges());
68         
69         // duplicate edge.
70         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() { 
71             @Override
72             public void run() throws Exception {
73                 graph.addEdge(e);   
74             }
75         }, IllegalArgumentException.class);
76         
77         // Remove node when edge is still present
78         AssertionUtils.assertException(new AssertionUtils.ErroneousCode() { 
79             @Override
80             public void run() throws Exception {
81                 graph.removeNode(x);
82             }
83         }, IllegalArgumentException.class);
84         
85         
86         Node a = new DefaultNode("a");
87         graph.addNode(a);
88         graph.addEdges(Arrays.asList(new Edge[] { new DefaultEdge(x, a), new DefaultEdge(y, a) }));
89         assertEquals(3, graph.getEdges().size());
90     }
91     
92     public void testExtend() { 
93         Graph graph = new Graph(); 
94         graph.addNode(new MyNode("x", new String[] { "a", "b"}));
95         graph.addNode(new MyNode("y", new String[] { "b", "c"}));
96         graph.addNode(new MyNode("z", new String[] { "a", "c"}));
97         graph.extend(new MyEdgeFactory());
98         
99         List<Edge> edges = graph.getEdges(); 
100         assertEquals(12, edges.size()); // 2 outgoing and 2 incoming nodes.
101     }
102     
103     public void testApplyFilter() { 
104         Graph graph = new Graph(); 
105         graph.addNode(new DefaultNode("x"));
106         graph.addNode(new DefaultNode("y"));
107         graph.addNode(new DefaultNode("z"));
108         graph.addEdge(new DefaultEdge(graph.findNode("x"), graph.findNode("y")));
109         graph.addEdge(new DefaultEdge(graph.findNode("y"), graph.findNode("z")));
110         graph.addEdge(new DefaultEdge(graph.findNode("z"), graph.findNode("x")));
111         
112         assertEquals(3, graph.getEdges().size());
113         
114         graph.applyFilter(new EdgeFilter() { 
115             @Override
116             public boolean isViolated(Edge aEdge) {
117                 if (aEdge.getFrom().getName().equals("x")) { 
118                     return false; 
119                 }
120                 return true; 
121             }
122         }); 
123         
124         assertEquals(1, graph.getEdges().size());
125         assertEquals("x", graph.getEdges().get(0).getFrom().getName());
126            
127     }
128     
129     public void testFindIncomingOutgoing() { 
130         Graph graph = new Graph();
131         Node x = new DefaultNode("x");
132         Node y = new DefaultNode("y"); 
133         graph.addNode(x);
134         graph.addNode(y);
135         Edge e = new DefaultEdge(x,y);
136         graph.addEdge(e);
137         
138         List<Edge> incoming = graph.findIncoming(x);
139         assertTrue(incoming.isEmpty()); 
140         List<Edge> outgoing = graph.findOutgoing(x);
141         assertEquals(1, outgoing.size());
142         assertSame(e, outgoing.get(0));
143         
144         incoming = graph.findIncoming(y);
145         assertEquals(1, incoming.size());
146         assertSame(e, incoming.get(0));
147            
148         outgoing = graph.findOutgoing(y);
149         assertTrue(outgoing.isEmpty()); 
150     }
151     
152     public void testAccept() {
153         Graph graph = new Graph(); 
154         Node x = new DefaultNode("x"); 
155         Node y = new DefaultNode("y"); 
156         Edge e = new DefaultEdge(x, y);
157         graph.addNode(x);
158         graph.addNode(y);
159         graph.addEdge(e);
160         Visitor visitor = mock(Visitor.class); 
161         
162         graph.accept(visitor);
163         verify(visitor).visitNode(x);
164         verify(visitor).visitNode(y);
165         verify(visitor).visitEdge(e);
166         
167         verifyNoMoreInteractions(visitor);
168     }
169 }