Source Code Cross Referenced for EJBTestModuleTest.java in  » Testing » mockrunner-0.4 » com » mockrunner » test » ejb » 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 » Testing » mockrunner 0.4 » com.mockrunner.test.ejb 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package com.mockrunner.test.ejb;
002:
003:        import java.rmi.RemoteException;
004:
005:        import javax.ejb.CreateException;
006:        import javax.ejb.EJBException;
007:        import javax.ejb.EntityBean;
008:        import javax.ejb.EntityContext;
009:        import javax.ejb.FinderException;
010:        import javax.ejb.MessageDrivenBean;
011:        import javax.ejb.MessageDrivenContext;
012:        import javax.ejb.SessionBean;
013:        import javax.ejb.SessionContext;
014:        import javax.jms.JMSException;
015:        import javax.jms.Message;
016:        import javax.jms.MessageListener;
017:        import javax.jms.QueueSender;
018:        import javax.jms.QueueSession;
019:        import javax.jms.Session;
020:        import javax.jms.TextMessage;
021:        import javax.jms.TopicPublisher;
022:        import javax.jms.TopicSession;
023:        import javax.naming.InitialContext;
024:        import javax.rmi.PortableRemoteObject;
025:
026:        import junit.framework.TestCase;
027:
028:        import org.mockejb.TransactionPolicy;
029:
030:        import com.mockrunner.ejb.EJBTestModule;
031:        import com.mockrunner.mock.ejb.EJBMockObjectFactory;
032:        import com.mockrunner.mock.ejb.MockUserTransaction;
033:        import com.mockrunner.mock.jms.JMSMockObjectFactory;
034:        import com.mockrunner.mock.jms.MockQueue;
035:        import com.mockrunner.mock.jms.MockQueueConnection;
036:        import com.mockrunner.mock.jms.MockQueueConnectionFactory;
037:        import com.mockrunner.mock.jms.MockQueueReceiver;
038:        import com.mockrunner.mock.jms.MockQueueSession;
039:        import com.mockrunner.mock.jms.MockTextMessage;
040:        import com.mockrunner.mock.jms.MockTopic;
041:        import com.mockrunner.mock.jms.MockTopicConnection;
042:        import com.mockrunner.mock.jms.MockTopicConnectionFactory;
043:        import com.mockrunner.mock.jms.MockTopicSession;
044:        import com.mockrunner.mock.jms.MockTopicSubscriber;
045:
046:        public class EJBTestModuleTest extends TestCase {
047:            private EJBMockObjectFactory ejbMockFactory;
048:            private EJBTestModule ejbModule;
049:            private JMSMockObjectFactory jmsMockFactory;
050:
051:            protected void setUp() throws Exception {
052:                super .setUp();
053:                ejbMockFactory = new EJBMockObjectFactory();
054:                ejbModule = new EJBTestModule(ejbMockFactory);
055:                jmsMockFactory = new JMSMockObjectFactory();
056:            }
057:
058:            protected void tearDown() throws Exception {
059:                super .tearDown();
060:                ejbMockFactory.resetMockContextFactory();
061:            }
062:
063:            public void testCreateSessionBean() throws Exception {
064:                ejbModule.deploySessionBean("com/MyLookupTest",
065:                        TestSessionBean.class);
066:                try {
067:                    ejbModule.createBean("com/MyLookupTestTest");
068:                    fail();
069:                } catch (RuntimeException exc) {
070:                    //should throw exception
071:                }
072:                Object bean = ejbModule.createBean("com/MyLookupTest");
073:                assertTrue(bean instanceof  TestSession);
074:                bean = ejbModule.createBean("com/MyLookupTest",
075:                        new Object[] { new Integer(1) });
076:                assertTrue(bean instanceof  TestSession);
077:                bean = ejbModule.createBean("com/MyLookupTest", new Object[] {
078:                        new Integer(1), Boolean.TRUE });
079:                assertTrue(bean instanceof  TestSession);
080:                bean = ejbModule.createBean("com/MyLookupTest",
081:                        "createWithPostfix", new Object[] { new Integer(1),
082:                                Boolean.TRUE });
083:                assertTrue(bean instanceof  TestSession);
084:                assertNull(ejbModule.createBean("com/MyLookupTest",
085:                        "createWithPostfiy", new Object[] { new Integer(1),
086:                                Boolean.TRUE }));
087:                try {
088:                    ejbModule.createBean("com/MyLookupTestTest", new Object[] {
089:                            Boolean.TRUE, new Integer(1) });
090:                    fail();
091:                } catch (RuntimeException exc) {
092:                    //should throw exception
093:                }
094:            }
095:
096:            public void testCreateEntityBean() throws Exception {
097:                ejbModule.setBusinessInterfaceSuffix("Bean");
098:                ejbModule.setImplementationSuffix("EJB");
099:                ejbModule.deployEntityBean("com/AnEntityBean",
100:                        TestEntityEJB.class);
101:                try {
102:                    ejbModule.createBean("com/AnEntity");
103:                    fail();
104:                } catch (RuntimeException exc) {
105:                    //should throw exception
106:                }
107:                Object bean = ejbModule.createEntityBean("com/AnEntityBean",
108:                        "myPk");
109:                assertTrue(bean instanceof  TestEntityBean);
110:                TestEntityHome home = (TestEntityHome) ejbModule
111:                        .lookup("com/AnEntityBean");
112:                assertSame(bean, home.findByPrimaryKey("myPk"));
113:                bean = ejbModule.createEntityBean("com/AnEntityBean",
114:                        "createWithName", new Object[] { "xyz" }, "anotherPk");
115:                assertSame(bean, home.findByPrimaryKey("anotherPk"));
116:                bean = ejbModule.createEntityBean("com/AnEntityBean",
117:                        new Object[] { new Short((short) 1) }, "thirdPk");
118:                assertSame(bean, home.findByPrimaryKey("thirdPk"));
119:                assertNull(ejbModule.createEntityBean("com/AnEntityBean",
120:                        new Object[] { "xyz" }, "thirdPk"));
121:                assertSame(bean, home.findByPrimaryKey("thirdPk"));
122:            }
123:
124:            public void testCreateWithNullParameters() throws Exception {
125:                ejbModule.deploySessionBean("com/MyLookupTest",
126:                        TestSessionBean.class);
127:                try {
128:                    ejbModule.createBean("com/MyLookupTest",
129:                            new Object[] { null });
130:                    fail();
131:                } catch (IllegalArgumentException exc) {
132:                    //should throw exception
133:                }
134:                Object bean = ejbModule.createBean("com/MyLookupTest",
135:                        "create", new Object[] { null },
136:                        new Class[] { Integer.class });
137:                assertTrue(bean instanceof  TestSession);
138:                bean = ejbModule.createBean("com/MyLookupTest", "create",
139:                        new Object[] { new Integer(1), null }, new Class[] {
140:                                Integer.TYPE, Boolean.class });
141:                assertTrue(bean instanceof  TestSession);
142:                bean = ejbModule.createBean("com/MyLookupTest", "create",
143:                        new Object[] { new Integer(1), null }, new Class[] {
144:                                Integer.class, Boolean.class });
145:                assertNull(bean);
146:                ejbModule.setBusinessInterfaceSuffix("Bean");
147:                ejbModule.setImplementationSuffix("EJB");
148:                ejbModule.deployEntityBean("com/AnEntityBean",
149:                        TestEntityEJB.class);
150:                try {
151:                    ejbModule.createEntityBean("com/AnEntityBean",
152:                            "createWithName", new Object[] { null }, "Pk");
153:                    fail();
154:                } catch (IllegalArgumentException exc) {
155:                    //should throw exception
156:                }
157:                bean = ejbModule.createEntityBean("com/AnEntityBean",
158:                        "createWithName", new Object[] { "xyz" },
159:                        new Class[] { String.class }, "Pk");
160:                assertTrue(bean instanceof  TestEntityBean);
161:            }
162:
163:            public void testFindByPrimaryKey() throws Exception {
164:                ejbModule.setBusinessInterfaceSuffix("Bean");
165:                ejbModule.setImplementationSuffix("EJB");
166:                ejbModule.deployEntityBean("com/AnEntityBean",
167:                        TestEntityEJB.class);
168:                Object bean1 = ejbModule.createEntityBean("com/AnEntityBean",
169:                        "myPk");
170:                Object bean2 = ejbModule.createEntityBean("com/AnEntityBean",
171:                        "createWithName", new Object[] { "xyz" }, "anotherPk");
172:                assertSame(bean1, ejbModule.findByPrimaryKey(
173:                        "com/AnEntityBean", "myPk"));
174:                assertSame(bean2, ejbModule.findByPrimaryKey(
175:                        "com/AnEntityBean", "anotherPk"));
176:                try {
177:                    ejbModule.findByPrimaryKey("com/AnEntity", "myPk");
178:                    fail();
179:                } catch (RuntimeException exc) {
180:                    //should throw exception
181:                }
182:                try {
183:                    ejbModule.findByPrimaryKey("com/AnEntity", "xyz");
184:                    fail();
185:                } catch (RuntimeException exc) {
186:                    //should throw exception
187:                }
188:            }
189:
190:            public void testDeploySessionBeanClass() throws Exception {
191:                try {
192:                    ejbModule
193:                            .deploySessionBean("test", EJBTestModuleTest.class);
194:                    fail();
195:                } catch (RuntimeException exc) {
196:                    //should throw exception
197:                }
198:                try {
199:                    ejbModule.setHomeInterfacePackage("com.mockrunner.test");
200:                    ejbModule.deploySessionBean("test", TestSessionBean.class);
201:                    fail();
202:                } catch (RuntimeException exc) {
203:                    //should throw exception
204:                }
205:                try {
206:                    ejbModule
207:                            .setHomeInterfacePackage("com.mockrunner.test.ejb");
208:                    ejbModule
209:                            .setBusinessInterfacePackage("com.mockrunner.test");
210:                    ejbModule.deploySessionBean("test", TestSessionBean.class);
211:                    fail();
212:                } catch (RuntimeException exc) {
213:                    //should throw exception
214:                }
215:                try {
216:                    ejbModule.setInterfacePackage("com.mockrunner.test.ejb");
217:                    ejbModule.setHomeInterfaceSuffix("Factory");
218:                    ejbModule.deploySessionBean("test", TestSessionBean.class);
219:                    fail();
220:                } catch (RuntimeException exc) {
221:                    //should throw exception
222:                }
223:                try {
224:                    ejbModule.setHomeInterfaceSuffix("Home");
225:                    ejbModule.setBusinessInterfaceSuffix("Business");
226:                    ejbModule.deploySessionBean("test", TestSessionBean.class);
227:                    fail();
228:                } catch (RuntimeException exc) {
229:                    //should throw exception
230:                }
231:                try {
232:                    ejbModule.setBusinessInterfaceSuffix("");
233:                    ejbModule.setImplementationSuffix("Impl");
234:                    ejbModule.deploySessionBean("test", TestSessionBean.class);
235:                    fail();
236:                } catch (RuntimeException exc) {
237:                    //should throw exception
238:                }
239:                ejbModule.setImplementationSuffix("Bean");
240:                ejbModule.deploySessionBean("test", TestSessionBean.class);
241:                InitialContext context = new InitialContext();
242:                Object home = context.lookup("test");
243:                assertNotNull(home);
244:                assertTrue(home instanceof  TestSessionHome);
245:            }
246:
247:            public void testDeploySessionBeanObject() throws Exception {
248:                try {
249:                    ejbModule.deploySessionBean("test", "abc");
250:                    fail();
251:                } catch (RuntimeException exc) {
252:                    //should throw exception
253:                }
254:                try {
255:                    ejbModule.setHomeInterfacePackage("com.mockrunner.test");
256:                    ejbModule.deploySessionBean("test", new TestSessionBean());
257:                    fail();
258:                } catch (RuntimeException exc) {
259:                    //should throw exception
260:                }
261:                try {
262:                    ejbModule.setInterfacePackage("com.mockrunner.test.ejb");
263:                    ejbModule.setHomeInterfaceSuffix("xyz");
264:                    ejbModule.deploySessionBean("test", new TestSessionBean());
265:                    fail();
266:                } catch (RuntimeException exc) {
267:                    //should throw exception
268:                }
269:                ejbModule.setHomeInterfaceSuffix("Home");
270:                ejbModule.setImplementationSuffix("Bean");
271:                ejbModule.deploySessionBean("test", new TestSessionBean());
272:                InitialContext context = new InitialContext();
273:                Object home = context.lookup("test");
274:                assertNotNull(home);
275:                assertTrue(home instanceof  TestSessionHome);
276:            }
277:
278:            public void testDeployEntityBean() throws Exception {
279:                try {
280:                    ejbModule.deployEntityBean("test", String.class);
281:                    fail();
282:                } catch (RuntimeException exc) {
283:                    //should throw exception
284:                }
285:                try {
286:                    ejbModule.setHomeInterfacePackage("com.mockrunner");
287:                    ejbModule.deployEntityBean("test", TestEntityEJB.class);
288:                    fail();
289:                } catch (RuntimeException exc) {
290:                    //should throw exception
291:                }
292:                try {
293:                    ejbModule.deployEntityBean("test", TestEntityEJB.class);
294:                    fail();
295:                } catch (RuntimeException exc) {
296:                    //should throw exception
297:                }
298:                try {
299:                    ejbModule.setBusinessInterfaceSuffix("");
300:                    ejbModule.setImplementationSuffix("EJB");
301:                    ejbModule.deployEntityBean("test", TestEntityEJB.class);
302:                    fail();
303:                } catch (RuntimeException exc) {
304:                    //should throw exception
305:                }
306:                ejbModule.setImplementationSuffix("EJB");
307:                ejbModule.setBusinessInterfaceSuffix("Bean");
308:                ejbModule.setHomeInterfacePackage("com.mockrunner.test.ejb");
309:                ejbModule.deployEntityBean("test", TestEntityEJB.class);
310:                InitialContext context = new InitialContext();
311:                Object home = context.lookup("test");
312:                assertNotNull(home);
313:                assertTrue(home instanceof  TestEntityHome);
314:            }
315:
316:            public void testDeployMessageBeanBoundToContext() throws Exception {
317:                MockQueueConnectionFactory queueFactory = jmsMockFactory
318:                        .getMockQueueConnectionFactory();
319:                MockQueue queue = jmsMockFactory.getDestinationManager()
320:                        .createQueue("queue");
321:                ejbModule.deployMessageBean("factoryJNDIQueueFactory",
322:                        "destinationJNDIQueue", queueFactory, queue,
323:                        new TestMessageBean());
324:                assertSame(queueFactory, ejbModule
325:                        .lookup("factoryJNDIQueueFactory"));
326:                assertSame(queue, ejbModule.lookup("destinationJNDIQueue"));
327:                MockTopicConnectionFactory topicFactory = jmsMockFactory
328:                        .getMockTopicConnectionFactory();
329:                MockTopic topic = jmsMockFactory.getDestinationManager()
330:                        .createTopic("topic");
331:                ejbModule.deployMessageBean("factoryJNDITopicFactory",
332:                        "destinationJNDITopic", topicFactory, topic,
333:                        new TestMessageBean());
334:                assertSame(topicFactory, ejbModule
335:                        .lookup("factoryJNDITopicFactory"));
336:                assertSame(topic, ejbModule.lookup("destinationJNDITopic"));
337:            }
338:
339:            public void testDeployMessageBeanQueueConnectionCreated()
340:                    throws Exception {
341:                MockQueueConnectionFactory queueFactory = jmsMockFactory
342:                        .getMockQueueConnectionFactory();
343:                MockQueue queue = jmsMockFactory.getDestinationManager()
344:                        .createQueue("queue");
345:                Object messageBean = new TestMessageBean();
346:                ejbModule.deployMessageBean("factoryJNDIQueueFactory",
347:                        "destinationJNDIQueue", queueFactory, queue,
348:                        messageBean);
349:                assertNotNull(queueFactory.getConnection(0));
350:                assertTrue(queueFactory.getConnection(0) instanceof  MockQueueConnection);
351:                MockQueueConnection queueConnection = queueFactory
352:                        .getLatestQueueConnection();
353:                assertNotNull(queueConnection.getQueueSession(0));
354:                assertTrue(queueConnection.getQueueSession(0) instanceof  MockQueueSession);
355:                MockQueueSession queueSession = (MockQueueSession) queueConnection
356:                        .getQueueSession(0);
357:                MockQueueReceiver queueReceiver = queueSession
358:                        .getQueueTransmissionManager()
359:                        .getQueueReceiver("queue");
360:                assertNotNull(queueReceiver);
361:                assertNotNull(queueReceiver.getMessageListener());
362:            }
363:
364:            public void testDeployMessageBeanTopicConnectionCreated()
365:                    throws Exception {
366:                MockTopicConnectionFactory topicFactory = jmsMockFactory
367:                        .getMockTopicConnectionFactory();
368:                MockTopic topic = jmsMockFactory.getDestinationManager()
369:                        .createTopic("topic");
370:                Object messageBean = new TestMessageBean();
371:                ejbModule.deployMessageBean("factoryJNDITopicFactory",
372:                        "destinationJNDITopic", topicFactory, topic,
373:                        messageBean);
374:                assertNotNull(topicFactory.getConnection(0));
375:                assertTrue(topicFactory.getConnection(0) instanceof  MockTopicConnection);
376:                MockTopicConnection topicConnection = topicFactory
377:                        .getLatestTopicConnection();
378:                assertNotNull(topicConnection.getTopicSession(0));
379:                assertTrue(topicConnection.getTopicSession(0) instanceof  MockTopicSession);
380:                MockTopicSession topicSession = (MockTopicSession) topicConnection
381:                        .getTopicSession(0);
382:                MockTopicSubscriber topicSubscriber = topicSession
383:                        .getTopicTransmissionManager().getTopicSubscriber(0);
384:                assertNotNull(topicSubscriber);
385:                assertNotNull(topicSubscriber.getMessageListener());
386:            }
387:
388:            public void testDeployMessageBeanSendMessage() throws Exception {
389:                MockQueueConnectionFactory queueFactory = jmsMockFactory
390:                        .getMockQueueConnectionFactory();
391:                MockQueue queue = jmsMockFactory.getDestinationManager()
392:                        .createQueue("queue");
393:                TestMessageBean messageBean = new TestMessageBean();
394:                ejbModule.deployMessageBean("factoryJNDIQueueFactory",
395:                        "destinationJNDIQueue", queueFactory, queue,
396:                        messageBean);
397:                QueueSession session = queueFactory.createQueueConnection()
398:                        .createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
399:                QueueSender sender = session.createSender(queue);
400:                MockTextMessage message = new MockTextMessage("message");
401:                sender.send(message);
402:                assertSame(message, messageBean.getMessage());
403:            }
404:
405:            public void testTransaction() throws Exception {
406:                ejbModule.deploySessionBean("mybean", new TestSessionBean(),
407:                        TransactionPolicy.REQUIRED);
408:                InitialContext context = new InitialContext();
409:                Object home = context.lookup("mybean");
410:                TestSessionHome testHome = (TestSessionHome) PortableRemoteObject
411:                        .narrow(home, TestSessionHome.class);
412:                TestSession testBean = (TestSession) testHome.create();
413:                testBean.test(false);
414:                ejbModule.verifyCommitted();
415:                ejbModule.verifyNotMarkedForRollback();
416:                ejbModule.verifyNotRolledBack();
417:                ejbModule.resetUserTransaction();
418:                testBean.test(true);
419:                ejbModule.verifyNotCommitted();
420:                ejbModule.verifyMarkedForRollback();
421:                ejbModule.verifyRolledBack();
422:                ejbModule.setBusinessInterfaceSuffix("Bean");
423:                ejbModule.setImplementationSuffix("EJB");
424:                ejbModule.deployEntityBean("myEntityBean", TestEntityEJB.class,
425:                        TransactionPolicy.REQUIRED);
426:                ejbModule.resetUserTransaction();
427:                home = context.lookup("myEntityBean");
428:                TestEntityHome testEntityHome = (TestEntityHome) PortableRemoteObject
429:                        .narrow(home, TestEntityHome.class);
430:                TestEntityBean testEntity = (TestEntityBean) testEntityHome
431:                        .create();
432:                testEntity.setName("aName");
433:                ejbModule.verifyCommitted();
434:                ejbModule.verifyNotMarkedForRollback();
435:                ejbModule.verifyNotRolledBack();
436:            }
437:
438:            public void testNoTransactionPolicy() throws Exception {
439:                ejbModule.deploySessionBean("mybean", new TestSessionBean(),
440:                        null);
441:                TestSession testBean = (TestSession) ejbModule
442:                        .createBean("mybean");
443:                testBean.test(false);
444:                MockUserTransaction transaction = ejbMockFactory
445:                        .getMockUserTransaction();
446:                assertFalse(transaction.wasBeginCalled());
447:                ejbModule.verifyNotCommitted();
448:                ejbModule.verifyNotMarkedForRollback();
449:                ejbModule.verifyNotRolledBack();
450:                ejbModule.resetUserTransaction();
451:                testBean.testBMT(false);
452:                assertTrue(transaction.wasBeginCalled());
453:                ejbModule.verifyCommitted();
454:                ejbModule.verifyNotMarkedForRollback();
455:                ejbModule.verifyNotRolledBack();
456:                ejbModule.resetUserTransaction();
457:                testBean.testBMT(true);
458:                assertTrue(transaction.wasBeginCalled());
459:                ejbModule.verifyNotCommitted();
460:                ejbModule.verifyNotMarkedForRollback();
461:                ejbModule.verifyRolledBack();
462:            }
463:
464:            public void testTransactionMessageBean() throws Exception {
465:                MockTopicConnectionFactory topicFactory = jmsMockFactory
466:                        .getMockTopicConnectionFactory();
467:                MockTopic topic = jmsMockFactory.getDestinationManager()
468:                        .createTopic("topic");
469:                Object messageBean = new TestMessageBean();
470:                ejbModule.deployMessageBean("factoryJNDITopicFactory",
471:                        "destinationJNDITopic", topicFactory, topic,
472:                        messageBean, TransactionPolicy.REQUIRED);
473:                TopicSession session = topicFactory.createTopicConnection()
474:                        .createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
475:                TopicPublisher publisher = session.createPublisher(topic);
476:                MockTextMessage message = new MockTextMessage("message");
477:                publisher.publish(message);
478:                ejbModule.verifyCommitted();
479:                ejbModule.verifyNotMarkedForRollback();
480:                ejbModule.verifyNotRolledBack();
481:                ejbModule.resetUserTransaction();
482:                message = new MockTextMessage("doRollback");
483:                publisher.publish(message);
484:                ejbModule.verifyNotCommitted();
485:                ejbModule.verifyMarkedForRollback();
486:                ejbModule.verifyRolledBack();
487:            }
488:
489:            public static class TestSessionBean implements  SessionBean {
490:                private SessionContext sessionContext;
491:
492:                public void test(boolean setRollbackOnly) {
493:                    if (setRollbackOnly)
494:                        sessionContext.setRollbackOnly();
495:                }
496:
497:                public void testBMT(boolean rollback) {
498:                    try {
499:                        sessionContext.getUserTransaction().begin();
500:                        if (rollback) {
501:                            sessionContext.getUserTransaction().rollback();
502:                        } else {
503:                            sessionContext.getUserTransaction().commit();
504:                        }
505:                    } catch (Exception exc) {
506:                        throw new RuntimeException(exc);
507:                    }
508:                }
509:
510:                public void ejbCreate() throws CreateException {
511:
512:                }
513:
514:                public void ejbCreate(Integer testInt) throws CreateException {
515:
516:                }
517:
518:                public void ejbCreate(int testInt, Boolean testBoolean)
519:                        throws CreateException {
520:
521:                }
522:
523:                public void ejbCreateWithPostfix(int testInt,
524:                        Boolean testBoolean) throws CreateException {
525:
526:                }
527:
528:                public void ejbActivate() throws EJBException, RemoteException {
529:
530:                }
531:
532:                public void ejbPassivate() throws EJBException, RemoteException {
533:
534:                }
535:
536:                public void ejbRemove() throws EJBException, RemoteException {
537:
538:                }
539:
540:                public void setSessionContext(SessionContext context)
541:                        throws EJBException, RemoteException {
542:                    sessionContext = context;
543:                }
544:            }
545:
546:            public static interface TestSession extends javax.ejb.EJBObject {
547:                public void test(boolean setRollbackOnly)
548:                        throws RemoteException;
549:
550:                public void testBMT(boolean rollback) throws RemoteException;
551:            }
552:
553:            public static interface TestSessionHome extends javax.ejb.EJBHome {
554:                public TestSession create() throws CreateException,
555:                        RemoteException;
556:
557:                public TestSession create(Integer testInt)
558:                        throws CreateException, RemoteException;
559:
560:                public TestSession create(int testInt, Boolean testBoolean)
561:                        throws CreateException, RemoteException;
562:
563:                public TestSession createWithPostfix(int testInt,
564:                        Boolean testBoolean) throws CreateException,
565:                        RemoteException;
566:            }
567:
568:            public static abstract class TestEntityEJB implements  EntityBean {
569:                public abstract String getName();
570:
571:                public abstract void setName(String name);
572:
573:                public String ejbCreate() throws CreateException {
574:                    return "testPk";
575:                }
576:
577:                public void ejbPostCreate() throws CreateException {
578:
579:                }
580:
581:                public String ejbCreate(Short param) throws CreateException {
582:                    return "testPk";
583:                }
584:
585:                public void ejbPostCreate(Short param) throws CreateException {
586:
587:                }
588:
589:                public String ejbCreateWithName(String name)
590:                        throws CreateException {
591:                    return name;
592:                }
593:
594:                public void ejbPostCreateWithName(String name)
595:                        throws CreateException {
596:
597:                }
598:
599:                public void ejbActivate() throws EJBException, RemoteException {
600:
601:                }
602:
603:                public void ejbPassivate() throws EJBException, RemoteException {
604:
605:                }
606:
607:                public void ejbRemove() throws EJBException, RemoteException {
608:
609:                }
610:
611:                public void setEntityContext(EntityContext context)
612:                        throws EJBException, RemoteException {
613:
614:                }
615:
616:                public void unsetEntityContext() throws EJBException,
617:                        RemoteException {
618:
619:                }
620:
621:                public void ejbLoad() throws EJBException, RemoteException {
622:
623:                }
624:
625:                public void ejbStore() throws EJBException, RemoteException {
626:
627:                }
628:            }
629:
630:            public static interface TestEntityBean extends javax.ejb.EJBObject {
631:                public String getName() throws RemoteException;
632:
633:                public void setName(String name) throws RemoteException;
634:            }
635:
636:            public static interface SuperEntityHome extends javax.ejb.EJBHome {
637:
638:            }
639:
640:            public static interface TestEntityHome extends SuperEntityHome {
641:                public TestEntityBean create() throws CreateException,
642:                        RemoteException;
643:
644:                public TestEntityBean create(Short param)
645:                        throws CreateException, RemoteException;
646:
647:                public TestEntityBean createWithName(String name)
648:                        throws CreateException, RemoteException;
649:
650:                public TestEntityBean findByPrimaryKey(String pk)
651:                        throws FinderException, RemoteException;
652:            }
653:
654:            public static class TestMessageBean implements  MessageDrivenBean,
655:                    MessageListener {
656:                private MessageDrivenContext messageContext;
657:                private Message message;
658:
659:                public void onMessage(Message message) {
660:                    try {
661:                        if ((message instanceof  TextMessage)
662:                                && (((TextMessage) message).getText()
663:                                        .equals("doRollback"))) {
664:                            messageContext.setRollbackOnly();
665:                        }
666:                    } catch (JMSException exc) {
667:                        throw new RuntimeException(exc.getMessage());
668:                    }
669:                    this .message = message;
670:                }
671:
672:                public Message getMessage() {
673:                    return message;
674:                }
675:
676:                public void ejbCreate() {
677:
678:                }
679:
680:                public void ejbRemove() {
681:
682:                }
683:
684:                public void setMessageDrivenContext(MessageDrivenContext context)
685:                        throws EJBException {
686:                    messageContext = context;
687:                }
688:            }
689:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.