1 25 26 package org.objectweb.jonas.jtests.clients.entity; 27 28 import java.util.ArrayList ; 29 import java.util.Collection ; 30 import java.util.Enumeration ; 31 import java.util.Hashtable ; 32 import javax.naming.NamingException ; 33 import javax.rmi.PortableRemoteObject ; 34 import junit.framework.Test; 35 import junit.framework.TestSuite; 36 import org.objectweb.jonas.jtests.beans.relation.pkcomp.APK; 37 import org.objectweb.jonas.jtests.beans.relation.pkcomp.AHomeRemote; 38 import org.objectweb.jonas.jtests.beans.relation.pkcomp.ARemote; 39 import org.objectweb.jonas.jtests.beans.relation.pkcomp.BPK; 40 import org.objectweb.jonas.jtests.beans.relation.pkcomp.BHomeRemote; 41 import org.objectweb.jonas.jtests.beans.relation.pkcomp.BRemote; 42 43 52 public class F_Relation_pkcompEC2 extends A_Cmp2Util { 53 54 static Hashtable tbRelationA2B = new Hashtable (); 55 56 57 static { 58 tbRelationA2B.put("a-0", new String []{}); 59 tbRelationA2B.put("a-1", new String []{"b-1", "b-2"}); 60 tbRelationA2B.put("a-2", new String []{"b-1", "b-2", "b-3"}); 61 tbRelationA2B.put("a-3", new String []{"b-2", "b-3", "b-4"}); 62 for (Enumeration ea = tbRelationA2B.keys(); ea.hasMoreElements();) { 64 String aname = (String ) (ea.nextElement()); 65 String [] tb = (String []) tbRelationA2B.get(aname); 66 ArrayList col = new ArrayList (tb.length); 67 for (int i = 0; i < tb.length; i++) { 68 BPK bpk = new BPK(getStringBeforeDash(tb[i]), getIntAfterDash(tb[i])); 69 col.add(bpk); 70 } 71 tbRelationA2B.put(aname, col); 72 } 73 } 74 75 static Hashtable tbRelationB2A = new Hashtable (); 76 77 static { 78 tbRelationB2A.put("b-0", new String []{}); 79 tbRelationB2A.put("b-1", new String []{"a-1", "a-2"}); 80 tbRelationB2A.put("b-2", new String []{"a-1", "a-2", "a-3"}); 81 tbRelationB2A.put("b-3", new String []{"a-2", "a-3"}); 82 tbRelationB2A.put("b-4", new String []{"a-3"}); 83 for (Enumeration eb = tbRelationB2A.keys(); eb.hasMoreElements();) { 85 String bname = (String ) (eb.nextElement()); 86 String [] tb = (String []) tbRelationB2A.get(bname); 87 ArrayList col = new ArrayList (tb.length); 88 for (int i = 0; i < tb.length; i++) { 89 APK apk = new APK(getStringBeforeDash(tb[i]), getIntAfterDash(tb[i])); 90 col.add(apk); 91 } 92 tbRelationB2A.put(bname, col); 93 } 94 } 95 96 private static String BEAN_HOME_A = "relation_pkcomp_AHome"; 97 protected static AHomeRemote ahome = null; 98 private static String BEAN_HOME_B = "relation_pkcomp_BHome"; 99 protected static BHomeRemote bhome = null; 100 101 102 public F_Relation_pkcompEC2(String name) { 103 super(name); 104 } 105 106 protected static boolean isInit = false; 107 108 protected void setUp() { 109 super.setUp(); 110 boolean ok = false; 111 int nbtry = 0; 112 while (!ok && nbtry < 3) { 113 if (!isInit) { 114 useBeans("pkcomp", false); 116 try { 118 ahome = (AHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_A), 119 AHomeRemote.class); 120 bhome = (BHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_B), 121 BHomeRemote.class); 122 } catch (NamingException e) { 123 fail("Cannot get bean home: " + e.getMessage()); 124 } 125 try { 127 ahome.findByPrimaryKey(new APK("a", 0)); 128 } catch (Exception e) { 129 try { 130 utx.begin(); 131 ARemote a0 = ahome.create("a", 0); 132 ARemote a1 = ahome.create("a", 1); 133 ARemote a2 = ahome.create("a", 2); 134 ARemote a3 = ahome.create("a", 3); 135 bhome.create("b", 0); 136 bhome.create("b", 1); 137 bhome.create("b", 2); 138 bhome.create("b", 3); 139 bhome.create("b", 4); 140 a1.assignB((Collection ) tbRelationA2B.get("a-1")); 141 a2.assignB((Collection ) tbRelationA2B.get("a-2")); 142 a3.assignB((Collection ) tbRelationA2B.get("a-3")); 143 } catch (Exception i) { 144 fail("InitialState creation problem: " + i); 145 } finally { 146 try { 147 utx.commit(); 148 } catch (Exception ii) { 149 debug("Cannot commit table creation" + ii); 150 } 151 } 152 } 153 isInit = true; 154 } 155 nbtry++; 158 try { 159 if (initStateOK()) { 160 ok = true; 161 } 162 } catch (Exception e) { 163 debug("Cannot check initial state: " + e); 164 } 165 if (!ok) { 166 isInit = false; 167 unloadBeans("pkcomp"); 168 } 169 } 170 } 171 172 176 boolean initStateOK() throws Exception { 177 boolean isOk = true; 178 msgerror = new StringBuffer (); 179 for (Enumeration ea = tbRelationA2B.keys(); ea.hasMoreElements();) { 181 String aname = (String ) (ea.nextElement()); 182 APK apk = new APK(getStringBeforeDash(aname), getIntAfterDash(aname)); 183 ARemote a = ahome.findByPrimaryKey(apk); 184 Collection colActual = a.retrieveB(); 185 ArrayList colExpected = (ArrayList ) (tbRelationA2B.get(aname)); 186 if (!isCollectionEqual(colExpected, colActual)) { 187 isOk = false; 188 msgerror = msgerror.append("Wrong relation for " + aname 189 + " (expected:" + colExpected 190 + ", found:" + colActual + ")"); 191 } 192 } 193 for (Enumeration eb = tbRelationB2A.keys(); eb.hasMoreElements();) { 195 String bname = (String ) (eb.nextElement()); 196 BPK bpk = new BPK(getStringBeforeDash(bname), getIntAfterDash(bname)); 197 BRemote b = bhome.findByPrimaryKey(bpk); 198 Collection colActual1 = b.retrieveA(); 199 ArrayList colExpected1 = (ArrayList ) (tbRelationB2A.get(bname)); 200 if (!isCollectionEqual(colExpected1, colActual1)) { 201 isOk = false; 202 msgerror = msgerror.append("Wrong relation for " + bname 203 + " (expected:" + colExpected1 204 + ", found:" + colActual1 + ")"); 205 } 206 } 207 return isOk; 208 } 209 210 213 public void _testCohRemoveInRel(int tx) throws Exception { 214 String bRemovedString = "b"; 215 int bRemovedInt = 4; 216 if ((tx == TX_CALL) || (tx == TX_RB)) { 217 utx.begin(); 218 } 219 ARemote a = ahome.findByPrimaryKey(new APK("a",3)); 220 if (tx == TX_CONT) { 221 a.removeFromBInNewTx(new BPK(bRemovedString, bRemovedInt)); 222 } else { 223 a.removeFromB(new BPK(bRemovedString, bRemovedInt)); 224 } 225 if (tx == TX_CALL) { 226 utx.commit(); 227 } else if (tx == TX_RB) { 228 utx.rollback(); 229 } 230 Collection ca = a.retrieveB(); 232 BRemote b4 = bhome.findByPrimaryKey(new BPK("b", 4)); 233 Collection cb4 = b4.retrieveA(); 234 if (tx != TX_RB) { 235 assertEquals("Wrong relations size for a-3: ", 2, ca.size()); 236 assertEquals("Wrong relations size for b-4: ", 0, cb4.size()); 237 } else { 238 assertEquals("Wrong relations size for a-3: ", 3, ca.size()); 239 assertEquals("Wrong relations size for b-4: ", 1, cb4.size()); 240 } 241 if (tx != TX_RB) { 243 a.addInB(new BPK(bRemovedString, bRemovedInt)); 244 } 245 checkIsInitialState(); 247 248 } 249 250 public void testCohRemoveInRelTxNo() throws Exception { 251 _testCohRemoveInRel(TX_NO); 252 } 253 254 public void testCohRemoveInRelTxCall() throws Exception { 255 _testCohRemoveInRel(TX_CALL); 256 } 257 258 public void testCohRemoveInRelTxCont() throws Exception { 259 _testCohRemoveInRel(TX_CONT); 260 } 261 262 public void testCohRemoveInRelTxRb() throws Exception { 263 _testCohRemoveInRel(TX_RB); 264 } 265 266 public static Test suite() { 267 return new TestSuite(F_Relation_pkcompEC2.class); 268 } 269 270 public static void main(String args[]) { 271 String testtorun = null; 272 for (int argn = 0; argn < args.length; argn++) { 274 String s_arg = args[argn]; 275 Integer i_arg; 276 if (s_arg.equals("-n")) { 277 testtorun = args[++argn]; 278 } 279 } 280 if (testtorun == null) { 281 junit.textui.TestRunner.run(suite()); 282 } else { 283 junit.textui.TestRunner.run(new F_Relation_pkcompEC2(testtorun)); 284 } 285 } 286 287 } 288 289 290 | Popular Tags |