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.speedoExtension;
025:
026: import java.util.ArrayList;
027: import java.util.Collection;
028: import java.util.Iterator;
029:
030: import javax.jdo.JDOException;
031: import javax.jdo.PersistenceManager;
032: import javax.jdo.Query;
033: import javax.jdo.datastore.Sequence;
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.sequence.id.speedoExtension.Card;
040: import org.objectweb.speedo.pobjects.sequence.id.speedoExtension.Maps;
041: import org.objectweb.util.monolog.api.BasicLevel;
042:
043: /**
044: * Test the sequence defined as speedo extension.
045: * 2 cases:
046: * Card: id field to map the sequence
047: * Map: the id is an hidden field
048: * @author Y.Bersihand
049: */
050: public class TestSequenceSE extends SpeedoTestHelper {
051:
052: public TestSequenceSE(String s) {
053: super (s);
054: }
055:
056: protected String getLoggerName() {
057: return LOG_NAME + ".rt.sequence.TestSequence";
058: }
059:
060: public static final String CARD_SEQ = "org.objectweb.speedo.pobjects.sequence.id.speedoExtension.card_sequence";
061: public static final String MAP_SEQ = "org.objectweb.speedo.pobjects.sequence.id.speedoExtension.map_sequence";
062:
063: /**
064: * Get a sequence and use it to make objects persistent.
065: */
066: public void testSequenceId() {
067: logger.log(BasicLevel.DEBUG,
068: "***************testSequenceId*****************");
069: PersistenceManager pm = pmf.getPersistenceManager();
070: pm.getObjectIdClass(Card.class);
071: //get the sequence
072: Sequence s = pm.getSequence(CARD_SEQ);
073: assertNotNull("Sequence " + CARD_SEQ + " should not be null.",
074: s);
075: Card c1 = new Card();
076: c1.setName("card 1");
077: Card c2 = new Card();
078: c2.setName("card 2");
079:
080: Collection c = new ArrayList();
081: c.add(c1);
082: c.add(c2);
083: //make persistent
084: pm.currentTransaction().begin();
085: pm.makePersistentAll(c);
086: pm.currentTransaction().commit();
087: assertTrue(c1.getId() < c2.getId());
088: pm.close();
089: }
090:
091: /**
092: * Get a sequence and use it to make objects persistent.
093: */
094: public void testSequenceHiddenField() {
095: logger
096: .log(BasicLevel.DEBUG,
097: "***************testSequenceHiddenField*****************");
098: PersistenceManager pm = pmf.getPersistenceManager();
099: pm.getObjectIdClass(Maps.class);
100: //get the sequence
101: Sequence s = pm.getSequence(MAP_SEQ);
102: assertNotNull("Sequence " + MAP_SEQ + " should not be null.", s);
103: Maps m1 = new Maps();
104: m1.setName("map 1");
105: Maps m2 = new Maps();
106: m2.setName("map 2");
107:
108: Collection c = new ArrayList();
109: c.add(m1);
110: c.add(m2);
111: //make persistent
112: pm.currentTransaction().begin();
113: pm.makePersistentAll(c);
114: pm.currentTransaction().commit();
115: pm.close();
116: }
117:
118: /**
119: * Remove all the persistent instances.
120: */
121: public void _testRemovingOfPersistentObject() {
122: PersistenceManager pm = pmf.getPersistenceManager();
123: try {
124: Class[] cs = new Class[] { Maps.class, Card.class };
125: pm.currentTransaction().begin();
126: for (int i = 0; i < cs.length; i++) {
127: Query query = pm.newQuery(cs[i]);
128: Collection col = (Collection) query.execute();
129: Iterator it = col.iterator();
130: while (it.hasNext()) {
131: Object o = it.next();
132: Assert.assertNotNull(
133: "null object in the query result"
134: + cs[i].getName(), o);
135: pm.deletePersistent(o);
136:
137: }
138: query.close(col);
139: }
140: pm.currentTransaction().commit();
141: } catch (JDOException e) {
142: Exception ie = ExceptionHelper.getNested(e);
143: logger.log(BasicLevel.ERROR, "", ie);
144: fail(ie.getMessage());
145: } finally {
146: pm.close();
147: }
148: }
149: }
|