1 25 26 package org.objectweb.jonas.jtests.clients.j2eeca; 27 28 import javax.naming.Context ; 29 import javax.naming.InitialContext ; 30 import javax.rmi.PortableRemoteObject ; 31 import javax.transaction.UserTransaction ; 32 33 import junit.framework.Test; 34 import junit.framework.TestSuite; 35 36 import org.objectweb.jonas.jtests.beans.j2eeca.transactedCA; 37 import org.objectweb.jonas.jtests.beans.j2eeca.transactedCAHome; 38 import org.objectweb.jonas.jtests.util.JTestCase; 39 40 43 44 public class F_xatransactionTest extends JTestCase { 45 46 private static UserTransaction utx = null; 47 48 static Context ctx = null; 49 50 52 protected static String BEAN_HOME = "transactedCAHome"; 53 protected static transactedCAHome home = null; 54 private static final String RAR_JNDI_NAME = "FictionalXATransaction"; 55 final public int CLOSE_HANDLE = 0; 56 final public int CLOSE_PHYSICAL = 1; 57 public int CloseType = 0; 58 static transactedCA bean = null; 59 public String UseBeans="j2eeca"; 60 61 public F_xatransactionTest(String name) { 62 super(name); 63 } 64 65 protected void setUp() { 66 super.setUp(); 67 try { 68 70 if (ctx == null) { 71 ctx = new InitialContext (); 72 } 73 74 76 if (utx == null) { 77 utx = (UserTransaction ) ctx.lookup("javax.transaction.UserTransaction"); 78 } 79 80 if (home == null) { 81 useBeans(UseBeans, false); 82 } 83 getBean(); 84 assertTrue (6 == 6); 85 86 } catch (Exception e) { 87 fail("Cannot lookup UserTransaction in setUp: " +e); 88 } 89 } 90 private void getBean() throws Exception { 91 93 if (home == null) { 94 home = (transactedCAHome)PortableRemoteObject.narrow( 95 ctx.lookup(BEAN_HOME), 96 transactedCAHome.class); 97 bean = home.create(); 98 } 99 } 100 protected void tearDown() throws Exception { 101 } 103 protected void startUp(String testName) { 104 try { 105 bean.method1(RAR_JNDI_NAME, testName); 106 } catch (Exception ee) { 107 ee.printStackTrace(); 108 System.exit(2); 109 } 110 } 111 112 114 122 public void testIsXaresource() { 123 CloseType=CLOSE_PHYSICAL; 124 125 try { 126 utx.begin(); 127 startUp("testIsXaresource"); 128 String ans = bean.getXid(); 129 assertEquals("OK", ans); 130 bean.closeUp(CloseType); 131 utx.commit(); 132 assertTrue(5 == 5); 133 } catch (Exception e) { 134 try { 135 utx.rollback(); 136 } catch (Exception f) { 137 assertTrue(5 == 5); 138 } 139 assertTrue(5 == 5); 140 } 141 } 142 153 public void testIsXAend1() { 154 CloseType=CLOSE_HANDLE; 156 try { 157 utx.begin(); 158 startUp("testIsXAend1"); 159 assertTrue(bean.getCMInstance()); bean.closeUp(CloseType); 161 assertTrue(bean.getCMInstance()); utx.commit(); 163 assertTrue(5 == 5); 164 } catch (Exception e) { 165 try { 166 utx.rollback(); 167 } catch (Exception f) { 168 assertTrue(5 == 5); 169 } 170 assertTrue(5 == 5); 171 } 172 } 173 183 public void testIsXAend2() { 184 CloseType=CLOSE_PHYSICAL; 185 186 try { 187 utx.begin(); 188 startUp("testIsXAend2"); 189 assertTrue(bean.getCMInstance()); bean.closeUp(CloseType); 191 String ans = bean.getXid(); 192 assertEquals("FAIL", ans); utx.commit(); 196 assertTrue(5 == 5); 197 } catch (Exception e) { 198 try { 199 utx.rollback(); 200 } catch (Exception f) { 201 assertTrue(5 == 5); 202 } 203 assertFalse(5 == 5); 204 } 205 } 206 207 public static Test suite() { 208 return new TestSuite(F_xatransactionTest.class); 209 } 210 211 public static void main (String args[]) { 212 213 String testtorun = null; 214 215 217 for (int argn = 0; argn < args.length; argn++) { 218 219 String s_arg = args[argn]; 220 Integer i_arg; 221 222 if (s_arg.equals("-n")) { 223 testtorun = args[++argn]; 224 } 225 } 226 227 if (testtorun == null) { 228 junit.textui.TestRunner.run(suite()); 229 } else { 230 junit.textui.TestRunner.run(new F_xatransactionTest(testtorun)); 231 } 232 } 233 } 234 | Popular Tags |