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