Source Code Cross Referenced for PersistenceContextStatefulBean.java in  » J2EE » openejb3 » org » apache » openejb » test » stateful » 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 » J2EE » openejb3 » org.apache.openejb.test.stateful 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         *
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         *
011:         *  Unless required by applicable law or agreed to in writing, software
012:         *  distributed under the License is distributed on an "AS IS" BASIS,
013:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         *  See the License for the specific language governing permissions and
015:         *  limitations under the License.
016:         */package org.apache.openejb.test.stateful;
017:
018:        import junit.framework.Assert;
019:        import junit.framework.AssertionFailedError;
020:        import org.apache.openejb.test.TestFailureException;
021:
022:        import javax.ejb.SessionContext;
023:        import javax.ejb.Remove;
024:        import javax.naming.InitialContext;
025:        import javax.persistence.EntityManager;
026:        import javax.transaction.UserTransaction;
027:
028:        /**
029:         * @version $Rev: 607077 $ $Date: 2007-12-27 06:55:23 -0800 $
030:         */
031:        public class PersistenceContextStatefulBean {
032:
033:            private EntityManager extendedEntityManager;
034:
035:            // Used for testing propigation
036:            private static EntityManager inheritedDelegate;
037:            private SessionContext ejbContext;
038:
039:            public void setSessionContext(SessionContext ctx) {
040:                ejbContext = ctx;
041:            }
042:
043:            @Remove
044:            public void remove() {
045:            }
046:
047:            public String remove(String arg) {
048:                return arg;
049:            }
050:
051:            public void testPersistenceContext() throws TestFailureException {
052:                try {
053:                    try {
054:                        InitialContext ctx = new InitialContext();
055:                        Assert.assertNotNull("The InitialContext is null", ctx);
056:                        EntityManager em = (EntityManager) ctx
057:                                .lookup("java:comp/env/persistence/TestContext");
058:                        Assert.assertNotNull("The EntityManager is null", em);
059:
060:                        // call a do nothing method to assure entity manager actually exists
061:                        em.getFlushMode();
062:                    } catch (Exception e) {
063:                        Assert.fail("Received Exception " + e.getClass()
064:                                + " : " + e.getMessage());
065:                    }
066:                } catch (AssertionFailedError afe) {
067:                    throw new TestFailureException(afe);
068:                }
069:            }
070:
071:            public void testExtendedPersistenceContext()
072:                    throws TestFailureException {
073:                try {
074:                    try {
075:                        InitialContext ctx = new InitialContext();
076:                        Assert.assertNotNull("The InitialContext is null", ctx);
077:                        EntityManager em = (EntityManager) ctx
078:                                .lookup("java:comp/env/persistence/ExtendedTestContext");
079:                        Assert.assertNotNull("The EntityManager is null", em);
080:
081:                        // call a do nothing method to assure entity manager actually exists
082:                        em.getFlushMode();
083:
084:                        if (extendedEntityManager != null) {
085:                            Assert
086:                                    .assertSame(
087:                                            "Extended entity manager should be the same instance that was found last time",
088:                                            extendedEntityManager, em);
089:                            Assert
090:                                    .assertSame(
091:                                            "Extended entity manager delegate should be the same instance that was found last time",
092:                                            extendedEntityManager.getDelegate(),
093:                                            em.getDelegate());
094:                        }
095:                        extendedEntityManager = em;
096:
097:                        UserTransaction userTransaction = ejbContext
098:                                .getUserTransaction();
099:                        userTransaction.begin();
100:                        try {
101:                            em.getFlushMode();
102:                        } finally {
103:                            userTransaction.commit();
104:                        }
105:                    } catch (Exception e) {
106:                        e.printStackTrace();
107:                        Assert.fail("Received Exception " + e.getClass()
108:                                + " : " + e.getMessage());
109:                    }
110:                } catch (AssertionFailedError afe) {
111:                    throw new TestFailureException(afe);
112:                }
113:            }
114:
115:            public void testPropagatedPersistenceContext()
116:                    throws TestFailureException {
117:                try {
118:                    try {
119:                        InitialContext ctx = new InitialContext();
120:                        Assert.assertNotNull("The InitialContext is null", ctx);
121:                        EntityManager em = (EntityManager) ctx
122:                                .lookup("java:comp/env/persistence/ExtendedTestContext");
123:                        Assert.assertNotNull("The EntityManager is null", em);
124:
125:                        // call a do nothing method to assure entity manager actually exists
126:                        em.getFlushMode();
127:
128:                        // get the raw entity manager so we can test it below
129:                        inheritedDelegate = (EntityManager) em.getDelegate();
130:
131:                        // The extended entity manager is not propigated to a non-extended entity manager unless there is a transaction
132:                        EntityManager nonExtendedEm = (EntityManager) ctx
133:                                .lookup("java:comp/env/persistence/TestContext");
134:                        nonExtendedEm.getFlushMode();
135:                        EntityManager nonExtendedDelegate = ((EntityManager) nonExtendedEm
136:                                .getDelegate());
137:                        Assert.assertTrue(
138:                                "non-extended entity manager should be open",
139:                                nonExtendedDelegate.isOpen());
140:                        Assert
141:                                .assertNotSame(
142:                                        "Extended non-extended entity manager shound not be the same instance as extendend entity manager when accessed out side of a transactions",
143:                                        inheritedDelegate, nonExtendedDelegate);
144:
145:                        // When the non-extended entity manager is accessed within a transaction is should see the stateful extended context.
146:                        //
147:                        // Note: this code also tests EBJ 3.0 Persistence spec 5.9.1 "UserTransaction is begun within the method, the
148:                        // container associates the persistence context with the JTA transaction and calls EntityManager.joinTransaction."
149:                        // If our the extended entity manager were not associted with the transaction, the non-extended entity manager would
150:                        // not see it.
151:                        UserTransaction userTransaction = ejbContext
152:                                .getUserTransaction();
153:                        userTransaction.begin();
154:                        try {
155:                            Assert
156:                                    .assertSame(
157:                                            "Extended non-extended entity manager to be same instance as extendend entity manager",
158:                                            inheritedDelegate, nonExtendedEm
159:                                                    .getDelegate());
160:                        } finally {
161:                            userTransaction.commit();
162:                        }
163:
164:                        // When a stateful bean with an extended entity manager creates another stateful bean, the new bean will
165:                        // inherit the extended entity manager (assuming it contains an extended entity manager for the same persistence
166:                        // unit).
167:                        PersistenceContextStatefulHome home = (PersistenceContextStatefulHome) ejbContext
168:                                .getEJBHome();
169:                        PersistenceContextStatefulObject object = home.create();
170:
171:                        // test the new stateful bean recieved the context
172:                        object.testPropgation();
173:
174:                        // remove the bean
175:                        object.remove();
176:                    } catch (Exception e) {
177:                        Assert.fail("Received Exception " + e.getClass()
178:                                + " : " + e.getMessage());
179:                    }
180:                } catch (AssertionFailedError afe) {
181:                    throw new TestFailureException(afe);
182:                }
183:            }
184:
185:            public void testPropgation() throws TestFailureException {
186:                if (inheritedDelegate == null)
187:                    return;
188:                try {
189:                    try {
190:                        InitialContext ctx = new InitialContext();
191:                        Assert.assertNotNull("The InitialContext is null", ctx);
192:                        EntityManager em = (EntityManager) ctx
193:                                .lookup("java:comp/env/persistence/ExtendedTestContext");
194:                        Assert.assertNotNull("The EntityManager is null", em);
195:
196:                        // call a do nothing method to assure entity manager actually exists
197:                        em.getFlushMode();
198:
199:                        EntityManager delegate = (EntityManager) em
200:                                .getDelegate();
201:                        Assert
202:                                .assertSame(
203:                                        "Extended entity manager delegate should be the same instance that was found last time",
204:                                        inheritedDelegate, delegate);
205:                    } catch (Exception e) {
206:                        e.printStackTrace();
207:                        Assert.fail("Received Exception " + e.getClass()
208:                                + " : " + e.getMessage());
209:                    }
210:                } catch (AssertionFailedError afe) {
211:                    throw new TestFailureException(afe);
212:                }
213:            }
214:
215:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.