001: /**
002: * Copyright (C) 2001-2006 France Telecom R&D
003: */package org.objectweb.speedo.runtime.inheritance;
004:
005: import org.objectweb.speedo.SpeedoTestHelper;
006: import org.objectweb.speedo.pobjects.inheritance.horizontal.Address;
007: import org.objectweb.speedo.pobjects.inheritance.horizontal.Person;
008: import org.objectweb.speedo.pobjects.inheritance.horizontal.Worker;
009:
010: import java.util.Collection;
011: import java.util.Date;
012: import java.util.Iterator;
013:
014: import javax.jdo.PersistenceManager;
015: import javax.jdo.Query;
016:
017: public class TestHorizontal extends SpeedoTestHelper {
018:
019: public TestHorizontal(String n) {
020: super (n);
021: }
022:
023: protected String getLoggerName() {
024: return LOG_NAME + ".rt.inheritance.TestHorizontal";
025: }
026:
027: public void test1() {
028: final int NB_PERSON = 3;
029: Date d = new Date();
030: PersistenceManager pm = pmf.getPersistenceManager();
031: pm.currentTransaction().begin();
032: for (int i = 0; i < NB_PERSON; i++) {
033: Address a = new Address("p_street_" + i, "p_city_" + i,
034: "p_state_" + i, "p_zipcode_" + i);
035: Person p = new Person("person_fn_" + i, "person_ln_" + i,
036: a, d);
037: pm.makePersistent(p);
038: }
039: final int NB_WORKER = 4;
040: for (int i = 0; i < NB_WORKER; i++) {
041: Address a = new Address("w_street_" + i, "w_city_" + i,
042: "w_state_" + i, "w_zipcode_" + i);
043: Worker w = new Worker("worker_fn_" + i, "worker_ln_" + i,
044: a, d, "jobName_" + i, "company_" + i,
045: (float) (i * 2000.0));
046: pm.makePersistent(w);
047: }
048: pm.currentTransaction().commit();
049: pm.evictAll();
050: pm.close();
051:
052: //find worker only
053: pm = pmf.getPersistenceManager();
054: pm.currentTransaction().begin();
055: Query q = pm.newQuery(Worker.class);
056: q.setOrdering("firstname ascending");
057: Collection c = (Collection) q.execute();
058: int cpt = 0;
059: try {
060: for (Iterator it = c.iterator(); it.hasNext();) {
061: Worker w = (Worker) it.next();
062: assertEquals("Bad worker first name", "worker_fn_"
063: + cpt, w.getFirstname());
064: cpt++;
065: }
066: assertEquals("Bad worker number", NB_WORKER, cpt);
067: } finally {
068: q.closeAll();
069: pm.currentTransaction().commit();
070: pm.evictAll();
071: pm.close();
072: }
073:
074: //find Person instance (without Worker)
075: pm = pmf.getPersistenceManager();
076: pm.currentTransaction().begin();
077: q = pm.newQuery(pm.getExtent(Person.class, false));
078: q.setOrdering("firstname ascending");
079: c = (Collection) q.execute();
080: cpt = 0;
081: try {
082: for (Iterator it = c.iterator(); it.hasNext();) {
083: Person p = (Person) it.next();
084: assertEquals("Bad person first name", "person_fn_"
085: + cpt, p.getFirstname());
086: cpt++;
087: }
088: assertEquals("Bad person and worker number", NB_PERSON, cpt);
089: } finally {
090: q.closeAll();
091: pm.currentTransaction().commit();
092: pm.evictAll();
093: pm.close();
094: }
095:
096: //find all persons (including Worker)
097: pm = pmf.getPersistenceManager();
098: pm.currentTransaction().begin();
099: q = pm.newQuery(pm.getExtent(Person.class, true));
100: q.setOrdering("firstname ascending");
101: c = (Collection) q.execute();
102: cpt = 0;
103: try {
104: for (Iterator it = c.iterator(); it.hasNext();) {
105: Person p = (Person) it.next();
106: String expectedFN;
107: if (cpt < NB_PERSON) {
108: expectedFN = "person_fn_" + cpt;
109: } else {
110: expectedFN = "worker_fn_" + (cpt - NB_PERSON);
111: }
112: assertEquals("Bad person first name", expectedFN, p
113: .getFirstname());
114: cpt++;
115: }
116: assertEquals("Bad person and worker number", NB_WORKER
117: + NB_PERSON, cpt);
118: } finally {
119: q.closeAll();
120: pm.currentTransaction().commit();
121: pm.evictAll();
122: pm.close();
123: }
124:
125: pm = pmf.getPersistenceManager();
126: pm.currentTransaction().begin();
127: q = pm.newQuery(pm.getExtent(Person.class, true));
128: pm.deletePersistentAll((Collection) q.execute());
129: q.closeAll();
130: pm.currentTransaction().commit();
131: pm.close();
132: }
133: }
|