0001: /**
0002: * Copyright (C) 2006 NetMind Consulting Bt.
0003: *
0004: * This library is free software; you can redistribute it and/or
0005: * modify it under the terms of the GNU Lesser General Public
0006: * License as published by the Free Software Foundation; either
0007: * version 3 of the License, or (at your option) any later version.
0008: *
0009: * This library is distributed in the hope that it will be useful,
0010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
0012: * Lesser General Public License for more details.
0013: *
0014: * You should have received a copy of the GNU Lesser General Public
0015: * License along with this library; if not, write to the Free Software
0016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0017: */package hu.netmind.persistence;
0018:
0019: import java.util.*;
0020: import org.apache.log4j.Logger;
0021:
0022: /**
0023: * Test for find syntax and functionality.
0024: * @author Brautigam Robert
0025: * @version Revision: $Revision$
0026: */
0027: public class SelectTests extends AbstractPersistenceTest {
0028: private static Logger logger = Logger.getLogger(SelectTests.class);
0029: private Vector refs = null;
0030:
0031: public SelectTests(String name) throws Exception {
0032: super (name);
0033: }
0034:
0035: private void createReferrers(int size) throws Exception {
0036: // Create referrers
0037: dropTables("referrer");
0038: refs = new Vector();
0039: for (int i = 0; i < size; i++) {
0040: Referrer ref = new Referrer(i);
0041: refs.add(ref);
0042: store.save(ref);
0043: }
0044: }
0045:
0046: public void testBatchSelect() throws Exception {
0047: createReferrers(100);
0048: // Select
0049: List result = store.find("find referrer");
0050: // Check
0051: Collections.sort(refs);
0052: Vector resultVector = new Vector(result);
0053: Collections.sort(resultVector);
0054: assertEquals(refs, resultVector);
0055: }
0056:
0057: public void testSimpleSelectPlainAttribute() throws Exception {
0058: createReferrers(5);
0059: // Select
0060: List result = store.find("find referrer where identity=1");
0061: // Check
0062: assertEquals(1, result.size());
0063: }
0064:
0065: public void testSimpleSelectSpecifiedAttribute() throws Exception {
0066: createReferrers(5);
0067: // Select
0068: List result = store
0069: .find("find referrer where referrer.identity=1");
0070: // Check
0071: assertEquals(1, result.size());
0072: }
0073:
0074: public void testSimpleSelectAliasedUnspecifiedAttribute()
0075: throws Exception {
0076: createReferrers(5);
0077: // Select
0078: List result = store.find("find r(referrer) where identity=1");
0079: // Check
0080: assertEquals(1, result.size());
0081: }
0082:
0083: public void testSimpleSelectAliasedSpecifiedAttribute()
0084: throws Exception {
0085: createReferrers(5);
0086: // Select
0087: List result = store.find("find r(referrer) where r.identity=1");
0088: // Check
0089: assertEquals(1, result.size());
0090: }
0091:
0092: public void testSimpleReferenceSelect() throws Exception {
0093: // Create test setup
0094: dropTables("referrer");
0095: Referrer ref1 = new Referrer(1);
0096: Referrer ref2 = new Referrer(2);
0097: ref1.setRef(ref2);
0098: store.save(ref1);
0099: // Select ref1
0100: List result = store
0101: .find("find referrer where referrer.ref.identity=2");
0102: assertEquals(1, result.size());
0103: assertEquals(ref1, result.get(0));
0104: // Select nothing
0105: result = store
0106: .find("find referrer where referrer.ref.identity=1");
0107: assertEquals(0, result.size());
0108: }
0109:
0110: public void testMultipleReferencedSelect() throws Exception {
0111: // Create test setup
0112: dropTables("referrer");
0113: Referrer ref1 = new Referrer(1);
0114: Referrer ref2 = new Referrer(2);
0115: ref1.setRef(ref2);
0116: ref2.setRef(ref1);
0117: store.save(ref1);
0118: // Select ref1
0119: List result = store
0120: .find("find referrer where referrer.ref.ref.ref.ref.identity=2");
0121: assertEquals(1, result.size());
0122: assertEquals(ref2, result.get(0));
0123: }
0124:
0125: public void testSimpleObjectSelect() throws Exception {
0126: // Create test setup
0127: dropTables("referrer");
0128: Referrer ref1 = new Referrer(1);
0129: Referrer ref2 = new Referrer(2);
0130: ref1.setRef(ref2);
0131: ref2.setRef(ref1);
0132: store.save(ref1);
0133: // Select ref1
0134: List result = store.find("find referrer where referrer.ref=?",
0135: new Object[] { ref2 });
0136: assertEquals(1, result.size());
0137: assertEquals(ref1, result.get(0));
0138: }
0139:
0140: public void testMultipleTermSelect() throws Exception {
0141: // Create test setup
0142: dropTables("referrer");
0143: Referrer ref1 = new Referrer(1);
0144: Referrer ref2 = new Referrer(2);
0145: ref1.setRef(ref2);
0146: store.save(ref1);
0147: // Select ref1
0148: List result = store
0149: .find("find referrer where identity=1 and referrer.ref.identity=2");
0150: assertEquals(1, result.size());
0151: assertEquals(ref1, result.get(0));
0152: }
0153:
0154: public void testContainsOperatorOnList() throws Exception {
0155: // Create test setup
0156: dropTables("book");
0157: // Create
0158: Book book = new Book("Starship internals", "1-3-5-7");
0159: Vector authors = new Vector();
0160: authors.add(new Author("Geordi", "LaForge"));
0161: authors.add(new Author("Data", ""));
0162: authors.add(new Author("Scott", "Montgomery"));
0163: book.setAuthors(authors);
0164: // Save
0165: store.save(book);
0166: // Select ref1
0167: List result = store
0168: .find("find book where book.authors contains author and author.firstname = 'Geordi'");
0169: assertEquals(1, result.size());
0170: assertEquals(book, result.get(0));
0171: }
0172:
0173: public void testMapSelection() throws Exception {
0174: // Drop
0175: dropTables("mapholder");
0176: // Create
0177: MapHolder mapHolder = new MapHolder();
0178: Book book = new Book("Book of Bokonon", "9");
0179: HashMap meta = new HashMap();
0180: meta.put("meta1", new Author("Geordi", "LaForge"));
0181: meta.put("meta2", new Author("Data", ""));
0182: meta.put("meta3", new Author("Scott", "Montgomery"));
0183: meta.put("book", book);
0184: mapHolder.setMeta(meta);
0185: // Save
0186: store.save(mapHolder);
0187: // Select
0188: List result = store
0189: .find("find mapholder where mapholder.meta['book'](book)=book and book.title like 'Book%'");
0190: assertEquals(1, result.size());
0191: result = store
0192: .find("find mapholder where mapholder.meta['book'](book)=book and book.title like '9'");
0193: assertEquals(0, result.size());
0194: }
0195:
0196: public void testMapContainsOperator() throws Exception {
0197: // Drop
0198: dropTables("mapholder");
0199: // Create
0200: MapHolder mapHolder = new MapHolder();
0201: Book book = new Book("Book of Bokonon", "9");
0202: HashMap meta = new HashMap();
0203: meta.put("meta1", new Author("Geordi", "LaForge"));
0204: meta.put("meta2", new Author("Data", ""));
0205: meta.put("meta3", new Author("Scott", "Montgomery"));
0206: meta.put("book", book);
0207: mapHolder.setMeta(meta);
0208: // Save
0209: store.save(mapHolder);
0210: // Select
0211: List result = store
0212: .find("find mapholder where mapholder.meta contains book and book.isbn='9'");
0213: assertEquals(1, result.size());
0214: }
0215:
0216: public void testMapSelectionWithoutClassSepcificer()
0217: throws Exception {
0218: // Drop
0219: dropTables("mapholder");
0220: // Create
0221: MapHolder mapHolder = new MapHolder();
0222: Book book = new Book("Book of Bokonon", "9");
0223: HashMap meta = new HashMap();
0224: meta.put("meta1", new Author("Geordi", "LaForge"));
0225: meta.put("meta2", new Author("Data", ""));
0226: meta.put("meta3", new Author("Scott", "Montgomery"));
0227: meta.put("book", book);
0228: mapHolder.setMeta(meta);
0229: // Save
0230: store.save(mapHolder);
0231: // Select
0232: List result = store
0233: .find("find mapholder where mapholder.meta['book']=book and book.title like 'Book%'");
0234: assertEquals(1, result.size());
0235: result = store
0236: .find("find mapholder where mapholder.meta['book']=book and book.title like '9'");
0237: assertEquals(0, result.size());
0238: }
0239:
0240: public void testKeywordCaseInsensitiveness() throws Exception {
0241: createReferrers(2);
0242: // Select
0243: List result = store.find("FiNd referrer wHeRe identity=1");
0244: // Check
0245: assertEquals(1, result.size());
0246: }
0247:
0248: public void testIdentifierCaseInsensitiveness() throws Exception {
0249: createReferrers(2);
0250: // Select
0251: List result = store.find("find rEfeRReR where iDeNtiTy=1");
0252: // Check
0253: assertEquals(1, result.size());
0254: }
0255:
0256: public void testStringCaseSensitiveness() throws Exception {
0257: // Create
0258: dropTables("book");
0259: Book book = new Book("Book of Bokonon", "9");
0260: store.save(book);
0261: // Select
0262: List result = store
0263: .find("find book where title='Book of Bokonon'");
0264: assertEquals(1, result.size());
0265: result = store.find("find book where title='boOK oF BokONon'");
0266: assertEquals(0, result.size());
0267: }
0268:
0269: public void testStringCaseSensitivenessInMaps() throws Exception {
0270: // Drop
0271: dropTables("mapholder");
0272: // Create
0273: MapHolder mapHolder = new MapHolder();
0274: Book book = new Book("Book of Bokonon", "9");
0275: HashMap meta = new HashMap();
0276: meta.put("book", book);
0277: mapHolder.setMeta(meta);
0278: store.save(mapHolder);
0279: // Select
0280: List result = store
0281: .find("find mapholder where mapholder.meta['book']=book and book.isbn='9'");
0282: assertEquals(1, result.size());
0283: result = store
0284: .find("find mapholder where mapholder.meta['bOoK']=book and book.isbn='9'");
0285: assertEquals(0, result.size());
0286: }
0287:
0288: public void testUnspecifiedSimpleOrderBy() throws Exception {
0289: createReferrers(10);
0290: // Select
0291: List result = store.find("find referrer order by identity");
0292: // Check
0293: assertEquals(10, result.size());
0294: for (int i = 0; i < 10; i++)
0295: assertEquals(i, ((Referrer) result.get(i)).getIdentity());
0296: }
0297:
0298: public void testSpecifiedSimpleOrderBy() throws Exception {
0299: createReferrers(10);
0300: // Select
0301: List result = store
0302: .find("find referrer order by referrer.identity");
0303: // Check
0304: assertEquals(10, result.size());
0305: for (int i = 0; i < 10; i++)
0306: assertEquals(i, ((Referrer) result.get(i)).getIdentity());
0307: }
0308:
0309: public void testSpecifiedAliasedSimpleOrderBy() throws Exception {
0310: createReferrers(10);
0311: // Select
0312: List result = store
0313: .find("find r(referrer) order by r.identity");
0314: // Check
0315: assertEquals(10, result.size());
0316: for (int i = 0; i < 10; i++)
0317: assertEquals(i, ((Referrer) result.get(i)).getIdentity());
0318: }
0319:
0320: public void testSpecifiedAliasedAscendingSimpleOrderBy()
0321: throws Exception {
0322: createReferrers(10);
0323: // Select
0324: List result = store
0325: .find("find r(referrer) order by r.identity asc");
0326: // Check
0327: assertEquals(10, result.size());
0328: for (int i = 0; i < 10; i++)
0329: assertEquals(i, ((Referrer) result.get(i)).getIdentity());
0330: }
0331:
0332: public void testSpecifiedAliasedDescendingSimpleOrderBy()
0333: throws Exception {
0334: createReferrers(10);
0335: // Select
0336: List result = store
0337: .find("find r(referrer) order by r.identity desc");
0338: // Check
0339: assertEquals(10, result.size());
0340: for (int i = 0; i < 10; i++)
0341: assertEquals(9 - i, ((Referrer) result.get(i))
0342: .getIdentity());
0343: }
0344:
0345: public void testMultipleOrderBy() throws Exception {
0346: dropTables("book");
0347: Book b1 = new Book("Book of Bokonon", "1-2-3-4");
0348: Book b2 = new Book("Book of Bokonon", "4-3-2-1");
0349: Book b3 = new Book("Starhip design", "2-2-2-2");
0350: Book b4 = new Book("NetMind Persistence tutorial", "0-0-0-0");
0351: // Save
0352: store.save(b1);
0353: store.save(b2);
0354: store.save(b3);
0355: store.save(b4);
0356: // Get
0357: List result = store
0358: .find("find book order by title asc,isbn desc");
0359: assertEquals(4, result.size());
0360: assertEquals(b2, result.get(0));
0361: assertEquals(b1, result.get(1));
0362: assertEquals(b4, result.get(2));
0363: assertEquals(b3, result.get(3));
0364: }
0365:
0366: public void testComplexSelectWithOrdering() throws Exception {
0367: dropTables("book");
0368: Vector authors = new Vector();
0369: authors.add(new Author("Tom", "Petty"));
0370: authors.add(new Author("Lynyrd", "Skynyrd"));
0371: Book b1 = new Book("Book of Bokonon", "1-2-3-4");
0372: b1.setAuthors(authors);
0373: b1.setMainAuthor(new Author("Bokonon Jr.", ""));
0374: Book b2 = new Book("Book of Bokonon", "4-3-2-1");
0375: authors = new Vector();
0376: authors.add(new Author("Tom", "Petty"));
0377: b2.setAuthors(authors);
0378: b2.setMainAuthor(new Author("Bokonon", ""));
0379: Book b3 = new Book("Starhip design", "2-2-2-2");
0380: Book b4 = new Book("NetMind Persistence tutorial", "0-0-0-0");
0381: // Save
0382: store.save(b1);
0383: store.save(b2);
0384: store.save(b3);
0385: store.save(b4);
0386: // Get
0387: List result = store
0388: .find("find book where (book.authors contains tom(author)) and tom.firstname='Tom' and book.authors contains lynyrd(author) and lynyrd.firstname='Lynyrd' and book.mainauthor.firstname like 'Bokonon%'");
0389: assertEquals(1, result.size());
0390: assertEquals(b1, result.get(0));
0391: }
0392:
0393: public void testNegatedContains() throws Exception {
0394: dropTables("book");
0395: Vector authors = new Vector();
0396: authors.add(new Author("Tom", "Petty"));
0397: authors.add(new Author("Lynyrd", "Skynyrd"));
0398: Book b1 = new Book("Book of Bokonon", "1-2-3-4");
0399: b1.setAuthors(authors);
0400: // Save
0401: store.save(b1);
0402: // Get
0403: try {
0404: List result = store
0405: .find("find book where not book.authors contains author and author.firstname='Tom'");
0406: fail("negated contains did not throw exception.");
0407: } catch (Exception e) {
0408: }
0409: }
0410:
0411: public void testComplexNegatedContains() throws Exception {
0412: dropTables("book");
0413: Vector authors = new Vector();
0414: authors.add(new Author("Tom", "Petty"));
0415: authors.add(new Author("Lynyrd", "Skynyrd"));
0416: Book b1 = new Book("Book of Bokonon", "1-2-3-4");
0417: b1.setAuthors(authors);
0418: // Save
0419: store.save(b1);
0420: // Get
0421: try {
0422: List result = store
0423: .find("find book where not (not (not book.authors contains author and author.firstname='Tom'))");
0424: fail("negated contains did not throw exception.");
0425: } catch (Exception e) {
0426: }
0427: }
0428:
0429: public void testIterationWithFor() throws Exception {
0430: createReferrers(150);
0431: // Search
0432: List result = store.find("find referrer");
0433: // Iterate
0434: for (int i = 0; i < result.size(); i++) {
0435: Referrer ref = (Referrer) result.get(i);
0436: }
0437: }
0438:
0439: public void testIterationWithIterator() throws Exception {
0440: createReferrers(150);
0441: // Search
0442: List result = store.find("find referrer");
0443: // Iterate
0444: Iterator iterator = result.iterator();
0445: while (iterator.hasNext()) {
0446: Referrer ref = (Referrer) iterator.next();
0447: }
0448: }
0449:
0450: public void testEmptyIterationWithIterator() throws Exception {
0451: createReferrers(0);
0452: // Search
0453: List result = store.find("find referrer");
0454: // Iterate
0455: Iterator iterator = result.iterator();
0456: while (iterator.hasNext()) {
0457: Referrer ref = (Referrer) iterator.next();
0458: }
0459: }
0460:
0461: public void testRandomIterations() throws Exception {
0462: Referrer ref;
0463: createReferrers(150);
0464: // Search
0465: List result = store.find("find referrer");
0466: assertEquals(150, result.size());
0467: // Iterate forward
0468: for (int i = 0; i < result.size(); i++)
0469: ref = (Referrer) result.get(i);
0470: // Iterate forward again
0471: for (int i = 0; i < result.size(); i++)
0472: ref = (Referrer) result.get(i);
0473: // Iterate backward
0474: for (int i = result.size(); i > 0; i--)
0475: ref = (Referrer) result.get(i - 1);
0476: // Iterate randomly around
0477: Random rnd = new Random();
0478: for (int i = 0; i < 50; i++)
0479: ref = (Referrer) result.get(rnd.nextInt(150));
0480: }
0481:
0482: public void testDistinctResult() throws Exception {
0483: // Clear
0484: dropTables("referrer");
0485:
0486: // Insert
0487: store.save(new Referrer(1));
0488: store.save(new Referrer(1));
0489:
0490: // Select
0491: List result = store
0492: .find("find referrer where referrer.identity=second(referrer).identity");
0493: assertEquals(2, result.size()); // We should receive 2 instead of 4
0494: }
0495:
0496: public void testNonDistinctByteArrays() throws Exception {
0497: // Clear
0498: dropTables("referredbytearray");
0499:
0500: // Insert
0501: store.save(new ReferredByteArray(1));
0502: store.save(new ReferredByteArray(1));
0503:
0504: // Select
0505: List result = store
0506: .find("find referredbytearray where referredbytearray.identity=second(referredbytearray).identity");
0507: assertEquals(4, result.size()); // We should receive 4
0508: }
0509:
0510: public void testNonDistinctByteArraysInSubclass() throws Exception {
0511: // Clear
0512: dropTables("bytearraysuper");
0513: dropTables("bytearrayclass");
0514:
0515: // Insert
0516: store.save(new ByteArrayClass(1));
0517: store.save(new ByteArrayClass(1));
0518:
0519: // Select
0520: List result = store
0521: .find("find bytearraysuper where bytearraysuper.identity=second(bytearraysuper).identity");
0522: assertEquals(4, result.size()); // We should receive 4
0523: }
0524:
0525: public void testDistinctOrderBysOnOtherClass() throws Exception {
0526: // Clear
0527: dropTables("referrer");
0528: dropTables("identitystuff");
0529:
0530: // Create
0531: store.save(new Referrer(1));
0532: store.save(new Referrer(1));
0533: IdentityStuff i1 = new IdentityStuff(1, new Author("Adam",
0534: "Smith"));
0535: store.save(i1);
0536: IdentityStuff i2 = new IdentityStuff(1, new Author("Cecil",
0537: "Hue"));
0538: store.save(i2);
0539: IdentityStuff i3 = new IdentityStuff(1, new Author("Eve",
0540: "Smith"));
0541: store.save(i3);
0542: IdentityStuff i4 = new IdentityStuff(1, new Author("Wade",
0543: "Low"));
0544: store.save(i4);
0545:
0546: // Select
0547: List result = store
0548: .find("find identitystuff where identitystuff.identity=referrer.identity order by identitystuff.author.firstName asc");
0549: assertEquals(4, result.size()); // Every identity stuff is selected twice, but it should be distinct
0550: assertEquals(i1, result.get(0));
0551: assertEquals(i2, result.get(1));
0552: assertEquals(i3, result.get(2));
0553: assertEquals(i4, result.get(3));
0554: }
0555:
0556: public void testListsStayNull() throws Exception {
0557: // Drop tables
0558: dropTables("book");
0559:
0560: // Create a book
0561: store.save(new Book("Title", "Isbn"));
0562:
0563: // Select
0564: List result = store.find("find book");
0565: assertEquals(1, result.size());
0566: assertNull(((Book) result.get(0)).getAuthors());
0567: }
0568:
0569: public void testMapsStayNull() throws Exception {
0570: // Drop tables
0571: dropTables("mapholder");
0572:
0573: // Create a book
0574: store.save(new MapHolder());
0575:
0576: // Select
0577: List result = store.find("find mapholder");
0578: assertEquals(1, result.size());
0579: assertNull(((MapHolder) result.get(0)).getMeta());
0580: }
0581:
0582: public void testLikeOperator() throws Exception {
0583: // Drop tables
0584: dropTables("book");
0585:
0586: // Create a book
0587: store.save(new Book("Title", "Isbn"));
0588:
0589: // Select
0590: List result = store.find("find book where title like 'ti%'");
0591: assertEquals(0, result.size());
0592: result = store.find("find book where title like 'Ti%'");
0593: assertEquals(1, result.size());
0594: }
0595:
0596: public void testCaseInsensitiveLikeOperator() throws Exception {
0597: // Drop tables
0598: dropTables("book");
0599:
0600: // Create a book
0601: store.save(new Book("Title", "Isbn"));
0602:
0603: // Select
0604: List result = store.find("find book where title ilike 'ti%'");
0605: assertEquals(1, result.size());
0606: result = store.find("find book where title ilike 'Ti%'");
0607: assertEquals(1, result.size());
0608: }
0609:
0610: public void testUnderscoreClass() throws Exception {
0611: // Drop tables
0612: dropTables("score");
0613:
0614: // Create
0615: store.save(new Under_Score());
0616:
0617: // Select
0618: List result = store.find("find under_score");
0619: assertEquals(1, result.size());
0620: }
0621:
0622: public void testSameClassName() throws Exception {
0623: // Drop tables
0624: dropTables("empty");
0625: dropTables("subpackage_empty");
0626:
0627: // Create
0628: store.save(new Empty());
0629: store.save(new hu.netmind.persistence.subpackage.Empty());
0630:
0631: // Selects
0632: List result = store.find("find empty");
0633: assertEquals(1, result.size());
0634: result = store.find("find empty(subpackage.empty)");
0635: assertEquals(1, result.size());
0636: result = store.find("find empty(persistence.empty)");
0637: assertEquals(1, result.size());
0638: }
0639:
0640: public void testNowKeyword() throws Exception {
0641: // Drop
0642: dropTables("movie");
0643:
0644: // Create
0645: store
0646: .save(new Movie("Raising Arizona", new Date(),
0647: new Date()));
0648:
0649: // Select
0650: List result = store.find("find movie where startdate <= now");
0651: assertEquals(1, result.size());
0652: result = store.find("find movie where enddate > now");
0653: assertEquals(0, result.size());
0654: }
0655:
0656: public void testSelectSymbolTable() throws Exception {
0657: // Drop books
0658: dropTables("book");
0659: dropTables("author");
0660:
0661: // Create
0662: Book book = new Book("Title", "Isbn");
0663: book.setMainAuthor(new Author("Peter", "Jackson"));
0664: store.save(book);
0665:
0666: // Select
0667: List result = store
0668: .find("find book where book.mainauthor.firstname='Peter' or book.mainauthor.firstname='Peter'");
0669: assertEquals(1, result.size());
0670: }
0671:
0672: public void testNullParameters() throws Exception {
0673: // Drop books
0674: dropTables("book");
0675: // Create
0676: Book book = new Book("Title", null);
0677: store.save(book);
0678: // Select
0679: List result = store.find("find book where isbn = ?",
0680: new Object[] { null });
0681: assertEquals(1, result.size());
0682: assertEquals(book, result.get(0));
0683: }
0684:
0685: public void testNullParameters2() throws Exception {
0686: // Drop books
0687: dropTables("book");
0688: // Create
0689: Book book = new Book("Title", "1-2-3-4");
0690: store.save(book);
0691: // Select
0692: List result = store.find("find book where isbn <> ?",
0693: new Object[] { null });
0694: assertEquals(1, result.size());
0695: assertEquals(book, result.get(0));
0696: }
0697:
0698: public void testSelectInTransaction() throws Exception {
0699: // Drop books
0700: dropTables("book");
0701: // Create and select
0702: Book book = new Book("Title", "1-2-3-4");
0703: Transaction tx = store.getTransactionTracker().getTransaction(
0704: TransactionTracker.TX_NEW);
0705: tx.begin();
0706: store.save(book);
0707: List result = store.find("find book");
0708: assertEquals(1, result.size());
0709: assertEquals(book, result.get(0));
0710: tx.commit();
0711: // List should contain the object still
0712: ((LazyList) result).refresh();
0713: assertEquals(1, result.size());
0714: assertEquals(book, result.get(0));
0715: }
0716:
0717: public void testSelectSingleNotExists() throws Exception {
0718: // Drop books
0719: dropTables("book");
0720: // Test
0721: assertNull(store.findSingle("find book"));
0722: }
0723:
0724: public void testSelectSingleExists() throws Exception {
0725: // Drop books
0726: dropTables("book");
0727: // Insert
0728: Book book = new Book("Title", "1-2-3-4");
0729: store.save(book);
0730: // Test
0731: assertEquals(book, store.findSingle("find book"));
0732: }
0733:
0734: public void testSelectSingleMultipleExists() throws Exception {
0735: // Drop books
0736: dropTables("book");
0737: // Insert
0738: Book book1 = new Book("Title 1", "1-2-3-4");
0739: store.save(book1);
0740: Book book2 = new Book("Title 2", "1-2-3-4");
0741: store.save(book2);
0742: // Test
0743: assertNotNull(store.findSingle("find book"));
0744: }
0745:
0746: public void testPaging() throws Exception {
0747: // Create referrers
0748: createReferrers(LazyList.BATCH_SIZE * 2);
0749: // Query
0750: Transaction tx = store.getTransactionTracker().getTransaction(
0751: TransactionTracker.TX_NEW);
0752: tx.begin();
0753: List result = store.find("find referrer");
0754: // Iterate forward
0755: for (int i = 0; i < LazyList.BATCH_SIZE * 2; i++)
0756: result.get(i);
0757: // Iterate backward
0758: for (int i = LazyList.BATCH_SIZE * 2; i > 0; i--)
0759: result.get(i - 1);
0760: tx.commit();
0761: // Check how many selects ran (1 count + 3 selects, or
0762: // sometimes, 2 selects, if the cache has enough memory)
0763: assertTrue(tx.getStats().getSelectCount() < 4);
0764: }
0765:
0766: public void testReservedTableOrderBy() throws Exception {
0767: // Drop the table
0768: dropTables("user%");
0769: // Create
0770: User user = new User();
0771: user.setName("demon");
0772: store.save(user);
0773: User aUser = new User();
0774: aUser.setName("root");
0775: store.save(aUser);
0776: // Do query
0777: List result = store.find("find user order by user.name");
0778: assertEquals(2, result.size());
0779: }
0780:
0781: public void testMemberListIndependentObjects() throws Exception {
0782: // Drop
0783: dropTables("listholder");
0784: // Create with many independent objects
0785: ListHolder holder = new ListHolder();
0786: holder.setList(new Vector());
0787: holder.getList().add(new Referrer(1));
0788: holder.getList().add(new Book("Independent Book", "1"));
0789: store.save(holder);
0790: // Now check
0791: Transaction tx = store.getTransactionTracker().getTransaction(
0792: TransactionTracker.TX_REQUIRED);
0793: tx.begin();
0794: ListHolder dbHolder = (ListHolder) store
0795: .findSingle("find listholder");
0796: // Check stats: 1 holder, +2 statements
0797: assertEquals(2, dbHolder.getList().size());
0798: assertEquals(4, tx.getStats().getSelectCount());
0799: tx.commit();
0800: }
0801:
0802: public void testMemberListDependentObjects() throws Exception {
0803: // Drop
0804: dropTables("listholder");
0805: // Create with many independent objects
0806: ListHolder holder = new ListHolder();
0807: holder.setList(new Vector());
0808: holder.getList().add(new Referrer(1));
0809: holder.getList().add(new Referrer(2));
0810: holder.getList().add(new ReferrerSubclass(3, 3));
0811: store.save(holder);
0812: // Now check
0813: Transaction tx = store.getTransactionTracker().getTransaction(
0814: TransactionTracker.TX_REQUIRED);
0815: tx.begin();
0816: ListHolder dbHolder = (ListHolder) store
0817: .findSingle("find listholder");
0818: // Check stats: 1 holder, +1 statement
0819: assertEquals(3, dbHolder.getList().size());
0820: assertEquals(2, tx.getStats().getSelectCount());
0821: tx.commit();
0822: }
0823:
0824: public void testMemberListSingleClassObjects() throws Exception {
0825: // Drop
0826: dropTables("listholder");
0827: // Create with many independent objects
0828: ListHolder holder = new ListHolder();
0829: holder.setList(new Vector());
0830: holder.getList().add(new Referrer(1));
0831: holder.getList().add(new Referrer(2));
0832: holder.getList().add(new Referrer(3));
0833: store.save(holder);
0834: // Now check
0835: Transaction tx = store.getTransactionTracker().getTransaction(
0836: TransactionTracker.TX_REQUIRED);
0837: tx.begin();
0838: ListHolder dbHolder = (ListHolder) store
0839: .findSingle("find listholder");
0840: // Check stats: 1 holder, +1 statement
0841: assertEquals(3, dbHolder.getList().size());
0842: assertEquals(2, tx.getStats().getSelectCount());
0843: tx.commit();
0844: }
0845:
0846: public void testMemberListAddSuperclassObjects() throws Exception {
0847: // Drop
0848: dropTables("listholder");
0849: // Create with many independent objects
0850: ListHolder holder = new ListHolder();
0851: holder.setList(new Vector());
0852: holder.getList().add(new ReferrerSubclass(1, 1));
0853: holder.getList().add(new ReferrerSubclass(2, 2));
0854: holder.getList().add(new ReferrerSubclass(3, 3));
0855: store.save(holder);
0856: // Now check
0857: Transaction tx = store.getTransactionTracker().getTransaction(
0858: TransactionTracker.TX_REQUIRED);
0859: tx.begin();
0860: ListHolder dbHolder = (ListHolder) store
0861: .findSingle("find listholder");
0862: // Check stats: 1 holder, +1 statement
0863: assertEquals(3, dbHolder.getList().size());
0864: assertEquals(2, tx.getStats().getSelectCount());
0865: tx.commit();
0866: // Now insert superclass
0867: dbHolder.getList().add(new Referrer(4));
0868: store.save(dbHolder);
0869: // Now check
0870: tx = store.getTransactionTracker().getTransaction(
0871: TransactionTracker.TX_REQUIRED);
0872: tx.begin();
0873: dbHolder = (ListHolder) store.findSingle("find listholder");
0874: // Check stats: 1 holder, +1 statement
0875: assertEquals(4, dbHolder.getList().size());
0876: assertEquals(2, tx.getStats().getSelectCount());
0877: tx.commit();
0878: }
0879:
0880: public void testMemberListAddSubclassObjects() throws Exception {
0881: // Drop
0882: dropTables("listholder");
0883: // Create with many independent objects
0884: ListHolder holder = new ListHolder();
0885: holder.setList(new Vector());
0886: holder.getList().add(new Referrer(1));
0887: holder.getList().add(new Referrer(2));
0888: holder.getList().add(new Referrer(3));
0889: store.save(holder);
0890: // Now check
0891: Transaction tx = store.getTransactionTracker().getTransaction(
0892: TransactionTracker.TX_REQUIRED);
0893: tx.begin();
0894: ListHolder dbHolder = (ListHolder) store
0895: .findSingle("find listholder");
0896: // Check stats: 1 holder, +1 statement
0897: assertEquals(3, dbHolder.getList().size());
0898: assertEquals(2, tx.getStats().getSelectCount());
0899: tx.commit();
0900: // Now insert superclass
0901: dbHolder.getList().add(new ReferrerSubclass(4, 4));
0902: store.save(dbHolder);
0903: // Now check
0904: tx = store.getTransactionTracker().getTransaction(
0905: TransactionTracker.TX_REQUIRED);
0906: tx.begin();
0907: dbHolder = (ListHolder) store.findSingle("find listholder");
0908: // Check stats: 1 holder, +1 statement
0909: assertEquals(4, dbHolder.getList().size());
0910: assertEquals(2, tx.getStats().getSelectCount());
0911: tx.commit();
0912: }
0913:
0914: public void testMemberListAddRemoveIndependentObjects()
0915: throws Exception {
0916: // Drop
0917: dropTables("listholder");
0918: // Create with many independent objects
0919: ListHolder holder = new ListHolder();
0920: holder.setList(new Vector());
0921: holder.getList().add(new Referrer(1));
0922: holder.getList().add(new Referrer(2));
0923: holder.getList().add(new Referrer(3));
0924: store.save(holder);
0925: // Now check
0926: Transaction tx = store.getTransactionTracker().getTransaction(
0927: TransactionTracker.TX_REQUIRED);
0928: tx.begin();
0929: ListHolder dbHolder = (ListHolder) store
0930: .findSingle("find listholder");
0931: // Check stats: 1 holder, +1 statement
0932: assertEquals(3, dbHolder.getList().size());
0933: assertEquals(2, tx.getStats().getSelectCount());
0934: tx.commit();
0935: // Now insert independent
0936: Book book = new Book("Independent Object", "1");
0937: dbHolder.getList().add(book);
0938: store.save(dbHolder);
0939: // Now check
0940: tx = store.getTransactionTracker().getTransaction(
0941: TransactionTracker.TX_REQUIRED);
0942: tx.begin();
0943: dbHolder = (ListHolder) store.findSingle("find listholder");
0944: // Check stats: 1 holder, +2 statement
0945: assertEquals(4, dbHolder.getList().size());
0946: assertEquals(4, tx.getStats().getSelectCount());
0947: tx.commit();
0948: // Now remove indep object
0949: dbHolder.getList().remove(book);
0950: store.save(dbHolder);
0951: // Add same class
0952: dbHolder.getList().add(new Referrer(6));
0953: store.save(dbHolder);
0954: // Now check
0955: tx = store.getTransactionTracker().getTransaction(
0956: TransactionTracker.TX_REQUIRED);
0957: tx.begin();
0958: dbHolder = (ListHolder) store.findSingle("find listholder");
0959: // Check stats: 1 holder, +2 statement
0960: assertEquals(4, dbHolder.getList().size());
0961: assertEquals(3, tx.getStats().getSelectCount());
0962: tx.commit();
0963: }
0964:
0965: public void testReferencesLoading() throws Exception {
0966: // Drop
0967: dropTables("referrer");
0968: // Create a list of referrers which refer to a single
0969: // referrer
0970: Referrer ref = new Referrer(1);
0971: store.save(ref);
0972: Referrer refs[] = new Referrer[10];
0973: for (int i = 0; i < refs.length; i++) {
0974: refs[i] = new Referrer(10 + i);
0975: refs[i].setRef(ref);
0976: store.save(refs[i]);
0977: }
0978: // Get
0979: List result = store.find("find referrer");
0980: // Check
0981: assertEquals(11, result.size());
0982: for (int i = 0; i < result.size(); i++) {
0983: Referrer referrer = (Referrer) result.get(i);
0984: if (referrer.getIdentity() == 1)
0985: continue;
0986: assertEquals(ref, referrer.getRef());
0987: }
0988: }
0989:
0990: public void testReferencesLoadingWithBadHashCode() throws Exception {
0991: // Drop
0992: dropTables("badhashcode");
0993: // Create a list of badhashcodes which refer to a single
0994: // badhashcode
0995: BadHashCode ref = new BadHashCode(1);
0996: store.save(ref);
0997: BadHashCode refs[] = new BadHashCode[10];
0998: for (int i = 0; i < refs.length; i++) {
0999: refs[i] = new BadHashCode(10 + i);
1000: refs[i].setRef(ref);
1001: store.save(refs[i]);
1002: }
1003: // Get
1004: List result = store.find("find badhashcode");
1005: // Check
1006: assertEquals(11, result.size());
1007: for (int i = 0; i < result.size(); i++) {
1008: BadHashCode badhashcode = (BadHashCode) result.get(i);
1009: if (badhashcode.getIdentity() == 1)
1010: continue;
1011: assertNotNull(badhashcode.getRef());
1012: }
1013: }
1014:
1015: public void testListToString() throws Exception {
1016: dropTables("book");
1017: // Create
1018: Book book = new Book("Starship internals", "1-3-5-7");
1019: store.save(book);
1020: // Select ref1
1021: List result = store.find("find book");
1022: result.toString();
1023: }
1024:
1025: public void testSelectPrimitiveAttribute() throws Exception {
1026: dropTables("objectholder");
1027: // Create
1028: ObjectHolder holder = new ObjectHolder();
1029: holder.setObj(new StringBuffer("String Ni").toString());
1030: store.save(holder);
1031: // Select
1032: List result = store
1033: .find("find objectholder where obj = 'String Ni'");
1034: assertEquals(1, result.size());
1035: result = store.find("find objectholder where obj = ?",
1036: new Object[] { "String Ni" });
1037: assertEquals(1, result.size());
1038: }
1039:
1040: public void testSelectPrimitiveContainerItem() throws Exception {
1041: dropTables("setholder");
1042: // Create
1043: SetHolder holder = new SetHolder();
1044: holder.setSet(new HashSet());
1045: holder.getSet().add(new StringBuffer("String Ni").toString());
1046: store.save(holder);
1047: // Select
1048: List result = store
1049: .find("find setholder where set contains 'String Ni'");
1050: assertEquals(1, result.size());
1051: result = store.find("find setholder where set contains ?",
1052: new Object[] { "String Ni" });
1053: assertEquals(1, result.size());
1054: }
1055:
1056: public void testSearchWithId() throws Exception {
1057: // Create test setup
1058: dropTables("author");
1059: // Create
1060: Author author = new Author("Stephenson", "Neal");
1061: store.save(author);
1062: long id = author.getPersistenceId();
1063: // Select
1064: List result = null;
1065: result = store.find("find author where persistenceid = " + id);
1066: assertEquals(1, result.size());
1067: result = store.find("find author where persistenceid = ?",
1068: new Object[] { new Long(id) });
1069: assertEquals(1, result.size());
1070: result = store.find("find author where author.persistenceid = "
1071: + id);
1072: assertEquals(1, result.size());
1073: result = store.find(
1074: "find author where author.persistenceid = ?",
1075: new Object[] { new Long(id) });
1076: assertEquals(1, result.size());
1077: }
1078:
1079: public void testMixedSelect() throws Exception {
1080: // Create test setup
1081: dropTables("book");
1082: // Create
1083: Book book = new Book("Starship internals", "1-3-5-7");
1084: book.setMainAuthor(new Author("Geordi", "LaForge"));
1085: // Save
1086: store.save(book);
1087: // Select
1088: List result = store.find("find book,book.mainauthor.firstName");
1089: logger.debug("result is: " + result);
1090: assertEquals(1, result.size());
1091: assertEquals(book, ((Map) result.get(0)).get("object"));
1092: assertEquals("Geordi", ((Map) result.get(0)).get("firstName"));
1093: }
1094:
1095: public void testMixedSelectWithAlias() throws Exception {
1096: // Create test setup
1097: dropTables("book");
1098: // Create
1099: Book book = new Book("Starship internals", "1-3-5-7");
1100: book.setMainAuthor(new Author("Geordi", "LaForge"));
1101: // Save
1102: store.save(book);
1103: // Select
1104: List result = store
1105: .find("find book,book.mainauthor.firstName authorname");
1106: logger.debug("result is: " + result);
1107: assertEquals(1, result.size());
1108: assertEquals(book, ((Map) result.get(0)).get("object"));
1109: assertEquals("Geordi", ((Map) result.get(0)).get("authorname"));
1110: }
1111:
1112: public void testInOperatorWithList() throws Exception {
1113: // Create test setup
1114: dropTables("book");
1115: dropTables("author");
1116: // Create
1117: Author author1 = new Author("Geordi", "LaForge");
1118: Author author2 = new Author("Data", "");
1119: Author author3 = new Author("Scott", "Montgomery");
1120: Book book = new Book("Starship internals", "1-3-5-7");
1121: book.setMainAuthor(author1);
1122: // Save
1123: store.save(book);
1124: store.save(author1);
1125: store.save(author2);
1126: store.save(author3);
1127: // Create list
1128: Vector authorList = new Vector();
1129: authorList.add(author1);
1130: authorList.add(author2);
1131: authorList.add(author3);
1132: // Select
1133: List result = store.find(
1134: "find book where book.mainauthor in ?",
1135: new Object[] { authorList });
1136: logger.debug("result is: " + result);
1137: assertEquals(1, result.size());
1138: }
1139:
1140: public void testSubclassSelects() throws Exception {
1141: // Drop
1142: dropTables("superclass");
1143: // Create
1144: store.save(new Subclass());
1145: // Select
1146: List result = store.find("view superclass.primitive");
1147: for (int i = 0; i < result.size(); i++)
1148: result.get(i);
1149: }
1150: }
|