Source Code Cross Referenced for PBRollbackTest.java in  » Database-ORM » db-ojb » org » apache » ojb » broker » 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 » db ojb » org.apache.ojb.broker 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        package org.apache.ojb.broker;
002:
003:        import org.apache.ojb.broker.query.Criteria;
004:        import org.apache.ojb.broker.query.Query;
005:        import org.apache.ojb.broker.query.QueryByCriteria;
006:        import org.apache.ojb.broker.util.collections.ManageableVector;
007:        import org.apache.ojb.broker.metadata.ObjectReferenceDescriptor;
008:        import org.apache.ojb.junit.PBTestCase;
009:
010:        import java.util.ArrayList;
011:        import java.util.Collection;
012:        import java.util.Iterator;
013:
014:        /**
015:         * Tests rollback and (simple) commit behaviour.
016:         *
017:         * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
018:         */
019:        public class PBRollbackTest extends PBTestCase {
020:            public PBRollbackTest(String s) {
021:                super (s);
022:            }
023:
024:            public void testEmptyTxDemarcation_1() {
025:                try {
026:                    broker.beginTransaction();
027:
028:                    broker.commitTransaction();
029:                } catch (Exception e) {
030:                    e.printStackTrace();
031:                    fail("'Empty' transaction demarcation sequence fails");
032:                }
033:            }
034:
035:            public void testEmptyTxDemarcation_2() {
036:                try {
037:                    broker.beginTransaction();
038:
039:                    broker.abortTransaction();
040:                } catch (Exception e) {
041:                    e.printStackTrace();
042:                    fail("'Empty' transaction demarcation sequence fails");
043:                }
044:            }
045:
046:            public void testUserCommitClearCache() throws Exception {
047:                Collection projects = getNewProjects(10);
048:                storeObjects(broker, projects);
049:                Criteria c = new Criteria();
050:                Query q = new QueryByCriteria(Project.class, c);
051:                int beforeCommit = broker.getCount(q);
052:
053:                broker.beginTransaction();
054:                broker.clearCache();
055:                storeObjects(broker, getNewProjects(10));
056:                //while transaction we should see all stored objects
057:                int whileTransaction = broker.getCount(q);
058:                ManageableCollection result = broker.getCollectionByQuery(
059:                        ManageableVector.class, q);
060:                int whileTransactionMC = 0;
061:                Iterator it = result.ojbIterator();
062:                while (it.hasNext()) {
063:                    it.next();
064:                    ++whileTransactionMC;
065:                }
066:                broker.commitTransaction();
067:                //explicit clear cache
068:                broker.clearCache();
069:
070:                c = new Criteria();
071:                q = new QueryByCriteria(Project.class, c);
072:                int afterCommit = broker.getCount(q);
073:
074:                assertEquals(beforeCommit + 10, afterCommit);
075:                assertEquals(beforeCommit + 10, whileTransaction);
076:                assertEquals(beforeCommit + 10, whileTransactionMC);
077:            }
078:
079:            public void testUserCommit() throws Exception {
080:                Collection projects = getNewProjects(10);
081:                storeObjects(broker, projects);
082:                Criteria c = new Criteria();
083:                Query q = new QueryByCriteria(Project.class, c);
084:                int beforeCommit = broker.getCount(q);
085:
086:                broker.beginTransaction();
087:                storeObjects(broker, getNewProjects(10));
088:                int whileTransaction = broker.getCount(q);
089:                ManageableCollection result = broker.getCollectionByQuery(
090:                        ManageableVector.class, q);
091:                int whileTransactionMC = 0;
092:                Iterator it = result.ojbIterator();
093:                while (it.hasNext()) {
094:                    it.next();
095:                    ++whileTransactionMC;
096:                }
097:                broker.commitTransaction();
098:
099:                c = new Criteria();
100:                q = new QueryByCriteria(Project.class, c);
101:                broker.beginTransaction();
102:                int afterCommit = broker.getCount(q);
103:                broker.commitTransaction();
104:
105:                assertEquals(beforeCommit + 10, afterCommit);
106:                assertEquals(beforeCommit + 10, whileTransaction);
107:                assertEquals(beforeCommit + 10, whileTransactionMC);
108:            }
109:
110:            public void testUserRollbackClearCache() throws Exception {
111:                Collection projects = getNewProjects(10);
112:                storeObjects(broker, projects);
113:                Criteria c = new Criteria();
114:                Query q = new QueryByCriteria(Project.class, c);
115:                broker.beginTransaction();
116:                int beforeRollback = broker.getCount(q);
117:                broker.commitTransaction();
118:
119:                broker.beginTransaction();
120:                storeObjects(broker, getNewProjects(10));
121:                broker.clearCache();
122:
123:                int whileTransaction = broker.getCount(q);
124:                ManageableCollection result = broker.getCollectionByQuery(
125:                        ManageableVector.class, q);
126:                int whileTransactionMC = 0;
127:                Iterator it = result.ojbIterator();
128:                while (it.hasNext()) {
129:                    it.next();
130:                    ++whileTransactionMC;
131:                }
132:
133:                broker.abortTransaction();
134:                //explicit clear cache
135:                broker.clearCache();
136:
137:                c = new Criteria();
138:                q = new QueryByCriteria(Project.class, c);
139:                int afterRollback = broker.getCount(q);
140:
141:                assertEquals(beforeRollback, afterRollback);
142:                assertEquals(beforeRollback + 10, whileTransaction);
143:                assertEquals(beforeRollback + 10, whileTransactionMC);
144:            }
145:
146:            public void testUserRollback() throws Exception {
147:                Collection projects = getNewProjects(10);
148:                storeObjects(broker, projects);
149:                Criteria c = new Criteria();
150:                Query q = new QueryByCriteria(Project.class, c);
151:                broker.beginTransaction();
152:                int beforeRollback = broker.getCount(q);
153:                broker.commitTransaction();
154:
155:                broker.beginTransaction();
156:                storeObjects(broker, getNewProjects(10));
157:
158:                int whileTransaction = broker.getCount(q);
159:                ManageableCollection result = broker.getCollectionByQuery(
160:                        ManageableVector.class, q);
161:                int whileTransactionMC = 0;
162:                Iterator it = result.ojbIterator();
163:                while (it.hasNext()) {
164:                    it.next();
165:                    ++whileTransactionMC;
166:                }
167:
168:                broker.abortTransaction();
169:
170:                c = new Criteria();
171:                q = new QueryByCriteria(Project.class, c);
172:                int afterRollback = broker.getCount(q);
173:
174:                assertEquals(beforeRollback, afterRollback);
175:                assertEquals(beforeRollback + 10, whileTransaction);
176:                assertEquals(beforeRollback + 10, whileTransactionMC);
177:            }
178:
179:            public void testRollbackCausedByNotExistingObject()
180:                    throws Exception {
181:                Collection projects = getNewProjects(10);
182:                Query q;
183:                int beforeRollback;
184:                try {
185:                    broker.beginTransaction();
186:                    storeObjects(broker, projects);
187:                    broker.commitTransaction();
188:
189:                    Criteria c = new Criteria();
190:                    q = new QueryByCriteria(Project.class, c);
191:                    beforeRollback = broker.getCount(q);
192:                } finally {
193:                    if (broker != null && !broker.isClosed())
194:                        broker.close();
195:                }
196:
197:                broker = PersistenceBrokerFactory.defaultPersistenceBroker();
198:                try {
199:                    broker.beginTransaction();
200:                    storeObjects(broker, getNewProjects(10));
201:                    //should fail
202:                    broker.store(new Dummy());
203:
204:                    fail("Test should throw a exception in place");
205:                    broker.commitTransaction();
206:                } catch (PersistenceBrokerException e) {
207:                    assertTrue(true);
208:                    // e.printStackTrace();
209:                    broker.abortTransaction();
210:                }
211:
212:                int afterRollback = broker.getCount(q);
213:
214:                assertEquals("Object count does not match after rollback",
215:                        beforeRollback, afterRollback);
216:            }
217:
218:            public void testRollbackCausedBySQLException() throws Exception {
219:                // first we change metadata settings
220:                ojbChangeReferenceSetting(Project.class, "persons", true,
221:                        ObjectReferenceDescriptor.CASCADE_OBJECT,
222:                        ObjectReferenceDescriptor.CASCADE_OBJECT, false);
223:                ArrayList projects = getNewProjects(5);
224:                Query q;
225:                int beforeRollback;
226:
227:                broker.beginTransaction();
228:                storeObjects(broker, projects);
229:                broker.commitTransaction();
230:
231:                Criteria c = new Criteria();
232:                q = new QueryByCriteria(Project.class, c);
233:                beforeRollback = broker.getCount(q);
234:
235:                broker = PersistenceBrokerFactory.defaultPersistenceBroker();
236:                try {
237:                    broker.beginTransaction();
238:                    projects = getNewProjects(5);
239:                    Project badProject = (Project) projects.get(0);
240:                    badProject.setTitle("Bad project!");
241:                    // set wrong kind of object to force exception
242:                    badProject.setPersons(projects);
243:
244:                    System.err
245:                            .println("!! The following SQLException is part of the Test !!");
246:                    storeObjects(broker, projects);
247:
248:                    fail("Test should throw a exception in place");
249:                    broker.commitTransaction();
250:                } catch (PersistenceBrokerException e) {
251:                    assertTrue(true);
252:                    // e.printStackTrace();
253:                    broker.abortTransaction();
254:                }
255:
256:                int afterRollback = broker.getCount(q);
257:
258:                assertEquals("Object count does not match after rollback",
259:                        beforeRollback, afterRollback);
260:            }
261:
262:            protected void storeObjects(PersistenceBroker broker,
263:                    Collection objects) {
264:                boolean needsCommit = false;
265:                if (!broker.isInTransaction()) {
266:                    broker.beginTransaction();
267:                    needsCommit = true;
268:                }
269:                for (Iterator it = objects.iterator(); it.hasNext();) {
270:                    broker.store(it.next());
271:                }
272:                if (needsCommit) {
273:                    broker.commitTransaction();
274:                }
275:            }
276:
277:            private static int counter;
278:
279:            protected ArrayList getNewProjects(int count) {
280:                ArrayList list = new ArrayList();
281:                for (int i = 0; i < count; i++) {
282:                    list.add(newProject());
283:                }
284:                return list;
285:            }
286:
287:            protected Project newProject() {
288:                Project p = new Project();
289:                ++counter;
290:                p.setDescription("Test project " + counter);
291:                p.setTitle("Test " + counter);
292:                return p;
293:            }
294:
295:            public static void main(String[] args) {
296:                String[] arr = { PBRollbackTest.class.getName() };
297:                junit.textui.TestRunner.main(arr);
298:            }
299:
300:            class Dummy {
301:
302:            }
303:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.