001: package org.apache.ojb.broker;
002:
003: import java.util.Iterator;
004: import java.util.Collection;
005:
006: import org.apache.ojb.broker.query.Criteria;
007: import org.apache.ojb.broker.query.Query;
008: import org.apache.ojb.broker.query.QueryFactory;
009: import org.apache.ojb.broker.metadata.ClassDescriptor;
010: import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
011: import org.apache.ojb.junit.PBTestCase;
012:
013: /**
014: * tests a bidirectional association A<-->B
015: * @see org.apache.ojb.odmg.BidirectionalAssociationTest for equivalent test in ODMG
016: */
017: public class BidirectionalAssociationTest extends PBTestCase {
018: public static void main(String[] args) {
019: String[] arr = { BidirectionalAssociationTest.class.getName() };
020: junit.textui.TestRunner.main(arr);
021: }
022:
023: public BidirectionalAssociationTest(String name) {
024: super (name);
025: }
026:
027: public void setUp() throws Exception {
028: super .setUp();
029: }
030:
031: public void tearDown() throws Exception {
032: super .tearDown();
033: }
034:
035: public void testAutoRefreshTrue() {
036: String pkSuffix = "_" + System.currentTimeMillis();
037: ObjectReferenceDescriptor ord_A = null;
038: ObjectReferenceDescriptor ord_B = null;
039: ClassDescriptor cld_A = broker
040: .getClassDescriptor(BidirectionalAssociationObjectA.class);
041: ord_A = cld_A.getObjectReferenceDescriptorByName("relatedB");
042: ClassDescriptor cld_B = broker
043: .getClassDescriptor(BidirectionalAssociationObjectB.class);
044: ord_B = cld_B.getObjectReferenceDescriptorByName("relatedA");
045: boolean oldA = ord_A.isRefresh();
046: boolean oldB = ord_B.isRefresh();
047: try {
048: ord_A.setRefresh(true);
049: ord_B.setRefresh(true);
050: createWithUpdate(pkSuffix);
051: Criteria crit = new Criteria();
052: crit.addLike("pk", "%" + pkSuffix);
053: Query query = QueryFactory.newQuery(
054: BidirectionalAssociationObjectB.class, crit);
055: Collection result = broker.getCollectionByQuery(query);
056: assertEquals(1, result.size());
057: } finally {
058: if (ord_A != null)
059: ord_A.setRefresh(oldA);
060: if (ord_B != null)
061: ord_B.setRefresh(oldB);
062: }
063: }
064:
065: public void testCreateDelete() {
066: String pkSuffix = "_" + System.currentTimeMillis();
067: createWithUpdate(pkSuffix);
068: deleteAllA();
069: deleteAllB();
070: }
071:
072: private void createWithUpdate(String pkSuffix) {
073: broker.beginTransaction();
074: BidirectionalAssociationObjectA a = new BidirectionalAssociationObjectA();
075: a.setPk("A" + pkSuffix);
076: BidirectionalAssociationObjectB b = new BidirectionalAssociationObjectB();
077: b.setPk("B" + pkSuffix);
078: broker.store(a);
079: broker.store(b);
080: /**
081: * now set relations
082: */
083: b.setRelatedA(a);
084: a.setRelatedB(b);
085: /**
086: * and update
087: */
088: broker.store(a);
089: broker.store(b);
090: broker.commitTransaction();
091: }
092:
093: public void testGetA() throws Exception {
094: String pkSuffix = "_" + System.currentTimeMillis();
095: createWithUpdate(pkSuffix);
096:
097: Criteria crit = new Criteria();
098: Query q;
099: Iterator iter;
100: q = QueryFactory.newQuery(
101: BidirectionalAssociationObjectA.class, crit);
102: iter = broker.getIteratorByQuery(q);
103: BidirectionalAssociationObjectA temp = null;
104: while (iter.hasNext()) {
105: temp = (BidirectionalAssociationObjectA) iter.next();
106: if (temp.getRelatedB() == null) {
107: fail("relatedB not found");
108: }
109: }
110:
111: deleteAllA();
112: deleteAllB();
113: }
114:
115: public void testGetB() throws Exception {
116: String pkSuffix = "_" + System.currentTimeMillis();
117: createWithUpdate(pkSuffix);
118:
119: Criteria crit = new Criteria();
120: Query q;
121: Iterator iter;
122: q = QueryFactory.newQuery(
123: BidirectionalAssociationObjectB.class, crit);
124: iter = broker.getIteratorByQuery(q);
125: BidirectionalAssociationObjectB temp = null;
126: while (iter.hasNext()) {
127: temp = (BidirectionalAssociationObjectB) iter.next();
128: if (temp.getRelatedA() == null) {
129: fail("relatedA not found");
130: }
131: }
132:
133: deleteAllA();
134: deleteAllB();
135: }
136:
137: public void testDeleteA() {
138: String pkSuffix = "_" + System.currentTimeMillis();
139: createWithUpdate(pkSuffix);
140: deleteAllA();
141: deleteAllB();
142: }
143:
144: public void testDeleteB() {
145: String pkSuffix = "_" + System.currentTimeMillis();
146: createWithUpdate(pkSuffix);
147: deleteAllB();
148: deleteAllA();
149: }
150:
151: private void deleteAllA() {
152: Criteria crit = new Criteria();
153: Query q;
154: Iterator iter;
155: q = QueryFactory.newQuery(
156: BidirectionalAssociationObjectA.class, crit);
157: iter = broker.getIteratorByQuery(q);
158: BidirectionalAssociationObjectA temp = null;
159: broker.beginTransaction();
160: while (iter.hasNext()) {
161: temp = (BidirectionalAssociationObjectA) iter.next();
162: BidirectionalAssociationObjectB b = temp.getRelatedB();
163: if (b != null) {
164: b.setRelatedA(null);
165: broker.store(b);
166: }
167: broker.delete(temp);
168: }
169: broker.commitTransaction();
170: }
171:
172: private void deleteAllB() {
173: Criteria crit = new Criteria();
174: Query q;
175: Iterator iter;
176: q = QueryFactory.newQuery(
177: BidirectionalAssociationObjectB.class, crit);
178: iter = broker.getIteratorByQuery(q);
179: BidirectionalAssociationObjectB temp = null;
180: broker.beginTransaction();
181: while (iter.hasNext()) {
182: temp = (BidirectionalAssociationObjectB) iter.next();
183: BidirectionalAssociationObjectA a = temp.getRelatedA();
184: if (a != null) {
185: a.setRelatedB(null);
186: broker.store(a);
187: }
188: broker.delete(temp);
189: }
190: broker.commitTransaction();
191: }
192: }
|