001: /**
002: * Copyright (C) 2006 NetMind Consulting Bt.
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 3 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package hu.netmind.persistence;
018:
019: import java.util.*;
020: import org.apache.log4j.Logger;
021:
022: /**
023: * Save and load tests. These tests mainly look at saving and restoring
024: * objects.
025: * @author Brautigam Robert
026: * @version Revision: $Revision$
027: */
028: public class SaveTests extends AbstractPersistenceTest {
029: private static Logger logger = Logger.getLogger(SaveTests.class);
030:
031: public SaveTests(String name) throws Exception {
032: super (name);
033: }
034:
035: public void testPrimitiveTypesRandom() throws Exception {
036: // Drop
037: dropTables("primitives");
038: // Create
039: Primitives p = new Primitives();
040: p.randomize();
041: store.save(p);
042: // Get back
043: List primitives = store.find("find primitives");
044: // Check stuff
045: assertEquals(1, primitives.size());
046: assertEquals(p, primitives.get(0));
047: }
048:
049: public void testPrimitiveTypesMaximums() throws Exception {
050: // Drop
051: dropTables("primitives");
052: // Create
053: Primitives p = new Primitives();
054: p.maximize();
055: store.save(p);
056: // Get back
057: List primitives = store.find("find primitives");
058: // Check stuff
059: assertEquals(1, primitives.size());
060: assertEquals(p, primitives.get(0));
061: }
062:
063: public void testPrimitiveTypeNulls() throws Exception {
064: // Drop
065: dropTables("primitives");
066: // Create
067: Primitives p = new Primitives();
068: p.maximize();
069: store.save(p);
070: p.nullize();
071: store.save(p);
072: // Get back
073: List primitives = store.find("find primitives");
074: // Check stuff
075: assertEquals(1, primitives.size());
076: assertEquals(p, primitives.get(0));
077: }
078:
079: public void testPrimitiveMix() throws Exception {
080: // Drop
081: dropTables("primitives");
082: // Create
083: Primitives p = new Primitives();
084: p.minimize();
085: store.save(p);
086: p.maximize();
087: store.save(p);
088: p.nullize();
089: store.save(p);
090: // Get back
091: List primitives = store.find("find primitives");
092: // Check stuff
093: assertEquals(1, primitives.size());
094: assertEquals(p, primitives.get(0));
095: }
096:
097: public void testPrimitiveTypesMinimums() throws Exception {
098: // Drop
099: dropTables("primitives");
100: // Create
101: Primitives p = new Primitives();
102: p.minimize();
103: store.save(p);
104: // Get back
105: List primitives = store.find("find primitives");
106: // Check stuff
107: assertEquals(1, primitives.size());
108: assertEquals(p, primitives.get(0));
109: }
110:
111: public void testPrimitiveTypesNulls() throws Exception {
112: // Drop
113: dropTables("primitives");
114: // Create
115: Primitives p = new Primitives();
116: p.minimize();
117: store.save(p);
118: // Get back
119: List primitives = store.find("find primitives");
120: // Check stuff
121: assertEquals(1, primitives.size());
122: assertEquals(p, primitives.get(0));
123: }
124:
125: public void testDisregardTransient() throws Exception {
126: // Drop
127: dropTables("transientattrobject");
128: // Insert
129: TransientAttrObject t = new TransientAttrObject("test", 1);
130: t.setTrans("transient field");
131: store.save(t);
132: // Select
133: List result = store.find("find transientattrobject");
134: assertEquals(1, result.size());
135: assertEquals(result.get(0), t);
136: assertNull(((TransientAttrObject) result.get(0)).getTrans());
137: }
138:
139: public void testObjectType() throws Exception {
140: // Drop
141: dropTables("book");
142: // Create entry
143: Book book = new Book("Object test object", "5-6-7-8");
144: book.setMainAuthor(new Author("John", "Doe"));
145: // Save
146: store.save(book);
147: // Recall
148: List result = store.find("find book");
149: // Test
150: assertEquals(1, result.size());
151: assertEquals(book, result.get(0));
152: assertEquals(book.getMainAuthor(), ((Book) result.get(0))
153: .getMainAuthor());
154: }
155:
156: public void testObjectTypeNulls() throws Exception {
157: // Drop
158: dropTables("book");
159: // Create entry
160: Book book = new Book("Object test object", "5-6-7-8");
161: book.setMainAuthor(null);
162: // Save
163: store.save(book);
164: // Recall
165: List result = store.find("find book");
166: // Test
167: assertEquals(1, result.size());
168: assertEquals(book, result.get(0));
169: assertNull(book.getMainAuthor());
170: }
171:
172: public void testObjectSelfReference() throws Exception {
173: // Drop
174: dropTables("referrer");
175: // Create self-referencing object
176: Referrer ref = new Referrer(1);
177: ref.setRef(ref);
178: // Save
179: store.save(ref);
180: // Recall
181: List result = store.find("find referrer");
182: // Test
183: assertEquals(1, result.size());
184: Referrer resultRef = (Referrer) result.get(0);
185: assertEquals(ref, resultRef);
186: assertEquals(ref, resultRef.getRef());
187: assertEquals(ref.getRef(), resultRef.getRef());
188: assertTrue(resultRef == resultRef.getRef());
189: }
190:
191: public void testObjectCircularReference() throws Exception {
192: // Drop
193: dropTables("referrer");
194: // Create self-referencing object
195: Referrer ref1 = new Referrer(1);
196: Referrer ref2 = new Referrer(2);
197: ref1.setRef(ref2);
198: ref2.setRef(ref1);
199: // Save
200: store.save(ref1);
201: // Recall
202: List result = store
203: .find("find referrer where referrer.identity=1");
204: // Test
205: assertEquals(1, result.size());
206: Referrer resultRef = (Referrer) result.get(0);
207: assertEquals(ref1, resultRef);
208: assertEquals(ref2, resultRef.getRef());
209: assertEquals(ref1, resultRef.getRef().getRef());
210: assertTrue(resultRef == resultRef.getRef().getRef());
211: }
212:
213: public void testObjectSuperReference() throws Exception {
214: // Drop
215: dropTables("referrer");
216: dropTables("referrersubclass");
217: // Create self-referencing object
218: Referrer ref = new Referrer(1);
219: ReferrerSubclass sub = new ReferrerSubclass(2);
220: ref.setRef(sub);
221: // Save
222: store.save(ref);
223: // Recall
224: List result = store.find("find referrer where identity<>2");
225: // Test
226: assertEquals(1, result.size());
227: Referrer resultRef = (Referrer) result.get(0);
228: assertEquals(ref, resultRef);
229: assertEquals(sub, resultRef.getRef());
230: assertTrue(resultRef.getRef() instanceof ReferrerSubclass);
231: }
232:
233: public void testListObjects() throws Exception {
234: // Drop
235: dropTables("book");
236: dropTables("author");
237: // Create
238: Book book = new Book("Starship internals", "1-3-5-7");
239: Vector authors = new Vector();
240: authors.add(new Author("Geordi", "LaForge"));
241: authors.add(new Author("Data", ""));
242: authors.add(new Author("Scott", "Montgomery"));
243: book.setAuthors(authors);
244: // Save
245: store.save(book);
246: // Recall
247: List result = store.find("find book");
248: // Check
249: assertEquals(1, result.size());
250: Book resultBook = (Book) result.get(0);
251: assertEquals(book, resultBook);
252: assertEquals(book.getAuthors().size(), resultBook.getAuthors()
253: .size());
254: assertTrue(resultBook.getAuthors().equals(book.getAuthors()));
255: }
256:
257: public void testMapObjects() throws Exception {
258: // Drop
259: dropTables("mapholder");
260: // Create
261: MapHolder mapHolder = new MapHolder();
262: Book book = new Book("Book of Bokonon", "9");
263: HashMap meta = new HashMap();
264: meta.put("meta1", new Author("Geordi", "LaForge"));
265: meta.put("meta2", new Author("Data", ""));
266: meta.put("meta3", new Author("Scott", "Montgomery"));
267: meta.put("book", book);
268: mapHolder.setMeta(meta);
269: // Save
270: store.save(mapHolder);
271: // Recall
272: List result = store.find("find mapholder");
273: // Check
274: assertEquals(1, result.size());
275: MapHolder resultHolder = (MapHolder) result.get(0);
276: assertEquals(mapHolder.getMeta(), resultHolder.getMeta());
277: }
278:
279: public void testPrimitiveTypesModify() throws Exception {
280: // Drop
281: dropTables("primitives");
282: // Create
283: Primitives p = new Primitives();
284: // Randomize
285: p.randomize();
286: store.save(p);
287: // Get back
288: List primitives = store.find("find primitives");
289: // Check stuff
290: assertEquals(1, primitives.size());
291: assertEquals(p, primitives.get(0));
292: // Minimize
293: p.minimize();
294: store.save(p);
295: // Get back
296: primitives = store.find("find primitives");
297: // Check stuff
298: assertEquals(1, primitives.size());
299: assertEquals(p, primitives.get(0));
300: // Maximize
301: p.maximize();
302: store.save(p);
303: // Get back
304: primitives = store.find("find primitives");
305: // Check stuff
306: assertEquals(1, primitives.size());
307: assertEquals(p, primitives.get(0));
308: }
309:
310: public void testModifyListObjects() throws Exception {
311: // Drop
312: dropTables("book");
313: dropTables("author");
314: // Create
315: Book book = new Book("Starship internals", "1-3-5-7");
316: Vector authors = new Vector();
317: Author geordi = new Author("Geordi", "LaForge");
318: authors.add(geordi);
319: authors.add(new Author("Data", ""));
320: authors.add(new Author("Scott", "Montgomery"));
321: book.setAuthors(authors);
322: // Save
323: store.save(book);
324: // Recall
325: List result = store.find("find book");
326: // Check
327: assertEquals(1, result.size());
328: Book resultBook = (Book) result.get(0);
329: assertEquals(book, resultBook);
330: assertEquals(book.getAuthors().size(), resultBook.getAuthors()
331: .size());
332: assertTrue(resultBook.getAuthors().equals(book.getAuthors()));
333:
334: // Remove an item from list
335: book.getAuthors().remove(geordi);
336: // Save
337: store.save(book);
338: // Recall
339: result = store.find("find book");
340: // Check
341: assertEquals(1, result.size());
342: resultBook = (Book) result.get(0);
343: assertEquals(book, resultBook);
344: assertEquals(book.getAuthors().size(), resultBook.getAuthors()
345: .size());
346: assertTrue(resultBook.getAuthors().equals(book.getAuthors()));
347:
348: // Add two items to list
349: book.getAuthors().add(geordi);
350: book.getAuthors().add(new Author("Sulu", "Hikaru"));
351: // Save
352: store.save(book);
353: // Recall
354: result = store.find("find book");
355: // Check
356: assertEquals(1, result.size());
357: resultBook = (Book) result.get(0);
358: assertEquals(book, resultBook);
359: assertEquals(book.getAuthors().size(), resultBook.getAuthors()
360: .size());
361: assertTrue(resultBook.getAuthors().equals(book.getAuthors()));
362:
363: // Empty the list
364: book.getAuthors().clear();
365: // Save
366: store.save(book);
367: // Recall
368: result = store.find("find book");
369: // Check
370: assertEquals(1, result.size());
371: resultBook = (Book) result.get(0);
372: assertEquals(book, resultBook);
373: assertEquals(0, book.getAuthors().size());
374: }
375:
376: public void testModifyMapObjects() throws Exception {
377: // Drop
378: dropTables("mapholder");
379: // Create
380: MapHolder mapHolder = new MapHolder();
381: Book book = new Book("Book of Bokonon", "9");
382: HashMap meta = new HashMap();
383: meta.put("meta1", new Author("Geordi", "LaForge"));
384: meta.put("meta2", new Author("Data", ""));
385: meta.put("meta3", new Author("Scott", "Montgomery"));
386: meta.put("book", book);
387: mapHolder.setMeta(meta);
388: // Save
389: logger.debug("test saving 1...");
390: store.save(mapHolder);
391: logger.debug("test saving 1 done.");
392: // Recall
393: List result = store.find("find mapholder");
394: // Check
395: assertEquals(1, result.size());
396: MapHolder resultHolder = (MapHolder) result.get(0);
397: logger.debug("test checking equality 1...");
398: assertEquals(mapHolder.getMeta(), resultHolder.getMeta());
399: logger.debug("test checking equality 1 done.");
400:
401: // Remove an item
402: mapHolder.getMeta().remove("meta1");
403: // Save
404: store.save(mapHolder);
405: // Recall
406: result = store.find("find mapholder");
407: // Check
408: assertEquals(1, result.size());
409: resultHolder = (MapHolder) result.get(0);
410: assertEquals(mapHolder.getMeta(), resultHolder.getMeta());
411:
412: // Add two items
413: mapHolder.getMeta().put("meta1",
414: new Author("Geordi", "LaForge2"));
415: mapHolder.getMeta().put("referrer", new Referrer(1));
416: // Save
417: store.save(mapHolder);
418: // Recall
419: result = store.find("find mapholder");
420: // Check
421: assertEquals(1, result.size());
422: resultHolder = (MapHolder) result.get(0);
423: assertEquals(mapHolder.getMeta(), resultHolder.getMeta());
424:
425: // Empty the map
426: meta = new HashMap();
427: mapHolder.setMeta(meta);
428: // Save
429: store.save(mapHolder);
430: // Recall
431: result = store.find("find mapholder");
432: // Check
433: assertEquals(1, result.size());
434: resultHolder = (MapHolder) result.get(0);
435: assertEquals(0, mapHolder.getMeta().size());
436: }
437:
438: public void testReplaceMapValue() throws Exception {
439: // Drop
440: dropTables("mapholder");
441: // Create
442: MapHolder mapHolder = new MapHolder();
443: Book book = new Book("Book of Bokonon", "9");
444: HashMap meta = new HashMap();
445: meta.put("meta1", new Author("Geordi", "LaForge"));
446: meta.put("meta2", new Author("Data", ""));
447: meta.put("meta3", new Author("Scott", "Montgomery"));
448: meta.put("book", book);
449: mapHolder.setMeta(meta);
450: // Save
451: store.save(mapHolder);
452: // Recall
453: List result = store.find("find mapholder");
454: // Check
455: assertEquals(1, result.size());
456: MapHolder resultHolder = (MapHolder) result.get(0);
457: assertEquals(mapHolder.getMeta(), resultHolder.getMeta());
458:
459: // Change an item
460: mapHolder.getMeta().put("meta1", new Author("Sulu", "Hikaru"));
461: // Save
462: store.save(mapHolder);
463: // Recall
464: result = store.find("find mapholder");
465: // Check
466: assertEquals(1, result.size());
467: resultHolder = (MapHolder) result.get(0);
468: assertEquals(mapHolder.getMeta(), resultHolder.getMeta());
469: }
470:
471: public void testPersistenceIdAccess() throws Exception {
472: // Drop table
473: dropTables("author");
474:
475: // Create
476: store.save(new Author("Frank", "Herbert"));
477:
478: // Check
479: List result = store.find("find author");
480: assertEquals(1, result.size());
481: Author a = (Author) result.get(0);
482: assertTrue(0 != a.getPersistenceId());
483:
484: // Check if save works (it should not)
485: a.setPersistenceId(0);
486: store.save(a);
487: assertTrue(0 != a.getPersistenceId());
488: a.setPersistenceId(a.getPersistenceId() + 1);
489:
490: // Reload
491: result = store.find("find author");
492: Author b = (Author) result.get(0);
493: assertEquals(a.getPersistenceId() - 1, b.getPersistenceId());
494: }
495:
496: public void testPersistenceIdSelect() throws Exception {
497: // Drop table
498: dropTables("author");
499:
500: // Create
501: Author a = new Author("Frank", "Herbert");
502: store.save(a);
503: assertTrue(0 != a.getPersistenceId());
504:
505: // Check
506: List result = store.find("find author where persistenceid="
507: + a.getPersistenceId());
508: assertEquals(1, result.size());
509: }
510:
511: public void testReservedAttributeNameRename() throws Exception {
512: // Drop table
513: dropTables("nametester");
514:
515: // Create
516: NameTester n = new NameTester();
517: n.setSelect("select");
518: store.save(n);
519:
520: // Check
521: List result = store.find("find nametester");
522: assertEquals(1, result.size());
523: assertEquals("select", ((NameTester) result.get(0)).getSelect());
524:
525: // Check query language
526: result = store.find("find nametester where seLEct = 'select'");
527: assertEquals(1, result.size());
528: }
529:
530: public void testNullInList() throws Exception {
531: // Drop
532: dropTables("book");
533: dropTables("author");
534: // Create
535: Book book = new Book("Starship internals", "1-3-5-7");
536: Vector authors = new Vector();
537: authors.add(null);
538: book.setAuthors(authors);
539: // Save
540: try {
541: store.save(book);
542: fail("null value was allowed in list");
543: } catch (StoreException e) {
544: // Ok, it threw exception
545: }
546: }
547:
548: public void testNullMapObjects() throws Exception {
549: // Drop
550: dropTables("mapholder");
551: // Create
552: MapHolder mapHolder = new MapHolder();
553: Book book = new Book("Book of Bokonon", "9");
554: HashMap meta = new HashMap();
555: meta.put("meta1", new Author("Geordi", "LaForge"));
556: meta.put("meta2", new Author("Data", ""));
557: meta.put("meta3", new Author("Scott", "Montgomery"));
558: meta.put("metanull", null);
559: meta.put("metanullagain", null);
560: meta.put("book", book);
561: mapHolder.setMeta(meta);
562: // Save
563: try {
564: store.save(mapHolder);
565: fail("null value was allowed in map");
566: } catch (StoreException e) {
567: // Ok, it threw exception
568: }
569: }
570:
571: public void testNullMapKeys() throws Exception {
572: // Drop
573: dropTables("mapholder");
574: // Create
575: MapHolder mapHolder = new MapHolder();
576: Book book = new Book("Book of Bokonon", "9");
577: HashMap meta = new HashMap();
578: meta.put(null, new Author("Geordi", "LaForge"));
579: mapHolder.setMeta(meta);
580: // Save
581: try {
582: store.save(mapHolder);
583: fail("null values in map key was allowed, it should not be");
584: } catch (StoreException e) {
585: // Good
586: }
587: }
588:
589: public void testMultipleModificationsSameObjectInTransaction()
590: throws Exception {
591: // Drop
592: dropTables("book");
593: // Create
594: Book book = new Book("Starship internals", "1-3-5-7");
595: store.save(book);
596: Transaction tx = store.getTransactionTracker().getTransaction(
597: TransactionTracker.TX_NEW);
598: tx.begin();
599: book.setTitle("Another Book");
600: store.save(book);
601: book.setIsbn("1-1-1-1");
602: store.save(book);
603: book.setTitle("Another Book 2");
604: store.save(book);
605: tx.commit();
606: // Check
607: List result = store.find("find book");
608: assertEquals(1, result.size());
609: assertEquals(book, result.get(0));
610: }
611:
612: public void testRemoveObjectContainedInResult() throws Exception {
613: // Remove an object contained in a result list in a transaction
614: // after the object has been inserted. This tests that the remove's
615: // overwrite of persistence_txid will not affect older lists.
616:
617: // Drop
618: dropTables("book");
619: // Create and check
620: Book book = new Book("Starship internals", "1-3-5-7");
621: store.save(book);
622: List result = store.find("find book");
623: assertEquals(1, result.size());
624: assertEquals(book, result.get(0));
625: // Remove
626: store.remove(book);
627: // Check again
628: ((LazyList) result).refresh();
629: assertEquals(1, result.size());
630: assertEquals(book, result.get(0));
631: }
632:
633: public void testDefaultConstructor() throws Exception {
634: // try to insert
635: try {
636: store.save(new NoDefault(1));
637: fail("library saved an object with no default constructor, it shouldn't");
638: } catch (StoreException e) {
639: // ok
640: }
641: }
642:
643: public void testModifyListObjectsInPlace() throws Exception {
644: // Drop
645: dropTables("book");
646: dropTables("author");
647: // Create
648: Book book = new Book("Starship internals", "1-3-5-7");
649: Vector authors = new Vector();
650: authors.add(new Author("Geordi", "LaForge"));
651: authors.add(new Author("Data", ""));
652: book.setAuthors(authors);
653: // Save
654: store.save(book);
655: // Recall
656: Book resultBook = (Book) store.findSingle("find book");
657: assertEquals(2, resultBook.getAuthors().size());
658: resultBook.getAuthors().add(new Author("Scott", "Montgomery"));
659: assertEquals(3, resultBook.getAuthors().size());
660: store.save(resultBook);
661: // Recall
662: Book resultBook2 = (Book) store.findSingle("find book");
663: assertEquals(3, resultBook2.getAuthors().size());
664: }
665:
666: public void testInnerClasses() {
667: try {
668: store.save(new Inner());
669: fail("could save object of inner class, but shouldn't");
670: } catch (StoreException e) {
671: // All OK
672: }
673: }
674:
675: public void testAnonymousClasses() {
676: try {
677: store.save(new Object() {
678: public void nop() {
679: }
680: });
681: fail("could save anonymous class object, but shouldn't");
682: } catch (StoreException e) {
683: // All OK
684: }
685: }
686:
687: public class Inner {
688: private String field;
689: }
690:
691: public void testListEmptyingWithNewStore() throws Exception {
692: if (!supportsMultipleStores())
693: return;
694: // Delete all books
695: dropTables("book");
696:
697: // First, insert a book
698: Book originalBook = new Book("New", "1111");
699: Vector authors = new Vector();
700: authors.add(new Author("Old", "Author"));
701: originalBook.setAuthors(authors);
702: store.save(originalBook);
703:
704: // Now terminate store and allocate new one
705: store.close();
706: store = getStore();
707:
708: // Now select and try to empty the book's authors
709: Book copyBook = (Book) store.findSingle("find book");
710: copyBook.setAuthors(new Vector());
711: store.save(copyBook);
712:
713: // Check
714: Book dbBook = (Book) store.findSingle("find book");
715: assertEquals(0, dbBook.getAuthors().size());
716: }
717:
718: public void testAttributeNullingWithNewStore() throws Exception {
719: if (!supportsMultipleStores())
720: return;
721: // Delete all books
722: dropTables("book");
723:
724: // First, insert a book
725: Book originalBook = new Book("New", "1111");
726: store.save(originalBook);
727:
728: // Now terminate store and allocate new one
729: store.close();
730: store = getStore();
731:
732: // Now select and try to empty the book's authors
733: Book copyBook = (Book) store.findSingle("find book");
734: copyBook.setIsbn(null);
735: store.save(copyBook);
736:
737: // Check
738: Book dbBook = (Book) store.findSingle("find book");
739: assertEquals(copyBook, dbBook);
740: }
741:
742: public void testAllAttributesAreSaved() throws Exception {
743: // Delete all books
744: dropTables("book");
745: // Insert a book
746: Book first = new Book("First Title", "1");
747: store.save(first);
748: // Select the book, and modify
749: Book second = (Book) store.findSingle("find book");
750: second.setTitle("Second Title");
751: store.save(second);
752: // Modify the original object
753: first.setIsbn("2");
754: store.save(first); // All attributes are saved
755: // Check
756: Book dbBook = (Book) store.findSingle("find book");
757: assertEquals(first, dbBook);
758: }
759:
760: public void testNothingChanged() throws Exception {
761: // Delete all books
762: dropTables("book");
763: // Insert a book
764: Book first = new Book("First Title", "1");
765: store.save(first);
766: // Select the book, and do not modify
767: Book second = (Book) store.findSingle("find book");
768: assertEquals(first, second);
769: store.save(second);
770: // Check
771: Book dbBook = (Book) store.findSingle("find book");
772: assertEquals(second, dbBook);
773: }
774:
775: public void testListReferencedInSave() throws Exception {
776: // Delete all books
777: dropTables("listholder");
778: // Insert a book
779: ListHolder list1 = new ListHolder();
780: Vector list1content = new Vector();
781: list1.setList(list1content);
782: store.save(list1);
783: // Select, and update
784: ListHolder dbHolder = (ListHolder) store
785: .findSingle("find listholder");
786: ListHolder holder2 = new ListHolder();
787: holder2.setList(dbHolder.getList());
788: Author gibson = new Author("Gibson", "William");
789: dbHolder.getList().add(gibson);
790: dbHolder.getList().add(holder2);
791: store.save(dbHolder);
792: // Now select holder 2, and check what list items it has
793: ListHolder dbHolder2 = (ListHolder) store.findSingle(
794: "find listholder where listholder=?",
795: new Object[] { holder2 });
796: // The list should contain itself, and gibson too.
797: assertNotNull(dbHolder2);
798: assertEquals(2, dbHolder2.getList().size());
799: }
800:
801: public void testReservedTableName() throws Exception {
802: // Drop the table
803: dropTables("user%");
804: // Create
805: User user = new User();
806: user.setName("demon");
807: store.save(user);
808: // Do query
809: assertEquals(1, store.find("find user").size());
810: }
811:
812: public void testPrimitiveObjectSaves() throws Exception {
813: // Drop the table
814: dropTables("objectholder");
815: // Create
816: ObjectHolder holder = new ObjectHolder();
817: holder.setObj("I am Primitive");
818: store.save(holder);
819: // Do query
820: ObjectHolder result = (ObjectHolder) store
821: .findSingle("find objectholder");
822: assertEquals(holder, result);
823: }
824:
825: public void testReservedAttributeSave() throws Exception {
826: // Drop
827: dropTables("user%");
828: // Create and save
829: User user = new User();
830: user.setPassword("xxx");
831: store.save(user);
832: // Check
833: User dbUser = (User) store.findSingle("find user");
834: assertEquals("xxx", dbUser.getPassword());
835: }
836:
837: public void testNonchangedSaveTwice() throws Exception {
838: // Drop book
839: dropTables("book");
840: // Create book with many attributes
841: Book book = new Book("The Book of the Unchanged", "1-2-3-4");
842: Author author = new Author("John", "Derek");
843: book.setMainAuthor(author);
844: Vector authors = new Vector();
845: authors.add(author);
846: book.setAuthors(authors);
847: store.save(book);
848: // Save again, and look whether something was done
849: Transaction tx = store.getTransactionTracker().getTransaction(
850: TransactionTracker.TX_NEW);
851: tx.begin();
852: store.save(book);
853: tx.commit();
854: // Test what happened
855: assertEquals(0, tx.getStats().getInsertCount()
856: + tx.getStats().getUpdateCount());
857: }
858:
859: public void testNonchangedSaveReload() throws Exception {
860: // Drop book
861: dropTables("book");
862: // Create book with many attributes
863: Book book = new Book("The Book of the Unchanged", "1-2-3-4");
864: Author author = new Author("John", "Derek");
865: book.setMainAuthor(author);
866: Vector authors = new Vector();
867: authors.add(author);
868: book.setAuthors(authors);
869: store.save(book);
870: // Reload
871: Book dbBook = (Book) store.findSingle("find book");
872: // Save again, and look whether something was done
873: Transaction tx = store.getTransactionTracker().getTransaction(
874: TransactionTracker.TX_NEW);
875: tx.begin();
876: store.save(dbBook);
877: tx.commit();
878: // Test what happened
879: assertEquals(0, tx.getStats().getInsertCount()
880: + tx.getStats().getUpdateCount());
881: }
882:
883: public void testNonstorableAbstractSuperclassWithNoDefaultConstructor()
884: throws Exception {
885: // Try to save an object with an abstract superclass, which
886: // is non-storable, and has no default constructor
887: store.save(new Car());
888: }
889:
890: public void testAbstractSuperclassWithNoDefaultConstructor()
891: throws Exception {
892: // Try to save an object with an abstract superclass, which
893: // is storable, and has no default constructor
894: store.save(new Swallow());
895: }
896:
897: }
|