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