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.Vector;
021: import java.util.Date;
022: import java.util.HashMap;
023: import java.util.Collections;
024: import org.apache.log4j.Logger;
025:
026: /**
027: * Test the historical functions if library.
028: * @author Brautigam Robert
029: * @version Revision: $Revision$
030: */
031: public class HistoricalTests extends AbstractPersistenceTest {
032: private static Logger logger = Logger
033: .getLogger(HistoricalTests.class);
034:
035: public HistoricalTests(String name) throws Exception {
036: super (name);
037: }
038:
039: public void testPrimitiveHistory() throws Exception {
040: // Drop
041: dropTables("primitives");
042: // Create
043: Primitives p = new Primitives();
044: p.minimize();
045: store.save(p);
046: Date minimumDate = new Date();
047: synchronized (this ) {
048: // We must wait so the modification does not
049: // commit in the same millisecond as minimumdate
050: wait(100);
051: }
052: // Update entry
053: p.maximize();
054: store.save(p);
055: // Get back
056: List primitives = store.find("find primitives at ?",
057: new Object[] { minimumDate });
058: // Check stuff
059: assertEquals(1, primitives.size());
060: p.minimize();
061: assertEquals(p, primitives.get(0));
062: }
063:
064: public void testMemberLists() throws Exception {
065: // Drop
066: dropTables("book");
067: dropTables("author");
068: // Create
069: Book book = new Book("Starship internals", "1-3-5-7");
070: Vector authors1 = new Vector();
071: authors1.add(new Author("Geordi", "LaForge"));
072: authors1.add(new Author("Data", ""));
073: authors1.add(new Author("Scott", "Montgomery"));
074: book.setAuthors(authors1);
075: // Save
076: store.save(book);
077: Date firstDate = new Date();
078: synchronized (this ) {
079: // We must wait so the modification does not
080: // commit in the same millisecond as minimumdate
081: wait(100);
082: }
083: // Modify
084: Vector authors2 = new Vector();
085: authors2.add(new Author("Torres", "B'Elanna")); // Ok, I had to actually look this up, who watches Voyager anyway..
086: book.setAuthors(authors2);
087: store.save(book);
088: // Recall
089: List result = store.find("find book at ?",
090: new Object[] { firstDate });
091: // Check
092: assertEquals(1, result.size());
093: Book resultBook = (Book) result.get(0);
094: Collections.sort(authors1);
095: Collections.sort(authors2);
096: Collections.sort(resultBook.getAuthors());
097: logger.debug("authors1 list is: " + authors1);
098: logger.debug("authors2 list is: " + authors2);
099: logger.debug("result authors list is: "
100: + resultBook.getAuthors());
101: assertEquals(authors1, resultBook.getAuthors());
102: assertFalse(authors2.equals(resultBook.getAuthors()));
103: }
104:
105: public void testMemberMaps() throws Exception {
106: // Drop
107: dropTables("mapholder");
108: // Create
109: MapHolder mapHolder = new MapHolder();
110: Book book = new Book("Book of Bokonon", "9");
111: HashMap meta1 = new HashMap();
112: meta1.put("meta1", new Author("Geordi", "LaForge"));
113: meta1.put("meta2", new Author("Data", ""));
114: meta1.put("meta3", new Author("Scott", "Montgomery"));
115: meta1.put("book", book);
116: mapHolder.setMeta(meta1);
117: // Save
118: store.save(mapHolder);
119: Date firstDate = new Date();
120: synchronized (this ) {
121: // We must wait so the modification does not
122: // commit in the same millisecond as minimumdate
123: wait(100);
124: }
125: // Alter
126: HashMap meta2 = new HashMap();
127: meta2.put("meta3", new Author("Scott", "Montgomery"));
128: meta2.put("book", book);
129: mapHolder.setMeta(meta2);
130: store.save(mapHolder);
131: // Recall
132: List result = store.find("find mapholder at ?",
133: new Object[] { firstDate });
134: // Check
135: assertEquals(1, result.size());
136: MapHolder resultHolder = (MapHolder) result.get(0);
137: assertEquals(meta1, resultHolder.getMeta());
138: assertFalse(meta2.equals(resultHolder.getMeta()));
139: }
140:
141: public void testConcurrentModification() throws Exception {
142: // Drop
143: dropTables("referrer");
144: // Create referrers
145: Referrer refs[] = new Referrer[100];
146: for (int i = 0; i < 100; i++) {
147: refs[i] = new Referrer(i);
148: store.save(refs[i]);
149: }
150: // Select
151: List result = store.find("find referrer order by identity asc");
152: // Iterate, and delete
153: for (int i = 0; i < 100; i++) {
154: store.remove(refs[i]);
155: assertEquals(refs[i], result.get(i));
156: }
157: }
158:
159: public void testSelectPolymorphicObjectsInPast() throws Exception {
160: // Drop
161: dropTables("referrer");
162: // Create referrers
163: ReferrerSubclass ref = new ReferrerSubclass(1);
164: store.save(ref);
165: Date currentDate = new Date();
166: // Select
167: List result = store.find("find referrer at ?",
168: new Object[] { currentDate });
169: assertEquals(1, result.size());
170: assertEquals(ref, result.get(0));
171: }
172:
173: public void testNonExistingObjectMetaData() throws Exception {
174: // Drop
175: dropTables("author");
176: // Nonexisting object
177: Author author = new Author("Johnny", "Cash");
178: PersistenceMetaData metaData = store
179: .getPersistenceMetaData(author);
180: assertFalse(metaData.getPersistenceId() == null);
181: assertTrue(metaData.getPersistenceStart() == null);
182: assertTrue(metaData.getPersistenceEnd() == null);
183: }
184:
185: public void testExistingObjectMetaData() throws Exception {
186: // Drop
187: dropTables("author");
188: // Create
189: Author author = new Author("Johnny", "Cash");
190: store.save(author);
191: // Check metadata
192: PersistenceMetaData metaData = store
193: .getPersistenceMetaData(author);
194: assertEquals(new Long(author.getPersistenceId()), metaData
195: .getPersistenceId());
196: assertFalse(metaData.getPersistenceStart() == null);
197: assertTrue(metaData.getPersistenceEnd() == null);
198: }
199:
200: public void testSavedObjectMetaData() throws Exception {
201: // Drop
202: dropTables("author");
203: // Create
204: Author author = new Author("Johnny", "Cash");
205: store.save(author);
206: // Check metadata
207: PersistenceMetaData metaData = store
208: .getPersistenceMetaData(author);
209: Long id = metaData.getPersistenceId();
210: Long start = metaData.getPersistenceStart();
211: Long end = metaData.getPersistenceEnd();
212: // Save again
213: author.setFirstName("John");
214: store.save(author);
215: // Check
216: assertFalse(start == null);
217: assertTrue(end == null);
218: assertEquals(id, metaData.getPersistenceId());
219: assertTrue(metaData.getPersistenceStart().longValue() > start
220: .longValue());
221: assertTrue(metaData.getPersistenceEnd() == null);
222: }
223:
224: public void testRemovedObjectMetaData() throws Exception {
225: // Drop
226: dropTables("author");
227: // Create
228: Author author = new Author("Johnny", "Cash");
229: store.save(author);
230: // Check metadata
231: PersistenceMetaData metaData = store
232: .getPersistenceMetaData(author);
233: Long id = metaData.getPersistenceId();
234: Long start = metaData.getPersistenceStart();
235: Long end = metaData.getPersistenceEnd();
236: // Remove
237: store.remove(author);
238: // Check
239: assertFalse(start == null);
240: assertTrue(end == null);
241: assertEquals(id, metaData.getPersistenceId());
242: assertTrue(metaData.getPersistenceStart().longValue() == start
243: .longValue());
244: assertFalse(metaData.getPersistenceEnd() == null);
245: }
246:
247: }
|