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