X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=system%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fsystem%2Fgraph%2FGraphTest.java;fp=system%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fsystem%2Fgraph%2FGraphTest.java;h=305765f90670da4abb60d599083d5f62bba6edbf;hb=436718e7b7ee0bb9f37db496dbde5c011d5f84e3;hp=0000000000000000000000000000000000000000;hpb=b15dcce03ecf71e9fa5f429187ff23f6d1af74fa;p=utils diff --git a/system/general/src/test/java/org/wamblee/system/graph/GraphTest.java b/system/general/src/test/java/org/wamblee/system/graph/GraphTest.java new file mode 100644 index 00000000..305765f9 --- /dev/null +++ b/system/general/src/test/java/org/wamblee/system/graph/GraphTest.java @@ -0,0 +1,141 @@ +/* + * Copyright 2008 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.wamblee.system.graph; + +import java.util.Arrays; +import java.util.List; + +import org.easymock.classextension.EasyMock; +import org.wamblee.test.AssertionUtils; + +import junit.framework.TestCase; + +public class GraphTest extends TestCase { + + public void testNodeMgt() { + final Graph graph = new Graph(); + assertTrue(graph.getNodes().isEmpty()); + assertTrue(graph.getEdges().isEmpty()); + + final Node x = new DefaultNode("x"); + graph.addNode(x); + assertEquals(Arrays.asList(new Node[] { x }), graph.getNodes()); + assertSame(x, graph.findNode("x")); + assertNull(graph.findNode("y")); + + assertTrue(graph.removeNode(x)); + assertTrue(graph.getNodes().isEmpty()); + + // Empty node set. + assertFalse(graph.removeNode(x)); + + Node y = new DefaultNode("y"); + graph.addNodes(Arrays.asList(new Node[] { x, y} )); + assertEquals(2, graph.getNodes().size()); + + // duplicate node + AssertionUtils.assertException(new AssertionUtils.ErroneousCode() { + @Override + public void run() throws Exception { + graph.addNode(new DefaultNode("x")); + } + }, IllegalArgumentException.class); + } + + public void testEdgeMgt() { + final Graph graph = new Graph(); + final Node x = new DefaultNode("x"); + final Node y = new DefaultNode("y"); + graph.addNode(x); + graph.addNode(y); + final Edge e = new DefaultEdge(x, y); + graph.addEdge(e); + assertEquals(Arrays.asList(new Edge[] { e }), graph.getEdges()); + + // duplicate edge. + AssertionUtils.assertException(new AssertionUtils.ErroneousCode() { + @Override + public void run() throws Exception { + graph.addEdge(e); + } + }, IllegalArgumentException.class); + + // Remove node when edge is still present + AssertionUtils.assertException(new AssertionUtils.ErroneousCode() { + @Override + public void run() throws Exception { + graph.removeNode(x); + } + }, IllegalArgumentException.class); + + + Node a = new DefaultNode("a"); + graph.addNode(a); + graph.addEdges(Arrays.asList(new Edge[] { new DefaultEdge(x, a), new DefaultEdge(y, a) })); + assertEquals(3, graph.getEdges().size()); + } + + public void testExtend() { + Graph graph = new Graph(); + graph.addNode(new MyNode("x", new String[] { "a", "b"})); + graph.addNode(new MyNode("y", new String[] { "b", "c"})); + graph.addNode(new MyNode("z", new String[] { "a", "c"})); + graph.extend(new MyEdgeFactory()); + + List edges = graph.getEdges(); + assertEquals(12, edges.size()); // 2 outgoing and 2 incoming nodes. + } + + public void testFindIncomingOutgoing() { + Graph graph = new Graph(); + Node x = new DefaultNode("x"); + Node y = new DefaultNode("y"); + graph.addNode(x); + graph.addNode(y); + Edge e = new DefaultEdge(x,y); + graph.addEdge(e); + + List incoming = graph.findIncoming(x); + assertTrue(incoming.isEmpty()); + List outgoing = graph.findOutgoing(x); + assertEquals(1, outgoing.size()); + assertSame(e, outgoing.get(0)); + + incoming = graph.findIncoming(y); + assertEquals(1, incoming.size()); + assertSame(e, incoming.get(0)); + + outgoing = graph.findOutgoing(y); + assertTrue(outgoing.isEmpty()); + } + + public void testAccept() { + Graph graph = new Graph(); + Node x = new DefaultNode("x"); + Node y = new DefaultNode("y"); + Edge e = new DefaultEdge(x, y); + graph.addNode(x); + graph.addNode(y); + graph.addEdge(e); + Visitor visitor = EasyMock.createMock(Visitor.class); + visitor.visitNode(x); + visitor.visitNode(y); + visitor.visitEdge(e); + EasyMock.replay(visitor); + graph.accept(visitor); + EasyMock.verify(visitor); + } +}