001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.runtime.inheritance;
018:
019: import org.objectweb.speedo.SpeedoTestHelper;
020: import org.objectweb.speedo.pobjects.inheritance.filtered.datastorelid.RootClass;
021: import org.objectweb.speedo.pobjects.inheritance.filtered.datastorelid.Subclass1;
022: import org.objectweb.speedo.pobjects.inheritance.filtered.datastorelid.Subclass12;
023: import org.objectweb.speedo.pobjects.inheritance.filtered.datastorelid.Subclass2;
024: import org.objectweb.util.monolog.api.BasicLevel;
025: import javax.jdo.Extent;
026: import javax.jdo.Query;
027: import javax.jdo.PersistenceManager;
028: import java.util.ArrayList;
029: import java.util.Collection;
030: import java.util.Iterator;
031:
032: /**
033: *
034: * @author S.Chassande-Barrioz
035: */
036: public class TestDatastorelid extends SpeedoTestHelper {
037:
038: public TestDatastorelid(String s) {
039: super (s);
040: }
041:
042: protected String getLoggerName() {
043: return LOG_NAME + "rt.inheritance.TestDatastorelid";
044: }
045:
046: public void testA() {
047: PersistenceManager pm = pmf.getPersistenceManager();
048: pm.currentTransaction().begin();
049: ArrayList rcs = new ArrayList();
050: rcs.add(new RootClass("rf1"));
051: rcs.add(new RootClass("rf2"));
052: rcs.add(new Subclass1("rf3", "f1_3"));
053: rcs.add(new Subclass12("rf4", "f1_4", "f12_4"));
054: rcs.add(new Subclass2("rf5", "f2_5"));
055: pm.makePersistentAll(rcs);
056: pm.currentTransaction().commit();
057:
058: pm.currentTransaction().begin();
059: checkNumberOfInstanceWithExtent(RootClass.class, false, 2, pm);
060: checkNumberOfInstanceWithExtent(RootClass.class, true, 5, pm);
061:
062: checkNumberOfInstanceWithExtent(Subclass1.class, false, 1, pm);
063: checkNumberOfInstanceWithExtent(Subclass1.class, true, 2, pm);
064:
065: checkNumberOfInstanceWithExtent(Subclass12.class, true, 1, pm);
066: checkNumberOfInstanceWithExtent(Subclass12.class, false, 1, pm);
067:
068: checkNumberOfInstanceWithExtent(Subclass2.class, false, 1, pm);
069: checkNumberOfInstanceWithExtent(Subclass2.class, true, 1, pm);
070: pm.currentTransaction().commit();
071:
072: pm.currentTransaction().begin();
073: for (int i = 0; i < rcs.size(); i++) {
074: rcs.set(i, pm.getObjectId(rcs.get(i)));
075: }
076: pm.currentTransaction().commit();
077: pm.evictAll();
078:
079: pm.currentTransaction().begin();
080: for (int i = 0; i < rcs.size(); i++) {
081: pm.getObjectById(rcs.get(i), false);
082: }
083: pm.currentTransaction().commit();
084:
085: pm.evictAll();
086:
087: pm.currentTransaction().begin();
088: Query query = pm.newQuery(RootClass.class);
089: Collection col = (Collection) query.execute();
090: Iterator it = col.iterator();
091: int size = 0;
092: while (it.hasNext()) {
093: RootClass rc = (RootClass) it.next();
094: size++;
095: logger.log(BasicLevel.DEBUG, "rc.rootField="
096: + rc.getRootField());
097: if ("rf1".equals(rc.getRootField())) {
098: } else if ("rf2".equals(rc.getRootField())) {
099: } else if ("rf3".equals(rc.getRootField())) {
100: assertTrue("Bad class instance"
101: + rc.getClass().getName(),
102: rc instanceof Subclass1);
103: assertEquals("Bad f1 value", "f1_3", ((Subclass1) rc)
104: .getF1());
105: } else if ("rf4".equals(rc.getRootField())) {
106: assertTrue("Bad class instance"
107: + rc.getClass().getName(),
108: rc instanceof Subclass12);
109: assertEquals("Bad f1 value", "f1_4", ((Subclass12) rc)
110: .getF1());
111: assertEquals("Bad f1 value", "f12_4", ((Subclass12) rc)
112: .getF12());
113: } else if ("rf5".equals(rc.getRootField())) {
114: assertTrue("Bad class instance"
115: + rc.getClass().getName(),
116: rc instanceof Subclass2);
117: assertEquals("Bad f1 value", "f2_5", ((Subclass2) rc)
118: .getF2());
119: } else {
120: fail("Unmanaged rootField value: " + rc.getRootField());
121: }
122: }
123: query.closeAll();
124: assertEquals("bad query result size", 5, size);
125: pm.currentTransaction().commit();
126:
127: pm.currentTransaction().begin();
128: for (int i = 0; i < rcs.size(); i++) {
129: pm.deletePersistent(pm.getObjectById(rcs.get(i), false));
130: }
131: pm.currentTransaction().commit();
132: pm.close();
133:
134: }
135:
136: private void checkNumberOfInstanceWithExtent(Class clazz,
137: boolean withSubClass, int expected, PersistenceManager pm) {
138: Extent e = pm.getExtent(clazz, withSubClass);
139: Iterator it = e.iterator();
140: int cpt = 0;
141: ArrayList rfs = new ArrayList(expected);
142: while (it.hasNext()) {
143: rfs.add(((RootClass) it.next()).getRootField());
144: cpt++;
145: }
146: e.closeAll();
147: assertEquals("Bad number of " + clazz.getName()
148: + " instance (withSubclass: " + withSubClass
149: + "): rfs=" + rfs, expected, cpt);
150: }
151: }
|