1 18 package org.objectweb.speedo.runtime.inheritance; 19 20 import org.objectweb.speedo.SpeedoTestHelper; 21 import org.objectweb.speedo.pobjects.inheritance.filtered.datastorelid.RootClass; 22 import org.objectweb.speedo.pobjects.inheritance.filtered.datastorelid.Subclass1; 23 import org.objectweb.speedo.pobjects.inheritance.filtered.datastorelid.Subclass12; 24 import org.objectweb.speedo.pobjects.inheritance.filtered.datastorelid.Subclass2; 25 import org.objectweb.util.monolog.api.BasicLevel; 26 import javax.jdo.Extent; 27 import javax.jdo.Query; 28 import javax.jdo.PersistenceManager; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.Iterator ; 32 33 37 public class TestDatastorelid extends SpeedoTestHelper { 38 39 public TestDatastorelid(String s) { 40 super(s); 41 } 42 43 protected String getLoggerName() { 44 return LOG_NAME + "rt.inheritance.TestDatastorelid"; 45 } 46 47 public void testA() { 48 PersistenceManager pm = pmf.getPersistenceManager(); 49 pm.currentTransaction().begin(); 50 ArrayList rcs = new ArrayList (); 51 rcs.add(new RootClass("rf1")); 52 rcs.add(new RootClass("rf2")); 53 rcs.add(new Subclass1("rf3", "f1_3")); 54 rcs.add(new Subclass12("rf4", "f1_4", "f12_4")); 55 rcs.add(new Subclass2("rf5", "f2_5")); 56 pm.makePersistentAll(rcs); 57 pm.currentTransaction().commit(); 58 59 pm.currentTransaction().begin(); 60 checkNumberOfInstanceWithExtent(RootClass.class, false, 2, pm); 61 checkNumberOfInstanceWithExtent(RootClass.class, true, 5, pm); 62 63 checkNumberOfInstanceWithExtent(Subclass1.class, false, 1, pm); 64 checkNumberOfInstanceWithExtent(Subclass1.class, true, 2, pm); 65 66 checkNumberOfInstanceWithExtent(Subclass12.class, true, 1, pm); 67 checkNumberOfInstanceWithExtent(Subclass12.class, false, 1, pm); 68 69 checkNumberOfInstanceWithExtent(Subclass2.class, false, 1, pm); 70 checkNumberOfInstanceWithExtent(Subclass2.class, true, 1, pm); 71 pm.currentTransaction().commit(); 72 73 pm.currentTransaction().begin(); 74 for(int i=0; i<rcs.size(); i++) { 75 rcs.set(i, pm.getObjectId(rcs.get(i))); 76 } 77 pm.currentTransaction().commit(); 78 pm.evictAll(); 79 80 pm.currentTransaction().begin(); 81 for(int i=0; i<rcs.size(); i++) { 82 pm.getObjectById(rcs.get(i), false); 83 } 84 pm.currentTransaction().commit(); 85 86 pm.evictAll(); 87 88 pm.currentTransaction().begin(); 89 Query query = pm.newQuery(RootClass.class); 90 Collection col = (Collection ) query.execute(); 91 Iterator it = col.iterator(); 92 int size = 0; 93 while(it.hasNext()) { 94 RootClass rc = (RootClass) it.next(); 95 size ++; 96 logger.log(BasicLevel.DEBUG, "rc.rootField=" + rc.getRootField()); 97 if ("rf1".equals(rc.getRootField())) { 98 } else if ("rf2".equals(rc.getRootField())) { 99 } else if ("rf3".equals(rc.getRootField())) { 100 assertTrue("Bad class instance" + rc.getClass().getName(), rc instanceof Subclass1); 101 assertEquals("Bad f1 value", "f1_3", ((Subclass1) rc).getF1()); 102 } else if ("rf4".equals(rc.getRootField())) { 103 assertTrue("Bad class instance" + rc.getClass().getName(), rc instanceof Subclass12); 104 assertEquals("Bad f1 value", "f1_4", ((Subclass12) rc).getF1()); 105 assertEquals("Bad f1 value", "f12_4", ((Subclass12) rc).getF12()); 106 } else if ("rf5".equals(rc.getRootField())) { 107 assertTrue("Bad class instance" + rc.getClass().getName(), rc instanceof Subclass2); 108 assertEquals("Bad f1 value", "f2_5", ((Subclass2) rc).getF2()); 109 } else { 110 fail("Unmanaged rootField value: " + rc.getRootField()); 111 } 112 } 113 query.closeAll(); 114 assertEquals("bad query result size", 5, size); 115 pm.currentTransaction().commit(); 116 117 pm.currentTransaction().begin(); 118 for(int i=0; i<rcs.size(); i++) { 119 pm.deletePersistent(pm.getObjectById(rcs.get(i), false)); 120 } 121 pm.currentTransaction().commit(); 122 pm.close(); 123 124 } 125 126 private void checkNumberOfInstanceWithExtent(Class clazz, 127 boolean withSubClass, 128 int expected, 129 PersistenceManager pm) { 130 Extent e = pm.getExtent(clazz, withSubClass); 131 Iterator it = e.iterator(); 132 int cpt = 0; 133 ArrayList rfs = new ArrayList (expected); 134 while(it.hasNext()) { 135 rfs.add(((RootClass) it.next()).getRootField()); 136 cpt ++; 137 } 138 e.closeAll(); 139 assertEquals("Bad number of " + clazz.getName() 140 + " instance (withSubclass: " + withSubClass + "): rfs=" + rfs, 141 expected, cpt); 142 } 143 } 144 | Popular Tags |