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: * Test on polymorphic selects and saves.
024: * @author Brautigam Robert
025: * @version Revision: $Revision$
026: */
027: public class PolymorphicTests extends AbstractPersistenceTest {
028: private static Logger logger = Logger
029: .getLogger(PolymorphicTests.class);
030:
031: public PolymorphicTests(String name) throws Exception {
032: super (name);
033: }
034:
035: private int countClass(Class clazz, List list) {
036: int result = 0;
037: for (int i = 0; i < list.size(); i++)
038: if (list.get(i).getClass().equals(clazz))
039: result++;
040: return result;
041: }
042:
043: public void testSimplePolymorphicObjectRetrieve() throws Exception {
044: // Drop table
045: dropTables("article");
046: dropTables("writing");
047: dropTables("screenplay");
048: dropTables("moviescript");
049:
050: // Save
051: Article article = new Article("Persistence", "DDJ");
052: store.save(article);
053:
054: // Select
055: List result = store.find("find article");
056: assertEquals(1, result.size());
057: assertEquals(article, result.get(0));
058:
059: // Select superclass
060: result = store.find("find writing");
061: assertEquals(1, result.size());
062: assertEquals(article, result.get(0));
063: }
064:
065: public void testMultipleObjectSelect() throws Exception {
066: // Drop book table
067: dropTables("article");
068: dropTables("writing");
069: dropTables("screenplay");
070: dropTables("moviescript");
071:
072: // Save
073: for (int i = 0; i < 10; i++)
074: store.save(new Article("Article #" + i, "Art"));
075: for (int i = 0; i < 10; i++)
076: store.save(new ScreenPlay("Play #" + i, i));
077: for (int i = 0; i < 10; i++)
078: store.save(new MovieScript("Script #" + i, i, i));
079:
080: // Select simple
081: List result = store.find("find writing");
082: assertEquals(30, result.size());
083: assertEquals(10, countClass(Article.class, result));
084: assertEquals(10, countClass(ScreenPlay.class, result));
085: assertEquals(10, countClass(MovieScript.class, result));
086:
087: // Select with a condition
088: result = store.find("find writing where title='Article #5'");
089: assertEquals(1, result.size());
090: assertEquals(Article.class, result.get(0).getClass());
091:
092: // Select subclass only
093: result = store.find("find article");
094: assertEquals(10, result.size());
095: assertEquals(10, countClass(Article.class, result));
096:
097: // Select subclass only
098: result = store.find("find screenplay");
099: assertEquals(20, result.size());
100: assertEquals(10, countClass(MovieScript.class, result));
101: assertEquals(10, countClass(ScreenPlay.class, result));
102:
103: // Select subclass only
104: result = store.find("find moviescript");
105: assertEquals(10, result.size());
106: assertEquals(10, countClass(MovieScript.class, result));
107: }
108:
109: public void testBatchObjectSelect() throws Exception {
110: // Drop book table
111: dropTables("article");
112: dropTables("writing");
113: dropTables("screenplay");
114: dropTables("moviescript");
115:
116: // Save
117: for (int i = 0; i < 100; i++)
118: store.save(new Article("Article #" + i, "Art"));
119:
120: // Select simple
121: List result = store.find("find writing");
122: assertEquals(100, result.size());
123: assertEquals(100, countClass(Article.class, result));
124: assertEquals(0, countClass(ScreenPlay.class, result));
125: assertEquals(0, countClass(MovieScript.class, result));
126: }
127:
128: public void testBatchListAttributeSelect() throws Exception {
129: // Drop book table
130: dropTables("article");
131: dropTables("writing");
132: dropTables("screenplay");
133: dropTables("moviescript");
134: dropTables("articlecontainer");
135:
136: // Save
137: ArticleContainer container = new ArticleContainer();
138: for (int i = 0; i < 100; i++)
139: container.getArticles().add(
140: new Article("Article #" + i, "Art"));
141: store.save(container);
142:
143: // Select simple
144: List result = store.find("find articlecontainer");
145: assertEquals(1, result.size());
146: ArticleContainer resultContainer = (ArticleContainer) result
147: .get(0);
148: List articles = resultContainer.getArticles();
149: assertEquals(100, articles.size());
150: assertEquals(100, countClass(Article.class, articles));
151: assertEquals(0, countClass(ScreenPlay.class, articles));
152: assertEquals(0, countClass(MovieScript.class, articles));
153: }
154:
155: public void testSuperclassAttributeSelectUnspecified()
156: throws Exception {
157: // Delete
158: dropTables("referrersubclass");
159:
160: // Insert one
161: store.save(new ReferrerSubclass(1));
162:
163: // Try to select with superclass attribute
164: List result = store
165: .find("find referrersubclass where identity=1");
166: assertEquals(1, result.size());
167: }
168:
169: public void testSuperclassAttributeInWhere() throws Exception {
170: // Delete
171: dropTables("referrersubclass");
172: dropTables("identitystuff");
173:
174: // Insert one
175: store.save(new IdentityStuff(1));
176: store.save(new ReferrerSubclass(1));
177:
178: // Try to select with superclass attribute
179: List result = store
180: .find("find identitystuff where identitystuff.identity=referrersubclass.identity");
181: assertEquals(1, result.size());
182: }
183:
184: public void testSuperclassAttributeSelect() throws Exception {
185: // Delete
186: dropTables("referrersubclass");
187:
188: // Insert one
189: store.save(new ReferrerSubclass(1));
190:
191: // Try to select with superclass attribute
192: List result = store
193: .find("find referrersubclass where referrersubclass.identity=1");
194: assertEquals(1, result.size());
195: }
196:
197: public void testSuperclassObjectSelect() throws Exception {
198: // Delete
199: dropTables("subclass");
200: dropTables("superclass");
201:
202: // Insert
203: Subclass s = new Subclass();
204: s.setObject(s);
205: store.save(s);
206:
207: // Select
208: List result = store.find("find subclass where object = ?",
209: new Object[] { s });
210: assertEquals(1, result.size());
211: }
212:
213: public void testSuperclassMapSelect() throws Exception {
214: // Delete
215: dropTables("subclass");
216: dropTables("superclass");
217:
218: // Insert
219: Subclass s = new Subclass();
220: Subclass c = new Subclass();
221: Vector list = new Vector();
222: list.add(c);
223: s.setList(list);
224: store.save(s);
225:
226: // Select
227: List result = store.find("find subclass where list contains ?",
228: new Object[] { c });
229: assertEquals(1, result.size());
230: }
231:
232: public void testSuperclassListSelect() throws Exception {
233: // Delete
234: dropTables("subclass");
235: dropTables("superclass");
236:
237: // Insert
238: Subclass s = new Subclass();
239: Subclass c = new Subclass();
240: HashMap map = new HashMap();
241: map.put("contain", c);
242: s.setMap(map);
243: store.save(s);
244:
245: // Select
246: List result = store.find("find subclass where map contains ?",
247: new Object[] { c });
248: assertEquals(1, result.size());
249: }
250:
251: public void testSameAttribute() throws Exception {
252: dropTables("sameattrsuper");
253: dropTables("sameattrsub1");
254: dropTables("sameattrsub2");
255:
256: // Create two objects with same attributes
257: SameAttrSub1 sub1 = new SameAttrSub1(1, "aaa");
258: SameAttrSub2 sub2 = new SameAttrSub2(2, "bbb");
259: // Save
260: store.save(sub1);
261: // Find
262: try {
263: store.save(sub2);
264: fail("store had two columns with same name on classes with a common ancestor, it should throw exception, but did not.");
265: } catch (StoreException e) {
266: // This is good, query was invalid
267: }
268: }
269:
270: public void testPolymorphicListSelect() throws Exception {
271: // Drop book table
272: dropTables("article");
273: dropTables("writing");
274: dropTables("screenplay");
275: dropTables("moviescript");
276: dropTables("articlecontainer");
277:
278: // Save
279: ArticleContainer container = new ArticleContainer();
280: container.getArticles().add(new Article("Article #1", "Art"));
281: container.getArticles().add(new ScreenPlay("Title", 3));
282: container.getArticles().add(new Writing("Writing"));
283: store.save(container);
284:
285: // Select simple
286: List result = store.find("find articlecontainer");
287: assertEquals(1, result.size());
288: ArticleContainer resultContainer = (ArticleContainer) result
289: .get(0);
290: List articles = resultContainer.getArticles();
291: assertEquals(3, articles.size());
292: assertEquals(1, countClass(Article.class, articles));
293: assertEquals(1, countClass(ScreenPlay.class, articles));
294: assertEquals(1, countClass(Writing.class, articles));
295: }
296:
297: public void testExtremePolymorphismConcept() throws Exception {
298: // Drop all tables
299: dropTables("book");
300: dropTables("referrer");
301: dropTables("author");
302: dropTables("ids");
303: dropTables("tablemap");
304: dropTables("classes");
305: // New store
306: tearDown();
307: setUp();
308: // Insert a list of different objects into the store
309: Book book1 = new Book("Title 1", "1-2-3-4");
310: store.save(book1);
311: Book book2 = new Book("Title 2", "1-2-3-4");
312: store.save(book2);
313: Referrer ref1 = new Referrer(1);
314: store.save(ref1);
315: ReferrerSubclass refsub1 = new ReferrerSubclass(2, 2);
316: store.save(refsub1);
317: Author author1 = new Author("X", "Y");
318: store.save(author1);
319: // Query
320: List result = store.find("find object");
321: // Check
322: assertEquals(5, result.size());
323: assertTrue(result.contains(book1));
324: assertTrue(result.contains(book2));
325: assertTrue(result.contains(ref1));
326: assertTrue(result.contains(refsub1));
327: assertTrue(result.contains(author1));
328: }
329:
330: public void testNonStorableSelect() throws Exception {
331: // Drop all tables
332: dropTables("book");
333: dropTables("referrer");
334: dropTables("author");
335: dropTables("ids");
336: dropTables("tablemap");
337: dropTables("classes");
338: // New store
339: tearDown();
340: setUp();
341: // Insert a list of different objects into the store
342: Book book1 = new Book("Title 1", "1-2-3-4");
343: store.save(book1);
344: Book book2 = new Book("Title 2", "1-2-3-4");
345: store.save(book2);
346: Referrer ref1 = new Referrer(1);
347: store.save(ref1);
348: ReferrerSubclass refsub1 = new ReferrerSubclass(2, 2);
349: store.save(refsub1);
350: Author author1 = new Author("X", "Y");
351: store.save(author1);
352: // Create a list with only the books
353: ListHolder holder = new ListHolder();
354: Vector list = new Vector();
355: list.add(book1);
356: list.add(book2);
357: list.add(ref1);
358: holder.setList(list);
359: store.save(holder);
360: // Query
361: Transaction tx = store.getTransactionTracker().getTransaction(
362: TransactionTracker.TX_REQUIRED);
363: tx.begin();
364: List result = store
365: .find(
366: "find item(object) where listholder.list contains item and listholder = ?",
367: new Object[] { holder });
368: DatabaseStatistics stats = new DatabaseStatistics();
369: result.size(); // Force load
370: stats.add(tx.getStats());
371: tx.commit();
372: // Check
373: logger.debug("test result: " + result);
374: Iterator i = result.iterator();
375: while (i.hasNext())
376: logger.debug("test result it: " + i.next());
377: assertEquals(3, stats.getSelectCount());
378: assertEquals(3, result.size());
379: assertTrue(result.contains(book1));
380: assertTrue(result.contains(book2));
381: assertTrue(result.contains(ref1));
382: }
383:
384: public void testManyLeftTerms() throws Exception {
385: // Drop
386: dropTables("manysuper");
387: // Create a lot of manysuper classes
388: for (int i = 0; i < 50; i++) {
389: ManySuper obj = new ManySuper();
390: obj.setPersistenceDynamicName("ManySub" + i);
391: obj.put("index", new Integer(i));
392: store.save(obj);
393: }
394: // Now save a few other subclasses
395: for (int i = 0; i < 10; i++) {
396: ManySuper obj = new ManySuper();
397: obj.setPersistenceDynamicName("ManySubExtra");
398: obj.put("index", new Integer(100 + i));
399: store.save(obj);
400: }
401: // Now select all manysupers above index, which would result
402: // in a select with many left tables
403: List result = store.find("find manysuper where index >= 100");
404: assertEquals(10, result.size());
405: }
406:
407: public void testNonStorableMiddleTerm() throws Exception {
408: // Create test setup
409: dropTables("book");
410: // Create
411: Book book1 = new Book("Starship internals", "1-3-5-7");
412: Vector authors = new Vector();
413: authors.add(new Author("Geordi", "LaForge"));
414: Author data = new Author("Data", "");
415: authors.add(data);
416: authors.add(new Author("Scott", "Montgomery"));
417: book1.setAuthors(authors);
418: store.save(book1);
419: Book book2 = new Book("I, Robot", "3-3-5-7");
420: authors = new Vector();
421: authors.add(new Author("Spock", ""));
422: authors.add(data);
423: book2.setAuthors(authors);
424: store.save(book2);
425: // Make a select which uses a temporary non-storable term
426: List result = store
427: .find(
428: "find book where book.authors contains item(object) and originalbook(book).authors contains item and originalbook = ?",
429: new Object[] { book2 });
430: assertEquals(2, result.size());
431: }
432:
433: public void testSubclassAttributeSelect() throws Exception {
434: // Delete
435: dropTables("subclass");
436: dropTables("superclass");
437: // Insert a few objects
438: Superclass sup1 = new Superclass(1);
439: Superclass sup2 = new Superclass(2);
440: Superclass sup3 = new Superclass(3);
441: store.save(sup1);
442: store.save(sup2);
443: store.save(sup3);
444: Subclass s1 = new Subclass();
445: s1.setPrimitive(5);
446: s1.setSubint(1);
447: s1.setObject(sup1);
448: store.save(s1);
449: Subclass s2 = new Subclass();
450: s2.setPrimitive(6);
451: s2.setSubint(2);
452: s2.setObject(sup2);
453: store.save(s2);
454: // Select
455: List result = store
456: .find("find superclass where subclass.object = superclass and subclass.subint = 1");
457: assertEquals(1, result.size());
458: }
459:
460: public void testCommonInterfaceclassSameAttributes()
461: throws Exception {
462: // Delete
463: dropTables("car");
464: dropTables("vehicle");
465: // Try to save both
466: store.save(new Car());
467: store.save(new Truck());
468: }
469:
470: }
|