2 * Copyright 2005-2010 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;
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());
37 final Node x = new DefaultNode("x");
39 assertEquals(Arrays.asList(new Node[] { x }), graph.getNodes());
40 assertSame(x, graph.findNode("x"));
41 assertNull(graph.findNode("y"));
43 assertTrue(graph.removeNode(x));
44 assertTrue(graph.getNodes().isEmpty());
47 assertFalse(graph.removeNode(x));
49 Node y = new DefaultNode("y");
50 graph.addNodes(Arrays.asList(new Node[] { x, y }));
51 assertEquals(2, graph.getNodes().size());
54 AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
56 public void run() throws Exception {
57 graph.addNode(new DefaultNode("x"));
59 }, IllegalArgumentException.class);
62 public void testEdgeMgt() {
63 final Graph graph = new Graph();
64 final Node x = new DefaultNode("x");
65 final Node y = new DefaultNode("y");
69 final Edge e = new DefaultEdge(x, y);
71 assertEquals(Arrays.asList(new Edge[] { e }), graph.getEdges());
74 AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
76 public void run() throws Exception {
79 }, IllegalArgumentException.class);
81 // Remove node when edge is still present
82 AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
84 public void run() throws Exception {
87 }, IllegalArgumentException.class);
89 Node a = new DefaultNode("a");
91 graph.addEdges(Arrays.asList(new Edge[] { new DefaultEdge(x, a),
92 new DefaultEdge(y, a) }));
93 assertEquals(3, graph.getEdges().size());
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());
103 List<Edge> edges = graph.getEdges();
104 assertEquals(12, edges.size()); // 2 outgoing and 2 incoming nodes.
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"));
113 .addEdge(new DefaultEdge(graph.findNode("x"), graph.findNode("y")));
115 .addEdge(new DefaultEdge(graph.findNode("y"), graph.findNode("z")));
117 .addEdge(new DefaultEdge(graph.findNode("z"), graph.findNode("x")));
119 assertEquals(3, graph.getEdges().size());
121 graph.applyFilter(new EdgeFilter() {
123 public boolean isViolated(Edge aEdge) {
124 if (aEdge.getFrom().getName().equals("x")) {
132 assertEquals(1, graph.getEdges().size());
133 assertEquals("x", graph.getEdges().get(0).getFrom().getName());
136 public void testFindIncomingOutgoing() {
137 Graph graph = new Graph();
138 Node x = new DefaultNode("x");
139 Node y = new DefaultNode("y");
143 Edge e = new DefaultEdge(x, y);
146 List<Edge> incoming = graph.findIncoming(x);
147 assertTrue(incoming.isEmpty());
149 List<Edge> outgoing = graph.findOutgoing(x);
150 assertEquals(1, outgoing.size());
151 assertSame(e, outgoing.get(0));
153 incoming = graph.findIncoming(y);
154 assertEquals(1, incoming.size());
155 assertSame(e, incoming.get(0));
157 outgoing = graph.findOutgoing(y);
158 assertTrue(outgoing.isEmpty());
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);
170 Visitor visitor = mock(Visitor.class);
172 graph.accept(visitor);
173 verify(visitor).visitNode(x);
174 verify(visitor).visitNode(y);
175 verify(visitor).visitEdge(e);
177 verifyNoMoreInteractions(visitor);