1 25 26 package org.objectweb.jonas.jtests.clients.distribution; 27 28 import java.util.Collection ; 29 30 import javax.naming.NamingException ; 31 import javax.rmi.PortableRemoteObject ; 32 import javax.transaction.RollbackException ; 33 import javax.transaction.UserTransaction ; 34 35 import junit.framework.Test; 36 import junit.framework.TestSuite; 37 38 import org.objectweb.jonas.jtests.beans.cluster.Identity; 39 import org.objectweb.jonas.jtests.beans.cluster.IdentityHome; 40 import org.objectweb.jonas.jtests.util.JTestCase; 41 42 48 49 53 class A_clthread extends Thread { 54 int function; 55 IdentityHome home; 56 String name; 57 int number; 58 UserTransaction utx = F_Cluster.utx; 59 60 public A_clthread(IdentityHome home, int function, String name, int number) { 61 this.home = home; 62 this.function = function; 63 this.name = name; 64 this.number = number; 65 } 66 67 public void run() { 68 Identity id = null; 69 70 try { 71 utx.begin(); 73 74 switch (function) { 76 case F_Cluster.READ: 77 id = home.findByPrimaryKey(name); 78 id.getNumber(); 79 break; 80 case F_Cluster.WRITE: 81 id = home.findByPrimaryKey(name); 82 id.setNumber(number); 83 break; 84 case F_Cluster.CREATE: 85 id = home.create(name, number); 86 break; 87 case F_Cluster.REMOVE: 88 home.remove(name); 89 break; 90 } 91 92 utx.commit(); 94 } catch (Exception e) { 95 F_Cluster.threadError = e; 96 } 97 } 98 } 99 100 public class F_Cluster extends JTestCase { 101 102 protected static IdentityHome home1 = null; 103 protected static IdentityHome home2 = null; 104 protected static IdentityHome home3 = null; 105 106 public static Exception threadError = null; 107 108 public static final int READ = 1; 110 public static final int WRITE = 2; 111 public static final int CREATE = 3; 112 public static final int REMOVE = 4; 113 114 public F_Cluster(String name) { 115 super(name); 116 } 117 118 protected void setUp() { 119 super.setUp(); 120 if (home3 == null) { 121 useBeans("cluster", true); 122 try { 123 home1 = (IdentityHome) PortableRemoteObject.narrow(ictx.lookup("clusterId_1"), IdentityHome.class); 124 assertNotNull("home of clusterId_1 is null", home1); 125 home2 = (IdentityHome) PortableRemoteObject.narrow(ictx.lookup("clusterId_2"), IdentityHome.class); 126 assertNotNull("home of clusterId_2 is null", home2); 127 home3 = (IdentityHome) PortableRemoteObject.narrow(ictx.lookup("clusterId_3"), IdentityHome.class); 128 assertNotNull("home of clusterId_3 is null", home3); 129 } catch (NamingException e) { 130 fail("Cannot get bean home: " + e.getMessage()); 131 } 132 } 133 } 134 135 protected void tearDown() throws Exception { 136 threadError = null; 137 super.tearDown(); 138 } 139 140 145 public void testSingle() throws Exception { 146 Identity name1 = home3.findByPrimaryKey("name1"); 148 assertTrue(name1.getNumber() == 1000); 149 Collection c1000 = home3.findByNumber(1000); 150 home3.findAll(); 151 Identity newname = home3.create("name28", 300); 153 int nv = 200; 154 newname.setNumber(nv); 155 assertEquals("Wrong field value", nv, newname.getNumber()); 156 newname.remove(); 157 } 158 159 162 public void test2Home() throws Exception { 163 int val1 = 31; 164 int val2 = 32; 165 Identity name1 = home1.create("john", val1); 166 Identity name2 = home2.findByPrimaryKey("john"); 167 assertTrue(name2.getNumber() == val1); 168 name2.setNumber(val2); 169 assertEquals("Wrong field value", val2, name1.getNumber()); 170 name2.remove(); 171 name1 = home1.create("john", val1); 172 home2.remove("john"); 173 } 174 175 182 public void testIso1() throws Exception { 183 int val1 = random(500); 184 int val2 = val1+1; 185 Identity name11 = home1.findByPrimaryKey("name11"); 186 utx.begin(); 187 name11.setNumber(val1); 188 A_clthread th2 = new A_clthread(home2, WRITE, "name11", val2); 190 th2.start(); 191 th2.join(); 193 assertEquals("Value has been modified (no transaction isolation)", 195 val1, name11.getNumber()); 196 try { 197 utx.commit(); 198 } catch (RollbackException e) { 199 assertEquals("Both transactions are not serialized", 201 val2, name11.getNumber()); 202 return; 203 } 204 if (threadError != null) { 205 throw new RuntimeException ("error in 2nd thread", threadError); 207 } 208 fail("both transactions should not pass"); 209 } 210 211 212 public static Test suite() { 213 return new TestSuite(F_Cluster.class); 214 } 215 216 public static void main (String args[]) { 217 String testtorun = null; 218 for (int argn = 0; argn < args.length; argn++) { 220 String s_arg = args[argn]; 221 Integer i_arg; 222 if (s_arg.equals("-n")) { 223 testtorun = args[++argn]; 224 } 225 } 226 if (testtorun == null) { 227 junit.textui.TestRunner.run(suite()); 228 } else { 229 junit.textui.TestRunner.run(new F_Cluster(testtorun)); 230 } 231 } 232 } 233 | Popular Tags |