Source Code Cross Referenced for StatefulSessionUnitTestCase.java in  » EJB-Server-JBoss-4.2.1 » testsuite » org » jboss » test » cts » test » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » EJB Server JBoss 4.2.1 » testsuite » org.jboss.test.cts.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * JBoss, Home of Professional Open Source.
003:         * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004:         * as indicated by the @author tags. See the copyright.txt file in the
005:         * distribution for a full listing of individual contributors.
006:         *
007:         * This is free software; you can redistribute it and/or modify it
008:         * under the terms of the GNU Lesser General Public License as
009:         * published by the Free Software Foundation; either version 2.1 of
010:         * the License, or (at your option) any later version.
011:         *
012:         * This software is distributed in the hope that it will be useful,
013:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
014:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015:         * Lesser General Public License for more details.
016:         *
017:         * You should have received a copy of the GNU Lesser General Public
018:         * License along with this software; if not, write to the Free
019:         * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020:         * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021:         */
022:        package org.jboss.test.cts.test;
023:
024:        import java.io.ByteArrayInputStream;
025:        import java.io.ByteArrayOutputStream;
026:        import java.io.FileInputStream;
027:        import java.io.FileOutputStream;
028:        import java.io.ObjectInputStream;
029:        import java.io.ObjectOutputStream;
030:        import java.rmi.RemoteException;
031:        import java.util.Properties;
032:
033:        import javax.ejb.Handle;
034:        import javax.ejb.RemoveException;
035:        import javax.management.MBeanServerConnection;
036:        import javax.naming.Context;
037:        import javax.naming.InitialContext;
038:        import javax.rmi.PortableRemoteObject;
039:        import javax.transaction.UserTransaction;
040:
041:        import junit.framework.Test;
042:
043:        import org.jboss.test.JBossTestCase;
044:        import org.jboss.test.cts.interfaces.BeanContextInfo;
045:        import org.jboss.test.cts.interfaces.StatefulSession;
046:        import org.jboss.test.cts.interfaces.StatefulSessionHome;
047:        import org.jboss.test.cts.interfaces.StrictlyPooledSessionHome;
048:        import org.jboss.test.cts.keys.AccountPK;
049:        import org.jboss.tm.TransactionManagerServiceMBean;
050:
051:        import EDU.oswego.cs.dl.util.concurrent.CountDown;
052:
053:        /** Tests of stateful session beans
054:         *
055:         *   @author kimptoc 
056:         *   @author Scott.Stark@jboss.org 
057:         *   @author d_jencks converted to JBossTestCase, added logging.
058:         *   @version $Revision: 57211 $
059:         */
060:        public class StatefulSessionUnitTestCase extends JBossTestCase {
061:            static final int MAX_SIZE = 20;
062:
063:            public StatefulSessionUnitTestCase(String name) {
064:                super (name);
065:            }
066:
067:            /** Create a StatefulSessionBean and then create a local interface to
068:            an entity in the session. This bean is then passivated by creating another
069:            session bean and then activated by invoking a business method. The
070:            purpose is to test that the session is passivated and that the local
071:            interface is restored.
072:             */
073:            public void testLocalInterfacePassivation() throws Exception {
074:                Context ctx = new InitialContext();
075:                getLog().debug("+++ testLocalInterfacePassivation");
076:                StatefulSessionHome sessionHome = (StatefulSessionHome) ctx
077:                        .lookup("ejbcts/StatefulSessionBean");
078:                StatefulSession sessionBean = sessionHome
079:                        .create("testLocalInterfacePassivation");
080:
081:                getLog().debug("Creating local home");
082:                AccountPK pk = new AccountPK("123456789");
083:                sessionBean.createLocalEntity(pk, "jduke");
084:
085:                getLog()
086:                        .debug(
087:                                "Creating a second session bean, forcing the first one to be passivated?");
088:                // The pool size has been set to 1 in the container
089:                // config, so creating another ASession here should
090:                // cause the first one to be passivated.
091:                StatefulSession anotherSession = sessionHome
092:                        .create("testLocalInterfacePassivation2");
093:                getLog().debug("OK, anotherSession=" + anotherSession);
094:
095:                getLog().debug("Checking for complete passivation/activation");
096:                Thread.sleep(1000);
097:                assertTrue(sessionBean.getWasPassivated() == true);
098:                getLog().debug("OK");
099:
100:                // Some other checks
101:                getLog().debug(
102:                        "Checking reactivation of session bean attributes");
103:                String name = sessionBean.readAndRemoveEntity();
104:                assertTrue(name.equals("jduke"));
105:                getLog().debug("OK");
106:            }
107:
108:            /** Create a StatefulSessionBean and then create a handle to a session
109:            bean in the session. This bean is then passivated by creating another
110:            session bean and then activated by invoking a business method. The
111:            purpose is to test that the session is passivated and that the session
112:            handle can be used to restore the session bean reference.
113:             */
114:            public void testSessionRefPassivation() throws Exception {
115:                Context ctx = new InitialContext();
116:                getLog().debug("+++ testSessionRefPassivation");
117:                StatefulSessionHome sessionHome = (StatefulSessionHome) ctx
118:                        .lookup("ejbcts/StatefulSessionBean");
119:                StatefulSession sessionBean = sessionHome
120:                        .create("testSessionRefPassivation");
121:
122:                getLog().debug("Creating session ref");
123:                sessionBean.createSessionRef();
124:
125:                getLog()
126:                        .debug(
127:                                "Creating a second session bean, forcing the first one to be passivated?");
128:                // The pool size has been set to 1 in the container
129:                // config, so creating another ASession here should
130:                // cause the first one to be passivated.
131:                StatefulSession anotherSession = sessionHome
132:                        .create("testSessionRefPassivation2");
133:                getLog().debug("OK, anotherSession=" + anotherSession);
134:
135:                getLog().debug("Checking for complete passivation/activation");
136:                Thread.sleep(1000);
137:                assertTrue(sessionBean.getWasPassivated() == true);
138:                getLog().debug("OK");
139:
140:                // Some other checks
141:                getLog().debug("Checking reactivation of session ref");
142:                String handle = sessionBean.useSessionRef();
143:                assertTrue(handle != null);
144:                getLog().debug("OK");
145:            }
146:
147:            /** Create a StatefulSessionBean and then create a handle to a session
148:            bean in the session. This bean is then passivated by creating another
149:            session bean and then activated by invoking a business method. The
150:            purpose is to test that the session is passivated and that the session
151:            handle can be used to restore the session bean reference.
152:             */
153:            public void testSessionHandlePassivation() throws Exception {
154:                Context ctx = new InitialContext();
155:                getLog().debug("+++ testSessionHandlePassivation");
156:                StatefulSessionHome sessionHome = (StatefulSessionHome) ctx
157:                        .lookup("ejbcts/StatefulSessionBean");
158:                StatefulSession sessionBean = sessionHome
159:                        .create("testSessionHandlePassivation");
160:
161:                getLog().debug("Creating session handle");
162:                sessionBean.createSessionHandle();
163:
164:                getLog()
165:                        .debug(
166:                                "Creating a second session bean, forcing the first one to be passivated?");
167:                // The pool size has been set to 1 in the container
168:                // config, so creating another ASession here should
169:                // cause the first one to be passivated.
170:                StatefulSession anotherSession = sessionHome
171:                        .create("testSessionHandlePassivation2");
172:                getLog().debug("OKm anotherSession=" + anotherSession);
173:
174:                getLog().debug("Checking for complete passivation/activation");
175:                Thread.sleep(1000);
176:                assertTrue(sessionBean.getWasPassivated() == true);
177:                getLog().debug("OK");
178:
179:                // Some other checks
180:                getLog().debug("Checking reactivation of session bean handle");
181:                String name = sessionBean.useSessionHandle("Hello");
182:                assertTrue(name.equals("Hello"));
183:                getLog().debug("OK");
184:            }
185:
186:            /** Create a StatefulSessionBean and then create a handle to a stateful
187:            session bean in the session. The handle is serialized into an in memory
188:            array to test serialization of handles in the absence of custom marshalling
189:            streams.
190:
191:            This has to use the FacadeStatefulSessionBean due to the fact that the
192:            MaxOne Stateful Session configuration is not able to passivate the
193:            outer session bean when it tries to create a new session due to the
194:            session context being locked by the invocation.
195:             */
196:            public void testInVMSessionHandlePassivation() throws Exception {
197:                Context ctx = new InitialContext();
198:                getLog().debug("+++ testInVMSessionHandlePassivation");
199:                StatefulSessionHome sessionHome = (StatefulSessionHome) ctx
200:                        .lookup("ejbcts/FacadeStatefulSessionBean");
201:                StatefulSession sessionBean = sessionHome
202:                        .create("testInVMSessionHandlePassivation");
203:
204:                getLog().debug("Creating stateful session handle");
205:                sessionBean
206:                        .createStatefulSessionHandle("testInVMSessionHandlePassivation2");
207:                sessionBean.useStatefulSessionHandle();
208:            }
209:
210:            /** Create a StatefulSessionBean and then force passivation by creating
211:             another session bean and then activated by invoking a business method.
212:             This relies on a custom container config that specifies
213:             container-cache-conf/cache-policy-conf/max-capacity=1
214:             */
215:            public void testPassivationBySize() throws Exception {
216:                Context ctx = new InitialContext();
217:                getLog().debug("+++ testPassivationBySize");
218:                StatefulSessionHome sessionHome = (StatefulSessionHome) ctx
219:                        .lookup("ejbcts/StatefulSessionBean");
220:                StatefulSession sessionBean1 = sessionHome
221:                        .create("testPassivationBySize");
222:                sessionBean1.method1("hello");
223:
224:                // Create a second bean to force passivation
225:                StatefulSession sessionBean2 = sessionHome
226:                        .create("testPassivationBySize2");
227:                sessionBean2.method1("hello");
228:
229:                // Validate that sessionBean1 was passivated and activated
230:                Thread.sleep(1000);
231:                boolean passivated = sessionBean1.getWasPassivated();
232:                assertTrue("sessionBean1 WasPassivated", passivated);
233:                boolean activated = sessionBean1.getWasActivated();
234:                assertTrue("sessionBean1 WasActivated", activated);
235:            }
236:
237:            /** Create a StatefulSessionBean and then force passivation by waiting
238:             for 45 seconds.
239:             This relies on a custom container config that specifies
240:             container-cache-conf/cache-policy-conf/remover-period=65
241:             container-cache-conf/cache-policy-conf/overager-period=40
242:             container-cache-conf/cache-policy-conf/max-bean-age=30
243:             container-cache-conf/cache-policy-conf/max-bean-life=60
244:             */
245:            public void testPassivationByTime() throws Exception {
246:                Context ctx = new InitialContext();
247:                getLog().debug("+++ testPassivationByTime");
248:                StatefulSessionHome sessionHome = (StatefulSessionHome) ctx
249:                        .lookup("ejbcts/StatefulSessionBean");
250:                StatefulSession sessionBean1 = sessionHome
251:                        .create("testPassivationByTime");
252:                sessionBean1.method1("hello");
253:
254:                getLog().debug("Retrieving handle for object");
255:                Handle handle = sessionBean1.getHandle();
256:
257:                getLog().debug("Waiting 41 seconds for passivation...");
258:                Thread.sleep(41 * 1000);
259:
260:                // Validate that sessionBean1 was passivated and activated
261:                boolean passivated = sessionBean1.getWasPassivated();
262:                assertTrue("sessionBean1 WasPassivated", passivated);
263:                boolean activated = sessionBean1.getWasActivated();
264:                assertTrue("sessionBean1 WasActivated", activated);
265:
266:                getLog().debug("Waiting 90 seconds for removal due to age...");
267:                Thread.sleep(90 * 1000);
268:
269:                try {
270:                    handle.getEJBObject();
271:                    fail("Was able to get the remote interface for a removed session");
272:                } catch (RemoteException expected) {
273:                    getLog()
274:                            .debug("Handle access failed as expected", expected);
275:                }
276:
277:                try {
278:                    passivated = sessionBean1.getWasPassivated();
279:                    fail("Was able to invoke getWasPassivated after bean should have been removed");
280:                } catch (Exception e) {
281:                    getLog().debug("Bean access failed as expected", e);
282:                }
283:            }
284:
285:            /**
286:             * EJB 1.1 (Page 40)
287:             *  "The container is responsible for making the home interfaces
288:             *   of its deployed enterprise of its deployed enterprise beans
289:             *   available to the client through JNDI API extension.
290:             */
291:            public void testBasicSession() throws Exception {
292:                getLog().debug("+++ testBasicSession()");
293:                Context ctx = new InitialContext();
294:                StatefulSessionHome sessionHome = (StatefulSessionHome) ctx
295:                        .lookup("ejbcts/StatefulSessionBean");
296:                StatefulSession sessionBean = sessionHome
297:                        .create("testBasicSession-create");
298:                String result = sessionBean.method1("CTS-Test");
299:                // Test response
300:                assertTrue(result.equals("CTS-Test"));
301:
302:                try {
303:                    sessionBean.remove();
304:                } catch (Exception ex) {
305:                    fail("could not remove stateless session bean" + ex);
306:                }
307:
308:                sessionBean = sessionHome.createAlt("testBasicSession-create");
309:                String altName = sessionBean.getTestName();
310:                assertTrue("testName == testBasicSession-createAlt", altName
311:                        .equals("testBasicSession-createAlt"));
312:
313:                result = sessionBean.method1("CTS-Test");
314:                // Test response
315:                assertTrue(result.equals("CTS-Test"));
316:
317:                try {
318:                    sessionBean.remove();
319:                } catch (Exception ex) {
320:                    fail("could not remove stateless session bean" + ex);
321:                }
322:            }
323:
324:            //
325:
326:            /**
327:             * Method testEJBHomeInterface
328:             *  EJB 1.1 (Page 42)
329:             *  "The home interface allows a client to do the following:"
330:             *   - Create a new session object
331:             *   - Remove session object
332:             *   - Get the javax.ejb.EJBMetaData interface for the
333:             *     session bean.
334:             *   - Obtain a handle for the home interface
335:             *
336:             * @throws Exception
337:             *
338:             */
339:            public void testEJBHomeInterface() throws Exception {
340:                getLog().debug("+++ testEJBHomeInterface()");
341:
342:                // Create a new session object
343:                Context ctx = new InitialContext();
344:                StatefulSessionHome sessionHome = (StatefulSessionHome) ctx
345:                        .lookup("ejbcts/StatefulSessionBean");
346:                StatefulSession sessionBean = sessionHome
347:                        .create("testEJBHomeInterface");
348:
349:                // Get the EJBMetaData
350:                javax.ejb.EJBMetaData md = sessionHome.getEJBMetaData();
351:
352:                getLog().debug("Verify EJBMetaData from home interface");
353:                assertTrue(md != null);
354:
355:                // Get the EJBMetaData constructs
356:                getLog().debug("Get Home interface class");
357:
358:                java.lang.Class homeInterface = md.getHomeInterfaceClass();
359:
360:                getLog().debug("home Interface : " + homeInterface.getName());
361:                assertTrue(homeInterface.getName().equals(
362:                        "org.jboss.test.cts.interfaces.StatefulSessionHome"));
363:                getLog().debug("Get Remote Interface class");
364:
365:                java.lang.Class remoteInterface = md.getRemoteInterfaceClass();
366:
367:                getLog()
368:                        .debug("remote Interface: " + remoteInterface.getName());
369:                assertTrue(remoteInterface.getName().equals(
370:                        "org.jboss.test.cts.interfaces.StatefulSession"));
371:                getLog().debug("Verify isSession..");
372:                assertTrue(md.isSession());
373:
374:                // EJB 1.1 only
375:                getLog().debug("Verify is not Stateless session...");
376:                assertTrue(!md.isStatelessSession());
377:
378:                try {
379:                    sessionBean.remove();
380:                } catch (Exception ex) {
381:                    fail("could not remove stateful session bean" + ex);
382:                }
383:            }
384:
385:            /**
386:             * Method testRemoveSessionObject
387:             * EJB 1.1 (Page 42)
388:             * Removing a Session Object.
389:             * Because session objects do not have primary keys that are
390:             * accessible to clients, invoking the javax.ejb.Home.remove( Object primaryKey )
391:             * method on a session results in the javax.ejb.RemoveException.
392:             *
393:             */
394:            public void testRemoveSessionObject() throws Exception {
395:                getLog().debug("+++ testRemoveSessionObject()");
396:                Context ctx = new InitialContext();
397:                StatefulSessionHome home = (StatefulSessionHome) ctx
398:                        .lookup("ejbcts/StatefulSessionBean");
399:                StatefulSession bean = home.create("testRemoveSessionObject");
400:                getLog().debug("OK, bean=" + bean);
401:                getLog().debug("Call remove using a primary key");
402:                try {
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:                } catch (RemoveException e) {
406:                    getLog().debug(
407:                            "Remove using a primary key failed as expected");
408:                }
409:            }
410:
411:            //-------------------------------------------------------------------------
412:            //-------------------------------------------------------------------------
413:
414:            /**
415:             * Method testCompareSerializeGetPK
416:             * EJB 1.1 [5.5] Page 43
417:             * EJBOjbect.getPrimaryKey results in a RemoteException
418:             * Get a serializable handle
419:             * Compare on bean to another for equality
420:             *
421:             *
422:             */
423:            public void testCompareSerializeGetPK() throws Exception {
424:                getLog().debug("+++ testCompareSerializeGetPK()");
425:
426:                Context ctx = new InitialContext();
427:                StatefulSessionHome home = (StatefulSessionHome) ctx
428:                        .lookup("ejbcts/StatefulSessionBean");
429:                StatefulSession bean = home.create("testCompareSerializeGetPK");
430:
431:                // Get the bean handle
432:                Handle hn = bean.getHandle();
433:
434:                assertTrue(hn != null);
435:
436:                // "Copy" the bean
437:                StatefulSession theOtherBean = (StatefulSession) javax.rmi.PortableRemoteObject
438:                        .narrow(hn.getEJBObject(), StatefulSession.class);
439:
440:                assertTrue(theOtherBean != null);
441:                assertTrue(bean.isIdentical(theOtherBean));
442:            }
443:
444:            /**
445:             * Method testSerialization
446:             * EJB 1.1 [5.7] Page 45
447:             * Session bean must be serializable.
448:             */
449:            public void testSerialization() throws Exception {
450:                getLog().debug("+++ testSerialize");
451:                Context ctx = new InitialContext();
452:                StatefulSessionHome home = (StatefulSessionHome) ctx
453:                        .lookup("ejbcts/StatefulSessionBean");
454:                StatefulSession bean = home.create("testSerialization");
455:                getLog().debug("Increment bean, count = 3");
456:
457:                // put on some state...
458:                bean.setCounter(1);
459:                bean.incCounter();
460:                bean.incCounter();
461:
462:                // bean should be=3;
463:                getLog().debug("Bean == 3?");
464:                assertTrue(bean.getCounter() == 3);
465:                getLog().debug("passes..");
466:
467:                // Get handle and serialize
468:                Handle beanHandle = bean.getHandle();
469:                FileOutputStream out = new FileOutputStream("abean.ser");
470:                ObjectOutputStream s = new ObjectOutputStream(out);
471:
472:                s.writeObject(beanHandle);
473:                s.flush();
474:            }
475:
476:            /**
477:             * Method testUnSerialization
478:             * EJB 1.1 [5.7] Page 45
479:             * Part II of the test, makes sure the bean can be resurected and used.
480:             */
481:            public void testUnSerialization() throws Exception {
482:                //We are deploying for each test, so we need to reserialize first.
483:                testSerialization();
484:                getLog().debug("+++ testUnSerialize");
485:
486:                StatefulSession bean = null;
487:                getLog().debug("Resurrect bean from .ser file");
488:                FileInputStream in = new FileInputStream("abean.ser");
489:                ObjectInputStream s = new ObjectInputStream(in);
490:                Handle beanHandle = (Handle) s.readObject();
491:                bean = (StatefulSession) beanHandle.getEJBObject();
492:
493:                // Should still equal '3'?
494:                getLog().debug(
495:                        "Bean reanimated, still equal '3'? bean = "
496:                                + bean.getCounter());
497:                assertTrue(bean.getCounter() == 3);
498:                getLog().debug("Yup, equal to '3'");
499:                bean.decCounter();
500:                bean.remove();
501:            }
502:
503:            /** Test of handle that is unmarshalled in a environment where
504:             * new InitialContext() will not work.
505:             * @throws Exception
506:             */
507:            public void testSessionHandleNoDefaultJNDI() throws Exception {
508:                getLog().debug("+++ testSessionHandleNoDefaultJNDI()");
509:
510:                Properties env = new Properties();
511:                env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
512:                        "org.jboss.naming.NamingContextFactory");
513:                InitialContext ctx = new InitialContext(env);
514:                StatefulSessionHome home = (StatefulSessionHome) ctx
515:                        .lookup("ejbcts/StatefulSessionBean");
516:                StatefulSession bean = home
517:                        .create("testSessionHandleNoDefaultJNDI");
518:
519:                Handle beanHandle = bean.getHandle();
520:                ByteArrayOutputStream out = new ByteArrayOutputStream();
521:                ObjectOutputStream oos = new ObjectOutputStream(out);
522:                oos.writeObject(beanHandle);
523:                oos.flush();
524:                byte[] bytes = out.toByteArray();
525:
526:                Properties sysProps = System.getProperties();
527:                Properties newProps = new Properties(sysProps);
528:                newProps.setProperty("java.naming.factory.initial",
529:                        "badFactory");
530:                newProps.setProperty("java.naming.provider.url",
531:                        "jnp://badhost:12345");
532:                System.setProperties(newProps);
533:                try {
534:                    getLog().debug("Unserialize bean handle...");
535:                    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
536:                    ObjectInputStream ois = new ObjectInputStream(in);
537:                    beanHandle = (Handle) ois.readObject();
538:                    bean = (StatefulSession) beanHandle.getEJBObject();
539:                    bean.method1("Hello");
540:                    getLog().debug("Called method1 on handle session bean");
541:                } finally {
542:                    System.setProperties(sysProps);
543:                }
544:            }
545:
546:            /** Test of BMT handle that is unmarshalled in a environment where
547:             * new InitialContext() will not work.
548:             * @throws Exception
549:             */
550:            public void testBMTSessionHandleNoDefaultJNDI() throws Exception {
551:                getLog().debug("+++ testBMTSessionHandleNoDefaultJNDI()");
552:
553:                Properties env = new Properties();
554:                env.setProperty(Context.INITIAL_CONTEXT_FACTORY,
555:                        "org.jboss.naming.NamingContextFactory");
556:                InitialContext ctx = new InitialContext(env);
557:                StatefulSessionHome home = (StatefulSessionHome) ctx
558:                        .lookup("ejbcts/BMTStatefulSessionBean");
559:                StatefulSession bean = home
560:                        .create("testBMTSessionHandleNoDefaultJNDI");
561:
562:                Handle beanHandle = bean.getHandle();
563:                ByteArrayOutputStream out = new ByteArrayOutputStream();
564:                ObjectOutputStream oos = new ObjectOutputStream(out);
565:                oos.writeObject(beanHandle);
566:                oos.flush();
567:                byte[] bytes = out.toByteArray();
568:
569:                Properties sysProps = System.getProperties();
570:                Properties newProps = new Properties(sysProps);
571:                newProps.setProperty("java.naming.factory.initial",
572:                        "badFactory");
573:                newProps.setProperty("java.naming.provider.url",
574:                        "jnp://badhost:12345");
575:                System.setProperties(newProps);
576:                try {
577:                    getLog().debug("Unserialize bean handle...");
578:                    ByteArrayInputStream in = new ByteArrayInputStream(bytes);
579:                    ObjectInputStream ois = new ObjectInputStream(in);
580:                    beanHandle = (Handle) ois.readObject();
581:                    bean = (StatefulSession) beanHandle.getEJBObject();
582:                    bean.method1("Hello");
583:                    getLog().debug("Called method1 on handle session bean");
584:                } finally {
585:                    System.setProperties(sysProps);
586:                }
587:            }
588:
589:            /** Test of accessing the home interface from the remote interface in an env
590:             * new InitialContext() will not work.
591:             * @throws Exception
592:             */
593:            public void testHomeFromRemoteNoDefaultJNDI() throws Exception {
594:                getLog().debug("+++ testHomeFromRemoteNoDefaultJNDI()");
595:
596:                // Override the JNDI variables in the System properties
597:                Properties sysProps = System.getProperties();
598:                Properties newProps = new Properties(sysProps);
599:                newProps.setProperty("java.naming.factory.initial",
600:                        "badFactory");
601:                newProps.setProperty("java.naming.provider.url",
602:                        "jnp://badhost:12345");
603:                System.setProperties(newProps);
604:
605:                // Do a lookup of the home and create a remote using a custom env
606:                Properties env = new Properties();
607:                env.setProperty("java.naming.factory.initial", super 
608:                        .getJndiInitFactory());
609:                env.setProperty("java.naming.provider.url", super .getJndiURL());
610:                try {
611:                    InitialContext ctx = new InitialContext(env);
612:                    Object ref = ctx.lookup("ejbcts/StatefulSessionBean");
613:                    StatefulSessionHome home = (StatefulSessionHome) PortableRemoteObject
614:                            .narrow(ref, StatefulSessionHome.class);
615:                    StatefulSession bean1 = home
616:                            .create("testHomeFromRemoteNoDefaultJNDI");
617:                    StatefulSessionHome home2 = (StatefulSessionHome) bean1
618:                            .getEJBHome();
619:                    StatefulSession bean2 = home2
620:                            .create("testHomeFromRemoteNoDefaultJNDI");
621:                    bean2.remove();
622:                } finally {
623:                    System.setProperties(sysProps);
624:                }
625:            }
626:
627:            /**
628:             * Method testProbeBeanContext
629:             *
630:             * EJB 1.1 [6.4.1] Page 51
631:             *
632:             */
633:            public void testProbeBeanContext() throws Exception {
634:                getLog().debug("+++ testProbeBeanContext");
635:                Context ctx = new InitialContext();
636:                StatefulSessionHome home = (StatefulSessionHome) ctx
637:                        .lookup("ejbcts/StatefulSessionBean");
638:                StatefulSession bean = home.create("testProbeBeanContext");
639:
640:                getLog().debug("Invoking bean...");
641:
642:                BeanContextInfo beanCtxInfo = bean.getBeanContextInfo();
643:
644:                assertTrue(beanCtxInfo != null);
645:
646:                getLog().debug(
647:                        "remote interface: " + beanCtxInfo.remoteInterface);
648:                getLog()
649:                        .debug("home interface:   " + beanCtxInfo.homeInterface);
650:
651:                getLog().debug("Testing rollback only setting...");
652:                assertTrue(beanCtxInfo.isRollbackOnly.booleanValue());
653:                getLog()
654:                        .debug(
655:                                "**************************************************************");
656:            }
657:
658:            /**
659:             * Method testLoopback
660:             * EJB 1.1 [6.5.6]
661:             * A client call to bean 'A' which calls bean 'B' and bean 'B'
662:             * in turn calls a method on bean 'A', there should be a
663:             * RemoteException should be thrown.
664:             */
665:            public void testLoopback() throws Exception {
666:                getLog().debug("+++ testLoopback");
667:
668:                // Create a new session object
669:                Context ctx = new InitialContext();
670:                StatefulSessionHome home = (StatefulSessionHome) ctx
671:                        .lookup("ejbcts/StatefulSessionBean");
672:                StatefulSession bean = home.create("testLoopback");
673:                getLog().debug("Calling loopbackTest( )....");
674:                try {
675:                    bean.loopbackTest();
676:                    fail("Was able to call loopbackTest()");
677:                } catch (Exception e) {
678:                    getLog().debug("The loopbackTest( ) failed as expected");
679:                }
680:            }
681:
682:            public void testUserTx() throws Exception {
683:                getLog().debug("+++ testUsrTx");
684:
685:                getLog().debug("Obtain home interface");
686:                // Create a new session object
687:                Context ctx = new InitialContext();
688:                Object ref = ctx.lookup("ejbcts/StatefulSessionBean");
689:                StatefulSessionHome home = (StatefulSessionHome) PortableRemoteObject
690:                        .narrow(ref, StatefulSessionHome.class);
691:                StatefulSession bean = home.create("testUserTx");
692:
693:                bean.setCounter(100);
694:                getLog().debug("Try to instantiate a UserTransaction");
695:                UserTransaction userTx = (UserTransaction) ctx
696:                        .lookup("UserTransaction");
697:                userTx.begin();
698:                bean.incCounter();
699:                bean.incCounter();
700:                userTx.commit();
701:                int counter = bean.getCounter();
702:                assertTrue("counter == 102", counter == 102);
703:
704:                bean.setCounter(100);
705:                userTx.begin();
706:                bean.incCounter();
707:                bean.incCounter();
708:                userTx.rollback();
709:                counter = bean.getCounter();
710:                assertTrue("counter == 100", counter == 100);
711:
712:                bean.remove();
713:            }
714:
715:            /** Run MAX_SIZE instances currently with a strictMaxSize setting of 5 to
716:             * see that at most 5 instances are active in the bean instance.
717:             * 
718:             * @throws Exception
719:             */
720:            public void testStrictPooling() throws Exception {
721:                log.debug("+++ testStrictPooling");
722:                CountDown done = new CountDown(MAX_SIZE);
723:                InitialContext ctx = new InitialContext();
724:                StrictlyPooledSessionHome home = (StrictlyPooledSessionHome) ctx
725:                        .lookup("ejbcts/StrictlyPooledStatefulBean");
726:                SessionInvoker[] threads = new SessionInvoker[MAX_SIZE];
727:                for (int n = 0; n < MAX_SIZE; n++) {
728:                    SessionInvoker t = new SessionInvoker(home, n, done,
729:                            getLog());
730:                    threads[n] = t;
731:                    t.start();
732:                }
733:                boolean ok = done.attempt(1500 * MAX_SIZE);
734:                super .assertTrue("Acquired done, remaining="
735:                        + done.currentCount(), ok);
736:
737:                for (int n = 0; n < MAX_SIZE; n++) {
738:                    SessionInvoker t = threads[n];
739:                    if (t.runEx != null) {
740:                        t.runEx.printStackTrace();
741:                        log.error("SessionInvoker.runEx != null", t.runEx);
742:                        fail("SessionInvoker.runEx != null");
743:                    }
744:                }
745:            }
746:
747:            public void testBadUserTx() throws Exception {
748:                getLog().debug("+++ testBadUsrTx");
749:
750:                getLog().debug("Obtain home interface");
751:                // Create a new session object
752:                Context ctx = new InitialContext();
753:                Object ref = ctx.lookup("ejbcts/BMTStatefulSessionBean");
754:                StatefulSessionHome home = (StatefulSessionHome) PortableRemoteObject
755:                        .narrow(ref, StatefulSessionHome.class);
756:                StatefulSession bean = home.create("testBadUserTx");
757:
758:                MBeanServerConnection server = getServer();
759:                Long before = (Long) server.getAttribute(
760:                        TransactionManagerServiceMBean.OBJECT_NAME,
761:                        "TransactionCount");
762:                try {
763:                    bean.testBadUserTx();
764:                    fail("Should throw an exception");
765:                } catch (Throwable expected) {
766:                    log.debug("Got exception", expected);
767:                }
768:                Long after = (Long) server.getAttribute(
769:                        TransactionManagerServiceMBean.OBJECT_NAME,
770:                        "TransactionCount");
771:                assertEquals("Transaction should no longer be active before="
772:                        + before + " after=" + after, before, after);
773:            }
774:
775:            public static Test suite() throws Exception {
776:                return getDeploySetup(StatefulSessionUnitTestCase.class,
777:                        "cts.jar");
778:            }
779:
780:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.