001: /**
002: * User: om
003: */package org.apache.ojb.otm;
004:
005: import java.util.Arrays;
006: import java.util.Date;
007: import java.util.List;
008: import java.util.Vector;
009:
010: import junit.framework.TestCase;
011: import org.apache.ojb.broker.metadata.ClassDescriptor;
012: import org.apache.ojb.broker.metadata.CollectionDescriptor;
013: import org.apache.ojb.broker.util.collections.ManageableArrayList;
014: import org.apache.ojb.broker.util.collections.RemovalAwareCollection;
015: import org.apache.ojb.broker.*;
016: import org.apache.ojb.otm.lock.LockingException;
017: import org.apache.ojb.otm.core.Transaction;
018:
019: /**
020: * @author <a href="mailto:mattbaird@yahoo.com">Matthew Baird</a>
021: * @version $Id$
022: */
023: public class MtoNTest extends TestCase {
024: private static Class CLASS = MtoNTest.class;
025: private TestKit _kit;
026: private OTMConnection _conn;
027: private static final int COUNT = 1;
028: private static final long TIME = System.currentTimeMillis();
029:
030: public void setUp() throws LockingException {
031: _kit = TestKit.getTestInstance();
032: _conn = _kit.acquireConnection(PersistenceBrokerFactory
033: .getDefaultKey());
034: }
035:
036: public void tearDown() throws LockingException {
037: _conn.close();
038: _conn = null;
039: };
040:
041: public static void main(String[] args) {
042: String[] arr = { CLASS.getName() };
043: junit.textui.TestRunner.main(arr);
044: }
045:
046: private Paper createPaper() throws LockingException {
047: String now = new Date().toString();
048: Paper paper = new Paper();
049: paper.setAuthor("Jonny Myers");
050: paper.setDate(now);
051:
052: Qualifier qual1 = new Topic();
053: qual1.setName("qual1 " + now);
054: Qualifier qual2 = new Topic();
055: qual2.setName("qual2 " + now);
056:
057: List qualifiers = new Vector();
058: qualifiers.add(qual1);
059: qualifiers.add(qual2);
060: paper.setQualifiers(qualifiers);
061: Transaction trans = _kit.getTransaction(_conn);
062: trans.begin();
063: _conn.makePersistent(qual1);
064: _conn.makePersistent(qual2);
065: _conn.makePersistent(paper);
066: Identity paperId = _conn.getIdentity(paper);
067: trans.commit();
068:
069: // sanity check
070: trans = _kit.getTransaction(_conn);
071: trans.begin();
072: Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
073: qualifiers = retPaper.getQualifiers();
074:
075: assertEquals(2, qualifiers.size());
076: trans.commit();
077:
078: return retPaper;
079: }
080:
081: public void testCreate() throws Exception {
082: Paper paper = createPaper();
083: }
084:
085: public void testStoringWithAutoUpdateFalse() throws Exception {
086: ClassDescriptor cld = _conn.getDescriptorFor(Paper.class);
087: CollectionDescriptor cod = cld
088: .getCollectionDescriptorByName("qualifiers");
089: boolean autoUpdate = cod.getCascadeStore();
090: cod.setCascadeStore(false);
091: try {
092: String now = new Date().toString();
093: Paper paper = new Paper();
094: paper.setAuthor("Jonny Myers");
095: paper.setDate(now);
096: Qualifier qual = new Topic();
097: qual.setName("qual " + now);
098: paper
099: .setQualifiers(Arrays
100: .asList(new Qualifier[] { qual }));
101: Transaction trans = _kit.getTransaction(_conn);
102: trans.begin();
103: _conn.makePersistent(paper); // store Paper and intermediary table only
104: Identity paperId = _conn.getIdentity(paper);
105: trans.commit();
106:
107: // broker.clearCache();
108: trans = _kit.getTransaction(_conn);
109: trans.begin();
110: Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
111: assertEquals(0, retPaper.getQualifiers().size());
112: trans.commit();
113: ;
114: } finally {
115: cod.setCascadeStore(autoUpdate);
116: }
117: }
118:
119: public void testStoringWithAutoUpdateTrue() throws Exception {
120: ClassDescriptor cld = _conn.getDescriptorFor(Paper.class);
121: CollectionDescriptor cod = cld
122: .getCollectionDescriptorByName("qualifiers");
123: boolean autoUpdate = cod.getCascadeStore();
124:
125: cod.setCascadeStore(true);
126:
127: try {
128: String now = new Date().toString();
129: Paper paper = new Paper();
130: paper.setAuthor("Jonny Myers");
131: paper.setDate(now);
132: Qualifier qual = new Topic();
133: qual.setName("qual " + now);
134: paper
135: .setQualifiers(Arrays
136: .asList(new Qualifier[] { qual }));
137: Transaction trans = _kit.getTransaction(_conn);
138: trans.begin();
139: _conn.makePersistent(paper); // store Paper, intermediary and Qualifier
140: Identity paperId = _conn.getIdentity(paper);
141: trans.commit();
142: //broker.clearCache();
143: trans = _kit.getTransaction(_conn);
144: trans.begin();
145: Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
146: assertEquals(1, retPaper.getQualifiers().size());
147: trans.commit();
148: ;
149: } finally {
150: cod.setCascadeStore(autoUpdate);
151: }
152: }
153:
154: // delete from intermediary table only when collection NOT removal aware
155: public void testDelete_NonRemovalAware() throws Exception {
156: ClassDescriptor cld = _conn.getDescriptorFor(Paper.class);
157: CollectionDescriptor cod = cld
158: .getCollectionDescriptorByName("qualifiers");
159: Class collectionClass = cod.getCollectionClass();
160:
161: cod.setCollectionClass(ManageableArrayList.class);
162:
163: try {
164: Paper paper = createPaper();
165: Identity paperId = _conn.getIdentity(paper);
166: List qualifiers = paper.getQualifiers();
167: Qualifier qual1 = (Qualifier) qualifiers.get(0);
168: Qualifier qual2 = (Qualifier) qualifiers.get(1);
169:
170: // remove first object
171: qualifiers.remove(0);
172: Transaction trans = _kit.getTransaction(_conn);
173: trans.begin();
174: _conn.makePersistent(paper);
175: trans.commit();
176: ;
177:
178: //broker.clearCache();
179: trans = _kit.getTransaction(_conn);
180: trans.begin();
181: Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
182: assertEquals(1, retPaper.getQualifiers().size());
183: // target object qual1 should NOT be deleted
184: Qualifier retQual1 = (Qualifier) _conn
185: .getObjectByIdentity(_conn.getIdentity(qual1));
186: Qualifier retQual2 = (Qualifier) _conn
187: .getObjectByIdentity(_conn.getIdentity(qual2));
188:
189: assertNotNull(retQual1);
190: assertNotNull(retQual2);
191:
192: trans.commit();
193: ;
194: } finally {
195: cod.setCollectionClass(collectionClass);
196: }
197:
198: }
199:
200: // delete from intermediary AND target-table when collection removal aware
201: public void testDelete_RemovalAware() throws Exception {
202: ClassDescriptor cld = _conn.getDescriptorFor(Paper.class);
203: CollectionDescriptor cod = cld
204: .getCollectionDescriptorByName("qualifiers");
205: Class collectionClass = cod.getCollectionClass();
206:
207: cod.setCollectionClass(RemovalAwareCollection.class);
208:
209: try {
210: Paper paper = createPaper();
211: List qualifiers = paper.getQualifiers();
212: Qualifier qual1 = (Qualifier) qualifiers.get(0);
213: Qualifier qual2 = (Qualifier) qualifiers.get(1);
214: Identity paperId = _conn.getIdentity(paper);
215:
216: // remove first object
217: qualifiers.remove(0);
218: Transaction trans = _kit.getTransaction(_conn);
219: trans.begin();
220: _conn.makePersistent(paper);
221: trans.commit();
222: ;
223:
224: // broker.clearCache();
225: trans = _kit.getTransaction(_conn);
226: trans.begin();
227: Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
228: assertEquals(1, retPaper.getQualifiers().size());
229:
230: // target object qual1 should be deleted
231: Qualifier retQual1 = (Qualifier) _conn
232: .getObjectByIdentity(_conn.getIdentity(qual1));
233: Qualifier retQual2 = (Qualifier) _conn
234: .getObjectByIdentity(_conn.getIdentity(qual2));
235:
236: assertNull(retQual1);
237: assertNotNull(retQual2);
238:
239: trans.commit();
240: ;
241: } finally {
242: cod.setCollectionClass(collectionClass);
243: }
244: }
245:
246: public void testDeletionFromIntermediaryTableWithNullList()
247: throws Exception {
248: Paper paper = createPaper();
249: Identity paperId = _conn.getIdentity(paper);
250: List qualifiers = paper.getQualifiers();
251: Qualifier qual1 = (Qualifier) qualifiers.get(0);
252: Qualifier qual2 = (Qualifier) qualifiers.get(1);
253:
254: // now set collection to null and check if changes get persisted
255: paper.setQualifiers(null);
256: Transaction trans = _kit.getTransaction(_conn);
257: trans.begin();
258: _conn.makePersistent(paper);
259: trans.commit();
260: ;
261:
262: //broker.clearCache();
263: trans = _kit.getTransaction(_conn);
264: trans.begin();
265: Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
266: assertEquals(0, retPaper.getQualifiers().size());
267:
268: // target objects should NOT be deleted
269: Qualifier retQual1 = (Qualifier) _conn
270: .getObjectByIdentity(_conn.getIdentity(qual1));
271: Qualifier retQual2 = (Qualifier) _conn
272: .getObjectByIdentity(_conn.getIdentity(qual2));
273:
274: assertNotNull(retQual1);
275: assertNotNull(retQual2);
276:
277: trans.commit();
278: ;
279: }
280:
281: public void testDeletionWithClearedList() throws Exception {
282: Paper paper = createPaper();
283: Identity paperId = _conn.getIdentity(paper);
284: List qualifiers = paper.getQualifiers();
285: Qualifier qual1 = (Qualifier) qualifiers.get(0);
286: Qualifier qual2 = (Qualifier) qualifiers.get(1);
287:
288: // now clear collection
289: paper.getQualifiers().clear();
290: Transaction trans = _kit.getTransaction(_conn);
291: trans.begin();
292: _conn.makePersistent(paper);
293: trans.commit();
294: ;
295:
296: //broker.clearCache();
297: trans = _kit.getTransaction(_conn);
298: trans.begin();
299: Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
300: assertEquals(0, retPaper.getQualifiers().size());
301:
302: // target objects should be deleted
303: Qualifier retQual1 = (Qualifier) _conn
304: .getObjectByIdentity(_conn.getIdentity(qual1));
305: Qualifier retQual2 = (Qualifier) _conn
306: .getObjectByIdentity(_conn.getIdentity(qual2));
307:
308: assertNull(retQual1);
309: assertNull(retQual2);
310:
311: trans.commit();
312: ;
313: }
314:
315: public void testDeletionFromIntermediaryTableWithEmptyList()
316: throws Exception {
317: Paper paper = createPaper();
318: Identity paperId = _conn.getIdentity(paper);
319: List qualifiers = paper.getQualifiers();
320: Qualifier qual1 = (Qualifier) qualifiers.get(0);
321: Qualifier qual2 = (Qualifier) qualifiers.get(1);
322:
323: // now empty collection and check if changes get persisted
324: paper.setQualifiers(new RemovalAwareCollection());
325: Transaction trans = _kit.getTransaction(_conn);
326: trans.begin();
327: _conn.makePersistent(paper);
328: trans.commit();
329: ;
330:
331: // broker.clearCache();
332: trans = _kit.getTransaction(_conn);
333: trans.begin();
334: Paper retPaper = (Paper) _conn.getObjectByIdentity(paperId);
335: assertEquals(0, retPaper.getQualifiers().size());
336:
337: // target objects should NOT be deleted
338: Qualifier retQual1 = (Qualifier) _conn
339: .getObjectByIdentity(_conn.getIdentity(qual1));
340: Qualifier retQual2 = (Qualifier) _conn
341: .getObjectByIdentity(_conn.getIdentity(qual2));
342:
343: assertNotNull(retQual1);
344: assertNotNull(retQual2);
345:
346: trans.commit();
347: ;
348: }
349:
350: }
|