1 22 package org.jboss.ejb3.test.initial.unit; 23 24 import javax.ejb.EJBAccessException ; 25 import javax.naming.InitialContext ; 26 import javax.transaction.UserTransaction ; 27 import org.jboss.ejb3.test.initial.ClassInjected; 28 import org.jboss.ejb3.test.initial.InterceptedSFTest; 29 import org.jboss.ejb3.test.initial.SecuredTest; 30 import org.jboss.ejb3.test.initial.StatefulTestRemote; 31 import org.jboss.ejb3.test.initial.TestRemote; 32 import org.jboss.logging.Logger; 33 import org.jboss.security.SecurityAssociation; 34 import org.jboss.security.SimplePrincipal; 35 import org.jboss.test.JBossTestCase; 36 import junit.framework.Test; 37 38 44 45 public class RemoteUnitTestCase 46 extends JBossTestCase 47 { 48 private static Logger log = Logger.getLogger(RemoteUnitTestCase.class); 49 50 static boolean deployed = false; 51 static int test = 0; 52 53 public RemoteUnitTestCase(String name) 54 { 55 56 super(name); 57 58 } 59 60 public void testStateful() throws Exception 61 { 62 StatefulTestRemote test = (StatefulTestRemote) getInitialContext().lookup("StatefulTestBean/remote"); 63 test.setState("hello world"); 64 assertEquals(test.getState(), "hello world"); 65 test.endSession(); 66 67 } 68 69 public void testSimple() throws Exception 70 { 71 TestRemote test = (TestRemote) this.getInitialContext().lookup("TestBean/remote"); 72 String echo = test.testMe("echo"); 73 assertEquals(echo, "echo"); 74 } 75 76 public void testTx() throws Exception 77 { 78 UserTransaction ut = (UserTransaction ) getInitialContext().lookup("UserTransaction"); 80 TestRemote test = (TestRemote) this.getInitialContext().lookup("TestBean/remote"); 81 ut.begin(); 82 test.mandatory(); 83 test.mandatory(); 85 ut.commit(); 86 87 } 88 89 public void testSecurity() throws Exception 90 { 91 InitialContext ctx = getInitialContext(); 93 SecuredTest test = (SecuredTest) ctx.lookup("SecuredTestBean/remote"); 94 95 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 96 SecurityAssociation.setCredential("password".toCharArray()); 97 98 System.out.println("Calling initial security tests...."); 99 test.unchecked(); 100 System.out.println("Calling testDefault()...."); 101 test.testDefault(); 102 System.out.println("Calling secured()...."); 103 test.secured(); 104 105 System.out.println("Calling security fine grain tests...."); 106 SecurityAssociation.setPrincipal(new SimplePrincipal("authfail")); 107 108 boolean securityFailure = true; 109 try 110 { 111 test.secured(); 112 } 113 catch (EJBAccessException ignored) 114 { 115 System.out.println("log="+log+":ignored:"+ignored); 116 log.info(ignored.getMessage()); 117 securityFailure = false; 118 } 119 120 if (securityFailure) throw new RuntimeException ("auth failure was not caught for method"); 121 122 securityFailure = true; 123 SecurityAssociation.setPrincipal(new SimplePrincipal("rolefail")); 124 try 125 { 126 test.secured(); 127 } 128 catch (EJBAccessException ignored) 129 { 130 log.info(ignored.getMessage()); 131 securityFailure = false; 132 } 133 if (securityFailure) throw new RuntimeException ("role failure was not caught for method"); 134 135 SecurityAssociation.setPrincipal(new SimplePrincipal("somebody")); 136 log.info("test exclusion"); 137 securityFailure = true; 138 try 139 { 140 test.excluded(); 141 } 142 catch (EJBAccessException ignored) 143 { 144 log.info(ignored.getMessage()); 145 securityFailure = false; 146 } 147 if (securityFailure) throw new RuntimeException ("excluded failure was not caught for method"); 148 149 } 150 151 public void testInterceptors() throws Exception 152 { 153 InterceptedSFTest test = (InterceptedSFTest) this.getInitialContext().lookup("InterceptedSFTestBean/remote"); 154 int ret = test.testMethod(5); 155 int expected = 1010; 156 if (ret != expected) throw new Exception ("return value was not " + expected + ", it was: " + ret); 157 } 158 159 public void testCallbacks() throws Exception 160 { 161 org.jboss.ejb3.test.initial.TestStatus status = 162 (org.jboss.ejb3.test.initial.TestStatus) getInitialContext().lookup("TestStatusBean/remote"); 163 status.clear(); 164 InterceptedSFTest test = (InterceptedSFTest) getInitialContext().lookup("InterceptedSFTestBean/remote"); 165 166 test.testMethod(5); 167 int val = test.getVal(); 168 if (val != 5) throw new Exception ("test.getVal() returned " + val + " instead of 5"); 169 if (!status.postConstruct()) throw new Exception ("PostConstruct should be called for SFSB"); 170 171 InterceptedSFTest test2 = (InterceptedSFTest) getInitialContext().lookup("InterceptedSFTestBean/remote"); 173 test2.testMethod(3); 174 175 if (!status.prePassivate()) throw new Exception ("PrePassivate should be called for SFSB"); 176 177 val = test.getVal(); 179 if (val != 5) throw new Exception ("test.getVal() returned " + val + " instead of 5"); 180 if (!status.postActivate()) throw new Exception ("PostActivate should be called for SFSB"); 181 182 test.clear(); 184 if (!status.preDestroy()) throw new Exception ("PreDestroy should be called for SFSB"); 185 186 test2.clear(); 187 } 188 189 public void testClassInjected() throws Exception 190 { 191 ClassInjected test = (ClassInjected)getInitialContext().lookup("ClassInjectedBean/remote"); 192 assertTrue(test.isInjected()); 193 } 194 195 public static Test suite() throws Exception 196 { 197 return getDeploySetup(RemoteUnitTestCase.class, "initial-ejb3-test.sar"); 198 } 199 200 } 201 | Popular Tags |