1 18 package org.objectweb.speedo.runtime.collection; 19 20 import org.objectweb.speedo.SpeedoTestHelper; 21 import org.objectweb.speedo.pobjects.collection.Employee; 22 import org.objectweb.speedo.pobjects.collection.AMMB; 23 import org.objectweb.speedo.pobjects.collection.BMMB; 24 import org.objectweb.util.monolog.api.BasicLevel; 25 26 import javax.jdo.PersistenceManager; 27 28 import junit.framework.Assert; 29 30 import java.util.List ; 31 import java.util.ArrayList ; 32 import java.util.Collection ; 33 import java.util.Iterator ; 34 35 39 public class POBuilder extends SpeedoTestHelper { 40 41 public final static String EMPLOYEE_NAME = "POBuilder_testCreateEmployee"; 42 public final static String EMPLOYEE_NAME2 = "POBuilder_testSetCollection"; 43 public final static int NB_ELEMENTS = 20; 44 public final static int NB_ELEMENTS2 = 5; 45 46 public static String getName(int i) { 47 return "testIterator_e" + i; 48 } 49 50 public static String getName2(int i) { 51 return "testSetCollection_e" + i; 52 } 53 54 public static List getElementNames() { 55 ArrayList res = new ArrayList (NB_ELEMENTS); 56 for(int i=0; i<NB_ELEMENTS; i++) { 57 res.add(getName(i)); 58 } 59 return res; 60 } 61 62 public static List getElementNames2() { 63 ArrayList res = new ArrayList (NB_ELEMENTS2); 64 for(int i=0; i<NB_ELEMENTS2; i++) { 65 res.add(getName2(i)); 66 } 67 return res; 68 } 69 70 public POBuilder(String s) { 71 super(s); 72 } 73 74 protected String getLoggerName() { 75 return LOG_NAME + ".rt.collection.POBuilder"; 76 } 77 78 public void testCreateEmployee() { 79 logger.log(BasicLevel.DEBUG, "Start testCreateEmployee"); 80 Employee e = new Employee(EMPLOYEE_NAME, null); 81 82 PersistenceManager pm = pmf.getPersistenceManager(); 83 pm.makePersistent(e); 84 Employee[] es = new Employee[NB_ELEMENTS]; 85 for(int i=0; i<NB_ELEMENTS; i++) { 86 es[i]= new Employee(getName(i)); 87 e.addFriend(es[i]); 88 e.addInt(i); 89 } 90 Assert.assertEquals("Bad sise:" , NB_ELEMENTS, e.friends.size()); 91 92 Collection elems = e.friends; 93 Assert.assertNotNull("Null collection field", elems); 94 Iterator elemIt = elems.iterator(); 95 Assert.assertNotNull("Null iterator over elements", elemIt); 96 ArrayList elemNames = new ArrayList (); 97 while(elemIt.hasNext()) { 98 Object elem = elemIt.next(); 99 Assert.assertNotNull("Null element", elem); 100 Assert.assertEquals("bad elem type", Employee.class, elem.getClass()); 101 elemNames.add(((Employee) elem).name); 102 } 103 assertSameCollection("Bad element names: ", POBuilder.getElementNames(), elemNames); 104 105 pm.close(); 106 } 107 108 public void testSetCollection() { 109 logger.log(BasicLevel.DEBUG, "Start testSetCollection"); 110 Employee e = new Employee(EMPLOYEE_NAME2, null); 111 112 PersistenceManager pm = pmf.getPersistenceManager(); 113 pm.currentTransaction().begin(); 114 pm.makePersistent(e); 115 pm.currentTransaction().commit(); 116 117 ArrayList es = new ArrayList (); 118 119 pm.currentTransaction().begin(); 120 for(int i=0; i<NB_ELEMENTS2; i++) { 121 es.add(new Employee(getName2(i))); 122 } 123 e.setFriends(es); 124 125 Collection elems = e.friends; 126 Assert.assertNotNull("Null collection field", elems); 127 Iterator elemIt = elems.iterator(); 128 Assert.assertNotNull("Null iterator over elements", elemIt); 129 ArrayList elemNames = new ArrayList (); 130 while(elemIt.hasNext()) { 131 Object elem = elemIt.next(); 132 Assert.assertNotNull("Null element", elem); 133 Assert.assertEquals("bad elem type", Employee.class, elem.getClass()); 134 elemNames.add(((Employee) elem).name); 135 } 136 assertSameCollection("Bad element names: ", POBuilder.getElementNames2(), elemNames); 137 138 pm.currentTransaction().commit(); 139 pm.close(); 140 } 141 142 public void testCreateXMMB() { 143 logger.log(BasicLevel.DEBUG, "Start testCreateXMMB"); 144 long ida = 1230; 145 AMMB a = new AMMB(ida); 146 ArrayList bs = new ArrayList (); 147 ArrayList as = new ArrayList (1); 148 as.add(a); 149 for(int i=0; i<5; i++) { 150 BMMB b = new BMMB(ida + i); 151 b.setAs(as); 152 bs.add(b); 153 } 154 a.setBs(bs); 155 PersistenceManager pm = pmf.getPersistenceManager(); 156 pm.makePersistent(a); 157 pm.close(); 158 } 159 } 160 | Popular Tags |