Source Code Cross Referenced for TestInstanceCallbacks.java in  » Database-ORM » openjpa » org » apache » openjpa » persistence » kernel » 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 » Database ORM » openjpa » org.apache.openjpa.persistence.kernel 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * TestInstanceCallbacks.java
003:         *
004:         * Created on October 12, 2006, 1:19 PM
005:         *
006:         * To change this template, choose Tools | Template Manager
007:         * and open the template in the editor.
008:         */
009:
010:        /*
011:         * Licensed to the Apache Software Foundation (ASF) under one
012:         * or more contributor license agreements.  See the NOTICE file
013:         * distributed with this work for additional information
014:         * regarding copyright ownership.  The ASF licenses this file
015:         * to you under the Apache License, Version 2.0 (the
016:         * "License"); you may not use this file except in compliance
017:         * with the License.  You may obtain a copy of the License at
018:         *
019:         * http://www.apache.org/licenses/LICENSE-2.0
020:         *
021:         * Unless required by applicable law or agreed to in writing,
022:         * software distributed under the License is distributed on an
023:         * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
024:         * KIND, either express or implied.  See the License for the
025:         * specific language governing permissions and limitations
026:         * under the License.    
027:         */
028:        package org.apache.openjpa.persistence.kernel;
029:
030:        import org.apache.openjpa.persistence.kernel.common.apps.InstanceCallbacksTest;
031:        import org.apache.openjpa.persistence.kernel.common.apps.RuntimeTest1;
032:        import junit.framework.AssertionFailedError;
033:
034:        import org.apache.openjpa.persistence.OpenJPAEntityManager;
035:
036:        public class TestInstanceCallbacks extends BaseKernelTest {
037:
038:            private static final int COMMIT = 0;
039:            private static final int FLUSH = 1;
040:            private static final int PRESTORE = 2;
041:
042:            private OpenJPAEntityManager _pm = null;
043:            private InstanceCallbacksTest _callbacks = null;
044:
045:            public TestInstanceCallbacks(String name) {
046:                super (name);
047:            }
048:
049:            /**
050:             * Creates a new instance of TestInstanceCallbacks
051:             */
052:            public TestInstanceCallbacks() {
053:            }
054:
055:            public void setUp() throws Exception {
056:                deleteAll(InstanceCallbacksTest.class);
057:                deleteAll(RuntimeTest1.class);
058:                _pm = getPM(true, true);
059:                startTx(_pm);
060:                _callbacks = new InstanceCallbacksTest();
061:                _callbacks.setStringField("foo");
062:                _pm.persist(_callbacks);
063:                Object id = _pm.getObjectId(_callbacks);
064:                endTx(_pm);
065:                endEm(_pm);
066:
067:                // re-find with different PM
068:                _pm = getPM();
069:                _callbacks = (InstanceCallbacksTest) _pm.find(
070:                        InstanceCallbacksTest.class, id);
071:            }
072:
073:            public void tearDown() throws Exception {
074:                rollbackTx(_pm);
075:                endEm(_pm);
076:                super .tearDown();
077:            }
078:
079:            public void testPostLoad() {
080:                _callbacks.getStringField();
081:                assertTrue(_callbacks.postLoadCalled);
082:            }
083:
084:            public void testPreStore() {
085:                preStoreTest(COMMIT);
086:            }
087:
088:            public void testPreStoreWithFlush() {
089:                preStoreTest(FLUSH);
090:            }
091:
092:            public void testPreStoreWithPreStore() {
093:                preStoreTest(PRESTORE);
094:            }
095:
096:            private void preStoreTest(int action) {
097:                assertNoCallbacksInvoked(_callbacks);
098:                //        _pm.begin();
099:                startTx(_pm);
100:
101:                _callbacks.setStringField("bar");
102:                Object oid = _pm.getObjectId(_callbacks);
103:                if (action == COMMIT) {
104:                    _pm.flush();
105:                    endTx(_pm);
106:                } else if (action == FLUSH)
107:                    _pm.flush();
108:                else if (action == PRESTORE)
109:                    _pm.preFlush();
110:                assertTrue("prestore wasnt called", _callbacks.preStoreCalled);
111:                if (action != COMMIT) {
112:                    //            _pm.commit();
113:                    if (action != FLUSH)
114:                        _pm.flush();
115:                    endTx(_pm);
116:                }
117:
118:                OpenJPAEntityManager pm = getPM();
119:                InstanceCallbacksTest callbacks = (InstanceCallbacksTest) pm
120:                        .find(InstanceCallbacksTest.class, oid);
121:                assertNoCallbacksInvoked(callbacks);
122:                assertEquals("getonetoone strng is not jdoprestore",
123:                        "jdoPreStore", callbacks.getOneOne().getStringField());
124:                endEm(pm);
125:            }
126:
127:            public void testPreDelete() {
128:                assertNoCallbacksInvoked(_callbacks);
129:                startTx(_pm);
130:                _pm.remove(_callbacks);
131:                assertTrue(_callbacks.preDeleteCalled);
132:                endTx(_pm);
133:            }
134:
135:            public void testPreDeleteRecursion() {
136:                assertNoCallbacksInvoked(_callbacks);
137:                startTx(_pm);
138:                _callbacks.preDeleteCycle = 0;
139:                _pm.remove(_callbacks);
140:                assertEquals(1, _callbacks.preDeleteCycle);
141:                endTx(_pm);
142:            }
143:
144:            public void testSetRelatedReferenceInPreStore() {
145:                assertNull(_callbacks.getRelId());
146:                InstanceCallbacksTest callbacks2 = new InstanceCallbacksTest();
147:                callbacks2.setRelId(_pm.getObjectId(_callbacks));
148:                startTx(_pm);
149:                _pm.persist(callbacks2);
150:                _pm.flush();
151:                endTx(_pm);
152:                assertEquals(8888, _callbacks.getIntField());
153:                try {
154:                    assertEquals(callbacks2, _callbacks.getRel());
155:                } catch (AssertionFailedError afe) {
156:                    bug(1162, afe, "Setting a related object reference in "
157:                            + "preStore fails");
158:                }
159:            }
160:
161:            public void testFlushCausesFlush() {
162:                //### JDO2MIG : this is failing because we're consuming exceptions
163:                // throws from callbacks; need to decide what to do with them
164:                causeFlushTest(FLUSH);
165:            }
166:
167:            public void testPreStoreCausesFlush() {
168:                //### JDO2MIG : this is failing because we're consuming exceptions
169:                // throws from callbacks; need to decide what to do with them
170:                causeFlushTest(PRESTORE);
171:            }
172:
173:            private void causeFlushTest(int action) {
174:                startTx(_pm);
175:                _callbacks.setStringField("sss");
176:                _callbacks.flushInPreStore = true;
177:                try {
178:                    if (action == FLUSH)
179:                        _pm.flush();
180:                    else
181:                        _pm.preFlush();
182:
183:                    bug(1139,
184:                            "Recursive flush allowed because exception swallowed");
185:                } catch (Exception je) {
186:                }
187:                rollbackTx(_pm);
188:            }
189:
190:            private void assertNoCallbacksInvoked(InstanceCallbacksTest pc) {
191:                OpenJPAEntityManager pm = (OpenJPAEntityManager) currentEntityManager();
192:
193:                assertFalse(
194:                        "Expected preDelete to not have been called for object ID "
195:                                + pm.getObjectId(pc), pc.preDeleteCalled);
196:                assertFalse(
197:                        "Expected preClear to not have been called for object ID "
198:                                + pm.getObjectId(pc), pc.preClearCalled);
199:                assertFalse(
200:                        "Expected preStore to not have been called for object ID "
201:                                + pm.getObjectId(pc), pc.preStoreCalled);
202:            }
203:
204:            /* 
205:            // no JPA equivalent
206:
207:            public void testDetachAttach()
208:            throws Exception {
209:                OpenJPAEntityManager pm = getPM();
210:                DetachAttachEvent pc = (DetachAttachEvent) pm.find
211:                        (DetachAttachEvent.class,createDetachableId(4));
212:                DetachAttachEvent.EVENTS.clear();
213:                pc = (DetachAttachEvent) pm.detach(pc);
214:                assertDetachEvents(new String[]{ "PRED4", "POSTD4" });
215:                endEm(pm,());
216:                
217:                assertTrue(pm.isDetached(pc));
218:                
219:                pm = getPM();
220:                startTx(pm,());
221:                //FIXME jthomas
222:                
223:            //        pm.addInstanceLifecycleListener(new CreateLifecycleListener() {
224:            //            public void postCreate(InstanceLifecycleEvent ev) {
225:            //                fail("No post create necessary");
226:            //            }
227:            //        }, null);
228:                pm.persist(pc);
229:                assertDetachEvents(new String[]{ "PREA4", "POSTA4" });
230:                endTx(pm,());
231:                endEm(pm,());
232:            }
233:            
234:            public void testDetachAttachRelations() {
235:                OpenJPAEntityManager pm = getPM();
236:                startTx(pm,());
237:                DetachAttachEvent pc = (DetachAttachEvent) pm.find
238:                        (DetachAttachEvent.class,createDetachableId(2));
239:                pc.setOneOne((DetachAttachEvent) pm.find
240:                        (DetachAttachEvent.class,createDetachableId(4)));
241:                endTx(pm,());
242:                DetachAttachEvent.EVENTS.clear();
243:                pc = (DetachAttachEvent) pm.detach(pc);
244:                endEm(pm,());
245:                assertDetachEvents(
246:                        new String[]{ "PRED2", "PRED4", "POSTD2", "POSTD4" });
247:                
248:                pm = getPM();
249:                startTx(pm,());
250:                pm.persist(pc);
251:                assertDetachEvents(
252:                        new String[]{ "PREA2", "PREA4", "POSTA2", "POSTA4" });
253:                rollbackTx(pm,());
254:                endEm(pm,());
255:            }
256:            
257:            private void assertDetachEvents(String[] expected) {
258:                Collection events = DetachAttachEvent.EVENTS;
259:                if (expected.length != events.size()) {
260:                    StringBuffer buf = new StringBuffer();
261:                    for (int i = 0; i < expected.length; i++)
262:                        buf.append(expected[i]).append(",");
263:                    buf.append("!=");
264:                    for (Iterator it = events.iterator(); it.hasNext();)
265:                        buf.append(it.next()).append(",");
266:                    fail("mismatch event count:" + buf);
267:                }
268:                String event;
269:                for (int i = 0; i < expected.length; i++) {
270:                    if (!events.remove(expected[i]))
271:                        fail("Event not fired:" + expected[i]);
272:                    if (events.contains(expected[i]))
273:                        fail("Event fired twice:" + expected[i]);
274:                }
275:                if (!events.isEmpty())
276:                    fail("Excess events fired:" + events);
277:                DetachAttachEvent.EVENTS.clear();
278:            }
279:            
280:            private Object createDetachableId(int field) {
281:                OpenJPAEntityManager pm = getPM();
282:                startTx(pm,());
283:                DetachAttachEvent pc = new DetachAttachEvent();
284:                pc.setIntField(field);
285:                pm.persist(pc);
286:                endTx(pm,());
287:                endEm(pm,());
288:            //        return pm.getObjectId(pc);
289:                return pc.getId();
290:            }
291:             */
292:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.