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


001:        package org.apache.ojb.odmg;
002:
003:        import java.util.ArrayList;
004:        import java.util.Collection;
005:        import java.util.Iterator;
006:        import java.util.List;
007:        import java.util.Collections;
008:        import java.util.Comparator;
009:
010:        import org.apache.commons.lang.builder.ToStringBuilder;
011:        import org.apache.commons.lang.builder.HashCodeBuilder;
012:        import org.apache.commons.lang.builder.EqualsBuilder;
013:        import org.apache.ojb.junit.ODMGTestCase;
014:        import org.odmg.DBag;
015:        import org.odmg.DList;
016:        import org.odmg.OQLQuery;
017:        import org.odmg.Transaction;
018:
019:        /**
020:         * Tests for OJB {@link org.odmg.DList} implementation.
021:         */
022:        public class DListTest extends ODMGTestCase {
023:            public static void main(String[] args) {
024:                String[] arr = { DListTest.class.getName() };
025:                junit.textui.TestRunner.main(arr);
026:            }
027:
028:            public DListTest(String name)
029:
030:            {
031:                super (name);
032:            }
033:
034:            protected DObject createObject(String name) throws Exception {
035:                DObject obj = new DObject();
036:                obj.setName(name);
037:                obj.setRandomName("rnd_" + ((int) (Math.random() * 1000)));
038:
039:                return obj;
040:            }
041:
042:            public void testAddingLockupWithTx() throws Exception {
043:                // create a unique name:
044:                final String name = "testAdding_" + System.currentTimeMillis();
045:
046:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
047:                tx.begin();
048:                // create DList and bound by name
049:                DList list = odmg.newDList();
050:                database.bind(list, name);
051:                tx.commit();
052:
053:                tx.begin();
054:                tx.getBroker().clearCache();
055:                Object obj = database.lookup(name);
056:                tx.commit();
057:                assertNotNull("binded DList not found", obj);
058:
059:                tx.begin();
060:                // add objects to list
061:                for (int i = 0; i < 5; i++) {
062:                    DObject a = createObject(name);
063:                    list.add(a);
064:                }
065:                tx.commit();
066:
067:                // check current list
068:                Iterator iter = list.iterator();
069:                while (iter.hasNext()) {
070:                    DObject a = (DObject) iter.next();
071:                    assertNotNull(a);
072:                }
073:
074:                tx.begin();
075:                tx.getBroker().clearCache();
076:
077:                // lookup list and check entries
078:                DList lookedUp = (DList) database.lookup(name);
079:                assertNotNull("binded DList not found", lookedUp);
080:
081:                //System.out.println("sequence of items in lookedup list:");
082:                iter = lookedUp.iterator();
083:                Iterator iter1 = list.iterator();
084:                while (iter.hasNext()) {
085:                    DObject a = (DObject) iter.next();
086:                    DObject b = (DObject) iter1.next();
087:                    assertNotNull(a);
088:                    assertNotNull(b);
089:                    assertEquals(a.getId(), b.getId());
090:                }
091:                tx.commit();
092:
093:                // add new entries to list
094:                tx.begin();
095:                for (int i = 0; i < 3; i++) {
096:                    DObject a = createObject(name + "_new_entry");
097:                    list.add(a);
098:                }
099:                tx.commit();
100:
101:                tx.begin();
102:                tx.getBroker().clearCache();
103:                lookedUp = (DList) database.lookup(name);
104:                iter = lookedUp.iterator();
105:                iter1 = list.iterator();
106:                assertEquals("Wrong number of DListEntry found", 8, list.size());
107:                while (iter.hasNext()) {
108:                    DObject a = (DObject) iter.next();
109:                    DObject b = (DObject) iter1.next();
110:                    assertNotNull(a);
111:                    assertNotNull(b);
112:                    assertEquals(a.getId(), b.getId());
113:                }
114:                tx.commit();
115:                assertNotNull("binded DList not found", lookedUp);
116:            }
117:
118:            public void testRemoveAdd() throws Exception {
119:                // create a unique name:
120:                final String name = "testRemoveAdd_"
121:                        + System.currentTimeMillis();
122:
123:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
124:                tx.begin();
125:                // create DList and bound by name
126:                DList list = odmg.newDList();
127:                database.bind(list, name);
128:
129:                // add object to list
130:                for (int i = 0; i < 5; i++) {
131:                    DObject a = createObject(name);
132:                    list.add(a);
133:                }
134:                tx.commit();
135:
136:                // check current list
137:                Iterator iter = list.iterator();
138:                while (iter.hasNext()) {
139:                    DObject a = (DObject) iter.next();
140:                    assertNotNull(a);
141:                }
142:
143:                tx.begin();
144:                tx.getBroker().clearCache();
145:
146:                // lookup list and check entries
147:                DList lookedUp = (DList) database.lookup(name);
148:                assertNotNull("binded DList not found", lookedUp);
149:
150:                //System.out.println("sequence of items in lookedup list:");
151:                iter = lookedUp.iterator();
152:                Iterator iter1 = list.iterator();
153:                while (iter.hasNext()) {
154:                    DObject a = (DObject) iter.next();
155:                    DObject b = (DObject) iter1.next();
156:                    assertNotNull(a);
157:                    assertNotNull(b);
158:                    assertEquals(a.getId(), b.getId());
159:                }
160:                tx.commit();
161:
162:                // add and remove new entries
163:                tx.begin();
164:                for (int i = 0; i < 3; i++) {
165:                    DObject a = createObject(name + "_new_entry_NOT_PERSIST");
166:                    list.add(a);
167:                    list.remove(list.size() - 1);
168:                }
169:                tx.commit();
170:
171:                tx.begin();
172:                tx.getBroker().clearCache();
173:                lookedUp = (DList) database.lookup(name);
174:                iter = lookedUp.iterator();
175:                iter1 = list.iterator();
176:                assertEquals("Wrong number of DListEntry found", 5, list.size());
177:                while (iter.hasNext()) {
178:                    DObject a = (DObject) iter.next();
179:                    DObject b = (DObject) iter1.next();
180:                    assertNotNull(a);
181:                    assertNotNull(b);
182:                    assertEquals(a.getId(), b.getId());
183:                }
184:                tx.commit();
185:                assertNotNull("binded DList not found", lookedUp);
186:
187:                tx.begin();
188:                for (int i = 0; i < 3; i++) {
189:                    DObject a = createObject(name + "_new_entry_new_persist");
190:                    list.add(a);
191:                    list.remove(0);
192:                }
193:                tx.commit();
194:
195:                tx.begin();
196:                tx.getBroker().clearCache();
197:                lookedUp = (DList) database.lookup(name);
198:                iter = lookedUp.iterator();
199:                iter1 = list.iterator();
200:                assertEquals("Wrong number of DListEntry found", 5, list.size());
201:                while (iter.hasNext()) {
202:                    DObject a = (DObject) iter.next();
203:                    DObject b = (DObject) iter1.next();
204:                    assertNotNull(a);
205:                    assertNotNull(b);
206:                    assertEquals(a.getId(), b.getId());
207:                }
208:                tx.commit();
209:                assertNotNull("binded DList not found", lookedUp);
210:            }
211:
212:            public void testReadAndStore() throws Exception {
213:                // create a unique name:
214:                final String name = "testReadAndStore_"
215:                        + System.currentTimeMillis();
216:
217:                // create test objects
218:                Transaction tx = odmg.newTransaction();
219:                tx.begin();
220:                // add objects to list
221:                for (int i = 0; i < 5; i++) {
222:                    tx.lock(createObject(name), Transaction.WRITE);
223:                }
224:                tx.commit();
225:
226:                tx.begin();
227:                // query test objects
228:                OQLQuery q = odmg.newOQLQuery();
229:                q.create("select all from " + DObject.class.getName()
230:                        + " where name=$1");
231:                q.bind(name);
232:                Collection ret = (Collection) q.execute();
233:                // check result list size
234:                assertEquals(5, ret.size());
235:                // do read lock
236:                for (Iterator it = ret.iterator(); it.hasNext();) {
237:                    tx.lock(it.next(), Transaction.READ);
238:                }
239:                // create new list for results
240:                ArrayList result = new ArrayList();
241:                result.addAll(ret);
242:                tx.commit();
243:            }
244:
245:            public void testIterateWithoutTx() throws Exception {
246:                // create a unique name:
247:                final String name = "testAdding_" + System.currentTimeMillis();
248:
249:                // get DList and fill with objects
250:                List list = odmg.newDList();
251:                TransactionExt tx = (TransactionExt) odmg.newTransaction();
252:                tx.begin();
253:                for (int i = 0; i < 5; i++) {
254:                    DObject a = createObject(name);
255:                    list.add(a);
256:                }
257:                // bind the new list
258:                database.bind(list, name);
259:                tx.commit();
260:
261:                tx = (TransactionExt) odmg.newTransaction();
262:                tx.begin();
263:                Object obj = database.lookup(name);
264:                tx.commit();
265:                assertNotNull("binded DList not found", obj);
266:
267:                // iterate list
268:                Iterator iter = list.iterator();
269:                while (iter.hasNext()) {
270:                    DObject a = (DObject) iter.next();
271:                    assertNotNull(a);
272:                }
273:                assertEquals(5, list.size());
274:
275:                tx = (TransactionExt) odmg.newTransaction();
276:                tx.begin();
277:                tx.getBroker().clearCache();
278:
279:                List lookedUp = (List) database.lookup(name);
280:                tx.commit();
281:                assertNotNull("binded DList not found", lookedUp);
282:
283:                // DList doesn't support #set(...) method, so 
284:                list = new ArrayList(list);
285:                lookedUp = new ArrayList(lookedUp);
286:
287:                Collections.sort(list, new Comparator() {
288:                    public int compare(Object o1, Object o2) {
289:                        DObject d1 = (DObject) o1;
290:                        DObject d2 = (DObject) o2;
291:                        return d1.getId().compareTo(d2.getId());
292:                    }
293:                });
294:
295:                Collections.sort(lookedUp, new Comparator() {
296:                    public int compare(Object o1, Object o2) {
297:                        DObject d1 = (DObject) o1;
298:                        DObject d2 = (DObject) o2;
299:                        return d1.getId().compareTo(d2.getId());
300:                    }
301:                });
302:
303:                assertEquals(list.size(), lookedUp.size());
304:
305:                for (int i = 0; i < lookedUp.size(); i++) {
306:                    DObject a = (DObject) lookedUp.get(i);
307:                    DObject aa = (DObject) list.get(i);
308:                    assertNotNull(a);
309:                    assertNotNull(aa);
310:                    assertEquals(a.getId(), aa.getId());
311:                }
312:            }
313:
314:            /**
315:             * this test checks if removing item from DList works
316:             */
317:            public void testRemoving() throws Exception {
318:                // create a unique name:
319:                String name = "testRemoving_" + System.currentTimeMillis();
320:
321:                Transaction tx = odmg.newTransaction();
322:                tx.begin();
323:                DList list = odmg.newDList();
324:                // bind the list to the name:
325:                database.bind(list, name);
326:
327:                for (int i = 0; i < 5; i++) {
328:                    DObject a = createObject(name);
329:                    list.add(a);
330:                }
331:                assertEquals(5, list.size());
332:                tx.commit();
333:
334:                // delete two items
335:                tx = odmg.newTransaction();
336:                tx.begin();
337:                ((HasBroker) odmg.currentTransaction()).getBroker()
338:                        .clearCache();
339:                DList lookedUp = (DList) database.lookup(name);
340:                assertNotNull("database lookup does not find the named DList",
341:                        lookedUp);
342:                assertEquals("Wrong number of list entries", 5, lookedUp.size());
343:                lookedUp.remove(2);
344:                lookedUp.remove(1);
345:                tx.commit();
346:
347:                // check if deletion was successful
348:                tx = odmg.newTransaction();
349:                tx.begin();
350:                ((HasBroker) odmg.currentTransaction()).getBroker()
351:                        .clearCache();
352:                lookedUp = (DList) database.lookup(name);
353:                tx.commit();
354:
355:                assertEquals(3, lookedUp.size());
356:            }
357:
358:            public void testAddingWithIndex() throws Exception {
359:                // create a unique name:
360:                String name = "testAddingWithIndex_"
361:                        + System.currentTimeMillis();
362:
363:                Transaction tx = odmg.newTransaction();
364:                tx.begin();
365:                DList list = odmg.newDList();
366:                database.bind(list, name);
367:                tx.commit();
368:
369:                tx = odmg.newTransaction();
370:                tx.begin();
371:                for (int i = 0; i < 5; i++) {
372:                    DObject a = createObject(name);
373:                    list.add(a);
374:                }
375:
376:                list.add(2, createObject(name + "_pos2"));
377:                list.add(0, createObject(name + "_pos0"));
378:                list.add(7, createObject(name + "_pos7"));
379:                tx.commit();
380:
381:                tx.begin();
382:                ((TransactionImpl) tx).getBroker().clearCache();
383:                // System.out.println("list: " + list);
384:                // System.out.println("lookup list: " + db.lookup(name));
385:                tx.commit();
386:
387:                //System.out.println("sequence of items in list:");
388:                Iterator iter = list.iterator();
389:                DObject a;
390:                while (iter.hasNext()) {
391:                    a = (DObject) iter.next();
392:                    assertNotNull(a);
393:                    //System.out.print(a.getArticleId() + ", ");
394:                }
395:
396:                tx = odmg.newTransaction();
397:                tx.begin();
398:                ((TransactionImpl) tx).getBroker().clearCache();
399:                DList lookedUp = (DList) database.lookup(name);
400:                // System.out.println("lookup list: " + lookedUp);
401:                assertNotNull("database lookup does not find DList", lookedUp);
402:                assertEquals(8, lookedUp.size());
403:                iter = lookedUp.iterator();
404:                while (iter.hasNext()) {
405:                    a = (DObject) iter.next();
406:                }
407:                tx.commit();
408:            }
409:
410:            public void testDBag() throws Exception {
411:                String name = "testDBag_" + System.currentTimeMillis();
412:
413:                Transaction tx = odmg.newTransaction();
414:                tx.begin();
415:                DBag bag1 = odmg.newDBag();
416:                DBag bag2 = odmg.newDBag();
417:                DObject a, b, c, d, e;
418:                a = createObject(name);
419:                b = createObject(name);
420:                c = createObject(name);
421:                d = createObject(name);
422:                e = createObject(name);
423:                bag1.add(a);
424:                bag1.add(b);
425:                bag1.add(c);
426:                bag2.add(b);
427:                bag2.add(c);
428:                bag2.add(d);
429:                bag2.add(e);
430:                DBag bag3 = bag1.difference(bag2);
431:                assertEquals("should contain only 1 element", 1, bag3.size());
432:
433:                bag3 = bag1.intersection(bag2);
434:                assertEquals("should contain two elements", 2, bag3.size());
435:
436:                tx.commit();
437:            }
438:
439:            public static class DObject {
440:                Integer id;
441:                String name;
442:                String randomName;
443:
444:                public DObject() {
445:                }
446:
447:                public boolean equals(Object obj) {
448:                    if (obj instanceof  DObject) {
449:                        DObject target = ((DObject) obj);
450:                        return new EqualsBuilder().append(id, target.getId())
451:                                .append(name, target.getName()).append(
452:                                        randomName, target.getRandomName())
453:                                .isEquals();
454:                    } else {
455:                        return false;
456:                    }
457:                }
458:
459:                public int hashCode() {
460:                    return new HashCodeBuilder().append(id).append(name)
461:                            .append(randomName).hashCode();
462:                }
463:
464:                public String toString() {
465:                    ToStringBuilder buf = new ToStringBuilder(this );
466:                    buf.append("id", id);
467:                    buf.append("name", name);
468:                    buf.append("randonName", randomName);
469:                    return buf.toString();
470:                }
471:
472:                public Integer getId() {
473:                    return id;
474:                }
475:
476:                public void setId(Integer id) {
477:                    this .id = id;
478:                }
479:
480:                public String getName() {
481:                    return name;
482:                }
483:
484:                public void setName(String name) {
485:                    this .name = name;
486:                }
487:
488:                public String getRandomName() {
489:                    return randomName;
490:                }
491:
492:                public void setRandomName(String randomName) {
493:                    this.randomName = randomName;
494:                }
495:            }
496:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.