/**
* Speedo: an implementation of JDO compliant personality on top of JORM generic
* I/O sub-system.
* Copyright (C) 2001-2004 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*
*
* Contact: speedo@objectweb.org
*
*/
package org.objectweb.speedo.runtime.sequence.id.speedoExtension;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import javax.jdo.JDOException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.jdo.datastore.Sequence;
import junit.framework.Assert;
import org.objectweb.speedo.SpeedoTestHelper;
import org.objectweb.speedo.api.ExceptionHelper;
import org.objectweb.speedo.pobjects.sequence.id.speedoExtension.Card;
import org.objectweb.speedo.pobjects.sequence.id.speedoExtension.Maps;
import org.objectweb.util.monolog.api.BasicLevel;
/**
* Test the sequence defined as speedo extension.
* 2 cases:
* Card: id field to map the sequence
* Map: the id is an hidden field
* @author Y.Bersihand
*/
public class TestSequenceSE extends SpeedoTestHelper {
public TestSequenceSE(String s) {
super(s);
}
protected String getLoggerName() {
return LOG_NAME + ".rt.sequence.TestSequence";
}
public static final String CARD_SEQ = "org.objectweb.speedo.pobjects.sequence.id.speedoExtension.card_sequence";
public static final String MAP_SEQ = "org.objectweb.speedo.pobjects.sequence.id.speedoExtension.map_sequence";
/**
* Get a sequence and use it to make objects persistent.
*/
public void testSequenceId() {
logger.log(BasicLevel.DEBUG, "***************testSequenceId*****************");
PersistenceManager pm = pmf.getPersistenceManager();
pm.getObjectIdClass(Card.class);
//get the sequence
Sequence s = pm.getSequence(CARD_SEQ);
assertNotNull("Sequence " + CARD_SEQ + " should not be null.", s);
Card c1 = new Card();
c1.setName("card 1");
Card c2 = new Card();
c2.setName("card 2");
Collection c = new ArrayList();
c.add(c1);
c.add(c2);
//make persistent
pm.currentTransaction().begin();
pm.makePersistentAll(c);
pm.currentTransaction().commit();
assertTrue(c1.getId() < c2.getId());
pm.close();
}
/**
* Get a sequence and use it to make objects persistent.
*/
public void testSequenceHiddenField() {
logger.log(BasicLevel.DEBUG, "***************testSequenceHiddenField*****************");
PersistenceManager pm = pmf.getPersistenceManager();
pm.getObjectIdClass(Maps.class);
//get the sequence
Sequence s = pm.getSequence(MAP_SEQ);
assertNotNull("Sequence " + MAP_SEQ + " should not be null.", s);
Maps m1 = new Maps();
m1.setName("map 1");
Maps m2 = new Maps();
m2.setName("map 2");
Collection c = new ArrayList();
c.add(m1);
c.add(m2);
//make persistent
pm.currentTransaction().begin();
pm.makePersistentAll(c);
pm.currentTransaction().commit();
pm.close();
}
/**
* Remove all the persistent instances.
*/
public void _testRemovingOfPersistentObject() {
PersistenceManager pm = pmf.getPersistenceManager();
try {
Class[] cs = new Class[]{Maps.class, Card.class};
pm.currentTransaction().begin();
for(int i=0; i<cs.length; i++) {
Query query = pm.newQuery(cs[i]);
Collection col = (Collection) query.execute();
Iterator it = col.iterator();
while(it.hasNext()) {
Object o = it.next();
Assert.assertNotNull("null object in the query result"
+ cs[i].getName(), o);
pm.deletePersistent(o);
}
query.close(col);
}
pm.currentTransaction().commit();
} catch (JDOException e) {
Exception ie = ExceptionHelper.getNested(e);
logger.log(BasicLevel.ERROR, "", ie);
fail(ie.getMessage());
} finally {
pm.close();
}
}
}