KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cts > test > StatefulSessionUnitTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cts.test;
23
24 import java.io.FileInputStream JavaDoc;
25 import java.io.FileOutputStream JavaDoc;
26 import java.io.ObjectInputStream JavaDoc;
27 import java.io.ObjectOutputStream JavaDoc;
28 import java.io.ByteArrayOutputStream JavaDoc;
29 import java.io.ByteArrayInputStream JavaDoc;
30 import java.rmi.RemoteException JavaDoc;
31 import java.util.Properties JavaDoc;
32 import javax.ejb.Handle JavaDoc;
33 import javax.ejb.RemoveException JavaDoc;
34 import javax.management.MBeanServerConnection JavaDoc;
35 import javax.naming.InitialContext JavaDoc;
36 import javax.naming.Context JavaDoc;
37 import javax.rmi.PortableRemoteObject JavaDoc;
38 import javax.transaction.UserTransaction JavaDoc;
39
40 import org.jboss.test.cts.interfaces.StatefulSession;
41 import org.jboss.test.cts.interfaces.StatefulSessionHome;
42 import org.jboss.test.cts.interfaces.BeanContextInfo;
43 import org.jboss.test.cts.interfaces.StrictlyPooledSessionHome;
44 import org.jboss.test.cts.keys.AccountPK;
45 import org.jboss.test.JBossTestCase;
46 import org.jboss.tm.TransactionManagerServiceMBean;
47
48 import junit.framework.Test;
49
50 import EDU.oswego.cs.dl.util.concurrent.CountDown;
51
52
53 /** Tests of stateful session beans
54  *
55  * @author kimptoc
56  * @author Scott.Stark@jboss.org
57  * @author d_jencks converted to JBossTestCase, added logging.
58  * @version $Revision: 37406 $
59  */

60 public class StatefulSessionUnitTestCase
61    extends JBossTestCase
62 {
63    static final int MAX_SIZE = 20;
64
65    public StatefulSessionUnitTestCase (String JavaDoc name)
66    {
67       super(name);
68    }
69
70     /** Create a StatefulSessionBean and then create a local interface to
71     an entity in the session. This bean is then passivated by creating another
72     session bean and then activated by invoking a business method. The
73     purpose is to test that the session is passivated and that the local
74     interface is restored.
75      */

76     public void testLocalInterfacePassivation() throws Exception JavaDoc
77     {
78       Context JavaDoc ctx = new InitialContext JavaDoc();
79       getLog().debug("+++ testLocalInterfacePassivation");
80       StatefulSessionHome sessionHome = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean");
81       StatefulSession sessionBean = sessionHome.create("testLocalInterfacePassivation");
82
83         getLog().debug("Creating local home");
84       AccountPK pk = new AccountPK("123456789");
85       sessionBean.createLocalEntity(pk, "jduke");
86
87       getLog().debug("Creating a second session bean, forcing the first one to be passivated?");
88       // The pool size has been set to 1 in the container
89
// config, so creating another ASession here should
90
// cause the first one to be passivated.
91
StatefulSession anotherSession = sessionHome.create("testLocalInterfacePassivation2");
92       getLog().debug("OK, anotherSession="+anotherSession);
93
94       getLog().debug("Checking for complete passivation/activation");
95       Thread.sleep(1000);
96       assertTrue(sessionBean.getWasPassivated() == true);
97       getLog().debug("OK");
98
99       // Some other checks
100
getLog().debug("Checking reactivation of session bean attributes");
101       String JavaDoc name = sessionBean.readAndRemoveEntity();
102       assertTrue(name.equals("jduke"));
103       getLog().debug("OK");
104     }
105
106     /** Create a StatefulSessionBean and then create a handle to a session
107     bean in the session. This bean is then passivated by creating another
108     session bean and then activated by invoking a business method. The
109     purpose is to test that the session is passivated and that the session
110     handle can be used to restore the session bean reference.
111      */

112     public void testSessionRefPassivation() throws Exception JavaDoc
113     {
114       Context JavaDoc ctx = new InitialContext JavaDoc();
115       getLog().debug("+++ testSessionRefPassivation");
116       StatefulSessionHome sessionHome = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean");
117       StatefulSession sessionBean = sessionHome.create("testSessionRefPassivation");
118
119         getLog().debug("Creating session ref");
120       sessionBean.createSessionRef();
121
122       getLog().debug("Creating a second session bean, forcing the first one to be passivated?");
123       // The pool size has been set to 1 in the container
124
// config, so creating another ASession here should
125
// cause the first one to be passivated.
126
StatefulSession anotherSession = sessionHome.create("testSessionRefPassivation2");
127       getLog().debug("OK, anotherSession="+anotherSession);
128
129       getLog().debug("Checking for complete passivation/activation");
130       Thread.sleep(1000);
131       assertTrue(sessionBean.getWasPassivated() == true);
132       getLog().debug("OK");
133
134       // Some other checks
135
getLog().debug("Checking reactivation of session ref");
136       String JavaDoc handle = sessionBean.useSessionRef();
137       assertTrue(handle != null);
138       getLog().debug("OK");
139     }
140
141     /** Create a StatefulSessionBean and then create a handle to a session
142     bean in the session. This bean is then passivated by creating another
143     session bean and then activated by invoking a business method. The
144     purpose is to test that the session is passivated and that the session
145     handle can be used to restore the session bean reference.
146      */

147     public void testSessionHandlePassivation() throws Exception JavaDoc
148     {
149       Context JavaDoc ctx = new InitialContext JavaDoc();
150       getLog().debug("+++ testSessionHandlePassivation");
151       StatefulSessionHome sessionHome = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean");
152       StatefulSession sessionBean = sessionHome.create("testSessionHandlePassivation");
153
154         getLog().debug("Creating session handle");
155       sessionBean.createSessionHandle();
156
157       getLog().debug("Creating a second session bean, forcing the first one to be passivated?");
158       // The pool size has been set to 1 in the container
159
// config, so creating another ASession here should
160
// cause the first one to be passivated.
161
StatefulSession anotherSession = sessionHome.create("testSessionHandlePassivation2");
162       getLog().debug("OKm anotherSession="+anotherSession);
163
164       getLog().debug("Checking for complete passivation/activation");
165       Thread.sleep(1000);
166       assertTrue(sessionBean.getWasPassivated() == true);
167       getLog().debug("OK");
168
169       // Some other checks
170
getLog().debug("Checking reactivation of session bean handle");
171       String JavaDoc name = sessionBean.useSessionHandle("Hello");
172       assertTrue(name.equals("Hello"));
173       getLog().debug("OK");
174     }
175
176     /** Create a StatefulSessionBean and then create a handle to a stateful
177     session bean in the session. The handle is serialized into an in memory
178     array to test serialization of handles in the absence of custom marshalling
179     streams.
180
181     This has to use the FacadeStatefulSessionBean due to the fact that the
182     MaxOne Stateful Session configuration is not able to passivate the
183     outer session bean when it tries to create a new session due to the
184     session context being locked by the invocation.
185      */

186     public void testInVMSessionHandlePassivation() throws Exception JavaDoc
187     {
188       Context JavaDoc ctx = new InitialContext JavaDoc();
189       getLog().debug("+++ testInVMSessionHandlePassivation");
190       StatefulSessionHome sessionHome = (StatefulSessionHome) ctx.lookup("ejbcts/FacadeStatefulSessionBean");
191       StatefulSession sessionBean = sessionHome.create("testInVMSessionHandlePassivation");
192
193         getLog().debug("Creating stateful session handle");
194       sessionBean.createStatefulSessionHandle("testInVMSessionHandlePassivation2");
195       sessionBean.useStatefulSessionHandle();
196    }
197
198    /** Create a StatefulSessionBean and then force passivation by creating
199     another session bean and then activated by invoking a business method.
200     This relies on a custom container config that specifies
201     container-cache-conf/cache-policy-conf/max-capacity=1
202      */

203    public void testPassivationBySize() throws Exception JavaDoc
204    {
205       Context JavaDoc ctx = new InitialContext JavaDoc();
206       getLog().debug("+++ testPassivationBySize");
207       StatefulSessionHome sessionHome = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean");
208       StatefulSession sessionBean1 = sessionHome.create("testPassivationBySize");
209       sessionBean1.method1("hello");
210
211       // Create a second bean to force passivation
212
StatefulSession sessionBean2 = sessionHome.create("testPassivationBySize2");
213       sessionBean2.method1("hello");
214
215       // Validate that sessionBean1 was passivated and activated
216
Thread.sleep(1000);
217       boolean passivated = sessionBean1.getWasPassivated();
218       assertTrue("sessionBean1 WasPassivated", passivated);
219       boolean activated = sessionBean1.getWasActivated();
220       assertTrue("sessionBean1 WasActivated", activated);
221    }
222
223    /** Create a StatefulSessionBean and then force passivation by waiting
224     for 45 seconds.
225     This relies on a custom container config that specifies
226     container-cache-conf/cache-policy-conf/remover-period=65
227     container-cache-conf/cache-policy-conf/overager-period=40
228     container-cache-conf/cache-policy-conf/max-bean-age=30
229     container-cache-conf/cache-policy-conf/max-bean-life=60
230      */

231    public void testPassivationByTime() throws Exception JavaDoc
232    {
233       Context JavaDoc ctx = new InitialContext JavaDoc();
234       getLog().debug("+++ testPassivationByTime");
235       StatefulSessionHome sessionHome = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean");
236       StatefulSession sessionBean1 = sessionHome.create("testPassivationByTime");
237       sessionBean1.method1("hello");
238
239       getLog().debug("Retrieving handle for object");
240       Handle JavaDoc handle = sessionBean1.getHandle();
241
242       getLog().debug("Waiting 41 seconds for passivation...");
243       Thread.sleep(41*1000);
244
245       // Validate that sessionBean1 was passivated and activated
246
boolean passivated = sessionBean1.getWasPassivated();
247       assertTrue("sessionBean1 WasPassivated", passivated);
248       boolean activated = sessionBean1.getWasActivated();
249       assertTrue("sessionBean1 WasActivated", activated);
250
251       getLog().debug("Waiting 90 seconds for removal due to age...");
252       Thread.sleep(90*1000);
253
254       try
255       {
256          handle.getEJBObject();
257          fail("Was able to get the remote interface for a removed session");
258       }
259       catch (RemoteException JavaDoc expected)
260       {
261          getLog().debug("Handle access failed as expected", expected);
262       }
263
264       try
265       {
266          passivated = sessionBean1.getWasPassivated();
267          fail("Was able to invoke getWasPassivated after bean should have been removed");
268       }
269       catch(Exception JavaDoc e)
270       {
271          getLog().debug("Bean access failed as expected", e);
272       }
273    }
274
275    /**
276     * EJB 1.1 (Page 40)
277     * "The container is responsible for making the home interfaces
278     * of its deployed enterprise of its deployed enterprise beans
279     * available to the client through JNDI API extension.
280     */

281    public void testBasicSession ()
282       throws Exception JavaDoc
283    {
284       getLog().debug("+++ testBasicSession()");
285       Context JavaDoc ctx = new InitialContext JavaDoc();
286       StatefulSessionHome sessionHome =
287          ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean");
288       StatefulSession sessionBean = sessionHome.create("testBasicSession-create");
289       String JavaDoc result = sessionBean.method1("CTS-Test");
290       // Test response
291
assertTrue(result.equals("CTS-Test"));
292
293       try
294       {
295          sessionBean.remove();
296       }
297       catch (Exception JavaDoc ex)
298       {
299           fail("could not remove stateless session bean" + ex);
300       }
301
302       sessionBean = sessionHome.createAlt("testBasicSession-create");
303       String JavaDoc altName = sessionBean.getTestName();
304       assertTrue("testName == testBasicSession-createAlt",
305          altName.equals("testBasicSession-createAlt"));
306
307       result = sessionBean.method1("CTS-Test");
308       // Test response
309
assertTrue(result.equals("CTS-Test"));
310
311       try
312       {
313          sessionBean.remove();
314       }
315       catch (Exception JavaDoc ex)
316       {
317           fail("could not remove stateless session bean" + ex);
318       }
319    }
320
321    //
322

323    /**
324     * Method testEJBHomeInterface
325     * EJB 1.1 (Page 42)
326     * "The home interface allows a client to do the following:"
327     * - Create a new session object
328     * - Remove session object
329     * - Get the javax.ejb.EJBMetaData interface for the
330     * session bean.
331     * - Obtain a handle for the home interface
332     *
333     * @throws Exception
334     *
335     */

336    public void testEJBHomeInterface ()
337       throws Exception JavaDoc
338    {
339       getLog().debug("+++ testEJBHomeInterface()");
340
341       // Create a new session object
342
Context JavaDoc ctx = new InitialContext JavaDoc();
343       StatefulSessionHome sessionHome =
344          ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean");
345       StatefulSession sessionBean = sessionHome.create("testEJBHomeInterface");
346
347       // Get the EJBMetaData
348
javax.ejb.EJBMetaData JavaDoc md = sessionHome.getEJBMetaData();
349
350       getLog().debug("Verify EJBMetaData from home interface");
351       assertTrue(md != null);
352
353       // Get the EJBMetaData constructs
354
getLog().debug("Get Home interface class");
355
356       java.lang.Class JavaDoc homeInterface = md.getHomeInterfaceClass();
357
358       getLog().debug("home Interface : " + homeInterface.getName());
359       assertTrue(homeInterface.getName().equals("org.jboss.test.cts.interfaces.StatefulSessionHome"));
360       getLog().debug("Get Remote Interface class");
361
362       java.lang.Class JavaDoc remoteInterface = md.getRemoteInterfaceClass();
363
364       getLog().debug("remote Interface: " + remoteInterface.getName());
365       assertTrue(remoteInterface.getName().equals("org.jboss.test.cts.interfaces.StatefulSession"));
366       getLog().debug("Verify isSession..");
367       assertTrue(md.isSession());
368
369       // EJB 1.1 only
370
getLog().debug("Verify is not Stateless session...");
371       assertTrue(!md.isStatelessSession());
372
373       try
374       {
375          sessionBean.remove();
376       }
377       catch (Exception JavaDoc ex)
378       {
379           fail("could not remove stateful session bean" + ex);
380       }
381    }
382
383    /**
384     * Method testRemoveSessionObject
385     * EJB 1.1 (Page 42)
386     * Removing a Session Object.
387     * Because session objects do not have primary keys that are
388     * accessible to clients, invoking the javax.ejb.Home.remove( Object primaryKey )
389     * method on a session results in the javax.ejb.RemoveException.
390     *
391     */

392    public void testRemoveSessionObject ()
393       throws Exception JavaDoc
394    {
395       getLog().debug("+++ testRemoveSessionObject()");
396       Context JavaDoc ctx = new InitialContext JavaDoc();
397       StatefulSessionHome home = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean");
398       StatefulSession bean = home.create("testRemoveSessionObject");
399       getLog().debug("OK, bean="+bean);
400       getLog().debug("Call remove using a primary key");
401       try
402       {
403          home.remove(new AccountPK("pk"));
404          fail("[EJB 1.1, p42, section 5.3.2] Expected 'RemoveException' when remove-ing a session object, got NO exception");
405       }
406       catch(RemoveException JavaDoc e)
407       {
408          getLog().debug("Remove using a primary key failed as expected");
409       }
410    }
411
412    //-------------------------------------------------------------------------
413
//-------------------------------------------------------------------------
414

415    /**
416     * Method testCompareSerializeGetPK
417     * EJB 1.1 [5.5] Page 43
418     * EJBOjbect.getPrimaryKey results in a RemoteException
419     * Get a serializable handle
420     * Compare on bean to another for equality
421     *
422     *
423     */

424    public void testCompareSerializeGetPK ()
425       throws Exception JavaDoc
426    {
427       getLog().debug("+++ testCompareSerializeGetPK()");
428
429       Context JavaDoc ctx = new InitialContext JavaDoc();
430       StatefulSessionHome home = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean");
431       StatefulSession bean = home.create("testCompareSerializeGetPK");
432
433       // Get the bean handle
434
Handle JavaDoc hn = bean.getHandle();
435
436       assertTrue(hn != null);
437
438       // "Copy" the bean
439
StatefulSession theOtherBean =
440          ( StatefulSession ) javax.rmi.PortableRemoteObject.narrow(
441             hn.getEJBObject(), StatefulSession.class);
442
443       assertTrue(theOtherBean != null);
444       assertTrue(bean.isIdentical(theOtherBean));
445    }
446
447    /**
448     * Method testSerialization
449     * EJB 1.1 [5.7] Page 45
450     * Session bean must be serializable.
451     */

452    public void testSerialization ()
453       throws Exception JavaDoc
454    {
455       getLog().debug("+++ testSerialize");
456       Context JavaDoc ctx = new InitialContext JavaDoc();
457       StatefulSessionHome home = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean");
458       StatefulSession bean = home.create("testSerialization");
459       getLog().debug("Increment bean, count = 3");
460
461       // put on some state...
462
bean.setCounter(1);
463       bean.incCounter();
464       bean.incCounter();
465
466       // bean should be=3;
467
getLog().debug("Bean == 3?");
468       assertTrue(bean.getCounter() == 3);
469       getLog().debug("passes..");
470
471       // Get handle and serialize
472
Handle JavaDoc beanHandle = bean.getHandle();
473       FileOutputStream JavaDoc out = new FileOutputStream JavaDoc("abean.ser");
474       ObjectOutputStream JavaDoc s = new ObjectOutputStream JavaDoc(out);
475
476       s.writeObject(beanHandle);
477       s.flush();
478    }
479
480    /**
481     * Method testUnSerialization
482     * EJB 1.1 [5.7] Page 45
483     * Part II of the test, makes sure the bean can be resurected and used.
484     */

485    public void testUnSerialization ()
486       throws Exception JavaDoc
487    {
488       //We are deploying for each test, so we need to reserialize first.
489
testSerialization();
490       getLog().debug("+++ testUnSerialize");
491
492       StatefulSession bean = null;
493       getLog().debug("Resurrect bean from .ser file");
494       FileInputStream JavaDoc in = new FileInputStream JavaDoc("abean.ser");
495       ObjectInputStream JavaDoc s = new ObjectInputStream JavaDoc(in);
496       Handle JavaDoc beanHandle = ( Handle JavaDoc ) s.readObject();
497       bean = ( StatefulSession ) beanHandle.getEJBObject();
498
499       // Should still equal '3'?
500
getLog().debug("Bean reanimated, still equal '3'? bean = "
501                          + bean.getCounter());
502       assertTrue(bean.getCounter() == 3);
503       getLog().debug("Yup, equal to '3'");
504       bean.decCounter();
505       bean.remove();
506    }
507
508    /** Test of handle that is unmarshalled in a environment where
509     * new InitialContext() will not work.
510     * @throws Exception
511     */

512    public void testSessionHandleNoDefaultJNDI()
513          throws Exception JavaDoc
514    {
515       getLog().debug("+++ testSessionHandleNoDefaultJNDI()");
516
517       Properties JavaDoc env = new Properties JavaDoc();
518       env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.NamingContextFactory");
519       InitialContext JavaDoc ctx = new InitialContext JavaDoc(env);
520       StatefulSessionHome home = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean");
521       StatefulSession bean = home.create("testSessionHandleNoDefaultJNDI");
522
523       Handle JavaDoc beanHandle = bean.getHandle();
524       ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
525       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(out);
526       oos.writeObject(beanHandle);
527       oos.flush();
528       byte[] bytes = out.toByteArray();
529
530       Properties JavaDoc sysProps = System.getProperties();
531       Properties JavaDoc newProps = new Properties JavaDoc(sysProps);
532       newProps.setProperty("java.naming.factory.initial", "badFactory");
533       newProps.setProperty("java.naming.provider.url", "jnp://badhost:12345");
534       System.setProperties(newProps);
535       try
536       {
537          getLog().debug("Unserialize bean handle...");
538          ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(bytes);
539          ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(in);
540          beanHandle = (Handle JavaDoc) ois.readObject();
541          bean = (StatefulSession) beanHandle.getEJBObject();
542          bean.method1("Hello");
543          getLog().debug("Called method1 on handle session bean");
544       }
545       finally
546       {
547          System.setProperties(sysProps);
548       }
549    }
550
551    /** Test of BMT handle that is unmarshalled in a environment where
552     * new InitialContext() will not work.
553     * @throws Exception
554     */

555    public void testBMTSessionHandleNoDefaultJNDI()
556          throws Exception JavaDoc
557    {
558       getLog().debug("+++ testBMTSessionHandleNoDefaultJNDI()");
559
560       Properties JavaDoc env = new Properties JavaDoc();
561       env.setProperty(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.NamingContextFactory");
562       InitialContext JavaDoc ctx = new InitialContext JavaDoc(env);
563       StatefulSessionHome home = ( StatefulSessionHome ) ctx.lookup("ejbcts/BMTStatefulSessionBean");
564       StatefulSession bean = home.create("testBMTSessionHandleNoDefaultJNDI");
565
566       Handle JavaDoc beanHandle = bean.getHandle();
567       ByteArrayOutputStream JavaDoc out = new ByteArrayOutputStream JavaDoc();
568       ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc(out);
569       oos.writeObject(beanHandle);
570       oos.flush();
571       byte[] bytes = out.toByteArray();
572
573       Properties JavaDoc sysProps = System.getProperties();
574       Properties JavaDoc newProps = new Properties JavaDoc(sysProps);
575       newProps.setProperty("java.naming.factory.initial", "badFactory");
576       newProps.setProperty("java.naming.provider.url", "jnp://badhost:12345");
577       System.setProperties(newProps);
578       try
579       {
580          getLog().debug("Unserialize bean handle...");
581          ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(bytes);
582          ObjectInputStream JavaDoc ois = new ObjectInputStream JavaDoc(in);
583          beanHandle = (Handle JavaDoc) ois.readObject();
584          bean = (StatefulSession) beanHandle.getEJBObject();
585          bean.method1("Hello");
586          getLog().debug("Called method1 on handle session bean");
587       }
588       finally
589       {
590          System.setProperties(sysProps);
591       }
592    }
593
594    /** Test of accessing the home interface from the remote interface in an env
595     * new InitialContext() will not work.
596     * @throws Exception
597     */

598    public void testHomeFromRemoteNoDefaultJNDI()
599          throws Exception JavaDoc
600    {
601       getLog().debug("+++ testHomeFromRemoteNoDefaultJNDI()");
602
603       // Override the JNDI variables in the System properties
604
Properties JavaDoc sysProps = System.getProperties();
605       Properties JavaDoc newProps = new Properties JavaDoc(sysProps);
606       newProps.setProperty("java.naming.factory.initial", "badFactory");
607       newProps.setProperty("java.naming.provider.url", "jnp://badhost:12345");
608       System.setProperties(newProps);
609
610       // Do a lookup of the home and create a remote using a custom env
611
Properties JavaDoc env = new Properties JavaDoc();
612       env.setProperty("java.naming.factory.initial", super.getJndiInitFactory());
613       env.setProperty("java.naming.provider.url", super.getJndiURL());
614       try
615       {
616          InitialContext JavaDoc ctx = new InitialContext JavaDoc(env);
617          Object JavaDoc ref = ctx.lookup("ejbcts/StatefulSessionBean");
618          StatefulSessionHome home = (StatefulSessionHome)
619                PortableRemoteObject.narrow(ref, StatefulSessionHome.class);
620          StatefulSession bean1 = home.create("testHomeFromRemoteNoDefaultJNDI");
621          StatefulSessionHome home2 = (StatefulSessionHome) bean1.getEJBHome();
622          StatefulSession bean2 = home2.create("testHomeFromRemoteNoDefaultJNDI");
623          bean2.remove();
624       }
625       finally
626       {
627          System.setProperties(sysProps);
628       }
629    }
630
631    /**
632     * Method testProbeBeanContext
633     *
634     * EJB 1.1 [6.4.1] Page 51
635     *
636     */

637    public void testProbeBeanContext ()
638       throws Exception JavaDoc
639    {
640       getLog().debug("+++ testProbeBeanContext");
641       Context JavaDoc ctx = new InitialContext JavaDoc();
642       StatefulSessionHome home = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean");
643       StatefulSession bean = home.create("testProbeBeanContext");
644
645       getLog().debug("Invoking bean...");
646
647       BeanContextInfo beanCtxInfo = bean.getBeanContextInfo();
648
649       assertTrue(beanCtxInfo != null);
650
651       getLog().debug("remote interface: "
652                          + beanCtxInfo.remoteInterface);
653       getLog().debug("home interface: "
654                          + beanCtxInfo.homeInterface);
655
656       getLog().debug("Testing rollback only setting...");
657       assertTrue(beanCtxInfo.isRollbackOnly.booleanValue());
658       getLog().debug(
659          "**************************************************************");
660    }
661
662    /**
663     * Method testLoopback
664     * EJB 1.1 [6.5.6]
665     * A client call to bean 'A' which calls bean 'B' and bean 'B'
666     * in turn calls a method on bean 'A', there should be a
667     * RemoteException should be thrown.
668     */

669    public void testLoopback ()
670       throws Exception JavaDoc
671    {
672       getLog().debug("+++ testLoopback");
673
674       // Create a new session object
675
Context JavaDoc ctx = new InitialContext JavaDoc();
676       StatefulSessionHome home = ( StatefulSessionHome ) ctx.lookup("ejbcts/StatefulSessionBean");
677       StatefulSession bean = home.create("testLoopback");
678       getLog().debug("Calling loopbackTest( )....");
679       try
680       {
681          bean.loopbackTest();
682          fail("Was able to call loopbackTest()");
683       }
684       catch(Exception JavaDoc e)
685       {
686          getLog().debug("The loopbackTest( ) failed as expected");
687       }
688    }
689
690    public void testUserTx()
691       throws Exception JavaDoc
692    {
693       getLog().debug("+++ testUsrTx");
694
695       getLog().debug("Obtain home interface");
696       // Create a new session object
697
Context JavaDoc ctx = new InitialContext JavaDoc();
698       Object JavaDoc ref = ctx.lookup("ejbcts/StatefulSessionBean");
699       StatefulSessionHome home = ( StatefulSessionHome ) PortableRemoteObject.narrow(ref,
700          StatefulSessionHome.class);
701       StatefulSession bean = home.create("testUserTx");
702
703       bean.setCounter(100);
704       getLog().debug("Try to instantiate a UserTransaction");
705       UserTransaction JavaDoc userTx = (UserTransaction JavaDoc)ctx.lookup("UserTransaction");
706       userTx.begin();
707          bean.incCounter();
708          bean.incCounter();
709       userTx.commit();
710       int counter = bean.getCounter();
711       assertTrue("counter == 102", counter == 102);
712
713       bean.setCounter(100);
714       userTx.begin();
715          bean.incCounter();
716          bean.incCounter();
717       userTx.rollback();
718       counter = bean.getCounter();
719       assertTrue("counter == 100", counter == 100);
720
721       bean.remove();
722    }
723
724    /** Run MAX_SIZE instances currently with a strictMaxSize setting of 5 to
725     * see that at most 5 instances are active in the bean instance.
726     *
727     * @throws Exception
728     */

729    public void testStrictPooling() throws Exception JavaDoc
730    {
731       log.debug("+++ testStrictPooling");
732       CountDown done = new CountDown(MAX_SIZE);
733       InitialContext JavaDoc ctx = new InitialContext JavaDoc();
734       StrictlyPooledSessionHome home = (StrictlyPooledSessionHome)
735             ctx.lookup("ejbcts/StrictlyPooledStatefulBean");
736       SessionInvoker[] threads = new SessionInvoker[MAX_SIZE];
737       for(int n = 0; n < MAX_SIZE; n ++)
738       {
739          SessionInvoker t = new SessionInvoker(home, n, done, getLog());
740          threads[n] = t;
741          t.start();
742       }
743       boolean ok = done.attempt(1500 * MAX_SIZE);
744       assertTrue("Acquired done, remaining="+done.currentCount(), ok);
745
746       for(int n = 0; n < MAX_SIZE; n ++)
747       {
748          SessionInvoker t = threads[n];
749          if( t.runEx != null )
750          {
751             t.runEx.printStackTrace();
752             log.error("SessionInvoker.runEx != null", t.runEx);
753             fail("SessionInvoker.runEx != null");
754          }
755       }
756    }
757
758    public void testBadUserTx() throws Exception JavaDoc
759    {
760       getLog().debug("+++ testBadUsrTx");
761
762       getLog().debug("Obtain home interface");
763       // Create a new session object
764
Context JavaDoc ctx = new InitialContext JavaDoc();
765       Object JavaDoc ref = ctx.lookup("ejbcts/BMTStatefulSessionBean");
766       StatefulSessionHome home = (StatefulSessionHome) PortableRemoteObject.narrow(ref,
767          StatefulSessionHome.class);
768       StatefulSession bean = home.create("testBadUserTx");
769
770       MBeanServerConnection JavaDoc server = getServer();
771       Long JavaDoc before = (Long JavaDoc) server.getAttribute(TransactionManagerServiceMBean.OBJECT_NAME, "TransactionCount");
772       try
773       {
774          bean.testBadUserTx();
775          fail("Should throw an exception");
776       }
777       catch (Throwable JavaDoc expected)
778       {
779          log.debug("Got exception", expected);
780       }
781       Long JavaDoc after = (Long JavaDoc) server.getAttribute(TransactionManagerServiceMBean.OBJECT_NAME, "TransactionCount");
782       assertEquals("Transaction should no longer be active before=" + before + " after=" + after, before, after);
783    }
784
785    public static Test suite() throws Exception JavaDoc
786    {
787       return getDeploySetup(StatefulSessionUnitTestCase.class, "cts.jar");
788    }
789
790 }
791
Popular Tags