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.Date;
020: import java.util.Calendar;
021: import java.util.List;
022: import java.util.Vector;
023: import java.util.HashMap;
024: import org.apache.log4j.Logger;
025:
026: /**
027: * Tests dynamic object, which can dynamically alter it's attribute
028: * set runtime.
029: * @author Brautigam Robert
030: * @version Revision: $Revision$
031: */
032: public class DynamicObjectTests extends AbstractPersistenceTest {
033: private static Logger logger = Logger
034: .getLogger(DynamicObjectTests.class);
035:
036: public DynamicObjectTests(String name) throws Exception {
037: super (name);
038: }
039:
040: public void testConcept() throws Exception {
041: // Clear
042: dropTables("dynamicobjectimpl");
043: DynamicObjectImpl.init();
044: // Create dynamic object, and save it
045: DynamicObjectImpl obj = new DynamicObjectImpl();
046: obj.put("index", new Integer(1));
047: obj.put("name", "Jim");
048: obj.put("male", Boolean.TRUE);
049: store.save(obj);
050: // Select and check
051: DynamicObjectImpl result = (DynamicObjectImpl) store
052: .findSingle("find dynamicobjectimpl");
053: assertEquals(obj, result);
054: }
055:
056: public void testAddAttributeRuntime() throws Exception {
057: // Clear
058: dropTables("dynamicobjectimpl");
059: DynamicObjectImpl.init();
060: // Create dynamic object, and save it
061: DynamicObjectImpl obj = new DynamicObjectImpl();
062: obj.put("index", new Integer(1));
063: obj.put("name", "Jim");
064: obj.put("male", Boolean.TRUE);
065: store.save(obj);
066: // Increase schema, and add again
067: DynamicObjectImpl.getPersistenceAttributeTypes(
068: DynamicObjectImpl.class, null)
069: .put("addr", String.class);
070: DynamicObjectImpl obj2 = new DynamicObjectImpl();
071: obj2.put("index", new Integer(2));
072: obj2.put("name", "Jack");
073: obj2.put("male", Boolean.TRUE);
074: obj2.put("addr", "Here");
075: store.save(obj2);
076: // Select and check
077: DynamicObjectImpl result = (DynamicObjectImpl) store
078: .findSingle("find dynamicobjectimpl where index=1");
079: obj.remove("addr");
080: assertEquals(obj, result);
081: result = (DynamicObjectImpl) store
082: .findSingle("find dynamicobjectimpl where index=2");
083: assertEquals(obj2, result);
084: }
085:
086: public void testRemoveAttributeRuntime() throws Exception {
087: // Clear
088: dropTables("dynamicobjectimpl");
089: DynamicObjectImpl.init();
090: // Create dynamic object, and save it
091: DynamicObjectImpl obj = new DynamicObjectImpl();
092: obj.put("index", new Integer(1));
093: obj.put("name", "Jim");
094: obj.put("male", Boolean.TRUE);
095: store.save(obj);
096: // Increase schema, and add again
097: DynamicObjectImpl.getPersistenceAttributeTypes(
098: DynamicObjectImpl.class, null).remove("male");
099: DynamicObjectImpl obj2 = new DynamicObjectImpl();
100: obj2.put("index", new Integer(2));
101: obj2.put("name", "Jack");
102: store.save(obj2);
103: // Select and check
104: DynamicObjectImpl result = (DynamicObjectImpl) store
105: .findSingle("find dynamicobjectimpl where index=1");
106: obj.remove("male");
107: assertEquals(obj, result);
108: result = (DynamicObjectImpl) store
109: .findSingle("find dynamicobjectimpl where index=2");
110: assertEquals(obj2, result);
111: }
112:
113: public void testDynamicTypes() throws Exception {
114: // Clear
115: dropTables("dynamicobjecttypes");
116: // Create dynamic object, and save it
117: DynamicObjectTypes obj = new DynamicObjectTypes();
118: obj.put("index", new Integer(1));
119: obj.put("name", "Jim");
120: obj.put("male", Boolean.TRUE);
121: Calendar cal = Calendar.getInstance();
122: cal.setTime(new Date());
123: cal.set(Calendar.MILLISECOND, 0);
124: Date date = cal.getTime();
125: obj.put("birthdate", date);
126: obj.put("ttl", new Long(123));
127: obj.put("initial", new Character('X'));
128: obj.put("iq", new Byte((byte) -1));
129: store.save(obj);
130: // Select and check
131: DynamicObjectTypes result = (DynamicObjectTypes) store
132: .findSingle("find dynamicobjecttypes");
133: logger.debug("birthdate class: "
134: + result.get("birthdate").getClass());
135: assertEquals(obj, result);
136: }
137:
138: public void testDynamicCaseInsensitive() throws Exception {
139: // Clear
140: dropTables("dynamicobjectcase");
141: // Create dynamic object, and save it
142: DynamicObjectCase obj = new DynamicObjectCase();
143: obj.put("IndeX", new Integer(1));
144: obj.put("Name", "Jim");
145: store.save(obj);
146: // Select and check
147: DynamicObjectCase result = (DynamicObjectCase) store
148: .findSingle("find dynamicobjectcase");
149: assertEquals(obj, result);
150: // Check case insensitive queries
151: result = (DynamicObjectCase) store
152: .findSingle("find dynamicobjectcase where index = 1");
153: assertEquals(obj, result);
154: result = (DynamicObjectCase) store
155: .findSingle("find dynamicobjectcase where INDEX = 1");
156: assertEquals(obj, result);
157: result = (DynamicObjectCase) store
158: .findSingle("find dynamicobjectcase where name = 'Jim'");
159: assertEquals(obj, result);
160: }
161:
162: public void testDynamicClassObjectWithNoName() throws Exception {
163: dropTables("dynamicobjectwithclass");
164: // Create object
165: DynamicObjectWithClass dyn = new DynamicObjectWithClass();
166: dyn.setPersistenceDynamicName(null);
167: dyn.put("index", new Integer(1));
168: dyn.put("name", "Jim");
169: dyn.put("male", Boolean.TRUE);
170: // Save
171: store.save(dyn);
172: // Select
173: DynamicObjectWithClass result = (DynamicObjectWithClass) store
174: .findSingle("find dynamicobjectwithclass");
175: assertEquals(dyn, result);
176: assertNull(dyn.getPersistenceDynamicName());
177: }
178:
179: public void testDynamicClassObjectWithName() throws Exception {
180: dropTables("carclass");
181: dropTables("dynamicobjectwithclass");
182: // Create object
183: DynamicObjectWithClass dyn = new DynamicObjectWithClass();
184: dyn.setPersistenceDynamicName("CarClass");
185: dyn.put("model", "Jaguar");
186: dyn.put("doors", new Integer(4));
187: dyn.put("index", new Integer(1));
188: dyn.put("name", "Jim");
189: dyn.put("male", Boolean.TRUE);
190: // Save
191: store.save(dyn);
192: // Select
193: DynamicObjectWithClass result = (DynamicObjectWithClass) store
194: .findSingle("find carclass");
195: assertEquals(dyn, result);
196: assertEquals("CarClass", dyn.getPersistenceDynamicName());
197: // Try selection for dynamic class
198: result = (DynamicObjectWithClass) store
199: .findSingle("find dynamicobjectwithclass");
200: assertEquals(dyn, result);
201: assertEquals("CarClass", dyn.getPersistenceDynamicName());
202: }
203:
204: public void testDynamicAttributeWithNoName() throws Exception {
205: dropTables("dynamicattributeobject");
206: // Create object
207: DynamicAttributeObject obj = new DynamicAttributeObject();
208: obj.setIndex(1);
209: DynamicObjectWithClass dyn = new DynamicObjectWithClass();
210: dyn.setPersistenceDynamicName(null);
211: dyn.put("index", new Integer(1));
212: dyn.put("name", "Jim");
213: dyn.put("male", Boolean.TRUE);
214: obj.setObj(dyn);
215: // Save
216: store.save(obj);
217: // Select
218: DynamicAttributeObject result = (DynamicAttributeObject) store
219: .findSingle("find dynamicattributeobject");
220: assertEquals(obj.getIndex(), result.getIndex());
221: assertNull(result.getObj().getPersistenceDynamicName());
222: assertEquals(dyn, result.getObj());
223: }
224:
225: public void testDynamicAttributeWithName() throws Exception {
226: dropTables("dynamicattributeobject");
227: // Create object
228: DynamicAttributeObject obj = new DynamicAttributeObject();
229: obj.setIndex(1);
230: DynamicObjectWithClass dyn = new DynamicObjectWithClass();
231: dyn.setPersistenceDynamicName("CarClass");
232: dyn.put("model", "Jaguar");
233: dyn.put("doors", new Integer(4));
234: dyn.put("index", new Integer(1));
235: dyn.put("name", "Jim");
236: dyn.put("male", Boolean.TRUE);
237: obj.setObj(dyn);
238: // Save
239: store.save(obj);
240: // Select
241: DynamicAttributeObject result = (DynamicAttributeObject) store
242: .findSingle("find dynamicattributeobject");
243: assertEquals(obj.getIndex(), result.getIndex());
244: assertEquals("CarClass", result.getObj()
245: .getPersistenceDynamicName());
246: assertEquals(dyn, result.getObj());
247: }
248:
249: public void testDynamicAttributeMultipleObjects() throws Exception {
250: dropTables("dynamicattributeobject");
251: // Create object
252: DynamicAttributeObject objs[] = new DynamicAttributeObject[10];
253: for (int i = 0; i < 10; i++) {
254: // Create object
255: DynamicAttributeObject obj = new DynamicAttributeObject();
256: objs[i] = obj;
257: obj.setIndex(i);
258: DynamicObjectWithClass dyn = new DynamicObjectWithClass();
259: obj.setObj(dyn);
260: if (i % 2 == 0) {
261: dyn.setPersistenceDynamicName("CarClass");
262: dyn.put("model", "Jaguar");
263: dyn.put("doors", new Integer(4));
264: dyn.put("index", new Integer(2));
265: dyn.put("name", "Jimbo");
266: dyn.put("male", Boolean.TRUE);
267: } else {
268: dyn.put("index", new Integer(1));
269: dyn.put("name", "Jim");
270: dyn.put("male", Boolean.TRUE);
271: }
272: // Save
273: store.save(obj);
274: }
275: // Select
276: List result = store
277: .find("find dynamicattributeobject order by index asc");
278: // Check
279: for (int i = 0; i < 10; i++)
280: assertEquals(objs[i].getObj(),
281: ((DynamicAttributeObject) result.get(i)).getObj());
282: }
283:
284: public void testDynamicListAttribute() throws Exception {
285: // Clear
286: dropTables("listholder");
287: // Make object
288: ListHolder obj = new ListHolder();
289: Vector list = new Vector();
290: DynamicObjectWithClass dyn1 = new DynamicObjectWithClass();
291: dyn1.setPersistenceDynamicName("CarClass");
292: dyn1.put("model", "Jaguar");
293: dyn1.put("doors", new Integer(4));
294: dyn1.put("index", new Integer(2));
295: dyn1.put("name", "Jimba");
296: dyn1.put("male", Boolean.FALSE);
297: DynamicObjectWithClass dyn2 = new DynamicObjectWithClass();
298: dyn2.setPersistenceDynamicName(null);
299: dyn2.put("index", new Integer(1));
300: dyn2.put("name", "Jim");
301: dyn2.put("male", Boolean.TRUE);
302: list.add(dyn1);
303: list.add(dyn2);
304: obj.setList(list);
305: // Insert
306: store.save(obj);
307: // Select
308: ListHolder result = (ListHolder) store
309: .findSingle("find listholder");
310: result.getList().size(); // Force load
311: // Check
312: logger.debug("listholder result list: " + result.getList());
313: assertTrue(result.getList().contains(dyn1));
314: assertTrue(result.getList().contains(dyn2));
315: }
316:
317: public void testDynamicMapAttribute() throws Exception {
318: // Clear
319: dropTables("mapholder");
320: // Make object
321: MapHolder obj = new MapHolder();
322: HashMap map = new HashMap();
323: DynamicObjectWithClass dyn1 = new DynamicObjectWithClass();
324: dyn1.setPersistenceDynamicName("CarClass");
325: dyn1.put("model", "Jaguar");
326: dyn1.put("doors", new Integer(4));
327: DynamicObjectWithClass dyn2 = new DynamicObjectWithClass();
328: dyn2.setPersistenceDynamicName(null);
329: dyn2.put("index", new Integer(1));
330: dyn2.put("name", "Jim");
331: dyn2.put("male", Boolean.TRUE);
332: map.put("Dynamic 1", dyn1);
333: map.put("Dynamic 2", dyn2);
334: obj.setMeta(map);
335: // Insert
336: store.save(obj);
337: // Select
338: MapHolder result = (MapHolder) store
339: .findSingle("find mapholder");
340: // Check
341: assertEquals(map, result.getMeta());
342: }
343:
344: public void testDynamicAttributeInSelectWithClassSpec()
345: throws Exception {
346: dropTables("dynamicattributeobject");
347: // Create object
348: DynamicAttributeObject obj = new DynamicAttributeObject();
349: obj.setIndex(1);
350: DynamicObjectWithClass dyn = new DynamicObjectWithClass();
351: dyn.setPersistenceDynamicName("CarClass");
352: dyn.put("model", "Jaguar");
353: dyn.put("doors", new Integer(4));
354: dyn.put("index", new Integer(1));
355: dyn.put("name", "Jim");
356: dyn.put("male", Boolean.TRUE);
357: obj.setObj(dyn);
358: // Save
359: store.save(obj);
360: // Select
361: DynamicAttributeObject result = (DynamicAttributeObject) store
362: .findSingle("find dynamicattributeobject where dynamicattributeobject.obj(carclass).model='Jaguar'");
363: assertEquals(dyn, result.getObj());
364: }
365:
366: public void testDynamicAttributeInSelectWithNoClassSpec()
367: throws Exception {
368: dropTables("dynamicattributeobject");
369: // Create object
370: DynamicAttributeObject obj = new DynamicAttributeObject();
371: obj.setIndex(1);
372: DynamicObjectWithClass dyn = new DynamicObjectWithClass();
373: dyn.setPersistenceDynamicName(null);
374: dyn.put("index", new Integer(1));
375: dyn.put("name", "Jim");
376: dyn.put("male", Boolean.TRUE);
377: obj.setObj(dyn);
378: // Save
379: store.save(obj);
380: // Select
381: DynamicAttributeObject result = (DynamicAttributeObject) store
382: .findSingle("find dynamicattributeobject where dynamicattributeobject.obj.index=1");
383: assertEquals(dyn, result.getObj());
384: }
385:
386: public void testDynamicObjectWithStaticAttributes()
387: throws Exception {
388: // Clear
389: dropTables("dynamicobjectwithstaticattributes");
390: DynamicObjectWithStaticAttributes.init();
391: // Create dynamic object, and save it
392: DynamicObjectWithStaticAttributes obj = new DynamicObjectWithStaticAttributes();
393: obj.put("index", new Integer(1));
394: obj.put("name", "Jim");
395: obj.put("male", Boolean.TRUE);
396: obj.setOwnAttribute("something");
397: store.save(obj);
398: // Select and check
399: DynamicObjectWithStaticAttributes result = (DynamicObjectWithStaticAttributes) store
400: .findSingle("find dynamicobjectwithstaticattributes");
401: assertEquals(obj, result);
402: assertEquals("something", result.getOwnAttribute());
403: }
404:
405: }
|