Source Code Cross Referenced for OptimisticLockingTest.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.junit.PBTestCase;
004:
005:        /**
006:         * This TestClass tests OJB facilities for optimistic locking.
007:         */
008:        public class OptimisticLockingTest extends PBTestCase {
009:
010:            public static void main(String[] args) {
011:                String[] arr = { OptimisticLockingTest.class.getName() };
012:                junit.textui.TestRunner.main(arr);
013:            }
014:
015:            public OptimisticLockingTest(String name) {
016:                super (name);
017:            }
018:
019:            /** Test optimistic Lock by version.*/
020:            public void testVersionLock() throws Exception {
021:                LockedByVersion obj = new LockedByVersion();
022:                obj.setValue("original");
023:                Identity oid = new Identity(obj, broker);
024:                broker.beginTransaction();
025:                broker.store(obj);
026:                broker.commitTransaction();
027:
028:                broker.clearCache();
029:                LockedByVersion copy1 = (LockedByVersion) broker
030:                        .getObjectByIdentity(oid);
031:                broker.clearCache();
032:                LockedByVersion copy2 = (LockedByVersion) broker
033:                        .getObjectByIdentity(oid);
034:
035:                copy1.setValue("copy 1");
036:                copy2.setValue("copy 2");
037:                assertEquals("Expect same version number", copy1.getVersion(),
038:                        copy2.getVersion());
039:
040:                broker.beginTransaction();
041:                broker.store(copy1);
042:                broker.commitTransaction();
043:                assertTrue("Expect different version number", copy1
044:                        .getVersion() != copy2.getVersion());
045:                try {
046:                    // as copy1 has already been stored the version info of copy2
047:                    // is out of sync with the database !
048:                    broker.beginTransaction();
049:                    broker.store(copy2);
050:                    broker.commitTransaction();
051:                } catch (OptimisticLockException ex) {
052:                    assertTrue(true);
053:                    //LoggerFactory.getDefaultLogger().debug(ex);
054:                    broker.abortTransaction();
055:                    return;
056:                }
057:                fail("Should throw an Optimistic Lock exception");
058:            }
059:
060:            /**
061:             * demonstrates how OptimisticLockExceptions can be used
062:             * to handle resynchronization of conflicting instances.
063:             */
064:            public void testLockHandling() throws Exception {
065:                LockedByVersion obj = new LockedByVersion();
066:                obj.setValue("original");
067:                Identity oid = new Identity(obj, broker);
068:                broker.beginTransaction();
069:                broker.store(obj);
070:                broker.commitTransaction();
071:
072:                broker.clearCache();
073:                LockedByVersion copy1 = (LockedByVersion) broker
074:                        .getObjectByIdentity(oid);
075:                broker.clearCache();
076:                LockedByVersion copy2 = (LockedByVersion) broker
077:                        .getObjectByIdentity(oid);
078:
079:                copy1.setValue("copy 1");
080:                copy2.setValue("copy 2");
081:                assertEquals("Expect same version number", copy1.getVersion(),
082:                        copy2.getVersion());
083:
084:                broker.beginTransaction();
085:                broker.store(copy1);
086:                broker.commitTransaction();
087:                assertTrue("Expect different version number", copy1
088:                        .getVersion() != copy2.getVersion());
089:                try {
090:                    // as copy1 has already been stored the version info of copy2
091:                    // is out of sync with the database !
092:                    broker.beginTransaction();
093:                    broker.store(copy2);
094:                    broker.commitTransaction();
095:                } catch (OptimisticLockException ex) {
096:                    // obtain conflicting object from exception
097:                    Object conflictingObject = ex.getSourceObject();
098:
099:                    // get a synchronized instance
100:                    broker.removeFromCache(conflictingObject);
101:                    Object syncronizedObject = broker
102:                            .getObjectByIdentity(new Identity(
103:                                    conflictingObject, broker));
104:
105:                    // modify synchronized copy and call store again without trouble
106:                    ((LockedByVersion) syncronizedObject).setValue("copy 3");
107:                    if (!broker.isInTransaction())
108:                        broker.beginTransaction();
109:                    broker.store(syncronizedObject);
110:                    broker.commitTransaction();
111:                    return;
112:                }
113:                fail("Should throw an Optimistic Lock exception");
114:            }
115:
116:            /** Test optimistic Lock by timestamp.*/
117:            public void testTimestampLock() throws Exception {
118:                LockedByTimestamp obj = new LockedByTimestamp();
119:                obj.setValue("original");
120:                Identity oid = new Identity(obj, broker);
121:                broker.beginTransaction();
122:                broker.store(obj);
123:                broker.commitTransaction();
124:
125:                broker.clearCache();
126:                LockedByTimestamp copy1 = (LockedByTimestamp) broker
127:                        .getObjectByIdentity(oid);
128:                broker.clearCache();
129:                LockedByTimestamp copy2 = (LockedByTimestamp) broker
130:                        .getObjectByIdentity(oid);
131:
132:                /*
133:                //mysql timestamp does not support milliseconds
134:                arminw:
135:                For proper test we need millisecond precision, so if mysql does not support
136:                this, better we let fail this test for mysql
137:                 */
138:                Thread.sleep(50);
139:
140:                copy1.setValue("copy 1");
141:                copy2.setValue("copy 2");
142:                assertEquals("Expect same version number",
143:                        copy1.getTimestamp(), copy2.getTimestamp());
144:
145:                broker.beginTransaction();
146:                broker.store(copy1);
147:                broker.commitTransaction();
148:                assertTrue("Expect different version number", copy1
149:                        .getTimestamp() != copy2.getTimestamp());
150:                try {
151:                    // as copy1 has already been stored the timestamp info
152:                    // of copy2  is out of sync with the database !
153:                    broker.beginTransaction();
154:                    broker.store(copy2);
155:                    broker.commitTransaction();
156:
157:                    fail("Should throw an Optimistic Lock exception");
158:                } catch (OptimisticLockException ex) {
159:                    assertTrue(true);
160:                    broker.abortTransaction();
161:                }
162:
163:                if (!broker.isInTransaction())
164:                    broker.beginTransaction();
165:                broker.delete(copy1);
166:                broker.commitTransaction();
167:
168:                try {
169:                    broker.beginTransaction();
170:                    broker.delete(copy1);
171:                    broker.commitTransaction();
172:                } catch (OptimisticLockException e) {
173:                    // BRJ: exception thrown if object has been modified or deleted
174:                    //
175:                    // fail("If an object which use optimistic locking was deleted two times, OJB" +
176:                    //         " should not throw an optimistic locking exception: "+e.getMessage());
177:                    broker.abortTransaction();
178:                    return;
179:                }
180:                fail("Should throw an Optimistic Lock exception");
181:            }
182:
183:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.