001: /**
002: * Speedo: an implementation of JDO compliant personality on top of JORM generic
003: * I/O sub-system.
004: * Copyright (C) 2001-2004 France Telecom R&D
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: *
021: *
022: * Contact: speedo@objectweb.org
023: *
024: */package org.objectweb.speedo.runtime.inheritance;
025:
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Iterator;
029:
030: import javax.jdo.Extent;
031: import javax.jdo.JDOException;
032: import javax.jdo.PersistenceManager;
033: import javax.jdo.Query;
034:
035: import junit.framework.Assert;
036:
037: import org.objectweb.speedo.SpeedoTestHelper;
038: import org.objectweb.speedo.api.ExceptionHelper;
039: import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.composite.A1;
040: import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.composite.A21;
041: import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.composite.A22;
042: import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.composite.A3;
043: import org.objectweb.speedo.pobjects.inheritance.filterOutOfPK.composite.B;
044: import org.objectweb.util.monolog.api.BasicLevel;
045:
046: /**
047: * Test the inheritance with a single user id and a filter out of the id.
048: * @author Y.Bersihand
049: */
050: public class TestCompositeFilterOutOfPK extends SpeedoTestHelper {
051:
052: public TestCompositeFilterOutOfPK(String s) {
053: super (s);
054: }
055:
056: protected String getLoggerName() {
057: return LOG_NAME + ".rt.inheritance.TestCompositeFilterOutOfPK";
058: }
059:
060: int nbOfA1 = 3;
061: int nbOfA22 = 2;
062: int nbOfA3 = 2;
063: int nbToRetrieve = 0;
064:
065: public void testCompositeFilterOutOfPK() {
066: logger
067: .log(BasicLevel.DEBUG,
068: "***************testCompositeFilterOutOfPK*****************");
069: //create the forms
070: createInheritedObjects();
071: //get all the A1
072: iterateExtent(A1.class);
073: //get all the A21
074: iterateExtent(A21.class);
075: //get all the A22
076: iterateExtent(A22.class);
077: //get all the A3
078: iterateExtent(A3.class);
079: //query
080: retrieveAllXXA(1000);
081: }
082:
083: /**
084: * Create persistent objects
085: */
086: public void createInheritedObjects() {
087: PersistenceManager pm = pmf.getPersistenceManager();
088: try {
089: Collection col = new ArrayList();
090:
091: B b1 = new B(1000, "1000");
092: col.add(b1);
093: B b2 = new B(2000, "2000");
094: col.add(b2);
095: //create A1
096: A1[] a1 = new A1[nbOfA1];
097: for (int i = 0; i < a1.length; i++) {
098: a1[i] = new A1(i + 1, "" + (i + 1), 1, b1);
099: col.add(a1[i]);
100: nbToRetrieve++;
101: }
102: //create A22
103: A22[] a22 = new A22[nbOfA22];
104: for (int i = 0; i < a22.length; i++) {
105: a22[i] = new A22(i + 1 + nbOfA1, "" + (i + 1 + nbOfA1),
106: 22, b1, "this is an A22");
107: col.add(a22[i]);
108: nbToRetrieve++;
109: }
110: //create A3
111: A3[] a3 = new A3[nbOfA3];
112: for (int i = 0; i < a3.length; i++) {
113: a3[i] = new A3(i + 1 + nbOfA1 + nbOfA22, ""
114: + (i + 1 + nbOfA1 + nbOfA22), 3, b2, true, 9);
115: col.add(a3[i]);
116: }
117:
118: // make persistent all the forms
119: pm.currentTransaction().begin();
120: pm.makePersistentAll(col);
121: pm.currentTransaction().commit();
122: } catch (Exception e) {
123: fail(e.getMessage());
124: } finally {
125: if (pm.currentTransaction().isActive())
126: pm.currentTransaction().rollback();
127: //remove all the objects from the cache
128: pm.evictAll();
129: pm.close();
130: }
131: }
132:
133: //iterate over all the instances of a class
134: public void iterateExtent(Class cl) {
135: PersistenceManager pm = pmf.getPersistenceManager();
136: try {
137: int nb = 0;
138: int nbExpected = 0;
139: if (cl == A1.class) {
140: nbExpected = nbOfA1 + nbOfA22 + nbOfA3;
141: } else if (cl == A21.class) {
142: nbExpected = nbOfA22;
143: } else if (cl == A22.class) {
144: nbExpected = nbOfA22;
145: } else if (cl == A3.class) {
146: nbExpected = nbOfA3;
147: }
148:
149: pm.currentTransaction().begin();
150: Extent extent = pm.getExtent(cl, true);
151: Iterator it = extent.iterator();
152: String className = cl.getName().substring(
153: cl.getName().lastIndexOf("."));
154: logger.log(BasicLevel.DEBUG, "All " + cl.getName()
155: + " instances:");
156: while (it.hasNext()) {
157: A1 a = (A1) it.next();
158: assertNotNull("The A should not be null", a);
159: nb++;
160: }
161: extent.close(it);
162: pm.currentTransaction().commit();
163: assertEquals(
164: "The size of elements retrieved is not right.",
165: nbExpected, nb);
166: } catch (Exception e) {
167: fail(e.getMessage());
168: } finally {
169: if (pm.currentTransaction().isActive())
170: pm.currentTransaction().rollback();
171: pm.evictAll();
172: pm.close();
173: }
174: }
175:
176: public void retrieveAllXXA(int bid) {
177: PersistenceManager pm = pmf.getPersistenceManager();
178: try {
179: Query query = pm.newQuery(A1.class, "b.b1==" + bid);
180: Collection results = (Collection) query.execute();
181: assertEquals(nbToRetrieve, results.size());
182: logger.log(BasicLevel.DEBUG, "All A1 linked to the " + bid
183: + " B:");
184: Iterator it = results.iterator();
185: while (it.hasNext()) {
186: A1 a = (A1) it.next();
187: assertEquals(bid, a.getB().getB1());
188: }
189: query.closeAll();
190: } catch (Exception e) {
191: fail(e.getMessage());
192: } finally {
193: pm.evictAll();
194: pm.close();
195: }
196: }
197:
198: public void testRemovingOfPersistentObject() {
199: PersistenceManager pm = pmf.getPersistenceManager();
200: try {
201: Class[] cs = new Class[] { A1.class, B.class };
202: pm.currentTransaction().begin();
203: for (int i = 0; i < cs.length; i++) {
204: Query query = pm.newQuery(cs[i]);
205: Collection col = (Collection) query.execute();
206: Iterator it = col.iterator();
207: while (it.hasNext()) {
208: Object o = it.next();
209: Assert.assertNotNull(
210: "null object in the query result"
211: + cs[i].getName(), o);
212: pm.deletePersistent(o);
213:
214: }
215: query.close(col);
216: }
217: pm.currentTransaction().commit();
218: } catch (JDOException e) {
219: Exception ie = ExceptionHelper.getNested(e);
220: logger.log(BasicLevel.ERROR, "", ie);
221: fail(ie.getMessage());
222: } finally {
223: pm.close();
224: }
225: }
226: }
|