001: package org.apache.ojb.broker;
002:
003: import org.apache.ojb.broker.query.Criteria;
004: import org.apache.ojb.broker.query.Query;
005: import org.apache.ojb.broker.query.QueryFactory;
006: import org.apache.ojb.junit.PBTestCase;
007:
008: import java.util.Collection;
009: import java.util.Iterator;
010:
011: /**
012: * Testing graph structure, which mean two relations between two classes.
013: */
014: public class GraphTest extends PBTestCase {
015: public static void main(String[] args) {
016: String[] arr = { GraphTest.class.getName() };
017: junit.textui.TestRunner.main(arr);
018: }
019:
020: public GraphTest(String name) {
021: super (name);
022: }
023:
024: private void clearDatabase() {
025: Criteria crit = new Criteria();
026: Query q;
027: Iterator iter;
028:
029: q = QueryFactory.newQuery(GraphNode.class, crit);
030: iter = broker.getIteratorByQuery(q);
031: broker.beginTransaction();
032: while (iter.hasNext()) {
033: broker.delete(iter.next());
034: }
035: broker.commitTransaction();
036:
037: q = QueryFactory.newQuery(GraphEdge.class, crit);
038: iter = broker.getIteratorByQuery(q);
039: broker.beginTransaction();
040: while (iter.hasNext()) {
041: broker.delete(iter.next());
042: }
043: broker.commitTransaction();
044: }
045:
046: public void testCreate() {
047: clearDatabase(); // Clear database before inserting new data
048:
049: GraphNode a = new GraphNode("A");
050: GraphNode b = new GraphNode("B");
051: GraphNode c = new GraphNode("C");
052: Identity oid = new Identity(a, broker);
053: new Identity(b, broker);
054: new Identity(c, broker);
055:
056: GraphEdge aa = new GraphEdge(a, a);
057: GraphEdge ab = new GraphEdge(a, b);
058: GraphEdge bc = new GraphEdge(b, c);
059: GraphEdge ac = new GraphEdge(a, c);
060: new Identity(aa, broker);
061: new Identity(ab, broker);
062: new Identity(bc, broker);
063: new Identity(ac, broker);
064:
065: Point locA = new Point(0, 0);
066: Point locB = new Point(1, 0);
067: Point locC = new Point(1, 1);
068:
069: new Identity(locA, broker);
070: new Identity(locB, broker);
071: new Identity(locC, broker);
072:
073: broker.beginTransaction();
074: broker.store(locA);
075: broker.store(locB);
076: broker.store(locC);
077: broker.commitTransaction();
078:
079: a.setLocation(locA);
080: b.setLocation(locB);
081: c.setLocation(locC);
082:
083: broker.beginTransaction();
084: broker.store(a);
085: broker.store(b);
086: broker.store(c);
087: broker.commitTransaction();
088:
089: broker.clearCache();
090:
091: GraphNode retrieved = (GraphNode) broker
092: .getObjectByIdentity(oid);
093: assertEquals("check graph structure",
094: "A [(A -> A), (A -> B [(B -> C [])]), (A -> C [])]",
095: retrieved.toString());
096: }
097:
098: public void testEqualToFieldQuery() {
099: Criteria crit;
100: Query q;
101: Collection results;
102:
103: crit = new Criteria();
104: crit.addEqualToField("name", "outgoingEdges.sink.name");
105: q = QueryFactory.newQuery(GraphNode.class, crit);
106: results = broker.getCollectionByQuery(q);
107: //System.out.println(results);
108: assertNotNull(results);
109: assertEquals(results.size(), 1); // only "bc" conforms
110:
111: }
112:
113: public void testSingleJoin() {
114: Criteria crit;
115: Query q;
116: Collection results;
117: crit = new Criteria();
118: crit.addEqualTo("source.name", "A");
119: q = QueryFactory.newQuery(GraphEdge.class, crit);
120: results = broker.getCollectionByQuery(q);
121: assertNotNull(results);
122: assertEquals(results.size(), 3); // only "bc" conforms
123: }
124:
125: public void testNestedJoin() {
126: Criteria crit;
127: Query q;
128: Collection results;
129: crit = new Criteria();
130: crit.addGreaterThan("source.location.x", new Integer(0));
131: crit.addEqualTo("source.location.y", new Integer(0));
132: q = QueryFactory.newQuery(GraphEdge.class, crit);
133: results = broker.getCollectionByQuery(q);
134: assertNotNull(results);
135: assertEquals(results.size(), 1); // only "bc" conforms
136: }
137:
138: public void testMultiNonNestedJoin() {
139: Criteria crit;
140: Query q;
141: Collection results;
142: crit = new Criteria();
143: crit.addEqualTo("source.name", "A");
144: crit.addEqualTo("sink.name", "B");
145: q = QueryFactory.newQuery(GraphEdge.class, crit);
146: results = broker.getCollectionByQuery(q);
147: assertNotNull(results);
148: assertEquals(results.size(), 1); // only "bc" conforms
149: }
150: }
|