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.sequence.id.inheritance;
025:
026: import java.util.Collection;
027: import java.util.Iterator;
028:
029: import javax.jdo.JDOException;
030: import javax.jdo.PersistenceManager;
031: import javax.jdo.Query;
032: import javax.jdo.datastore.Sequence;
033:
034: import junit.framework.Assert;
035:
036: import org.objectweb.speedo.SpeedoTestHelper;
037: import org.objectweb.speedo.api.ExceptionHelper;
038: import org.objectweb.speedo.pobjects.sequence.id.inheritance.Article;
039: import org.objectweb.speedo.pobjects.sequence.id.inheritance.Materiel;
040: import org.objectweb.speedo.pobjects.sequence.id.inheritance.Service;
041: import org.objectweb.util.monolog.api.BasicLevel;
042:
043: /**
044: * @author Y.Bersihand
045: */
046: public class TestSequenceInheritance extends SpeedoTestHelper {
047:
048: public static final int ADDITIONAL = 5;
049:
050: public TestSequenceInheritance(String s) {
051: super (s);
052: }
053:
054: protected String getLoggerName() {
055: return LOG_NAME + ".rt.sequence.TestSequence";
056: }
057:
058: public static final String ARTICLE_SEQ = "org.objectweb.speedo.pobjects.sequence.id.inheritance.article_seq";
059:
060: /**
061: * Get a sequence and use it to make objects persistent.
062: * A field has a value-strategy set to sequence.
063: */
064: public void testSequenceInheritance1() {
065: logger
066: .log(BasicLevel.DEBUG,
067: "***************testSequenceInheritance1*****************");
068: PersistenceManager pm = pmf.getPersistenceManager();
069: try {
070: pm.getObjectIdClass(Article.class);
071: // get the sequence
072: Sequence s = pm.getSequence(ARTICLE_SEQ);
073: assertNotNull("Sequence " + ARTICLE_SEQ
074: + " should not be null.", s);
075: s.allocate(ADDITIONAL);
076: Article articles[] = new Article[ADDITIONAL];
077: for (int i = 0; i < ADDITIONAL; i++) {
078: if (i % 2 == 0) {
079: articles[i] = new Service();
080: } else {
081: articles[i] = new Materiel();
082: }
083: }
084: // make persistent
085: pm.currentTransaction().begin();
086: pm.makePersistentAll(articles);
087: pm.currentTransaction().commit();
088: for (int i = 0; i < ADDITIONAL - 1; i++) {
089: assertTrue(articles[i].getId() < articles[i + 1]
090: .getId());
091: }
092: } catch (Exception e) {
093: fail(e.getMessage());
094: } finally {
095: if (pm.currentTransaction().isActive())
096: pm.currentTransaction().rollback();
097: pm.close();
098: }
099: }
100:
101: /**
102: * Remove all the persistent instances.
103: */
104: public void testRemovingOfPersistentObject() {
105: PersistenceManager pm = pmf.getPersistenceManager();
106: try {
107: Class[] cs = new Class[] { Article.class };
108: pm.currentTransaction().begin();
109: for (int i = 0; i < cs.length; i++) {
110: Query query = pm.newQuery(cs[i]);
111: Collection col = (Collection) query.execute();
112: Iterator it = col.iterator();
113: while (it.hasNext()) {
114: Object o = it.next();
115: Assert.assertNotNull(
116: "null object in the query result"
117: + cs[i].getName(), o);
118: pm.deletePersistent(o);
119:
120: }
121: query.close(col);
122: }
123: pm.currentTransaction().commit();
124: } catch (JDOException e) {
125: Exception ie = ExceptionHelper.getNested(e);
126: logger.log(BasicLevel.ERROR, "", ie);
127: fail(ie.getMessage());
128: } finally {
129: pm.close();
130: }
131: }
132: }
|