1 22 package org.jboss.ejb3.test.iiop.unit; 23 24 import java.rmi.MarshalledObject ; 25 import java.util.Date ; 26 import java.util.Properties ; 27 28 import javax.ejb.Handle ; 29 import javax.ejb.HomeHandle ; 30 import javax.naming.Context ; 31 import javax.naming.InitialContext ; 32 import javax.naming.NamingException ; 33 import javax.rmi.PortableRemoteObject ; 34 import javax.transaction.UserTransaction ; 35 36 import junit.framework.Test; 37 38 import org.jboss.ejb3.test.CustomJNDIJBossTestCase; 39 import org.jboss.ejb3.test.iiop.HomedStatelessHome; 40 import org.jboss.ejb3.test.iiop.MySession; 41 import org.jboss.ejb3.test.iiop.MyStateful; 42 import org.jboss.ejb3.test.iiop.MyStatefulHome; 43 import org.jboss.ejb3.test.iiop.TxTester; 44 import org.jboss.security.SecurityAssociation; 45 import org.jboss.security.SimplePrincipal; 46 47 53 public class IiopRemoteUnitTestCase extends CustomJNDIJBossTestCase 54 { 55 56 public IiopRemoteUnitTestCase(String name) 57 { 58 super(name); 59 } 60 61 @Override 62 public InitialContext getInitialContext() throws NamingException 63 { 64 return new InitialContext (getJndiProperties()); 65 } 66 67 private Properties getJndiProperties() 68 { 69 Properties props = new Properties (); 70 props.put("java.naming.factory.initial", "com.sun.jndi.cosnaming.CNCtxFactory"); 71 props.put("java.naming.provider.url", "corbaloc::localhost:3528/NameService"); 72 props.put("java.naming.factory.object", "org.jboss.tm.iiop.client.IIOPClientUserTransactionObjectFactory"); 73 props.put(Context.URL_PKG_PREFIXES, "org.jboss.naming.client:org.jnp.interfaces"); 74 props.put("j2ee.clientName", "iiop-unit-test"); 75 76 79 return props; 80 } 82 83 public void test0() throws Exception 84 { 85 System.out.println("java: " + getInitialContext().lookup("java:comp/HandleDelegate")); 86 } 87 88 public void test1() throws Exception 89 { 90 SimplePrincipal principal = new SimplePrincipal("somebody"); 91 SecurityAssociation.setPrincipal(principal); 92 SecurityAssociation.setCredential("password".toCharArray()); 93 94 InitialContext ctx = getInitialContext(); 95 Object obj = ctx.lookup("MySessionBean/remote"); 96 System.err.println(obj.getClass()); 97 MySession session = (MySession) PortableRemoteObject.narrow(obj, MySession.class); 98 assertNotNull(session); 99 String me = new Date ().toString(); 100 String response = session.sayHelloTo(me); 101 assertEquals("Hi " + me, response); 102 } 103 104 public void test2() throws Exception 105 { 106 InitialContext ctx = getInitialContext(); 107 Object obj = ctx.lookup("MyStatefulBean/remoteHome"); 108 MyStatefulHome home = (MyStatefulHome) PortableRemoteObject.narrow(obj, MyStatefulHome.class); 109 MyStateful bean1 = home.create(); 111 bean1.setName("bean1"); 112 String response = bean1.sayHello(); 113 assertEquals("Hello bean1", response); 114 bean1.remove(); 115 } 116 117 public void testGetEJBHome() throws Exception 118 { 119 InitialContext ctx = getInitialContext(); 120 Object obj = ctx.lookup("HomedStatelessBean/remoteHome"); 121 HomedStatelessHome home = (HomedStatelessHome) PortableRemoteObject.narrow(obj, HomedStatelessHome.class); 122 MySession session = home.create(); 123 Object o = session.getEJBHome(); 124 HomedStatelessHome home2 = (HomedStatelessHome) PortableRemoteObject.narrow(o, HomedStatelessHome.class); 125 } 127 128 public void testGetHandle() throws Exception 129 { 130 InitialContext ctx = getInitialContext(); 131 Object obj = ctx.lookup("HomedStatelessBean/remoteHome"); 132 HomedStatelessHome home = (HomedStatelessHome) PortableRemoteObject.narrow(obj, HomedStatelessHome.class); 133 MySession session = home.create(); 134 Handle h = session.getHandle(); 135 MarshalledObject mo = new MarshalledObject (h); 136 Handle h2 = (Handle ) mo.get(); 137 Object o = h2.getEJBObject(); 138 MySession session2 = (MySession) PortableRemoteObject.narrow(o, MySession.class); 139 String me = new Date ().toString(); 140 String response = session2.sayHelloTo(me); 141 assertEquals("Hi " + me, response); 142 } 143 144 public void testGetHomeHandle() throws Exception 145 { 146 InitialContext ctx = getInitialContext(); 147 Object obj = ctx.lookup("HomedStatelessBean/remoteHome"); 148 HomedStatelessHome home = (HomedStatelessHome) PortableRemoteObject.narrow(obj, HomedStatelessHome.class); 149 HomeHandle h = home.getHomeHandle(); 150 MarshalledObject mo = new MarshalledObject (h); 151 HomeHandle h2 = (HomeHandle ) mo.get(); 152 Object o = h2.getEJBHome(); 153 HomedStatelessHome home2 = (HomedStatelessHome) PortableRemoteObject.narrow(o, HomedStatelessHome.class); 154 } 156 157 public void testGetPrimaryKeyAndRemove() throws Exception 158 { 159 InitialContext ctx = getInitialContext(); 160 Object obj = ctx.lookup("MyStatefulBean/remoteHome"); 161 MyStatefulHome home = (MyStatefulHome) PortableRemoteObject.narrow(obj, MyStatefulHome.class); 162 MyStateful session = home.create(); 163 Object primaryKey = session.getPrimaryKey(); 164 assertNotNull(primaryKey); 165 166 home.remove(primaryKey); 167 try 168 { 169 session.sayHello(); 170 fail("should throw an exception"); 171 } 172 catch(Exception e) 173 { 174 } 176 } 177 178 public void testHomedStateless() throws Exception 179 { 180 InitialContext ctx = getInitialContext(); 181 Object obj = ctx.lookup("HomedStatelessBean/remoteHome"); 182 HomedStatelessHome home = (HomedStatelessHome) PortableRemoteObject.narrow(obj, HomedStatelessHome.class); 183 MySession session = home.create(); 184 String me = new Date ().toString(); 185 String response = session.sayHelloTo(me); 186 assertEquals("Hi " + me, response); 187 } 188 189 public void testIsIdentical() throws Exception 190 { 191 InitialContext ctx = getInitialContext(); 192 Object obj = ctx.lookup("MyStatefulBean/remoteHome"); 193 MyStatefulHome home = (MyStatefulHome) PortableRemoteObject.narrow(obj, MyStatefulHome.class); 194 MyStateful session = home.create(); 195 Handle h = session.getHandle(); 196 MarshalledObject mo = new MarshalledObject (h); 197 Handle h2 = (Handle ) mo.get(); 198 Object o = h2.getEJBObject(); 199 MyStateful session2 = (MyStateful) PortableRemoteObject.narrow(o, MyStateful.class); 200 assertTrue(session.isIdentical(session2)); 201 } 202 203 public void testRemoveByHandle() throws Exception 204 { 205 InitialContext ctx = getInitialContext(); 206 Object obj = ctx.lookup("MyStatefulBean/remoteHome"); 207 MyStatefulHome home = (MyStatefulHome) PortableRemoteObject.narrow(obj, MyStatefulHome.class); 208 MyStateful session = home.create(); 209 session.setName("Me"); 210 home.remove(session.getHandle()); 211 try 212 { 213 session.sayHello(); 214 fail("should throw an exception"); 215 } 216 catch(Exception e) 217 { 218 } 220 } 221 222 public void testSecurity() throws Exception 223 { 224 SimplePrincipal principal = new SimplePrincipal("somebody"); 225 SecurityAssociation.setPrincipal(principal); 226 227 InitialContext ctx = getInitialContext(); 228 Object obj = ctx.lookup("MySessionBean/remote"); 229 System.err.println(obj.getClass()); 230 MySession session = (MySession) PortableRemoteObject.narrow(obj, MySession.class); 231 assertNotNull(session); 232 String actual = session.getWhoAmI(); 233 System.err.println("whoAmI = " + actual); 234 } 235 236 public void testService() throws Exception 237 { 238 InitialContext ctx = getInitialContext(); 239 Object obj = ctx.lookup("MyServiceBean/remote"); 240 MyStateful bean1 = (MyStateful) PortableRemoteObject.narrow(obj, MyStateful.class); 241 bean1.setName("bean1"); 242 String response = bean1.sayHello(); 243 assertEquals("Hello bean1", response); 244 } 245 246 public void testTxPropegation() throws Exception 247 { 248 InitialContext ctx = getInitialContext(); 249 Object obj = ctx.lookup("TxTesterBean/remote"); 250 TxTester session = (TxTester) PortableRemoteObject.narrow(obj, TxTester.class); 251 assertNotNull(session); 252 UserTransaction tx = (UserTransaction ) PortableRemoteObject.narrow(ctx.lookup("UserTransaction"), UserTransaction .class); 253 tx.begin(); 254 try 255 { 256 session.txMandatoryMethod(); 257 } 258 finally 259 { 260 tx.rollback(); 261 } 262 } 264 265 public void testTxRequired() throws Exception 266 { 267 InitialContext ctx = getInitialContext(); 268 Object obj = ctx.lookup("TxTesterBean/remote"); 269 TxTester session = (TxTester) PortableRemoteObject.narrow(obj, TxTester.class); 270 assertNotNull(session); 271 try 272 { 273 session.txMandatoryMethod(); 274 fail("Expected an exception"); 275 } 276 catch(Exception e) 277 { 278 } 280 } 282 283 public static Test suite() throws Exception 284 { 285 try 286 { 287 System.err.println(IiopRemoteUnitTestCase.class.getClassLoader().getResource("jacorb.properties")); 288 } 289 catch(Throwable t) 290 { 291 t.printStackTrace(); 292 } 293 return getDeploySetup(IiopRemoteUnitTestCase.class, "iiop.jar"); 294 } 295 296 } 297 | Popular Tags |