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.List;
020: import java.util.Map;
021:
022: /**
023: * Class tests the view capability of library.
024: * @author Brautigam Robert
025: * @version Revision: $Revision$
026: */
027: public class ViewTests extends AbstractPersistenceTest {
028: public ViewTests(String name) throws Exception {
029: super (name);
030: }
031:
032: public void testNonExistentTable() throws Exception {
033: // Test
034: List result = store.find("view nonexist.self");
035: assertEquals(0, result.size());
036: }
037:
038: public void testViewSimple1() throws Exception {
039: // Drop table
040: dropTables("book");
041: // Insert two books
042: store.save(new Book("Title1", ""));
043: store.save(new Book("Title2", ""));
044: // Get view of titles
045: List result = store
046: .find("view book.title order by book.title asc");
047: // Check
048: assertEquals(2, result.size());
049: for (int i = 0; i < 2; i++) {
050: Map value = (Map) result.get(i);
051: assertEquals("Title" + (i + 1), value.get("title")
052: .toString());
053: }
054: }
055:
056: public void testViewSimple2() throws Exception {
057: // Drop table
058: dropTables("book");
059: // Insert two books
060: store.save(new Book("Title1", "12341"));
061: store.save(new Book("Title2", "12342"));
062: // Get view of titles
063: List result = store
064: .find("view book.title,book.isbn order by book.title asc");
065: // Check
066: assertEquals(2, result.size());
067: for (int i = 0; i < 2; i++) {
068: Map value = (Map) result.get(i);
069: assertEquals("Title" + (i + 1), value.get("title")
070: .toString());
071: assertEquals("1234" + (i + 1), value.get("isbn").toString());
072: }
073: }
074:
075: public void testViewMoreTables() throws Exception {
076: // Drop table
077: dropTables("book");
078: dropTables("author");
079: // Insert two books
080: Book book1 = new Book("Java For Dummies", "51966");
081: book1.setMainAuthor(new Author("Ada", "Lovelace"));
082: store.save(book1);
083: store.save(new Author("Bjarne", "Stroustrup"));
084: // Get view of titles
085: List result = store
086: .find("view book.title,author.firstname where book.mainauthor=author");
087: // Check
088: assertEquals(1, result.size());
089: Map value = (Map) result.get(0);
090: assertEquals("Java For Dummies", value.get("title").toString());
091: assertEquals("Ada", value.get("firstname").toString());
092: }
093:
094: public void testViewComplexAttribute() throws Exception {
095: // Drop table
096: dropTables("book");
097: dropTables("author");
098: // Insert two books
099: Book book1 = new Book("Java For Dummies", "51966");
100: book1.setMainAuthor(new Author("Ada", "Lovelace"));
101: store.save(book1);
102: store.save(new Author("Bjarne", "Stroustrup"));
103: // Get view of titles
104: List result = store
105: .find("view book.title,book.mainauthor.firstname");
106: // Check
107: assertEquals(1, result.size());
108: Map value = (Map) result.get(0);
109: assertEquals("Java For Dummies", value.get("title").toString());
110: assertEquals("Ada", value.get("firstname").toString());
111: }
112:
113: public void testViewComplexAttributeWithAliases() throws Exception {
114: // Drop table
115: dropTables("book");
116: dropTables("author");
117: // Insert two books
118: Book book1 = new Book("Java For Dummies", "51966");
119: book1.setMainAuthor(new Author("Ada", "Lovelace"));
120: store.save(book1);
121: store.save(new Author("Bjarne", "Stroustrup"));
122: // Get view of titles
123: List result = store
124: .find("view book.title a,book.mainauthor.firstname b");
125: // Check
126: assertEquals(1, result.size());
127: Map value = (Map) result.get(0);
128: assertEquals("Java For Dummies", value.get("a").toString());
129: assertEquals("Ada", value.get("b").toString());
130: }
131:
132: public void testTargetSubclass() throws Exception {
133: // Drop tables
134: dropTables("referrer");
135: dropTables("referrersubclass");
136: // Insert stuff
137: store.save(new ReferrerSubclass(1, 1));
138: store.save(new Referrer(2));
139: // Select
140: List result = store
141: .find("view referrersubclass.subidentity,referrersubclass.identity order by referrersubclass.identity");
142: // Check
143: assertEquals(1, result.size());
144: Map map1 = (Map) result.get(0);
145: assertEquals("" + 1, map1.get("identity").toString());
146: assertEquals("" + 1, map1.get("subidentity").toString());
147: }
148:
149: public void testTargetSubclassSuperclassFirst() throws Exception {
150: // Drop tables
151: dropTables("referrer");
152: dropTables("referrersubclass");
153: // Insert stuff
154: store.save(new ReferrerSubclass(1, 1));
155: store.save(new Referrer(2));
156: // Select
157: List result = store
158: .find("view referrersubclass.identity,referrersubclass.subidentity order by referrersubclass.identity");
159: // Check
160: assertEquals(1, result.size());
161: Map map1 = (Map) result.get(0);
162: assertEquals("" + 1, map1.get("identity").toString());
163: assertEquals("" + 1, map1.get("subidentity").toString());
164: }
165:
166: public void testOrderByNonExistentAttribute() throws Exception {
167: // Drop table
168: dropTables("book");
169: // Insert two books
170: store.save(new Book("Title1", ""));
171: store.save(new Book("Title2", ""));
172: // Get view of titles
173: List result = store
174: .find("view book.isbn order by book.title asc");
175: // Check
176: assertEquals(2, result.size());
177: }
178:
179: public void testKeywordAttribute() throws Exception {
180: // Drop table
181: dropTables("nametester");
182: // Create
183: NameTester n = new NameTester();
184: n.setSelect("OK");
185: store.save(n);
186: // Select
187: List result = store.find("view nametester.select");
188: assertEquals(1, result.size());
189: assertEquals("OK", ((Map) result.get(0)).get("select"));
190: }
191:
192: public void testUnspecifiedAttribute() throws Exception {
193: // Drop table
194: dropTables("book");
195: // Insert book
196: store.save(new Book("Title", "Isbn"));
197: // Get view
198: List result = store.find("view book.title,isbn");
199: // Check
200: assertEquals(0, result.size());
201: }
202:
203: public void testSimilarNamedAttributes() throws Exception {
204: // Drop table
205: dropTables("book");
206: // Insert books
207: store.save(new Book("Title 1", "ISBN"));
208: store.save(new Book("Title 2", "ISBN"));
209: store.save(new Book("Title 3", "NONE"));
210: // Get joined view of titles
211: List result = store
212: .find("view book1(book).title a, book2(book).title b where book1.isbn=book2.isbn and book1.title<>book2.title order by book1.title asc");
213: // Check
214: assertEquals(2, result.size());
215: Map value1 = (Map) result.get(0);
216: assertEquals("Title 1", value1.get("a"));
217: assertEquals("Title 2", value1.get("b"));
218: Map value2 = (Map) result.get(1);
219: assertEquals("Title 2", value2.get("a"));
220: assertEquals("Title 1", value2.get("b"));
221: }
222:
223: }
|