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 org.easymock.classextension.EasyMock;
22 import org.wamblee.test.AssertionUtils;
24 import junit.framework.TestCase;
26 public class GraphTest extends TestCase {
28 public void testNodeMgt() {
29 final Graph graph = new Graph();
30 assertTrue(graph.getNodes().isEmpty());
31 assertTrue(graph.getEdges().isEmpty());
33 final Node x = new DefaultNode("x");
35 assertEquals(Arrays.asList(new Node[] { x }), graph.getNodes());
36 assertSame(x, graph.findNode("x"));
37 assertNull(graph.findNode("y"));
39 assertTrue(graph.removeNode(x));
40 assertTrue(graph.getNodes().isEmpty());
43 assertFalse(graph.removeNode(x));
45 Node y = new DefaultNode("y");
46 graph.addNodes(Arrays.asList(new Node[] { x, y} ));
47 assertEquals(2, graph.getNodes().size());
50 AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
52 public void run() throws Exception {
53 graph.addNode(new DefaultNode("x"));
55 }, IllegalArgumentException.class);
58 public void testEdgeMgt() {
59 final Graph graph = new Graph();
60 final Node x = new DefaultNode("x");
61 final Node y = new DefaultNode("y");
64 final Edge e = new DefaultEdge(x, y);
66 assertEquals(Arrays.asList(new Edge[] { e }), graph.getEdges());
69 AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
71 public void run() throws Exception {
74 }, IllegalArgumentException.class);
76 // Remove node when edge is still present
77 AssertionUtils.assertException(new AssertionUtils.ErroneousCode() {
79 public void run() throws Exception {
82 }, IllegalArgumentException.class);
85 Node a = new DefaultNode("a");
87 graph.addEdges(Arrays.asList(new Edge[] { new DefaultEdge(x, a), new DefaultEdge(y, a) }));
88 assertEquals(3, graph.getEdges().size());
91 public void testExtend() {
92 Graph graph = new Graph();
93 graph.addNode(new MyNode("x", new String[] { "a", "b"}));
94 graph.addNode(new MyNode("y", new String[] { "b", "c"}));
95 graph.addNode(new MyNode("z", new String[] { "a", "c"}));
96 graph.extend(new MyEdgeFactory());
98 List<Edge> edges = graph.getEdges();
99 assertEquals(12, edges.size()); // 2 outgoing and 2 incoming nodes.
102 public void testApplyFilter() {
103 Graph graph = new Graph();
104 graph.addNode(new DefaultNode("x"));
105 graph.addNode(new DefaultNode("y"));
106 graph.addNode(new DefaultNode("z"));
107 graph.addEdge(new DefaultEdge(graph.findNode("x"), graph.findNode("y")));
108 graph.addEdge(new DefaultEdge(graph.findNode("y"), graph.findNode("z")));
109 graph.addEdge(new DefaultEdge(graph.findNode("z"), graph.findNode("x")));
111 assertEquals(3, graph.getEdges().size());
113 graph.applyFilter(new EdgeFilter() {
115 public boolean isViolated(Edge aEdge) {
116 if (aEdge.getFrom().getName().equals("x")) {
123 assertEquals(1, graph.getEdges().size());
124 assertEquals("x", graph.getEdges().get(0).getFrom().getName());
128 public void testFindIncomingOutgoing() {
129 Graph graph = new Graph();
130 Node x = new DefaultNode("x");
131 Node y = new DefaultNode("y");
134 Edge e = new DefaultEdge(x,y);
137 List<Edge> incoming = graph.findIncoming(x);
138 assertTrue(incoming.isEmpty());
139 List<Edge> outgoing = graph.findOutgoing(x);
140 assertEquals(1, outgoing.size());
141 assertSame(e, outgoing.get(0));
143 incoming = graph.findIncoming(y);
144 assertEquals(1, incoming.size());
145 assertSame(e, incoming.get(0));
147 outgoing = graph.findOutgoing(y);
148 assertTrue(outgoing.isEmpty());
151 public void testAccept() {
152 Graph graph = new Graph();
153 Node x = new DefaultNode("x");
154 Node y = new DefaultNode("y");
155 Edge e = new DefaultEdge(x, y);
159 Visitor visitor = EasyMock.createMock(Visitor.class);
160 visitor.visitNode(x);
161 visitor.visitNode(y);
162 visitor.visitEdge(e);
163 EasyMock.replay(visitor);
164 graph.accept(visitor);
165 EasyMock.verify(visitor);