1 18 package org.objectweb.speedo.runtime.map; 19 20 import org.objectweb.speedo.SpeedoTestHelper; 21 import org.objectweb.speedo.pobjects.map.C; 22 import org.objectweb.speedo.pobjects.map.D; 23 24 import java.util.ArrayList ; 25 import java.util.Map ; 26 import javax.jdo.PersistenceManager; 27 28 32 public class TestKeyFieldMap extends SpeedoTestHelper { 33 34 public TestKeyFieldMap(String s) { 35 super(s); 36 } 37 38 protected String getLoggerName() { 39 return LOG_NAME + ".rt.map.TestKeyFieldMap"; 40 } 41 42 public void test1() { 43 int MAP_SIZE = 10; 44 String F1_PREFIX = "f1_"; 45 PersistenceManager pm = pmf.getPersistenceManager(); 46 47 pm.currentTransaction().begin(); 49 C c = new C(1); 50 ArrayList ds = new ArrayList (MAP_SIZE * 2); 51 for(int i=0; i<MAP_SIZE; i++) { 52 String f1 = F1_PREFIX + i; 53 ds.add(new D(i, f1)); 54 } 55 pm.makePersistent(c); 56 pm.makePersistentAll(ds); 57 pm.currentTransaction().commit(); 58 59 pm.currentTransaction().begin(); 61 Map m = c.getDkey2d(); 62 for(int i=0; i<MAP_SIZE; i++) { 63 D d = (D) ds.get(i); 64 m.put(d.getF1(), d); 65 } 66 c = null; 67 ds = null; 68 pm.currentTransaction().commit(); 69 finishTest(pm, MAP_SIZE, F1_PREFIX); 70 } 71 72 public void test2() { 73 int MAP_SIZE = 10; 74 String F1_PREFIX = "f1_"; 75 PersistenceManager pm = pmf.getPersistenceManager(); 76 pm.currentTransaction().begin(); 77 C c = new C(1); 78 Map m = c.getDkey2d(); 79 for(int i=0; i<MAP_SIZE; i++) { 80 String f1 = "f1_" + i; 81 D d = new D(i, f1); 82 m.put(f1, d); 83 } 84 pm.makePersistent(c); 85 pm.currentTransaction().commit(); 86 finishTest(pm, MAP_SIZE, F1_PREFIX); 87 } 88 89 public void test3() { 90 int MAP_SIZE = 10; 91 String F1_PREFIX = "f1_"; 92 PersistenceManager pm = pmf.getPersistenceManager(); 93 pm.currentTransaction().begin(); 94 C c = new C(1); 95 for(int i=0; i<MAP_SIZE; i++) { 96 String f1 = "f1_" + i; 97 D d = new D(i, f1); 98 d.setMyc(c); 99 pm.makePersistent(d); 100 } 101 pm.currentTransaction().commit(); 102 finishTest(pm, MAP_SIZE, F1_PREFIX); 103 } 104 105 private void finishTest(PersistenceManager pm, 106 final int MAP_SIZE, 107 final String F1_PREFIX) { 108 pm.currentTransaction().begin(); 109 long id = MAP_SIZE/2; 110 String f1 = F1_PREFIX + id; 111 C c = (C) pm.getObjectById(pm.newObjectIdInstance(C.class, "1"), false); 112 Object d = c.getDkey2d().get(f1); 113 assertNotNull("No Object associated to the key '" + f1 + "'", d); 114 assertTrue("Object associated to the key '" + f1 +"'", d instanceof D); 115 assertEquals("Bad D identifier ", id, ((D) d).getMyid()); 116 assertNotNull("Bad D identifier ", ((D) d).getMyc()); 117 assertEquals("Bad C identifier ", 1, ((D) d).getMyc().getMyid()); 118 c = ((D) d).getMyc(); 119 ((D) d).setMyc(null); 120 assertNull("The map contains again the D instance", c.getDkey2d().get(f1)); 121 pm.currentTransaction().commit(); 122 123 pm.currentTransaction().begin(); 125 pm.deletePersistent(d); 126 pm.deletePersistentAll(c.getDkey2d().values()); 127 pm.deletePersistent(c); 128 pm.currentTransaction().commit(); 129 pm.close(); 130 } 131 } 132 | Popular Tags |