1 25 26 package org.objectweb.speedo.runtime.sequence.basic; 27 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 32 import javax.jdo.JDODataStoreException; 33 import javax.jdo.JDOException; 34 import javax.jdo.PersistenceManager; 35 import javax.jdo.Query; 36 import javax.jdo.datastore.Sequence; 37 38 import junit.framework.Assert; 39 40 import org.objectweb.speedo.SpeedoTestHelper; 41 import org.objectweb.speedo.api.ExceptionHelper; 42 import org.objectweb.speedo.pobjects.sequence.basic.Cow; 43 import org.objectweb.util.monolog.api.BasicLevel; 44 45 49 public class TestSequence extends SpeedoTestHelper { 50 51 public TestSequence(String s) { 52 super(s); 53 } 54 55 protected String getLoggerName() { 56 return LOG_NAME + ".rt.sequence.TestSequence"; 57 } 58 59 public static final String COW_SEQ = "org.objectweb.speedo.pobjects.sequence.basic.cowseq"; 60 61 public static final String COW_SEQ_TRANSACTIONAL = "org.objectweb.speedo.pobjects.sequence.basic.cowseq_transactional"; 62 63 public static final String COW_SEQ_DATASTORE = "org.objectweb.speedo.pobjects.sequence.basic.cowseq_datastore"; 64 65 public static final int ADDITIONAL = 5; 66 67 public static final int COMPARE_NUMBER = 100; 68 71 public void testSequence() { 72 logger.log(BasicLevel.DEBUG, "***************testSequence*****************"); 73 PersistenceManager pm = pmf.getPersistenceManager(); 74 pm.getObjectIdClass(Cow.class); 75 Sequence s = pm.getSequence(COW_SEQ); 77 assertNotNull("Sequence " + COW_SEQ + " should not be null.", s); 78 Cow c1 = new Cow("cowSeq1", ((Long )s.next()).longValue()); 79 Cow c2 = new Cow("cowSeq2", ((Long )s.next()).longValue()); 80 Cow c3 = new Cow("cowSeq3", ((Long )s.next()).longValue()); 81 assertTrue(c1.getNbOfLegs() < c2.getNbOfLegs()); 82 assertTrue(c2.getNbOfLegs() < c3.getNbOfLegs()); 83 Collection c = new ArrayList (); 84 c.add(c1); 85 c.add(c2); 86 c.add(c3); 87 pm.currentTransaction().begin(); 89 pm.makePersistentAll(c); 90 pm.currentTransaction().commit(); 91 logger.log(BasicLevel.DEBUG, "c1: " + c1.getNbOfLegs()); 92 logger.log(BasicLevel.DEBUG, "c2: " + c2.getNbOfLegs()); 93 logger.log(BasicLevel.DEBUG, "c3: " + c3.getNbOfLegs()); 94 pm.close(); 95 } 96 97 100 public void testAllocate() { 101 logger.log(BasicLevel.DEBUG, "***************testAllocate*****************"); 102 PersistenceManager pm = pmf.getPersistenceManager(); 103 pm.getObjectIdClass(Cow.class); 104 Sequence s = pm.getSequence(COW_SEQ); 106 assertNotNull("Sequence " + COW_SEQ + " should not be null.", s); 107 long beforeAllocate = ((Long ) s.current()).longValue(); 108 Collection col = new ArrayList (); 109 Cow cow = null; 110 for (int i = 1; i <= ADDITIONAL; i++) { 111 cow = new Cow("cowAllocate" + i, ((Long )s.next()).longValue()); 112 col.add(cow); 113 } 114 assertEquals(beforeAllocate + ADDITIONAL, ((Long ) s.current()).longValue()); 115 cow = new Cow("cowAfterAllocate", ((Long )s.next()).longValue()); 116 col.add(cow); 117 pm.currentTransaction().begin(); 118 pm.makePersistentAll(col); 120 s.current(); 121 pm.currentTransaction().commit(); 122 Iterator it = col.iterator(); 123 while (it.hasNext()) { 124 Cow c = (Cow) it.next(); 125 logger.log(BasicLevel.DEBUG, "cow " + c.getName() + ": " + c.getNbOfLegs()); 126 } 127 pm.close(); 128 } 129 130 135 public void testCompareNextToAllocate() { 136 logger.log(BasicLevel.DEBUG, "***************testCompareNextToAllocate*****************"); 137 PersistenceManager pm = pmf.getPersistenceManager(); 138 pm.getObjectIdClass(Cow.class); 139 Sequence s = pm.getSequence(COW_SEQ); 141 assertNotNull("Sequence " + COW_SEQ + " should not be null.", s); 142 long timeToGetManyNext = System.currentTimeMillis(); 143 for (int i = 0; i< COMPARE_NUMBER; i++) { 144 s.next(); 145 } 146 timeToGetManyNext = System.currentTimeMillis() - timeToGetManyNext; 147 long timeToGetManyAllocate = System.currentTimeMillis(); 148 s.allocate(COMPARE_NUMBER); 149 for (int i = 0; i< COMPARE_NUMBER; i++) { 150 s.next(); 151 } 152 timeToGetManyAllocate = System.currentTimeMillis() - timeToGetManyAllocate; 153 logger.log(BasicLevel.DEBUG, COMPARE_NUMBER + " ids generated:\nwith allocate: " + timeToGetManyAllocate 154 + "\nwith next: " + timeToGetManyNext); 155 assertTrue("The time to get many ids with an allocate should be inferior to the time to get many ids with next()", 156 timeToGetManyAllocate<timeToGetManyNext); 157 } 158 159 162 public void testSequenceTransactional1() { 163 logger.log(BasicLevel.DEBUG, "***************testSequenceTransactional1*****************"); 164 PersistenceManager pm = pmf.getPersistenceManager(); 165 pm.getObjectIdClass(Cow.class); 166 Sequence s = pm.getSequence(COW_SEQ_TRANSACTIONAL); 168 assertNotNull("Sequence " + COW_SEQ_TRANSACTIONAL + " should not be null.", s); 169 try { 170 Cow c1 = new Cow("cowSeqNC1", ((Long )s.next()).longValue()); 172 fail("An exception should be caught: a transactional sequence is used outside a transaction."); 173 } catch (Exception e) { 174 assertEquals("Should be a JDODatastoreException due to the fact the transactional" 175 + "sequence has been used outside of a transaction. " + e.getMessage(), JDODataStoreException.class, e.getClass()); 176 } finally { 177 if (pm.currentTransaction().isActive()) 178 pm.currentTransaction().rollback(); 179 pm.close(); 180 } 181 } 182 183 186 public void testSequenceTransactional2() { 187 logger.log(BasicLevel.DEBUG, "***************testSequenceTransactional2*****************"); 188 PersistenceManager pm = pmf.getPersistenceManager(); 189 pm.getObjectIdClass(Cow.class); 190 Sequence s = pm.getSequence(COW_SEQ_TRANSACTIONAL); 192 assertNotNull("Sequence " + COW_SEQ_TRANSACTIONAL + " should not be null.", s); 193 pm.currentTransaction().begin(); 195 Cow c1 = new Cow("cowSeqNC1", ((Long )s.next()).longValue()); 196 Cow c2 = new Cow("cowSeqNC2", ((Long )s.next()).longValue()); 197 Cow c3 = new Cow("cowSeqNC3", ((Long )s.next()).longValue()); 198 assertTrue(c1.getNbOfLegs() < c2.getNbOfLegs()); 199 assertTrue(c2.getNbOfLegs() < c3.getNbOfLegs()); 200 Collection c = new ArrayList (); 201 c.add(c1); 202 c.add(c2); 203 c.add(c3); 204 pm.makePersistentAll(c); 206 pm.currentTransaction().commit(); 207 pm.currentTransaction().begin(); 209 Cow c4 = new Cow("cowSeqNC4", ((Long )s.next()).longValue()); 210 long index = c4.getNbOfLegs(); 212 pm.makePersistent(c4); 213 pm.currentTransaction().rollback(); 215 pm.currentTransaction().begin(); 217 assertTrue(((Long )s.next()).longValue() != index); 219 pm.currentTransaction().commit(); 220 pm.close(); 221 } 222 223 226 public void testSequenceDatastore() { 227 logger.log(BasicLevel.DEBUG, "***************testSequenceDatastore*****************"); 228 PersistenceManager pm = pmf.getPersistenceManager(); 229 pm.getObjectIdClass(Cow.class); 230 Sequence s = pm.getSequence(COW_SEQ_DATASTORE); 232 assertNotNull("Sequence " + COW_SEQ_DATASTORE + " should not be null.", s); 233 pm.currentTransaction().begin(); 235 Cow c1 = new Cow("cowSeqDS1", ((Long )s.next()).longValue()); 236 Cow c2 = new Cow("cowSeqDS2", ((Long )s.next()).longValue()); 237 Cow c3 = new Cow("cowSeqDS3", ((Long )s.next()).longValue()); 238 assertTrue(c1.getNbOfLegs() < c2.getNbOfLegs()); 239 assertTrue(c2.getNbOfLegs() < c3.getNbOfLegs()); 240 Collection c = new ArrayList (); 241 c.add(c1); 242 c.add(c2); 243 c.add(c3); 244 pm.makePersistentAll(c); 246 pm.currentTransaction().commit(); 247 pm.close(); 248 } 249 252 public void testRemovingOfPersistentObject() { 253 PersistenceManager pm = pmf.getPersistenceManager(); 254 try { 255 Class [] cs = new Class []{Cow.class}; 256 pm.currentTransaction().begin(); 257 for(int i=0; i<cs.length; i++) { 258 Query query = pm.newQuery(cs[i]); 259 Collection col = (Collection ) query.execute(); 260 Iterator it = col.iterator(); 261 while(it.hasNext()) { 262 Object o = it.next(); 263 Assert.assertNotNull("null object in the query result" 264 + cs[i].getName(), o); 265 pm.deletePersistent(o); 266 267 } 268 query.close(col); 269 } 270 pm.currentTransaction().commit(); 271 } catch (JDOException e) { 272 Exception ie = ExceptionHelper.getNested(e); 273 logger.log(BasicLevel.ERROR, "", ie); 274 fail(ie.getMessage()); 275 } finally { 276 pm.close(); 277 } 278 } 279 } 280 | Popular Tags |