Source Code Cross Referenced for MDBTest.java in  » Testing » MockEJB » org » mockejb » 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 » Testing » MockEJB » org.mockejb.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.mockejb.test;
002:
003:        import javax.jms.MessageListener;
004:        import javax.naming.*;
005:
006:        import junit.framework.TestCase;
007:
008:        import org.mockejb.*;
009:        import org.mockejb.interceptor.*;
010:        import org.mockejb.jndi.*;
011:
012:        /**
013:         * Demonstrates MDB testing with MockEJB. 
014:         * This class provides an example of the integration test where session bean
015:         * sends a message to MDB and MDB calls another session bean. 
016:         * We use the capability of MockEJB JMS implementation to deliver messages 
017:         * synchronously to the receivers, so we don't need to mess with threads. 
018:         * This class relies on MockEJB and it won't run in the container. 
019:         *  
020:         * @author Alexander Ananiev
021:         * @author Dimitar Gospodinov
022:         */
023:        public class MDBTest extends TestCase {
024:
025:            // JNDI context and MockContainer instance used by all tests in this class  
026:            private MockContainer mockContainer;
027:            private Context context;
028:
029:            public MDBTest(String name) {
030:                super (name);
031:            }
032:
033:            /**
034:             * Sets up our mock container, JNDI context and deploy the beans that we need.   
035:             */
036:            public void setUp() throws Exception {
037:
038:                // MockContextFactory becomes the primary JNDI provider            
039:                MockContextFactory.setAsInitial();
040:                context = new InitialContext();
041:
042:                mockContainer = new MockContainer(context);
043:
044:                // Create deployment descriptor of our sample session bean.
045:                // This session bean is used as a client for MDB.
046:                SessionBeanDescriptor sampleBeanDescriptor = new SessionBeanDescriptor(
047:                        SampleService.JNDI_NAME, SampleServiceHome.class,
048:                        SampleService.class, new SampleServiceBean());
049:
050:                mockContainer.deploy(sampleBeanDescriptor);
051:            }
052:
053:            /**
054:             * Deploys MDB and uses session bean to send a message to MDB.
055:             */
056:            public void testMessageBean() throws Exception {
057:
058:                /* Specify connection factory and destination that MDB will listen to.
059:                 * We also provide an instance of MDB implementaion class.
060:                 */
061:                MDBDescriptor sampleMDBDescriptor = new MDBDescriptor(
062:                        "SampleConnectionFactory", "SampleTopic",
063:                        new SampleMessageBean());
064:                // queue is the default, in this test we use topic
065:                sampleMDBDescriptor.setIsTopic(true);
066:                // This will create connection factory and destination, create MDB and set
067:                // it as the listener to the destination
068:                mockContainer.deploy(sampleMDBDescriptor);
069:
070:                // We can bind the same MDB to the queue as well
071:                // Note that the connection factory must be different for queues and topics!
072:                mockContainer.deploy(new MDBDescriptor(
073:                        "SampleQueueConnectionFactory", "SampleQueue",
074:                        new SampleMessageBean()));
075:
076:                /*
077:                 * If you want to use some other JMS provider, you need to create the connection factory
078:                 * and destination separately and bind them to JNDI directly. 
079:                 * Here we use MockEJB JMS as an example, but in reality you can use mockrunner,
080:                 * or any other JMS library (for this test though, the JMS provider must be synchronous). 
081:                 */
082:                context.rebind("AnotherSampleQueueConnectionFactory",
083:                        new org.mockejb.jms.QueueConnectionFactoryImpl());
084:                context.rebind("AnotherSampleQueue",
085:                        new org.mockejb.jms.MockQueue("AnotherSampleQueue"));
086:                MDBDescriptor foreignProviderMDBDescriptor = new MDBDescriptor(
087:                        "AnotherSampleQueueConnectionFactory",
088:                        "AnotherSampleQueue", new SampleMessageBean());
089:                //Then method tell MockEJB not to create queues/factories
090:                foreignProviderMDBDescriptor.setIsAlreadyBound(true);
091:                mockContainer.deploy(foreignProviderMDBDescriptor);
092:
093:                InvocationRecorder recorder = new InvocationRecorder();
094:                AspectSystem aspectSystem = AspectSystemFactory
095:                        .getAspectSystem();
096:                // Set our custom interceptor so it would handle all calls to Sample interface (w/o subclasses)
097:                aspectSystem.add(new ClassPointcut(SampleService.class, false),
098:                        recorder);
099:                // Intercept MDB too
100:                aspectSystem.add(
101:                        new ClassPointcut(MessageListener.class, false),
102:                        recorder);
103:
104:                // Obtain the session bean
105:                SampleServiceHome sampleServiceHome = (SampleServiceHome) context
106:                        .lookup(SampleService.JNDI_NAME);
107:                // create the bean
108:                SampleService sampleService = sampleServiceHome.create();
109:
110:                // send messages        
111:                sampleService.sendMessage("Test message");
112:
113:                // make sure that onMessage was called. 
114:                assertNotNull(recorder
115:                        .findByTargetMethod("SampleMessageBean.onMessage"));
116:
117:                // validate the call to the session bean and its parameters 
118:                InvocationContext echoStringInvocation = recorder
119:                        .findByTargetMethod("echoString");
120:                // ok, it was indeed called
121:                assertNotNull(echoStringInvocation);
122:                // and the content of the message was indeed passed in            
123:                assertEquals("Test message", echoStringInvocation
124:                        .getParamVals()[0]);
125:
126:            }
127:
128:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.