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


001:        package com.mockrunner.mock.ejb;
002:
003:        import javax.naming.Context;
004:        import javax.naming.NameNotFoundException;
005:        import javax.naming.NamingException;
006:        import javax.transaction.UserTransaction;
007:
008:        import org.mockejb.MockContainer;
009:
010:        import com.mockrunner.ejb.Configuration;
011:        import com.mockrunner.ejb.JNDIUtil;
012:
013:        /**
014:         * Used to create all types of EJB mock objects. 
015:         * Maintains the necessary dependencies between the mock objects.
016:         * If you use the mock objects returned by this factory in your tests 
017:         * you can be sure that they are all up to date.
018:         * This factory takes the <code>UserTransaction</code> from the JNDI context. 
019:         * If there's no transaction bound to the  context, the factory will create a
020:         * {@link com.mockrunner.mock.ejb.MockUserTransaction} and bind it to the context.
021:         * If the bound transaction is no
022:         * {@link com.mockrunner.mock.ejb.MockUserTransaction},
023:         * the method {@link #getMockUserTransaction} returns <code>null</code>.
024:         * Use {@link #getUserTransaction} instead in this case.
025:         * You can configure the JNDI name of the <code>UserTransaction</code> and
026:         * the JNDI <code>Context</code> with the class 
027:         * {@link com.mockrunner.ejb.Configuration}.
028:         */
029:        public class EJBMockObjectFactory {
030:            private Configuration configuration;
031:            private UserTransaction transaction;
032:            private MockContainer container;
033:            private Context context;
034:
035:            /**
036:             * Creates a new set of mock objects.
037:             */
038:            public EJBMockObjectFactory() {
039:                this (new Configuration());
040:            }
041:
042:            /**
043:             * Creates a new set of mock objects based on the specified configuration.
044:             */
045:            public EJBMockObjectFactory(Configuration configuration) {
046:                this .configuration = configuration;
047:                initializeContext();
048:                initializeEJBContainer();
049:                initializeUserTransaction();
050:            }
051:
052:            private void initializeContext() {
053:                context = JNDIUtil.getContext(configuration);
054:            }
055:
056:            private void initializeUserTransaction() {
057:                try {
058:                    try {
059:                        transaction = (UserTransaction) context
060:                                .lookup(configuration
061:                                        .getUserTransactionJNDIName());
062:                    } catch (NameNotFoundException nameExc) {
063:                        transaction = createMockUserTransaction();
064:                        JNDIUtil.bindUserTransaction(configuration, context,
065:                                transaction);
066:                    }
067:                } catch (Exception exc) {
068:                    transaction = createMockUserTransaction();
069:                }
070:                if (transaction instanceof  MockUserTransaction) {
071:                    ((MockUserTransaction) transaction).reset();
072:                }
073:            }
074:
075:            private void initializeEJBContainer() {
076:                container = new MockContainer(context);
077:            }
078:
079:            /**
080:             * Creates the {@link com.mockrunner.mock.ejb.MockUserTransaction} using <code>new</code>.
081:             * This method can be overridden to return a subclass of {@link com.mockrunner.mock.ejb.MockUserTransaction}.
082:             * @return the {@link com.mockrunner.mock.ejb.MockUserTransaction}
083:             */
084:            public MockUserTransaction createMockUserTransaction() {
085:                return new MockUserTransaction();
086:            }
087:
088:            /**
089:             * Calls <code>MockContextFactory.setAsInitial()</code>, if 
090:             * <code>MockContextFactory</code> is not already the current
091:             * context factory.
092:             */
093:            public void initMockContextFactory() throws NamingException {
094:                JNDIUtil.initMockContextFactory();
095:            }
096:
097:            /**
098:             * Calls <code>MockContextFactory.revertSetAsInitial()</code>, if 
099:             * <code>MockContextFactory</code> is the current context factory.
100:             */
101:            public void resetMockContextFactory() {
102:                JNDIUtil.resetMockContextFactory();
103:            }
104:
105:            /**
106:             * Returns the {@link com.mockrunner.mock.ejb.MockUserTransaction}.
107:             * If the bound transaction is no {@link com.mockrunner.mock.ejb.MockUserTransaction},
108:             * this method returns <code>null</code>.
109:             * @return the {@link com.mockrunner.mock.ejb.MockUserTransaction}
110:             */
111:            public MockUserTransaction getMockUserTransaction() {
112:                if (!(transaction instanceof  MockUserTransaction))
113:                    return null;
114:                return (MockUserTransaction) transaction;
115:            }
116:
117:            /**
118:             * Returns the <code>UserTransaction</code>.
119:             * @return the <code>UserTransaction</code>
120:             */
121:            public UserTransaction getUserTransaction() {
122:                return transaction;
123:            }
124:
125:            /**
126:             * Returns the MockEJB <code>MockContainer</code>.
127:             * @return the <code>MockContainer</code>
128:             */
129:            public MockContainer getMockContainer() {
130:                return container;
131:            }
132:
133:            /**
134:             * Returns the JNDI context that is used by this factory. If you do not set
135:             * a <code>Context</code> using {@link com.mockrunner.ejb.Configuration#setContext}}, 
136:             * the JNDI implementation of MockEJB is used.
137:             * @return the JNDI context
138:             */
139:            public Context getContext() {
140:                return context;
141:            }
142:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.