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 java.util.Arrays;
19 import java.util.List;
21 import static org.mockito.Mockito.*;
22 import org.wamblee.test.AssertionUtils;
25 import junit.framework.TestCase;
27 public class GraphTest extends TestCase {
29 public void testNodeMgt() {
30 final Graph graph = new Graph();
31 assertTrue(graph.getNodes().isEmpty());
32 assertTrue(graph.getEdges().isEmpty());
34 final Node x = new DefaultNode("x");
36 assertEquals(Arrays.asList(new Node[] { x }), graph.getNodes());
37 assertSame(x, graph.findNode("x"));
38 assertNull(graph.findNode("y"));
40 assertTrue(graph.removeNode(x));
41 assertTrue(graph.getNodes().isEmpty());
44 assertFalse(graph.removeNode(x));
46 Node y = new DefaultNode("y");
47 graph.addNodes(Arrays.asList(new Node[] { x, y} ));
48 assertEquals(2, graph.getNodes().size());
51 AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
53 public void run() throws Exception {
54 graph.addNode(new DefaultNode("x"));
56 }, IllegalArgumentException.class);
59 public void testEdgeMgt() {
60 final Graph graph = new Graph();
61 final Node x = new DefaultNode("x");
62 final Node y = new DefaultNode("y");
65 final Edge e = new DefaultEdge(x, y);
67 assertEquals(Arrays.asList(new Edge[] { e }), graph.getEdges());
70 AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
72 public void run() throws Exception {
75 }, IllegalArgumentException.class);
77 // Remove node when edge is still present
78 AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
80 public void run() throws Exception {
83 }, IllegalArgumentException.class);
86 Node a = new DefaultNode("a");
88 graph.addEdges(Arrays.asList(new Edge[] { new DefaultEdge(x, a), new DefaultEdge(y, a) }));
89 assertEquals(3, graph.getEdges().size());
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());
99 List<Edge> edges = graph.getEdges();
100 assertEquals(12, edges.size()); // 2 outgoing and 2 incoming nodes.
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")));
112 assertEquals(3, graph.getEdges().size());
114 graph.applyFilter(new EdgeFilter() {
116 public boolean isViolated(Edge aEdge) {
117 if (aEdge.getFrom().getName().equals("x")) {
124 assertEquals(1, graph.getEdges().size());
125 assertEquals("x", graph.getEdges().get(0).getFrom().getName());
129 public void testFindIncomingOutgoing() {
130 Graph graph = new Graph();
131 Node x = new DefaultNode("x");
132 Node y = new DefaultNode("y");
135 Edge e = new DefaultEdge(x,y);
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));
144 incoming = graph.findIncoming(y);
145 assertEquals(1, incoming.size());
146 assertSame(e, incoming.get(0));
148 outgoing = graph.findOutgoing(y);
149 assertTrue(outgoing.isEmpty());
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);
160 Visitor visitor = mock(Visitor.class);
162 graph.accept(visitor);
163 verify(visitor).visitNode(x);
164 verify(visitor).visitNode(y);
165 verify(visitor).visitEdge(e);
167 verifyNoMoreInteractions(visitor);