1 25 26 package org.objectweb.speedo.runtime.sequence.id; 27 28 import java.util.Collection ; 29 import java.util.Iterator ; 30 31 import javax.jdo.JDOException; 32 import javax.jdo.PersistenceManager; 33 import javax.jdo.Query; 34 import javax.jdo.datastore.Sequence; 35 36 import junit.framework.Assert; 37 38 import org.objectweb.speedo.SpeedoTestHelper; 39 import org.objectweb.speedo.api.ExceptionHelper; 40 import org.objectweb.speedo.pobjects.sequence.id.CompactDisc; 41 import org.objectweb.speedo.pobjects.sequence.id.Phone; 42 import org.objectweb.speedo.pobjects.sequence.id.Product; 43 import org.objectweb.speedo.pobjects.sequence.id.Suitcase; 44 import org.objectweb.util.monolog.api.BasicLevel; 45 46 49 public class TestSequenceId extends SpeedoTestHelper { 50 51 public static final int ADDITIONAL = 100; 52 53 public TestSequenceId(String s) { 54 super(s); 55 } 56 57 protected String getLoggerName() { 58 return LOG_NAME + ".rt.sequence.TestSequence"; 59 } 60 61 public static final String PRODUCT_SEQ = "org.objectweb.speedo.pobjects.sequence.id.product_seq"; 62 public static final String PHONE_SEQ = "org.objectweb.speedo.pobjects.sequence.id.phone_seq"; 63 public static final String SUITCASE_SEQ = "org.objectweb.speedo.pobjects.sequence.id.suitcase_seq"; 64 public static final String CD_SEQ = "org.objectweb.speedo.pobjects.sequence.id.cd_seq"; 65 66 70 public void testSequenceId1() { 71 logger.log(BasicLevel.DEBUG, "***************testSequenceId1*****************"); 72 PersistenceManager pm = pmf.getPersistenceManager(); 73 pm.getObjectIdClass(Product.class); 74 Sequence s = pm.getSequence(PRODUCT_SEQ); 76 assertNotNull("Sequence " + PRODUCT_SEQ + " should not be null.", s); 77 Product p1 = new Product(); 78 p1.setName("product 1"); 79 Product p2 = new Product(); 80 p2.setName("product 2"); 81 pm.currentTransaction().begin(); 83 pm.makePersistent(p1); 84 pm.makePersistent(p2); 85 pm.currentTransaction().commit(); 86 assertTrue(p1.getReference() < p2.getReference()); 87 pm.close(); 88 } 89 90 94 public void testSequenceId2() { 95 logger.log(BasicLevel.DEBUG, "***************testSequenceId2*****************"); 96 PersistenceManager pm = pmf.getPersistenceManager(); 97 pm.getObjectIdClass(Phone.class); 98 Sequence s = pm.getSequence(PHONE_SEQ); 100 assertNotNull("Sequence " + PHONE_SEQ + " should not be null.", s); 101 Phone p1 = new Phone(); 102 p1.setName("phone 1"); 103 Phone p2 = new Phone(); 104 p2.setName("phone 2"); 105 pm.currentTransaction().begin(); 107 pm.makePersistent(p1); 108 pm.makePersistent(p2); 109 pm.currentTransaction().commit(); 110 pm.close(); 111 } 112 113 public void testAllocateRdbSequence() { 114 logger.log(BasicLevel.DEBUG, "***************testAllocateRdbSequence*****************"); 115 PersistenceManager pm = pmf.getPersistenceManager(); 116 try { 117 pm.getObjectIdClass(Phone.class); 118 Sequence s = pm.getSequence(PHONE_SEQ); 120 assertNotNull("Sequence " + PHONE_SEQ + " should not be null.", s); 121 Phone[] phones = new Phone[ADDITIONAL]; 122 long timeAllocate = System.currentTimeMillis(); 123 pm.currentTransaction().begin(); 124 s.allocate(ADDITIONAL); 126 for (int i = 0; i < ADDITIONAL ; i++) { 127 phones[i] = new Phone(); 128 phones[i].setName("phone " + i); 129 } 130 pm.makePersistentAll(phones); 132 pm.currentTransaction().commit(); 133 timeAllocate = System.currentTimeMillis() - timeAllocate; 134 long timeNext = System.currentTimeMillis(); 135 pm.currentTransaction().begin(); 136 for (int i = ADDITIONAL; i < ADDITIONAL*2 ; i++) { 137 phones[i - ADDITIONAL] = new Phone(); 138 phones[i - ADDITIONAL].setName("phone " + i); 139 } 140 pm.makePersistentAll(phones); 142 pm.currentTransaction().commit(); 143 timeNext = System.currentTimeMillis() - timeNext; 144 assertTrue("Time with allocate should be <= to time with next. \nAllocate: " + timeAllocate 145 + "\nNext: " + timeNext, timeAllocate <= timeNext); 146 } catch (Exception e) { 147 fail(e.getMessage()); 148 } finally { 149 if (pm.currentTransaction().isActive()) 150 pm.currentTransaction().rollback(); 151 pm.close(); 152 } 153 } 154 155 160 public void testApplicationId1() { 161 logger.log(BasicLevel.DEBUG, "***************testApplicationId1*****************"); 162 PersistenceManager pm = pmf.getPersistenceManager(); 163 pm.getObjectIdClass(Suitcase.class); 164 Sequence s = pm.getSequence(SUITCASE_SEQ); 166 assertNotNull("Sequence " + SUITCASE_SEQ + " should not be null.", s); 167 Suitcase s1 = new Suitcase(); 168 s1.setColor("black"); 169 Suitcase s2 = new Suitcase(); 170 s2.setColor("grey"); 171 pm.currentTransaction().begin(); 173 pm.makePersistent(s1); 174 pm.makePersistent(s2); 175 pm.currentTransaction().commit(); 176 assertTrue(s1.getId() < s2.getId()); 177 pm.close(); 178 } 179 180 184 public void testApplicationId2() { 185 logger.log(BasicLevel.DEBUG, "***************testApplicationId2*****************"); 186 PersistenceManager pm = pmf.getPersistenceManager(); 187 pm.getObjectIdClass(CompactDisc.class); 188 Sequence s = pm.getSequence(CD_SEQ); 190 assertNotNull("Sequence " + CD_SEQ + " should not be null.", s); 191 CompactDisc cd1 = new CompactDisc(); 192 cd1.setTitle("cd1"); 193 CompactDisc cd2 = new CompactDisc(); 194 cd1.setTitle("cd2"); 195 pm.currentTransaction().begin(); 197 pm.makePersistent(cd1); 198 pm.makePersistent(cd2); 199 pm.currentTransaction().commit(); 200 pm.close(); 201 } 202 203 206 public void testRemovingOfPersistentObject() { 207 PersistenceManager pm = pmf.getPersistenceManager(); 208 try { 209 Class [] cs = new Class []{Product.class, 210 Phone.class, 211 Suitcase.class, 212 CompactDisc.class}; 213 pm.currentTransaction().begin(); 214 for(int i=0; i<cs.length; i++) { 215 Query query = pm.newQuery(cs[i]); 216 Collection col = (Collection ) query.execute(); 217 Iterator it = col.iterator(); 218 while(it.hasNext()) { 219 Object o = it.next(); 220 Assert.assertNotNull("null object in the query result" 221 + cs[i].getName(), o); 222 pm.deletePersistent(o); 223 224 } 225 query.close(col); 226 } 227 pm.currentTransaction().commit(); 228 } catch (JDOException e) { 229 Exception ie = ExceptionHelper.getNested(e); 230 logger.log(BasicLevel.ERROR, "", ie); 231 fail(ie.getMessage()); 232 } finally { 233 pm.close(); 234 } 235 } 236 } 237 | Popular Tags |