2 * Copyright 2008 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.system.graph;
18 import junit.framework.TestCase;
19 import static org.mockito.Mockito.*;
21 import org.wamblee.test.AssertionUtils;
23 import java.util.Arrays;
24 import java.util.List;
33 public class GraphTest extends TestCase {
37 public void testNodeMgt() {
38 final Graph graph = new Graph();
39 assertTrue(graph.getNodes().isEmpty());
40 assertTrue(graph.getEdges().isEmpty());
42 final Node x = new DefaultNode("x");
44 assertEquals(Arrays.asList(new Node[] { x }), graph.getNodes());
45 assertSame(x, graph.findNode("x"));
46 assertNull(graph.findNode("y"));
48 assertTrue(graph.removeNode(x));
49 assertTrue(graph.getNodes().isEmpty());
52 assertFalse(graph.removeNode(x));
54 Node y = new DefaultNode("y");
55 graph.addNodes(Arrays.asList(new Node[] { x, y }));
56 assertEquals(2, graph.getNodes().size());
59 AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
61 public void run() throws Exception {
62 graph.addNode(new DefaultNode("x"));
64 }, IllegalArgumentException.class);
70 public void testEdgeMgt() {
71 final Graph graph = new Graph();
72 final Node x = new DefaultNode("x");
73 final Node y = new DefaultNode("y");
77 final Edge e = new DefaultEdge(x, y);
79 assertEquals(Arrays.asList(new Edge[] { e }), graph.getEdges());
82 AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
84 public void run() throws Exception {
87 }, IllegalArgumentException.class);
89 // Remove node when edge is still present
90 AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
92 public void run() throws Exception {
95 }, IllegalArgumentException.class);
97 Node a = new DefaultNode("a");
99 graph.addEdges(Arrays.asList(
100 new Edge[] { new DefaultEdge(x, a), new DefaultEdge(y, a) }));
101 assertEquals(3, graph.getEdges().size());
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());
114 List<Edge> edges = graph.getEdges();
115 assertEquals(12, edges.size()); // 2 outgoing and 2 incoming nodes.
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")));
130 assertEquals(3, graph.getEdges().size());
132 graph.applyFilter(new EdgeFilter() {
134 public boolean isViolated(Edge aEdge) {
135 if (aEdge.getFrom().getName().equals("x")) {
143 assertEquals(1, graph.getEdges().size());
144 assertEquals("x", graph.getEdges().get(0).getFrom().getName());
150 public void testFindIncomingOutgoing() {
151 Graph graph = new Graph();
152 Node x = new DefaultNode("x");
153 Node y = new DefaultNode("y");
157 Edge e = new DefaultEdge(x, y);
160 List<Edge> incoming = graph.findIncoming(x);
161 assertTrue(incoming.isEmpty());
163 List<Edge> outgoing = graph.findOutgoing(x);
164 assertEquals(1, outgoing.size());
165 assertSame(e, outgoing.get(0));
167 incoming = graph.findIncoming(y);
168 assertEquals(1, incoming.size());
169 assertSame(e, incoming.get(0));
171 outgoing = graph.findOutgoing(y);
172 assertTrue(outgoing.isEmpty());
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);
187 Visitor visitor = mock(Visitor.class);
189 graph.accept(visitor);
190 verify(visitor).visitNode(x);
191 verify(visitor).visitNode(y);
192 verify(visitor).visitEdge(e);
194 verifyNoMoreInteractions(visitor);