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 java.util.Iterator ; 33 34 import javax.naming.NamingException ; 35 import javax.rmi.PortableRemoteObject ; 36 37 import junit.framework.Test; 38 import junit.framework.TestSuite; 39 40 import org.objectweb.jonas.jtests.beans.relation.s3pkcomp.AHomeRemote; 41 import org.objectweb.jonas.jtests.beans.relation.s3pkcomp.ARemote; 42 import org.objectweb.jonas.jtests.beans.relation.s3pkcomp.BHomeRemote; 43 import org.objectweb.jonas.jtests.beans.relation.s3pkcomp.Pk; 44 45 46 51 public class F_Relation_s3pkcompEC2 extends A_Cmp2Util { 52 53 private static String BEAN_HOME_A = "relation_s3pkcomp_AHome"; 54 protected static AHomeRemote ahome = null; 55 private static String BEAN_HOME_B = "relation_s3pkcomp_BHome"; 56 protected static BHomeRemote bhome = null; 57 58 static Hashtable tbRelationA2B = new Hashtable (); 59 60 static { 61 tbRelationA2B.put(new Integer (0), new int[]{}); 62 tbRelationA2B.put(new Integer (1), new int[]{1001, 1002, 1003}); 63 tbRelationA2B.put(new Integer (2), new int[]{2001, 2002, 2003}); 64 tbRelationA2B.put(new Integer (3), new int[]{3001}); 65 for (Enumeration ea = tbRelationA2B.keys(); ea.hasMoreElements();) { 67 Integer aid = (Integer ) (ea.nextElement()); 68 int[] tb = (int[]) tbRelationA2B.get(aid); 69 ArrayList col = new ArrayList (tb.length); 70 for (int i = 0; i < tb.length; i++) { 71 col.add(new Pk(tb[i])); 72 } 73 tbRelationA2B.put(aid, col); 74 } 75 } 76 77 public F_Relation_s3pkcompEC2(String name) { 78 super(name); 79 debug("F_Relation_s3pkcompEC2("+name+")"); 80 } 81 82 protected static boolean isInit = false; 83 84 protected void setUp() { 85 super.setUp(); 86 boolean ok = false; 87 int nbtry = 0; 88 while (!ok && nbtry < 3) { 89 if (!isInit) { 90 useBeans("s3pkcomp", false); 92 try { 93 ahome = (AHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_A), 94 AHomeRemote.class); 95 bhome = (BHomeRemote) PortableRemoteObject.narrow(ictx.lookup(BEAN_HOME_B), 96 BHomeRemote.class); 97 } catch (NamingException e) { 98 fail("Cannot get bean home: " + e.getMessage()); 99 } 100 try { 102 ahome.findByPrimaryKey(new Pk(0)); 103 } catch (Exception e) { 104 try { 105 utx.begin(); 106 bhome.create(0); 107 bhome.create(1001); 108 bhome.create(1002); 109 bhome.create(1003); 110 bhome.create(2001); 111 bhome.create(2002); 112 bhome.create(2003); 113 bhome.create(3001); 114 ARemote a0 = ahome.create(0); 115 ARemote a1 = ahome.create(1); 116 ARemote a2 = ahome.create(2); 117 ARemote a3 = ahome.create(3); 118 a1.assignB((Collection )tbRelationA2B.get(new Integer (1))); 119 a2.assignB((Collection )tbRelationA2B.get(new Integer (2))); 120 a3.assignB((Collection )tbRelationA2B.get(new Integer (3))); 121 } catch (Exception i) { 122 fail("InitialState creation problem:: "+i); 123 } finally { 124 try { 125 utx.commit(); 126 } catch (Exception ii) { 127 } 128 } 129 } 130 isInit = true; 131 } 132 nbtry++; 135 try { 136 if (initStateOK()) { 137 ok = true; 138 } 139 } catch (Exception e) { 140 } 141 if (!ok) { 142 isInit = false; 143 unloadBeans("s3pkcomp"); 144 } 145 } 146 } 147 148 152 boolean initStateOK() throws Exception { 153 boolean isOk = true; 154 msgerror = new StringBuffer (); 155 for (Enumeration ea = tbRelationA2B.keys(); ea.hasMoreElements();) { 156 Integer aid = (Integer ) (ea.nextElement()); 157 ARemote a = ahome.findByPrimaryKey(new Pk(aid)); 158 Collection colActual = a.retrieveB(); 159 ArrayList colExpected = (ArrayList ) (tbRelationA2B.get(aid)); 160 debug("Relation for " + aid + " : expected:" + colExpected + ", found:" + colActual ); 161 if (!isCollectionEqual(colExpected, colActual)) { 162 isOk = false; 163 msgerror = msgerror.append("Wrong relation for " + aid 164 + " (expected:" + colExpected 165 + ", found:" + colActual + ")"); 166 } 167 } 168 return isOk; 169 } 170 171 172 175 public void testEmpty() throws Exception { 176 checkIsInitialState(); 177 } 178 179 182 public void _testFindAllA(int tx) throws Exception { 183 if (tx == TX_CONT) { 184 fail("Transaction cannot be initiate by the container for this test"); 187 } 188 if ((tx == TX_CALL) || (tx == TX_RB)) { 189 utx.begin(); 190 } 191 Collection cA = ahome.findAll(); 192 if (tx == TX_CALL) { 193 utx.commit(); 194 } else if (tx == TX_RB) { 195 utx.rollback(); 196 } 197 int nbA = 0; 199 for (Iterator iA = cA.iterator(); iA.hasNext(); iA.next()) { 200 nbA++; 201 } 202 assertEquals("Wrong number for beans A for findAll: ", tbRelationA2B.size(), nbA); 203 } 204 205 public void testFindAllATxNo() throws Exception { 206 _testFindAllA(TX_NO); 207 } 208 209 public void testFindAllATxCall() throws Exception { 210 _testFindAllA(TX_CALL); 211 } 212 213 public void testFindAllATxRb() throws Exception { 214 _testFindAllA(TX_RB); 215 } 216 217 218 public static Test suite() { 219 return new TestSuite(F_Relation_s3pkcompEC2.class); 220 } 221 222 public static void main(String args[]) { 223 String testtorun = null; 224 for (int argn = 0; argn < args.length; argn++) { 226 String s_arg = args[argn]; 227 Integer i_arg; 228 if (s_arg.equals("-n")) { 229 testtorun = args[++argn]; 230 } 231 } 232 if (testtorun == null) { 233 junit.textui.TestRunner.run(suite()); 234 } else { 235 junit.textui.TestRunner.run(new F_Relation_s3pkcompEC2(testtorun)); 236 } 237 } 238 239 } 240 | Popular Tags |