1 25 26 package org.objectweb.speedo.runtime.sequence.id.inheritance; 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.inheritance.Article; 41 import org.objectweb.speedo.pobjects.sequence.id.inheritance.Materiel; 42 import org.objectweb.speedo.pobjects.sequence.id.inheritance.Service; 43 import org.objectweb.util.monolog.api.BasicLevel; 44 45 48 public class TestSequenceInheritance extends SpeedoTestHelper { 49 50 public static final int ADDITIONAL = 5; 51 52 public TestSequenceInheritance(String s) { 53 super(s); 54 } 55 56 protected String getLoggerName() { 57 return LOG_NAME + ".rt.sequence.TestSequence"; 58 } 59 60 public static final String ARTICLE_SEQ = "org.objectweb.speedo.pobjects.sequence.id.inheritance.article_seq"; 61 62 66 public void testSequenceInheritance1() { 67 logger.log(BasicLevel.DEBUG, "***************testSequenceInheritance1*****************"); 68 PersistenceManager pm = pmf.getPersistenceManager(); 69 try { 70 pm.getObjectIdClass(Article.class); 71 Sequence s = pm.getSequence(ARTICLE_SEQ); 73 assertNotNull("Sequence " + ARTICLE_SEQ + " should not be null.", s); 74 s.allocate(ADDITIONAL); 75 Article articles[] = new Article[ADDITIONAL]; 76 for (int i = 0; i < ADDITIONAL; i++) { 77 if (i%2 == 0) { 78 articles[i] = new Service(); 79 } else { 80 articles[i] = new Materiel(); 81 } 82 } 83 pm.currentTransaction().begin(); 85 pm.makePersistentAll(articles); 86 pm.currentTransaction().commit(); 87 for (int i = 0; i < ADDITIONAL-1; i++) { 88 assertTrue(articles[i].getId() < articles[i+1].getId()); 89 } 90 } catch (Exception e) { 91 fail(e.getMessage()); 92 } finally { 93 if (pm.currentTransaction().isActive()) 94 pm.currentTransaction().rollback(); 95 pm.close(); 96 } 97 } 98 99 102 public void testRemovingOfPersistentObject() { 103 PersistenceManager pm = pmf.getPersistenceManager(); 104 try { 105 Class [] cs = new Class []{Article.class}; 106 pm.currentTransaction().begin(); 107 for(int i=0; i<cs.length; i++) { 108 Query query = pm.newQuery(cs[i]); 109 Collection col = (Collection ) query.execute(); 110 Iterator it = col.iterator(); 111 while(it.hasNext()) { 112 Object o = it.next(); 113 Assert.assertNotNull("null object in the query result" 114 + cs[i].getName(), o); 115 pm.deletePersistent(o); 116 117 } 118 query.close(col); 119 } 120 pm.currentTransaction().commit(); 121 } catch (JDOException e) { 122 Exception ie = ExceptionHelper.getNested(e); 123 logger.log(BasicLevel.ERROR, "", ie); 124 fail(ie.getMessage()); 125 } finally { 126 pm.close(); 127 } 128 } 129 } 130 | Popular Tags |