001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.lib.graph;
020:
021: import java.util.Collection;
022: import java.util.Iterator;
023:
024: import org.apache.openjpa.lib.test.AbstractTestCase;
025:
026: /**
027: * <p>Tests the {@link Graph} type, and in so doing implicitly tests the
028: * {@link Edge} as well.</p>
029: *
030: * @author Abe White
031: */
032: public class TestGraph extends AbstractTestCase {
033:
034: private Graph _graph = new Graph();
035: private Object _node1 = new Object();
036: private Object _node2 = new Object();
037: private Object _node3 = new Object();
038: private Edge _edge1 = new Edge(_node1, _node2, true);
039: private Edge _edge2 = new Edge(_node2, _node3, true);
040: private Edge _edge3 = new Edge(_node1, _node3, false);
041: private Edge _edge4 = new Edge(_node2, _node2, false);
042:
043: public void setUp() {
044: _graph.addNode(_node1);
045: _graph.addNode(_node2);
046: _graph.addNode(_node3);
047: _graph.addEdge(_edge1);
048: _graph.addEdge(_edge2);
049: _graph.addEdge(_edge3);
050: _graph.addEdge(_edge4);
051: }
052:
053: /**
054: * Tests adding and retrieving nodes and edges.
055: */
056: public void testAddRetrieve() {
057: assertEquals(3, _graph.getNodes().size());
058: assertEquals(4, _graph.getEdges().size());
059:
060: Collection edges = _graph.getEdgesFrom(_node1);
061: assertEquals(2, edges.size());
062: Iterator itr = edges.iterator();
063: Edge edge0 = (Edge) itr.next();
064: Edge edge1 = (Edge) itr.next();
065: assertTrue((edge0 == _edge1 && edge1 == _edge3)
066: || (edge0 == _edge3 && edge1 == _edge1));
067:
068: edges = _graph.getEdgesTo(_node1);
069: assertEquals(1, edges.size());
070: assertEquals(_edge3, edges.iterator().next());
071:
072: edges = _graph.getEdges(_node1, _node3);
073: assertEquals(1, edges.size());
074: assertEquals(_edge3, edges.iterator().next());
075: edges = _graph.getEdges(_node3, _node1);
076: assertEquals(1, edges.size());
077: assertEquals(_edge3, edges.iterator().next());
078:
079: edges = _graph.getEdgesFrom(_node2);
080: assertEquals(2, edges.size());
081: itr = edges.iterator();
082: edge0 = (Edge) itr.next();
083: edge1 = (Edge) itr.next();
084: assertTrue((edge0 == _edge2 && edge1 == _edge4)
085: || (edge0 == _edge4 && edge1 == _edge2));
086:
087: edges = _graph.getEdgesTo(_node2);
088: assertEquals(2, edges.size());
089: itr = edges.iterator();
090: edge0 = (Edge) itr.next();
091: edge1 = (Edge) itr.next();
092: assertTrue((edge0 == _edge1 && edge1 == _edge4)
093: || (edge0 == _edge4 && edge1 == _edge1));
094:
095: edges = _graph.getEdges(_node2, _node2);
096: assertEquals(1, edges.size());
097: assertEquals(_edge4, edges.iterator().next());
098:
099: edges = _graph.getEdgesFrom(_node3);
100: assertEquals(1, edges.size());
101: assertEquals(_edge3, edges.iterator().next());
102: }
103:
104: /**
105: * Test removing edges.
106: */
107: public void testRemoveEdges() {
108: assertTrue(_graph.removeEdge(_edge2));
109: Collection edges = _graph.getEdgesFrom(_node2);
110: assertEquals(1, edges.size());
111: assertEquals(_edge4, edges.iterator().next());
112:
113: assertTrue(_graph.removeEdge(_edge3));
114: edges = _graph.getEdgesFrom(_node1);
115: assertEquals(1, edges.size());
116: assertEquals(_edge1, edges.iterator().next());
117: edges = _graph.getEdgesTo(_node1);
118: assertEquals(0, edges.size());
119: edges = _graph.getEdgesTo(_node3);
120: assertEquals(0, edges.size());
121: edges = _graph.getEdgesFrom(_node3);
122: assertEquals(0, edges.size());
123: }
124:
125: /**
126: * Test removing nodes.
127: */
128: public void testRemoveNodes() {
129: assertTrue(_graph.removeNode(_node3));
130: Collection edges = _graph.getEdges();
131: assertEquals(2, edges.size());
132: Iterator itr = edges.iterator();
133: Edge edge0 = (Edge) itr.next();
134: Edge edge1 = (Edge) itr.next();
135: assertTrue((edge0 == _edge1 && edge1 == _edge4)
136: || (edge0 == _edge4 && edge1 == _edge1));
137: edges = _graph.getEdgesFrom(_node1);
138: assertEquals(1, edges.size());
139: assertEquals(_edge1, edges.iterator().next());
140: edges = _graph.getEdgesTo(_node1);
141: assertEquals(0, edges.size());
142: }
143:
144: public static void main(String[] args) {
145: main(TestGraph.class);
146: }
147: }
|