Source Code Cross Referenced for RsIteratorTest.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 java.util.Iterator;
004:
005:        import junit.framework.TestCase;
006:        import org.apache.ojb.broker.query.*;
007:        import org.apache.ojb.broker.accesslayer.RsIterator;
008:
009:        /**
010:         * Test case for the RsIterator
011:         *
012:         * @author <a href="mailto:rongallagher@bellsouth.net">Ron Gallagher<a>
013:         * @version $Id: $
014:         */
015:        public class RsIteratorTest extends TestCase {
016:            private PersistenceBroker broker;
017:
018:            public static void main(String[] args) {
019:                String[] arr = { RsIteratorTest.class.getName() };
020:                junit.textui.TestRunner.main(arr);
021:            }
022:
023:            public RsIteratorTest(String name) {
024:                super (name);
025:            }
026:
027:            public void setUp() {
028:                broker = PersistenceBrokerFactory.defaultPersistenceBroker();
029:            }
030:
031:            public void tearDown() {
032:                if (broker != null) {
033:                    broker.close();
034:                }
035:            }
036:
037:            public void testRsIterator() throws Exception {
038:                String name = "testRsIterator_" + System.currentTimeMillis();
039:                prepareTest(name);
040:
041:                Criteria criteria = new Criteria();
042:                criteria.addLike("name", name + "*");
043:                Query query = new QueryByCriteria(
044:                        ObjectRepository.Component.class, criteria);
045:
046:                Iterator it = broker.getIteratorByQuery(query);
047:                int k = 0;
048:                while (it.hasNext()) {
049:                    it.next();
050:                    k++;
051:                }
052:                assertEquals("Wrong number of items found", 2, k);
053:            }
054:
055:            /**
056:             * Test RsIterator cleanup on PB.commitTransaction()
057:             */
058:            public void testRsIteratorAutomaticCleanupCheck_1()
059:                    throws Exception {
060:                String name = "testRsIteratorAutomaticCleanupCheck_1_"
061:                        + System.currentTimeMillis();
062:                prepareTest(name);
063:
064:                Criteria criteria = new Criteria();
065:                criteria.addLike("name", name + "*");
066:                Query query = new QueryByCriteria(
067:                        ObjectRepository.Component.class, criteria);
068:
069:                Iterator it = broker.getIteratorByQuery(query);
070:                it.hasNext();
071:                broker.beginTransaction();
072:                broker.commitTransaction();
073:                /*
074:                if tx was commited we invalidate RsIterator instance
075:                 */
076:                try {
077:                    it.next();
078:                    fail("We expect RsIterator has released resources on pb.commit..");
079:                } catch (RsIterator.ResourceClosedException e) {
080:                    assertTrue(true);
081:                }
082:
083:                it = broker.getIteratorByQuery(query);
084:                it.hasNext();
085:                it.next();
086:                broker.beginTransaction();
087:                broker.commitTransaction();
088:                /*
089:                if tx was commited we invalidate RsIterator instance
090:                 */
091:                try {
092:                    it.hasNext();
093:                    it.next();
094:                    fail("We expect RsIterator has released resources on pb.commit..");
095:                } catch (RsIterator.ResourceClosedException e) {
096:                    assertTrue(true);
097:                }
098:            }
099:
100:            /**
101:             * Test RsIterator cleanup on PB.abortTransaction()
102:             */
103:            public void testRsIteratorAutomaticCleanupCheck_2()
104:                    throws Exception {
105:                String name = "testRsIteratorAutomaticCleanupCheck_"
106:                        + System.currentTimeMillis();
107:                prepareTest(name);
108:
109:                Criteria criteria = new Criteria();
110:                criteria.addLike("name", name + "*");
111:                Query query = new QueryByCriteria(
112:                        ObjectRepository.Component.class, criteria);
113:
114:                Iterator it = broker.getIteratorByQuery(query);
115:                it.hasNext();
116:                broker.beginTransaction();
117:                broker.abortTransaction();
118:                /*
119:                if tx was aborted we invalidate RsIterator instance
120:                 */
121:                try {
122:                    it.next();
123:                    fail("We expect RsIterator has released resources on pb.commit..");
124:                } catch (RsIterator.ResourceClosedException e) {
125:                    assertTrue(true);
126:                }
127:
128:                it = broker.getIteratorByQuery(query);
129:                it.hasNext();
130:                it.next();
131:                broker.beginTransaction();
132:                broker.abortTransaction();
133:                /*
134:                if tx was aborted we invalidate RsIterator instance
135:                 */
136:                try {
137:                    it.hasNext();
138:                    it.next();
139:                    fail("We expect RsIterator has released resources on pb.commit..");
140:                } catch (RsIterator.ResourceClosedException e) {
141:                    assertTrue(true);
142:                }
143:            }
144:
145:            /**
146:             * Test RsIterator cleanup on PB.close()
147:             */
148:            public void testRsIteratorAutomaticCleanupCheck_3()
149:                    throws Exception {
150:                String name = "testRsIteratorAutomaticCleanupCheck_"
151:                        + System.currentTimeMillis();
152:                prepareTest(name);
153:
154:                Criteria criteria = new Criteria();
155:                criteria.addLike("name", name + "*");
156:                Query query = new QueryByCriteria(
157:                        ObjectRepository.Component.class, criteria);
158:
159:                Iterator it = broker.getIteratorByQuery(query);
160:                broker.close();
161:                /*
162:                if was closed we invalidate RsIterator instance
163:                 */
164:                try {
165:                    if (it.hasNext())
166:                        it.next();
167:                    fail("We expect RsIterator has released resources on pb.commit..");
168:                } catch (RsIterator.ResourceClosedException e) {
169:                    assertTrue(true);
170:                }
171:            }
172:
173:            /**
174:             * Test RsIterator cleanup on PB.abortTransaction()
175:             */
176:            public void testRsIteratorUserCleanup_1() throws Exception {
177:                String name = "testRsIteratorAutomaticCleanupCheck_"
178:                        + System.currentTimeMillis();
179:                prepareTest(name);
180:
181:                Criteria criteria = new Criteria();
182:                criteria.addLike("name", name + "*");
183:                Query query = new QueryByCriteria(
184:                        ObjectRepository.Component.class, criteria);
185:
186:                Iterator it = broker.getIteratorByQuery(query);
187:
188:                /*
189:                TODO: After integration of setAutoRelease into OJBIterator and changes
190:                in PB interface getIteratorXXX methods we don't need these casts any longer
191:                 */
192:                if (!(it instanceof  RsIterator)) {
193:                    // skip test
194:                    return;
195:                }
196:
197:                // TODO: Remove this cast one day
198:                ((RsIterator) it).setAutoRelease(false);
199:
200:                it.hasNext();
201:                broker.beginTransaction();
202:                broker.abortTransaction();
203:                /*
204:                if tx was aborted we invalidate RsIterator instance
205:                 */
206:                try {
207:                    it.next();
208:                    fail("We expect RsIterator has released resources on pb.commit..");
209:                } catch (RsIterator.ResourceClosedException e) {
210:                    assertTrue(true);
211:                }
212:
213:                it = broker.getIteratorByQuery(query);
214:                // TODO: Remove this cast one day
215:                ((RsIterator) it).setAutoRelease(false);
216:
217:                it.hasNext();
218:                it.next();
219:                broker.beginTransaction();
220:                broker.abortTransaction();
221:                /*
222:                if tx was aborted we invalidate RsIterator instance
223:                 */
224:                try {
225:                    it.hasNext();
226:                    it.next();
227:                    fail("We expect RsIterator has released resources on pb.commit..");
228:                } catch (RsIterator.ResourceClosedException e) {
229:                    assertTrue(true);
230:                }
231:            }
232:
233:            /**
234:             * Test RsIterator cleanup on PB.abortTransaction()
235:             */
236:            public void testRsIteratorUserCleanup_2() throws Exception {
237:                String name = "testRsIteratorAutomaticCleanupCheck_"
238:                        + System.currentTimeMillis();
239:                prepareTest(name);
240:
241:                Criteria criteria = new Criteria();
242:                criteria.addLike("name", name + "*");
243:                Query query = new QueryByCriteria(
244:                        ObjectRepository.Component.class, criteria);
245:
246:                Iterator it = broker.getIteratorByQuery(query);
247:
248:                /*
249:                TODO: After integration of setAutoRelease into OJBIterator and changes
250:                in PB interface getIteratorXXX methods we don't need these casts any longer
251:                 */
252:                if (!(it instanceof  RsIterator)) {
253:                    // skip test
254:                    return;
255:                }
256:
257:                // TODO: Remove this cast one day
258:                ((RsIterator) it).setAutoRelease(false);
259:
260:                while (it.hasNext()) {
261:                    ObjectRepository.Component c = (ObjectRepository.Component) it
262:                            .next();
263:                    assertNotNull(c.getId());
264:                }
265:
266:                try {
267:                    ((RsIterator) it).relative(1);
268:                } catch (RsIterator.ResourceClosedException e) {
269:                    fail("RsIterator should not close resources by itself");
270:                } catch (PersistenceBrokerException ignore) {
271:                }
272:
273:                // TODO: Remove this cast one day
274:                ((RsIterator) it).releaseDbResources();
275:            }
276:
277:            private void prepareTest(String objectName) {
278:                ObjectRepository.Component c1 = new ObjectRepository.Component();
279:                c1.setName(objectName + "_1");
280:                ObjectRepository.Component c2 = new ObjectRepository.Component();
281:                c2.setName(objectName + "_2");
282:
283:                broker.beginTransaction();
284:                broker.store(c1);
285:                broker.store(c2);
286:                broker.commitTransaction();
287:            }
288:
289:            /**
290:             * Test retrieving data via the rsIterator
291:             * test by Ron Gallagher
292:             */
293:            public void testInternUsedRsIterator() throws Exception {
294:                // Build the query
295:                Criteria criteria = new Criteria();
296:                criteria.addEqualTo("id", new Integer(1));
297:                Query query = new QueryByCriteria(Person.class, criteria);
298:                // Run the query.
299:                Person person = (Person) broker.getObjectByQuery(query);
300:                assertNotNull("Person with id 1 was not found", person);
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.