1 22 package org.jboss.test.cts.test; 23 24 25 import java.rmi.server.UnicastRemoteObject ; 26 import java.io.ByteArrayOutputStream ; 27 import java.io.ObjectOutputStream ; 28 import java.io.ByteArrayInputStream ; 29 import java.io.ObjectInputStream ; 30 import java.util.Properties ; 31 import javax.naming.InitialContext ; 32 import javax.rmi.PortableRemoteObject ; 33 import javax.ejb.Handle ; 34 import javax.ejb.CreateException ; 35 36 import junit.framework.Test; 37 38 import org.jboss.test.JBossTestCase; 39 import org.jboss.test.cts.interfaces.StatelessSession; 40 import org.jboss.test.cts.interfaces.StatelessSessionHome; 41 import org.jboss.test.cts.interfaces.StrictlyPooledSessionHome; 42 import org.jboss.test.cts.interfaces.StrictlyPooledSession; 43 import EDU.oswego.cs.dl.util.concurrent.CountDown; 44 45 52 public class StatelessSessionUnitTestCase 53 extends JBossTestCase 54 { 55 static final int MAX_SIZE = 20; 56 StatelessSession sessionBean; 57 58 public StatelessSessionUnitTestCase(String name) 59 { 60 super(name); 61 } 62 63 protected void setUp() throws Exception 64 { 65 InitialContext ctx = new InitialContext (); 66 Object ref = ctx.lookup("ejbcts/StatelessSessionHome"); 67 StatelessSessionHome home = (StatelessSessionHome) 68 PortableRemoteObject.narrow(ref, StatelessSessionHome.class); 69 sessionBean = home.create(); 70 } 71 72 protected void tearDown() throws Exception 73 { 74 if (sessionBean != null) 75 sessionBean.remove(); 76 } 77 78 82 public void testBasicStatelessSession() 83 throws Exception 84 { 85 getLog().debug("+++ testBasicStatelessSession()"); 86 String result = sessionBean.method1("testBasicStatelessSession"); 87 assertTrue(result.equals("testBasicStatelessSession")); 89 } 90 91 94 public void testSessionHandle() 95 throws Exception 96 { 97 getLog().debug("+++ testSessionHandle()"); 98 Handle beanHandle = sessionBean.getHandle(); 99 ByteArrayOutputStream out = new ByteArrayOutputStream (); 100 ObjectOutputStream oos = new ObjectOutputStream (out); 101 oos.writeObject(beanHandle); 102 oos.flush(); 103 byte[] bytes = out.toByteArray(); 104 105 getLog().debug("Unserialize bean handle..."); 106 ByteArrayInputStream in = new ByteArrayInputStream (bytes); 107 ObjectInputStream ois = new ObjectInputStream (in); 108 beanHandle = (Handle ) ois.readObject(); 109 StatelessSession theSession = (StatelessSession) beanHandle.getEJBObject(); 110 theSession.method1("Hello"); 111 getLog().debug("Called method1 on handle session bean"); 112 } 113 114 118 public void testSessionHandleNoDefaultJNDI() 119 throws Exception 120 { 121 getLog().debug("+++ testSessionHandleNoDefaultJNDI()"); 122 123 127 Properties homeProps = new Properties (); 128 homeProps.setProperty("java.naming.factory.initial", "org.jboss.naming.NamingContextFactory"); 129 InitialContext ic = new InitialContext (homeProps); 130 Handle beanHandle = sessionBean.getHandle(); 131 ByteArrayOutputStream out = new ByteArrayOutputStream (); 132 ObjectOutputStream oos = new ObjectOutputStream (out); 133 oos.writeObject(beanHandle); 134 oos.flush(); 135 byte[] bytes = out.toByteArray(); 136 137 Properties sysProps = System.getProperties(); 138 Properties newProps = new Properties (sysProps); 139 newProps.setProperty("java.naming.factory.initial", "badFactory"); 140 newProps.setProperty("java.naming.provider.url", "jnp://badhost:12345"); 141 System.setProperties(newProps); 142 try 143 { 144 getLog().debug("Unserialize bean handle..."); 145 ByteArrayInputStream in = new ByteArrayInputStream (bytes); 146 ObjectInputStream ois = new ObjectInputStream (in); 147 beanHandle = (Handle ) ois.readObject(); 148 StatelessSession theSession = (StatelessSession) beanHandle.getEJBObject(); 149 theSession.method1("Hello"); 150 getLog().debug("Called method1 on handle session bean"); 151 } 152 finally 153 { 154 System.setProperties(sysProps); 155 } 156 } 157 158 162 public void testHomeFromRemoteNoDefaultJNDI() 163 throws Exception 164 { 165 getLog().debug("+++ testHomeFromRemoteNoDefaultJNDI()"); 166 167 Properties sysProps = System.getProperties(); 169 Properties newProps = new Properties (sysProps); 170 newProps.setProperty("java.naming.factory.initial", "badFactory"); 171 newProps.setProperty("java.naming.provider.url", "jnp://badhost:12345"); 172 System.setProperties(newProps); 173 174 Properties env = new Properties (); 176 env.setProperty("java.naming.factory.initial", super.getJndiInitFactory()); 177 env.setProperty("java.naming.provider.url", super.getJndiURL()); 178 try 179 { 180 InitialContext ctx = new InitialContext (env); 181 Object ref = ctx.lookup("ejbcts/StatelessSessionHome"); 182 StatelessSessionHome home = (StatelessSessionHome) 183 PortableRemoteObject.narrow(ref, StatelessSessionHome.class); 184 sessionBean = home.create(); 185 StatelessSessionHome home2 = (StatelessSessionHome) sessionBean.getEJBHome(); 186 StatelessSession bean2 = home2.create(); 187 bean2.remove(); 188 } 189 finally 190 { 191 System.setProperties(sysProps); 192 } 193 } 194 195 public void testLocalStatelessSession() 196 throws Exception 197 { 198 getLog().debug("+++ testLocalStatelessSession()"); 199 sessionBean.testLocalHome(); 200 } 201 202 public void testClientCallback() 203 throws Exception 204 { 205 getLog().debug("+++ testClientCallback()"); 206 ClientCallbackImpl callback = new ClientCallbackImpl(); 207 UnicastRemoteObject.exportObject(callback); 208 sessionBean.callbackTest(callback, "testClientCallback"); 209 assertTrue(callback.wasCalled()); 211 UnicastRemoteObject.unexportObject(callback, true); 212 } 213 214 public void testStrictPooling() throws Exception 215 { 216 CountDown done = new CountDown(MAX_SIZE); 217 InitialContext ctx = new InitialContext (); 218 StrictlyPooledSessionHome home = (StrictlyPooledSessionHome) 219 ctx.lookup("ejbcts/StrictlyPooledStatelessBean"); 220 SessionInvoker[] threads = new SessionInvoker[MAX_SIZE]; 221 for (int n = 0; n < MAX_SIZE; n++) 222 { 223 SessionInvoker t = new SessionInvoker(home, n, done, getLog()); 224 threads[n] = t; 225 t.start(); 226 } 227 assertTrue("Acquired done", done.attempt(1500 * MAX_SIZE)); 228 229 for (int n = 0; n < MAX_SIZE; n++) 230 { 231 SessionInvoker t = threads[n]; 232 if (t.runEx != null) 233 { 234 t.runEx.printStackTrace(); 235 fail("SessionInvoker.runEx != null"); 236 } 237 } 238 } 239 240 public void testStrictPoolingException() throws Exception 241 { 242 log.info("+++ testStrictPoolingException"); 243 InitialContext ctx = new InitialContext (); 244 StrictlyPooledSessionHome home = (StrictlyPooledSessionHome) 245 ctx.lookup("ejbcts/StrictlyPooledCreateExceptionBean"); 246 247 try 248 { 249 StrictlyPooledSession bean = home.create(); 250 fail("Create did not fail, bean="+bean); 251 } 252 catch(CreateException e) 253 { 254 log.debug("Saw expected create failure", e); 255 } 256 257 for (int n = 0; n < MAX_SIZE; n++) 258 { 259 StrictlyPooledSession bean = home.create(); 260 bean.methodA(); 261 bean.remove(); 262 } 263 } 264 265 public static Test suite() throws Exception 266 { 267 return getDeploySetup(StatelessSessionUnitTestCase.class, "cts.jar"); 268 } 269 270 } 271 | Popular Tags |