001: package org.apache.ojb.broker;
002:
003: import java.util.List;
004:
005: import org.apache.ojb.junit.PBTestCase;
006:
007: /**
008: * tests for the essential create/update/delete/read for one-to-many related objects
009: */
010:
011: public class OneToManyTest extends PBTestCase {
012: public static void main(String[] args) {
013: String[] arr = { OneToManyTest.class.getName() };
014: junit.textui.TestRunner.main(arr);
015: }
016:
017: public OneToManyTest(String name) {
018: super (name);
019: }
020:
021: /**
022: * test the removal aware functionality.
023: */
024: public void testDeleteWithRemovalAwareCollection() {
025: long timestamp = System.currentTimeMillis();
026:
027: ProductGroupWithRemovalAwareCollection pg = new ProductGroupWithRemovalAwareCollection();
028: // auto-increment was not enabled in repository
029: // thus we set our own
030: pg.setId((int) timestamp % Integer.MAX_VALUE);
031: pg.setGroupName("testDeleteWithRemovalAwareCollection_"
032: + timestamp);
033:
034: Identity pgId = new Identity(pg, broker);
035:
036: Article a = new Article();
037: a.setArticleName("testDeleteWithRemovalAwareCollection_"
038: + timestamp);
039:
040: Article b = new Article();
041: b.setArticleName("testDeleteWithRemovalAwareCollection_"
042: + timestamp);
043:
044: Article c = new Article();
045: c.setArticleName("testDeleteWithRemovalAwareCollection_"
046: + timestamp);
047:
048: pg.add(a);
049: pg.add(b);
050: pg.add(c);
051: broker.beginTransaction();
052: broker.store(pg);
053: broker.commitTransaction();
054:
055: broker.clearCache();
056: pg = (ProductGroupWithRemovalAwareCollection) broker
057: .getObjectByIdentity(pgId);
058: assertEquals(3, pg.getAllArticles().size());
059:
060: pg.getAllArticles().remove(c);
061: pg.getAllArticles().remove(0);
062: broker.beginTransaction();
063: broker.store(pg);
064: broker.commitTransaction();
065:
066: broker.clearCache();
067: pg = (ProductGroupWithRemovalAwareCollection) broker
068: .getObjectByIdentity(pgId);
069: assertEquals(1, pg.getAllArticles().size());
070: }
071:
072: /**
073: * this tests if polymorph collections (i.e. collections of objects
074: * implementing a common interface) are treated correctly
075: */
076: public void testPolymorphOneToMany() {
077: long timestamp = System.currentTimeMillis();
078:
079: Zoo myZoo = new Zoo("London_" + timestamp);
080: Identity id = new Identity(myZoo, broker);
081:
082: Mammal elephant = new Mammal(2, "Jumbo_" + timestamp, 4);
083: Mammal cat = new Mammal(2, "Silvester_" + timestamp, 4);
084: Reptile snake = new Reptile(2, "Kaa_" + timestamp, "green");
085:
086: myZoo.addAnimal(snake);
087: myZoo.addAnimal(elephant);
088: myZoo.addAnimal(cat);
089: // System.out.println("## "+myZoo);
090: broker.beginTransaction();
091: broker.store(myZoo);
092: broker.commitTransaction();
093: // System.out.println("## "+myZoo);
094:
095: broker.clearCache();
096:
097: Zoo loadedZoo = (Zoo) broker.getObjectByIdentity(id);
098: List animals = loadedZoo.getAnimals();
099: assertEquals(3, animals.size());
100:
101: broker.beginTransaction();
102: for (int i = 0; i < animals.size(); i++) {
103: broker.delete(animals.get(i));
104: }
105: broker.delete(loadedZoo);
106: broker.commitTransaction();
107: }
108: }
|