1 package org.apache.ojb.broker; 2 3 import org.apache.ojb.broker.query.Criteria; 4 import org.apache.ojb.broker.query.Query; 5 import org.apache.ojb.broker.query.QueryFactory; 6 import org.apache.ojb.junit.PBTestCase; 7 8 import java.util.Collection ; 9 import java.util.Iterator ; 10 11 14 public class GraphTest extends PBTestCase 15 { 16 public static void main(String [] args) 17 { 18 String [] arr = {GraphTest.class.getName()}; 19 junit.textui.TestRunner.main(arr); 20 } 21 22 public GraphTest(String name) 23 { 24 super(name); 25 } 26 27 private void clearDatabase() 28 { 29 Criteria crit = new Criteria(); 30 Query q; 31 Iterator iter; 32 33 q = QueryFactory.newQuery(GraphNode.class, crit); 34 iter = broker.getIteratorByQuery(q); 35 broker.beginTransaction(); 36 while (iter.hasNext()) 37 { 38 broker.delete(iter.next()); 39 } 40 broker.commitTransaction(); 41 42 q = QueryFactory.newQuery(GraphEdge.class, crit); 43 iter = broker.getIteratorByQuery(q); 44 broker.beginTransaction(); 45 while (iter.hasNext()) 46 { 47 broker.delete(iter.next()); 48 } 49 broker.commitTransaction(); 50 } 51 52 53 public void testCreate() 54 { 55 clearDatabase(); 57 GraphNode a = new GraphNode("A"); 58 GraphNode b = new GraphNode("B"); 59 GraphNode c = new GraphNode("C"); 60 Identity oid = new Identity(a, broker); 61 new Identity(b, broker); 62 new Identity(c, broker); 63 64 GraphEdge aa = new GraphEdge(a, a); 65 GraphEdge ab = new GraphEdge(a, b); 66 GraphEdge bc = new GraphEdge(b, c); 67 GraphEdge ac = new GraphEdge(a, c); 68 new Identity(aa, broker); 69 new Identity(ab, broker); 70 new Identity(bc, broker); 71 new Identity(ac, broker); 72 73 Point locA = new Point(0, 0); 74 Point locB = new Point(1, 0); 75 Point locC = new Point(1, 1); 76 77 new Identity(locA, broker); 78 new Identity(locB, broker); 79 new Identity(locC, broker); 80 81 broker.beginTransaction(); 82 broker.store(locA); 83 broker.store(locB); 84 broker.store(locC); 85 broker.commitTransaction(); 86 87 a.setLocation(locA); 88 b.setLocation(locB); 89 c.setLocation(locC); 90 91 broker.beginTransaction(); 92 broker.store(a); 93 broker.store(b); 94 broker.store(c); 95 broker.commitTransaction(); 96 97 broker.clearCache(); 98 99 GraphNode retrieved = (GraphNode) broker.getObjectByIdentity(oid); 100 assertEquals("check graph structure", "A [(A -> A), (A -> B [(B -> C [])]), (A -> C [])]", retrieved.toString()); 101 } 102 103 public void testEqualToFieldQuery() 104 { 105 Criteria crit; 106 Query q; 107 Collection results; 108 109 crit = new Criteria(); 110 crit.addEqualToField("name", "outgoingEdges.sink.name"); 111 q = QueryFactory.newQuery(GraphNode.class, crit); 112 results = broker.getCollectionByQuery(q); 113 assertNotNull(results); 115 assertEquals(results.size(), 1); 117 } 118 119 120 public void testSingleJoin() 121 { 122 Criteria crit; 123 Query q; 124 Collection results; 125 crit = new Criteria(); 126 crit.addEqualTo("source.name", "A"); 127 q = QueryFactory.newQuery(GraphEdge.class, crit); 128 results = broker.getCollectionByQuery(q); 129 assertNotNull(results); 130 assertEquals(results.size(), 3); } 132 133 public void testNestedJoin() 134 { 135 Criteria crit; 136 Query q; 137 Collection results; 138 crit = new Criteria(); 139 crit.addGreaterThan("source.location.x", new Integer (0)); 140 crit.addEqualTo("source.location.y", new Integer (0)); 141 q = QueryFactory.newQuery(GraphEdge.class, crit); 142 results = broker.getCollectionByQuery(q); 143 assertNotNull(results); 144 assertEquals(results.size(), 1); } 146 147 public void testMultiNonNestedJoin() 148 { 149 Criteria crit; 150 Query q; 151 Collection results; 152 crit = new Criteria(); 153 crit.addEqualTo("source.name", "A"); 154 crit.addEqualTo("sink.name", "B"); 155 q = QueryFactory.newQuery(GraphEdge.class, crit); 156 results = broker.getCollectionByQuery(q); 157 assertNotNull(results); 158 assertEquals(results.size(), 1); } 160 } 161 | Popular Tags |