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.index.Daughter;
040: import org.objectweb.speedo.pobjects.inheritance.index.Mother;
041: import org.objectweb.util.monolog.api.BasicLevel;
042:
043: /**
044: * Test the queries and extent with prefetch in case of inheritance.
045: * The aim is to have coherent order between java fields and sql columns.
046: * @author Y.Bersihand
047: */
048: public class TestIndex extends SpeedoTestHelper {
049:
050: public TestIndex(String s) {
051: super (s);
052: }
053:
054: protected String getLoggerName() {
055: return LOG_NAME + ".rt.inheritance.TestIndex";
056: }
057:
058: /**
059: * create the objects
060: */
061: public void testCreate() {
062: PersistenceManager pm = pmf.getPersistenceManager();
063: try {
064: Mother m1 = new Mother(1, "mother1", true);
065: Mother m2 = new Mother(2, "mother2", true);
066: Daughter d1 = new Daughter(3, "daughter1", false, new Long(
067: 3));
068: Daughter d2 = new Daughter(4, "daughter2", false, new Long(
069: 4));
070: Collection col = new ArrayList();
071: col.add(m1);
072: col.add(m2);
073: col.add(d1);
074: col.add(d2);
075: // make persistent all the objects
076: pm.currentTransaction().begin();
077: pm.makePersistentAll(col);
078: pm.currentTransaction().commit();
079: } catch (Exception e) {
080: fail(e.getMessage());
081: } finally {
082: if (pm.currentTransaction().isActive())
083: pm.currentTransaction().rollback();
084: // remove all the objects from the cache
085: pm.evictAll();
086: pm.close();
087: }
088: }
089:
090: /**
091: * query: select super
092: */
093: public void testSelectSuper() {
094: PersistenceManager pm = pmf.getPersistenceManager();
095: try {
096: Query query = pm.newQuery(Mother.class);
097: String filter = "java1 > 0";
098: query.setFilter(filter);
099: Collection result = (Collection) query.execute();
100: assertNotNull(result);
101: Iterator it = result.iterator();
102: while (it.hasNext()) {
103: Mother m = (Mother) it.next();
104: assertTrue("Field java1 should be superior to 0.", m
105: .getJava1() > 0);
106: }
107: } catch (Exception e) {
108: fail(e.getMessage());
109: } finally {
110: pm.evictAll();
111: pm.close();
112: }
113: }
114:
115: /**
116: * query: select subclass
117: */
118: public void testSelectSubclass() {
119: PersistenceManager pm = pmf.getPersistenceManager();
120: try {
121: Query query = pm.newQuery(Daughter.class);
122: String filter = "java1 > 0";
123: query.setFilter(filter);
124: Collection result = (Collection) query.execute();
125: assertNotNull(result);
126: Iterator it = result.iterator();
127: while (it.hasNext()) {
128: Mother m = (Mother) it.next();
129: assertTrue("Field java1 should be superior to 0.", m
130: .getJava1() > 0);
131: }
132: } catch (Exception e) {
133: fail(e.getMessage());
134: } finally {
135: pm.evictAll();
136: pm.close();
137: }
138: }
139:
140: /**
141: * extent: super with subclasses
142: */
143: public void testExtentSuperWithSubclasses() {
144: computeExtent(Mother.class, true);
145: }
146:
147: /**
148: * extent: super without subclasses
149: */
150: public void testExtentSuperWithoutSubclasses() {
151: computeExtent(Mother.class, false);
152: }
153:
154: /**
155: * extent: subclass with subclasses
156: */
157: public void testExtentSubWithSubclasses() {
158: computeExtent(Daughter.class, true);
159: }
160:
161: /**
162: * extent: subclass without subclasses
163: */
164: public void testExtentSubWithoutSubclasses() {
165: computeExtent(Daughter.class, false);
166: }
167:
168: /**
169: * extent
170: */
171: public void computeExtent(Class cl, boolean withSubclasses) {
172: PersistenceManager pm = pmf.getPersistenceManager();
173: try {
174: Extent extent = pm.getExtent(cl, withSubclasses);
175: Iterator it = extent.iterator();
176: while (it.hasNext()) {
177: Mother m = (Mother) it.next();
178: assertTrue(m.getJava1() > 0);
179: }
180: } catch (Exception e) {
181: fail(e.getMessage());
182: } finally {
183: pm.evictAll();
184: pm.close();
185: }
186: }
187:
188: /**
189: * Remove the objects from the database.
190: *
191: */
192: public void testRemovingOfPersistentObject() {
193: PersistenceManager pm = pmf.getPersistenceManager();
194: try {
195: Class[] cs = new Class[] { Mother.class };
196: pm.currentTransaction().begin();
197: for (int i = 0; i < cs.length; i++) {
198: Query query = pm.newQuery(cs[i]);
199: Collection col = (Collection) query.execute();
200: Iterator it = col.iterator();
201: while (it.hasNext()) {
202: Object o = it.next();
203: Assert.assertNotNull(
204: "null object in the query result"
205: + cs[i].getName(), o);
206: pm.deletePersistent(o);
207:
208: }
209: query.close(col);
210: }
211: pm.currentTransaction().commit();
212: } catch (JDOException e) {
213: Exception ie = ExceptionHelper.getNested(e);
214: logger.log(BasicLevel.ERROR, "", ie);
215: fail(ie.getMessage());
216: } finally {
217: pm.close();
218: }
219: }
220: }
|