001: package org.apache.ojb.broker;
002:
003: import java.util.Collection;
004:
005: import org.apache.ojb.broker.core.proxy.ProxyHelper;
006: import org.apache.ojb.broker.metadata.ClassDescriptor;
007: import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
008: import org.apache.ojb.broker.query.Criteria;
009: import org.apache.ojb.broker.query.Query;
010: import org.apache.ojb.broker.query.QueryFactory;
011: import org.apache.ojb.junit.PBTestCase;
012:
013: /**
014: * Test case for checking the management of references, handling of PK used as FK too.
015: * In this test the PK of {@link org.apache.ojb.broker.PrimaryKeyForeignKeyTest.Person}
016: * is a FK to {@link org.apache.ojb.broker.PrimaryKeyForeignKeyTest.PersonDetail} too.
017: *
018: * Person has a reference to PersonDetail, the PK of Person is the FK to PersonDetail too!!
019: * PersonDetail PK is not autoincremented we have to set the PK of Person
020: *
021: * NOTE: Don't change metadata in production environments in such a way. All made changes are global
022: * changes and visible to all threads.
023: *
024: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
025: * @version $Id: PrimaryKeyForeignKeyTest.java,v 1.2 2004/06/22 18:16:22 arminw Exp $
026: */
027: public class PrimaryKeyForeignKeyTest extends PBTestCase {
028: public static void main(String[] args) {
029: String[] arr = { PrimaryKeyForeignKeyTest.class.getName() };
030: junit.textui.TestRunner.main(arr);
031: }
032:
033: public void tearDown() throws Exception {
034: try {
035: if (broker != null) {
036: changeRepositoryAutoSetting(Person.class, "detail",
037: true, ObjectReferenceDescriptor.CASCADE_NONE,
038: ObjectReferenceDescriptor.CASCADE_NONE, false);
039: changeRepositoryAutoSetting(PersonDetail.class,
040: "person", false,
041: ObjectReferenceDescriptor.CASCADE_NONE,
042: ObjectReferenceDescriptor.CASCADE_NONE, false);
043: changeRepositoryAutoSetting(PersonDetail.class,
044: "gender", true,
045: ObjectReferenceDescriptor.CASCADE_NONE,
046: ObjectReferenceDescriptor.CASCADE_NONE, false);
047: }
048: } finally {
049: super .tearDown();
050: }
051: }
052:
053: public void testStoreLookup() {
054: changeRepositoryAutoSetting(Person.class, "detail", true,
055: ObjectReferenceDescriptor.CASCADE_NONE,
056: ObjectReferenceDescriptor.CASCADE_NONE, false);
057: changeRepositoryAutoSetting(PersonDetail.class, "person",
058: false, ObjectReferenceDescriptor.CASCADE_NONE,
059: ObjectReferenceDescriptor.CASCADE_NONE, false);
060: changeRepositoryAutoSetting(PersonDetail.class, "gender", true,
061: ObjectReferenceDescriptor.CASCADE_NONE,
062: ObjectReferenceDescriptor.CASCADE_NONE, false);
063:
064: String postfix = "_" + System.currentTimeMillis();
065:
066: broker.beginTransaction();
067: Person jeff = new Person("jeff" + postfix, null);
068: // store Person first to assign PK, needed to set PK in PersonDetail
069: broker.store(jeff);
070: GenderIF gender = new Gender("male" + postfix);
071: PersonDetailIF jeffDetail = new PersonDetail(jeff.getId(),
072: "profile" + postfix, jeff, gender);
073: broker.store(gender);
074: broker.serviceBrokerHelper().link(jeffDetail, true);
075: broker.store(jeffDetail);
076: // now we can assign reference
077: jeff.setDetail(jeffDetail);
078: broker.store(jeff);
079: broker.commitTransaction();
080:
081: broker.clearCache();
082:
083: Criteria crit = new Criteria();
084: crit.addEqualTo("name", "jeff" + postfix);
085: Query query = QueryFactory.newQuery(Person.class, crit);
086: Collection result = broker.getCollectionByQuery(query);
087: assertEquals(1, result.size());
088: Person jeffNew = (Person) result.iterator().next();
089: assertEquals("jeff" + postfix, jeffNew.getName());
090: assertNotNull(jeffNew.getDetail());
091: assertFalse(ProxyHelper.isProxy(jeffNew.getDetail()));
092: PersonDetailIF jeffNewDetail = jeffNew.getDetail();
093: assertNotNull(jeffNewDetail.getGender());
094: assertEquals("male" + postfix, jeffNewDetail.getGender()
095: .getType());
096:
097: broker.beginTransaction();
098: broker.delete(jeffNew);
099: GenderIF g = jeffNewDetail.getGender();
100: broker.delete(jeffNewDetail);
101: broker.delete(g);
102: broker.commitTransaction();
103: }
104:
105: public void testStoreLookupProxy() {
106: changeRepositoryAutoSetting(Person.class, "detail", true,
107: ObjectReferenceDescriptor.CASCADE_NONE,
108: ObjectReferenceDescriptor.CASCADE_NONE, true);
109: changeRepositoryAutoSetting(PersonDetail.class, "person",
110: false, ObjectReferenceDescriptor.CASCADE_NONE,
111: ObjectReferenceDescriptor.CASCADE_NONE, true);
112: changeRepositoryAutoSetting(PersonDetail.class, "gender", true,
113: ObjectReferenceDescriptor.CASCADE_NONE,
114: ObjectReferenceDescriptor.CASCADE_NONE, true);
115:
116: String postfix = "_" + System.currentTimeMillis();
117:
118: broker.beginTransaction();
119: Person jeff = new Person("jeff" + postfix, null);
120: // store Person first to assign PK, needed to set PK in PersonDetail
121: broker.store(jeff);
122: GenderIF gender = new Gender("male" + postfix);
123: PersonDetailIF jeffDetail = new PersonDetail(jeff.getId(),
124: "profile" + postfix, jeff, gender);
125: broker.store(gender);
126: broker.serviceBrokerHelper().link(jeffDetail, true);
127: broker.store(jeffDetail);
128: // now we can assign reference
129: jeff.setDetail(jeffDetail);
130: broker.store(jeff);
131: broker.commitTransaction();
132:
133: broker.clearCache();
134:
135: Criteria crit = new Criteria();
136: crit.addEqualTo("name", "jeff" + postfix);
137: Query query = QueryFactory.newQuery(Person.class, crit);
138: Collection result = broker.getCollectionByQuery(query);
139: assertEquals(1, result.size());
140: Person jeffNew = (Person) result.iterator().next();
141: assertEquals("jeff" + postfix, jeffNew.getName());
142: assertNotNull(jeffNew.getDetail());
143: assertTrue(ProxyHelper.isProxy(jeffNew.getDetail()));
144: PersonDetailIF jeffNewDetail = jeffNew.getDetail();
145: assertNotNull(jeffNewDetail.getGender());
146: assertTrue(ProxyHelper.isProxy(jeffNewDetail.getGender()));
147: assertEquals("male" + postfix, jeffNewDetail.getGender()
148: .getType());
149:
150: broker.beginTransaction();
151: broker.delete(jeffNew);
152: GenderIF g = jeffNewDetail.getGender();
153: broker.delete(jeffNewDetail);
154: broker.delete(g);
155: broker.commitTransaction();
156: }
157:
158: public void testStoreLookup_2() {
159: changeRepositoryAutoSetting(Person.class, "detail", true,
160: ObjectReferenceDescriptor.CASCADE_OBJECT,
161: ObjectReferenceDescriptor.CASCADE_OBJECT, false);
162: changeRepositoryAutoSetting(PersonDetail.class, "person",
163: false, ObjectReferenceDescriptor.CASCADE_OBJECT,
164: ObjectReferenceDescriptor.CASCADE_OBJECT, false);
165: changeRepositoryAutoSetting(PersonDetail.class, "gender", true,
166: ObjectReferenceDescriptor.CASCADE_OBJECT,
167: ObjectReferenceDescriptor.CASCADE_OBJECT, false);
168:
169: String postfix = "_" + System.currentTimeMillis();
170:
171: broker.beginTransaction();
172: Person jeff = new Person("jeff" + postfix, null);
173: // store Person first to assign PK, needed to set PK in PersonDetail
174: broker.store(jeff);
175: GenderIF gender = new Gender("male" + postfix);
176: PersonDetailIF jeffDetail = new PersonDetail(jeff.getId(),
177: "profile" + postfix, jeff, gender);
178: broker.store(jeffDetail);
179: // now we can assign reference
180: jeff.setDetail(jeffDetail);
181: broker.store(jeff);
182: broker.commitTransaction();
183:
184: broker.clearCache();
185:
186: Criteria crit = new Criteria();
187: crit.addEqualTo("name", "jeff" + postfix);
188: Query query = QueryFactory.newQuery(Person.class, crit);
189: Collection result = broker.getCollectionByQuery(query);
190: assertEquals(1, result.size());
191: Person jeffNew = (Person) result.iterator().next();
192: assertEquals("jeff" + postfix, jeffNew.getName());
193: assertNotNull(jeffNew.getDetail());
194: assertFalse(ProxyHelper.isProxy(jeffNew.getDetail()));
195: PersonDetailIF jeffNewDetail = jeffNew.getDetail();
196: assertNotNull(jeffNewDetail.getGender());
197: assertEquals("male" + postfix, jeffNewDetail.getGender()
198: .getType());
199:
200: broker.beginTransaction();
201: broker.delete(jeffNew);
202: GenderIF g = jeffNewDetail.getGender();
203: broker.delete(jeffNewDetail);
204: broker.delete(g);
205: broker.commitTransaction();
206: }
207:
208: public void testStoreLookupProxy_2() {
209: changeRepositoryAutoSetting(Person.class, "detail", true,
210: ObjectReferenceDescriptor.CASCADE_OBJECT,
211: ObjectReferenceDescriptor.CASCADE_OBJECT, true);
212: changeRepositoryAutoSetting(PersonDetail.class, "person",
213: false, ObjectReferenceDescriptor.CASCADE_OBJECT,
214: ObjectReferenceDescriptor.CASCADE_OBJECT, true);
215: changeRepositoryAutoSetting(PersonDetail.class, "gender", true,
216: ObjectReferenceDescriptor.CASCADE_OBJECT,
217: ObjectReferenceDescriptor.CASCADE_OBJECT, true);
218:
219: String postfix = "_" + System.currentTimeMillis();
220:
221: broker.beginTransaction();
222: Person jeff = new Person("jeff" + postfix, null);
223: // store Person first to assign PK, needed to set PK in PersonDetail
224: broker.store(jeff);
225: GenderIF gender = new Gender("male" + postfix);
226: PersonDetailIF jeffDetail = new PersonDetail(jeff.getId(),
227: "profile" + postfix, jeff, gender);
228: broker.store(jeffDetail);
229: // now we can assign reference
230: jeff.setDetail(jeffDetail);
231: broker.store(jeff);
232: broker.commitTransaction();
233:
234: broker.clearCache();
235:
236: Criteria crit = new Criteria();
237: crit.addEqualTo("name", "jeff" + postfix);
238: Query query = QueryFactory.newQuery(Person.class, crit);
239: Collection result = broker.getCollectionByQuery(query);
240: assertEquals(1, result.size());
241: Person jeffNew = (Person) result.iterator().next();
242: assertEquals("jeff" + postfix, jeffNew.getName());
243: assertNotNull(jeffNew.getDetail());
244: assertTrue(ProxyHelper.isProxy(jeffNew.getDetail()));
245: PersonDetailIF jeffNewDetail = jeffNew.getDetail();
246: assertNotNull(jeffNewDetail.getGender());
247: assertTrue(ProxyHelper.isProxy(jeffNewDetail.getGender()));
248: assertEquals("male" + postfix, jeffNewDetail.getGender()
249: .getType());
250:
251: broker.beginTransaction();
252: broker.delete(jeffNew);
253: GenderIF g = jeffNewDetail.getGender();
254: broker.delete(jeffNewDetail);
255: broker.delete(g);
256: broker.commitTransaction();
257: }
258:
259: private void changeRepositoryAutoSetting(Class clazz,
260: String attributeName, boolean retrieve, int update,
261: int delete, boolean proxy) {
262: ClassDescriptor cld = broker.getClassDescriptor(clazz);
263: ObjectReferenceDescriptor ord = cld
264: .getObjectReferenceDescriptorByName(attributeName);
265: ord.setCascadeRetrieve(retrieve);
266: ord.setCascadingStore(update);
267: ord.setCascadingDelete(delete);
268: ord.setLazy(proxy);
269: }
270:
271: public static class Person {
272: Integer id;
273: String name;
274: PersonDetailIF detail;
275:
276: public Person() {
277: }
278:
279: public Person(String name, PersonDetailIF detail) {
280: this .name = name;
281: this .detail = detail;
282: }
283:
284: public Integer getId() {
285: return id;
286: }
287:
288: public void setId(Integer id) {
289: this .id = id;
290: }
291:
292: public String getName() {
293: return name;
294: }
295:
296: public void setName(String name) {
297: this .name = name;
298: }
299:
300: public PersonDetailIF getDetail() {
301: return detail;
302: }
303:
304: public void setDetail(PersonDetailIF detail) {
305: this .detail = detail;
306: }
307: }
308:
309: public static interface PersonDetailIF {
310: public Integer getId();
311:
312: public void setId(Integer id);
313:
314: public String getProfile();
315:
316: public void setProfile(String profile);
317:
318: public Person getPerson();
319:
320: public void setPerson(Person person);
321:
322: public GenderIF getGender();
323:
324: public void setGender(GenderIF person);
325: }
326:
327: public static class PersonDetail implements PersonDetailIF {
328: Integer id;
329: String profile;
330: Person person;
331: GenderIF gender;
332:
333: public PersonDetail() {
334: }
335:
336: public PersonDetail(Integer id, String profile, Person person,
337: GenderIF gender) {
338: this .id = id;
339: this .profile = profile;
340: this .person = person;
341: this .gender = gender;
342: }
343:
344: public Integer getId() {
345: return id;
346: }
347:
348: public void setId(Integer id) {
349: this .id = id;
350: }
351:
352: public String getProfile() {
353: return profile;
354: }
355:
356: public void setProfile(String profile) {
357: this .profile = profile;
358: }
359:
360: public Person getPerson() {
361: return person;
362: }
363:
364: public void setPerson(Person person) {
365: this .person = person;
366: }
367:
368: public GenderIF getGender() {
369: return gender;
370: }
371:
372: public void setGender(GenderIF gender) {
373: this .gender = gender;
374: }
375: }
376:
377: public static interface GenderIF {
378: public String getType();
379:
380: public void setType(String type);
381: }
382:
383: public static class Gender implements GenderIF {
384: String type;
385:
386: public Gender() {
387: }
388:
389: public Gender(String type) {
390: this .type = type;
391: }
392:
393: public String getType() {
394: return type;
395: }
396:
397: public void setType(String type) {
398: this.type = type;
399: }
400: }
401: }
|