001: /*
002: * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
003: *
004: * This file is part of TransferCM.
005: *
006: * TransferCM is free software; you can redistribute it and/or modify it under the
007: * terms of the GNU General Public License as published by the Free Software
008: * Foundation; either version 2 of the License, or (at your option) any later
009: * version.
010: *
011: * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
012: * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
013: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
014: * details.
015: *
016: * You should have received a copy of the GNU General Public License along with
017: * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
018: * Fifth Floor, Boston, MA 02110-1301 USA
019: */
020:
021: package com.methodhead.persistable;
022:
023: import java.sql.*;
024: import java.util.*;
025: import junit.framework.*;
026: import org.apache.log4j.*;
027: import com.methodhead.test.*;
028: import org.apache.commons.beanutils.*;
029: import com.methodhead.persistable.ConnectionSingleton;
030:
031: public class PersistableTest extends DbTestCase {
032:
033: Persistable persistable = null;
034:
035: protected static class TestPersistable extends Persistable {
036:
037: private static DynaClass dynaClass_ = null;
038:
039: static {
040: DynaProperty[] dynaProperties = new DynaProperty[] {
041: new DynaProperty("string_field", String.class),
042: new DynaProperty("int_field", Integer.class),
043: new DynaProperty("boolean_field", Boolean.class),
044: new DynaProperty("double_field", Double.class),
045: new DynaProperty("date_field", java.util.Date.class), };
046:
047: dynaClass_ = new BasicDynaClass("persistable",
048: TestPersistable.class, dynaProperties);
049: }
050:
051: public TestPersistable() {
052: super (dynaClass_);
053: }
054:
055: public TestPersistable(DynaClass dynaClass) {
056: super (dynaClass);
057: }
058: }
059:
060: protected static class PersistableComparator implements Comparator {
061: public int compare(Object o1, Object o2) {
062: try {
063: Integer i1 = (Integer) (((DynaBean) o1)
064: .get("int_field"));
065: Integer i2 = (Integer) (((DynaBean) o2)
066: .get("int_field"));
067: return i1.compareTo(i2);
068: } catch (PersistableException e) {
069: return -1;
070: }
071: }
072:
073: public boolean equals(Object o1, Object o2) {
074: try {
075: Integer i1 = (Integer) (((DynaBean) o1)
076: .get("int_field"));
077: Integer i2 = (Integer) (((DynaBean) o2)
078: .get("int_field"));
079: return i1.equals(i2);
080: } catch (PersistableException e) {
081: return false;
082: }
083: }
084: }
085:
086: DynaClass dynaClass_ = null;
087:
088: Persistable persistable_ = null;
089: Persistable persistable1_ = null;
090: Persistable persistable2_ = null;
091: Persistable persistable3_ = null;
092:
093: java.util.Date date1_ = null;
094: java.util.Date date2_ = null;
095: java.util.Date date3_ = null;
096: java.util.Date date4_ = null;
097: java.util.Date date5_ = null;
098:
099: public PersistableTest(String name) {
100: super (name);
101: }
102:
103: protected void setUp() {
104: //setLogLevel( Level.DEBUG );
105: try {
106: try {
107: ConnectionSingleton.runUpdate("DROP TABLE persistable");
108: } catch (SQLException e) {
109: }
110:
111: if (ConnectionSingleton.getDatabaseType().equals(
112: ConnectionSingleton.DBTYPE_SQLSERVER)) {
113: ConnectionSingleton
114: .runUpdate("CREATE TABLE persistable ( "
115: + " string_field VARCHAR(32), "
116: + " int_field INT, "
117: + " boolean_field BIT, "
118: + " double_field FLOAT, "
119: + " date_field DATETIME " + ")");
120: } else {
121: ConnectionSingleton
122: .runUpdate("CREATE TABLE persistable ( "
123: + " string_field VARCHAR(32), "
124: + " int_field INT, "
125: + " boolean_field BIT, "
126: + " double_field FLOAT, "
127: + " date_field TIMESTAMP " + ")");
128: }
129:
130: Calendar cal = new GregorianCalendar();
131:
132: cal.set(2003, 1, 20, 20, 20, 10);
133: date1_ = cal.getTime();
134:
135: cal.set(2003, 2, 25, 20, 20, 10);
136: date2_ = cal.getTime();
137:
138: cal.set(2003, 3, 25, 20, 20, 10);
139: date3_ = cal.getTime();
140:
141: cal.set(2003, 1, 20, 20, 20, 0);
142: date4_ = cal.getTime();
143:
144: cal.set(2003, 1, 20, 0, 0, 0);
145: date5_ = cal.getTime();
146:
147: DynaProperty[] dynaProperties = new DynaProperty[] {
148: new DynaProperty("string_field", String.class),
149: new DynaProperty("int_field", Integer.class),
150: new DynaProperty("boolean_field", Boolean.class),
151: new DynaProperty("double_field", Double.class),
152: new DynaProperty("date_field", java.util.Date.class), };
153:
154: dynaClass_ = new BasicDynaClass("persistable",
155: TestPersistable.class, dynaProperties);
156:
157: persistable_ = new Persistable(dynaClass_);
158:
159: persistable1_ = new Persistable(dynaClass_);
160: persistable1_.setString("string_field", "string_value");
161: persistable1_.setInt("int_field", 666);
162: persistable1_.setBoolean("boolean_field", true);
163: persistable1_.setDouble("double_field", 6.66);
164: persistable1_.setDate("date_field", date1_);
165: persistable1_.saveNew();
166:
167: persistable2_ = new Persistable(dynaClass_);
168: persistable2_.setString("string_field", "string_value2");
169: persistable2_.setInt("int_field", 777);
170: persistable2_.setBoolean("boolean_field", false);
171: persistable2_.setDouble("double_field", 0.0);
172: persistable2_.setDate("date_field", date2_);
173: persistable2_.saveNew();
174:
175: persistable3_ = new Persistable(dynaClass_);
176: persistable3_.setString("string_field", "string_value3");
177: persistable3_.setInt("int_field", 888);
178: persistable3_.setBoolean("boolean_field", true);
179: persistable3_.setDouble("double_field", 8.88);
180: persistable3_.setDate("date_field", date3_);
181: persistable3_.saveNew();
182: } catch (Exception e) {
183: e.printStackTrace();
184: fail();
185: }
186: }
187:
188: protected void tearDown() {
189: }
190:
191: public void testGettersAndSetters() {
192:
193: assertNull(persistable_.getString("string_field"));
194:
195: persistable_.setString("string_field", "foo");
196:
197: assertEquals("foo", persistable_.getString("string_field"));
198: assertEquals(0, persistable_.getInt("int_field"));
199:
200: persistable_.setInt("int_field", 666);
201:
202: assertEquals(666, persistable_.getInt("int_field"));
203: assertEquals(false, persistable_.getBoolean("boolean_field"));
204:
205: persistable_.setBoolean("boolean_field", true);
206:
207: assertEquals(true, persistable_.getBoolean("boolean_field"));
208: assertEquals(0.00, persistable_.getDouble("double_field"),
209: 0.001);
210:
211: persistable_.setDouble("double_field", 6.66);
212:
213: assertEquals(6.66, persistable_.getDouble("double_field"),
214: 0.001);
215: assertNull(persistable_.getDate("date_field"));
216:
217: persistable_.setDate("date_field", date1_);
218:
219: assertDatesEqual(date1_, persistable_.getDate("date_field"));
220: }
221:
222: public void testSetAsString() {
223:
224: java.util.Date d = null;
225:
226: //
227: // string
228: //
229: persistable_.setAsString("string_field", "string_value");
230:
231: assertEquals("string_value", persistable_
232: .getString("string_field"));
233:
234: persistable_.setAsString("string_field", null);
235:
236: assertEquals("", persistable_.getString("string_field"));
237:
238: //
239: // int
240: //
241: persistable_.setAsString("int_field", "666");
242:
243: assertEquals(666, persistable_.getInt("int_field"));
244:
245: persistable_.setAsString("int_field", null);
246:
247: assertEquals(0, persistable_.getInt("int_field"));
248:
249: persistable_.setAsString("int_field", "");
250:
251: assertEquals(0, persistable_.getInt("int_field"));
252:
253: //
254: // double
255: //
256: persistable_.setAsString("double_field", "6.66");
257:
258: assertEquals(6.66, persistable_.getDouble("double_field"),
259: 0.001);
260:
261: persistable_.setAsString("double_field", null);
262:
263: assertEquals(0.0, persistable_.getDouble("double_field"), 0.001);
264:
265: persistable_.setAsString("double_field", "");
266:
267: assertEquals(0.0, persistable_.getDouble("double_field"), 0.001);
268:
269: //
270: // boolean
271: //
272: persistable_.setAsString("boolean_field", "true");
273:
274: assertEquals(true, persistable_.getBoolean("boolean_field"));
275:
276: persistable_.setAsString("boolean_field", "True");
277:
278: assertEquals(true, persistable_.getBoolean("boolean_field"));
279:
280: persistable_.setAsString("boolean_field", "yes");
281:
282: assertEquals(true, persistable_.getBoolean("boolean_field"));
283:
284: persistable_.setAsString("boolean_field", "Yes");
285:
286: assertEquals(true, persistable_.getBoolean("boolean_field"));
287:
288: persistable_.setAsString("boolean_field", "on");
289:
290: assertEquals(true, persistable_.getBoolean("boolean_field"));
291:
292: persistable_.setAsString("boolean_field", "On");
293:
294: assertEquals(true, persistable_.getBoolean("boolean_field"));
295:
296: persistable_.setAsString("boolean_field", "false");
297:
298: assertEquals(false, persistable_.getBoolean("boolean_field"));
299:
300: persistable_.setAsString("boolean_field", "");
301:
302: assertEquals(false, persistable_.getBoolean("boolean_field"));
303:
304: persistable_.setAsString("boolean_field", null);
305:
306: assertEquals(false, persistable_.getBoolean("boolean_field"));
307:
308: //
309: // date
310: //
311: d = new java.util.Date();
312: persistable_.setAsString("date_field", null);
313:
314: assertDatesEqual(d, persistable_.getDate("date_field"));
315:
316: d = new java.util.Date();
317: persistable_.setAsString("date_field", "");
318:
319: assertDatesEqual(d, persistable_.getDate("date_field"));
320:
321: persistable_.setAsString("date_field", "2/20/03 8:20 PM");
322:
323: assertDatesEqual(date4_, persistable_.getDate("date_field"));
324:
325: persistable_.setAsString("date_field", "2/20/2003");
326:
327: assertDatesEqual(date5_, persistable_.getDate("date_field"));
328: }
329:
330: public void testSetAsObject() {
331:
332: //
333: // string
334: //
335: persistable_.setAsObject("string_field", "string_value");
336:
337: assertEquals("string_value", persistable_
338: .getString("string_field"));
339:
340: persistable_.setAsObject("string_field", null);
341:
342: assertEquals("", persistable_.getString("string_field"));
343: }
344:
345: public void testGetSqlLiteralString() {
346: assertEquals("''", Persistable.getSqlLiteral(""));
347: assertEquals("'test'", Persistable.getSqlLiteral("test"));
348: assertEquals("'''test'''", Persistable.getSqlLiteral("'test'"));
349: }
350:
351: public void testGetSqlLiteralDate() {
352: assertEquals("'2003-02-20 20:20:10'", Persistable
353: .getSqlLiteral(date1_));
354: }
355:
356: public void testGetSqlLiteralBoolean() {
357: assertEquals("'1'", Persistable
358: .getSqlLiteral(new Boolean(true)));
359: assertEquals("'0'", Persistable
360: .getSqlLiteral(new Boolean(false)));
361: }
362:
363: public void testGetSqlLiteralField() {
364: try {
365: persistable_.setString("string_field", "string_value");
366: persistable_.setInt("int_field", 666);
367: persistable_.setBoolean("boolean_field", true);
368: persistable_.setDouble("double_field", 6.66);
369: persistable_.setDate("date_field", date1_);
370:
371: assertEquals("'string_value'", persistable_
372: .getSqlLiteral(persistable_.getDynaClass()
373: .getDynaProperty("string_field")));
374: assertEquals("666", persistable_.getSqlLiteral(persistable_
375: .getDynaClass().getDynaProperty("int_field")));
376: assertEquals("'1'", persistable_.getSqlLiteral(persistable_
377: .getDynaClass().getDynaProperty("boolean_field")));
378: assertEquals("6.66", persistable_
379: .getSqlLiteral(persistable_.getDynaClass()
380: .getDynaProperty("double_field")));
381: assertEquals("'2003-02-20 20:20:10'", persistable_
382: .getSqlLiteral(persistable_.getDynaClass()
383: .getDynaProperty("date_field")));
384:
385: persistable_.setBoolean("boolean_field", false);
386:
387: assertEquals("'0'", persistable_.getSqlLiteral(persistable_
388: .getDynaClass().getDynaProperty("boolean_field")));
389: } catch (PersistableException e) {
390: fail(e.getMessage());
391: }
392: }
393:
394: public void testSaveNew() {
395: try {
396: persistable_.setString("string_field", "foo");
397: persistable_.setInt("int_field", 999);
398: persistable_.setBoolean("boolean_field", true);
399: persistable_.setDouble("double_field", 6.66);
400: persistable_.setDate("date_field", date1_);
401: persistable_.saveNew();
402: ResultSet rs = ConnectionSingleton
403: .runQuery("SELECT string_field, int_field, boolean_field, double_field, date_field FROM persistable WHERE int_field=999");
404:
405: assertNotNull(rs);
406: assertTrue(rs.next());
407: assertEquals("foo", rs.getString("string_field"));
408: assertEquals(999, rs.getInt("int_field"));
409: assertEquals(true, rs.getBoolean("boolean_field"));
410: assertEquals(6.66, rs.getDouble("double_field"), 0.01);
411: assertDatesEqual(date1_, rs.getTimestamp("date_field"));
412: assertTrue(!rs.next());
413:
414: ConnectionSingleton.close(rs);
415: } catch (Exception e) {
416: fail(e.toString());
417: }
418: }
419:
420: public void testSave() {
421: try {
422: persistable1_.setString("string_field", "bar");
423: persistable1_.setBoolean("boolean_field", true);
424: persistable1_.setDouble("double_field", 7.77);
425: persistable1_.setDate("date_field", date1_);
426: persistable1_.save("int_field=666");
427: ResultSet rs = ConnectionSingleton
428: .runQuery("SELECT string_field, int_field, boolean_field, double_field, date_field FROM persistable WHERE int_field=666");
429:
430: assertNotNull(rs);
431: assertTrue(rs.next());
432: assertEquals("bar", rs.getString("string_field"));
433: assertEquals(true, rs.getBoolean("boolean_field"));
434: assertEquals(7.77, rs.getDouble("double_field"), 0.01);
435: assertDatesEqual(date1_, rs.getTimestamp("date_field"));
436: assertTrue(!rs.next());
437:
438: ConnectionSingleton.close(rs);
439: } catch (Exception e) {
440: fail(e.toString());
441: }
442: }
443:
444: public void testLoad() {
445: try {
446: //
447: // load a typical row
448: //
449: persistable_.load("int_field=666");
450:
451: assertEquals("string_value", persistable_
452: .getString("string_field"));
453: assertEquals(666, persistable_.getInt("int_field"));
454: assertEquals(true, persistable_.getBoolean("boolean_field"));
455: assertEquals(6.66, persistable_.getDouble("double_field"),
456: 0.01);
457: assertDatesEqual(date1_, persistable_.getDate("date_field"));
458:
459: //
460: // load a row with a 0.0 double value (this causes problems in sql server)
461: //
462: persistable_.load("int_field=777");
463:
464: assertEquals(0.00, persistable_.getDouble("double_field"),
465: 0.01);
466: } catch (Exception e) {
467: fail(e.toString());
468: }
469: }
470:
471: public void testLoadAll() {
472: try {
473: List l = TestPersistable.loadAll(dynaClass_, null, null);
474:
475: assertNotNull(l);
476: assertEquals(3, l.size());
477:
478: Collections.sort(l, new PersistableComparator());
479: TestPersistable testPersistable = (TestPersistable) l
480: .get(0);
481: testPersistable = (TestPersistable) l.get(0);
482:
483: assertEquals(persistable1_.getString("string_field"),
484: testPersistable.getString("string_field"));
485: assertEquals(persistable1_.getInt("int_field"),
486: testPersistable.getInt("int_field"));
487: assertEquals(persistable1_.getBoolean("boolean_field"),
488: testPersistable.getBoolean("boolean_field"));
489: assertEquals(persistable1_.getDouble("double_field"),
490: testPersistable.getDouble("double_field"), 0.01);
491: assertDatesEqual(persistable1_.getDate("date_field"),
492: testPersistable.getDate("date_field"));
493:
494: testPersistable = (TestPersistable) l.get(1);
495:
496: assertEquals(persistable2_.getString("string_field"),
497: testPersistable.getString("string_field"));
498: assertEquals(persistable2_.getInt("int_field"),
499: testPersistable.getInt("int_field"));
500: assertEquals(persistable2_.getBoolean("boolean_field"),
501: testPersistable.getBoolean("boolean_field"));
502: assertEquals(persistable2_.getDouble("double_field"),
503: testPersistable.getDouble("double_field"), 0.01);
504: assertDatesEqual(persistable2_.getDate("date_field"),
505: testPersistable.getDate("date_field"));
506:
507: testPersistable = (TestPersistable) l.get(2);
508:
509: assertEquals(persistable3_.getString("string_field"),
510: testPersistable.getString("string_field"));
511: assertEquals(persistable3_.getInt("int_field"),
512: testPersistable.getInt("int_field"));
513: assertEquals(persistable3_.getBoolean("boolean_field"),
514: testPersistable.getBoolean("boolean_field"));
515: assertEquals(persistable3_.getDouble("double_field"),
516: testPersistable.getDouble("double_field"), 0.01);
517: assertDatesEqual(persistable3_.getDate("date_field"),
518: testPersistable.getDate("date_field"));
519: } catch (Exception e) {
520: e.printStackTrace();
521: fail();
522: }
523: }
524:
525: public void testLoadAllWhere() {
526: try {
527: List l = TestPersistable.loadAll(dynaClass_,
528: "string_field='string_value2'", null);
529:
530: assertNotNull(l);
531: assertEquals(1, l.size());
532:
533: TestPersistable testPersistable = (TestPersistable) l
534: .get(0);
535:
536: assertEquals(persistable2_.getString("string_field"),
537: testPersistable.getString("string_field"));
538: assertEquals(persistable2_.getInt("int_field"),
539: testPersistable.getInt("int_field"));
540: assertEquals(persistable2_.getBoolean("boolean_field"),
541: testPersistable.getBoolean("boolean_field"));
542: assertEquals(persistable2_.getDouble("double_field"),
543: testPersistable.getDouble("double_field"), 0.01);
544: assertDatesEqual(persistable2_.getDate("date_field"),
545: testPersistable.getDate("date_field"));
546: } catch (Exception e) {
547: fail(e.toString());
548: }
549: }
550:
551: public void testLoadAllOrderBy() {
552: try {
553: List l = TestPersistable.loadAll(dynaClass_, null,
554: "int_field DESC");
555:
556: assertNotNull(l);
557: assertEquals(3, l.size());
558:
559: TestPersistable testPersistable = (TestPersistable) l
560: .get(0);
561:
562: assertEquals(persistable3_.getInt("int_field"),
563: testPersistable.getInt("int_field"));
564:
565: testPersistable = (TestPersistable) l.get(1);
566:
567: assertEquals(persistable2_.getInt("int_field"),
568: testPersistable.getInt("int_field"));
569:
570: testPersistable = (TestPersistable) l.get(2);
571:
572: assertEquals(persistable1_.getInt("int_field"),
573: testPersistable.getInt("int_field"));
574: } catch (Exception e) {
575: fail(e.toString());
576: }
577: }
578:
579: public void testLoadAllNonStatic() {
580: try {
581: persistable = new TestPersistable();
582: List l = persistable.loadAll(null, "int_field DESC");
583:
584: assertNotNull(l);
585: assertEquals(3, l.size());
586:
587: TestPersistable testPersistable = (TestPersistable) l
588: .get(0);
589:
590: assertEquals(persistable3_.getInt("int_field"),
591: testPersistable.getInt("int_field"));
592:
593: testPersistable = (TestPersistable) l.get(1);
594:
595: assertEquals(persistable2_.getInt("int_field"),
596: testPersistable.getInt("int_field"));
597:
598: testPersistable = (TestPersistable) l.get(2);
599:
600: assertEquals(persistable1_.getInt("int_field"),
601: testPersistable.getInt("int_field"));
602: } catch (Exception e) {
603: fail(e.toString());
604: }
605: }
606:
607: public void testDeleteAll() {
608: ResultSet rs = null;
609: try {
610: Persistable.deleteAll(dynaClass_, null);
611: rs = ConnectionSingleton
612: .runQuery("SELECT int_field FROM persistable");
613:
614: assertNotNull(rs);
615: assertTrue(!rs.next());
616:
617: ConnectionSingleton.close(rs);
618: } catch (Exception e) {
619: if (rs != null)
620: ConnectionSingleton.close(rs);
621: fail(e.toString());
622: }
623: }
624:
625: public void testDeleteAllNonStatic() {
626: ResultSet rs = null;
627: try {
628: persistable = new TestPersistable();
629: persistable.deleteAll(null);
630: rs = ConnectionSingleton
631: .runQuery("SELECT int_field FROM persistable");
632:
633: assertNotNull(rs);
634: assertTrue(!rs.next());
635:
636: ConnectionSingleton.close(rs);
637: } catch (Exception e) {
638: if (rs != null)
639: ConnectionSingleton.close(rs);
640: fail(e.toString());
641: }
642: }
643:
644: public void testDeleteAllWhere() {
645: ResultSet rs = null;
646: try {
647: Persistable.deleteAll(dynaClass_, "int_field=777");
648: rs = ConnectionSingleton
649: .runQuery("SELECT int_field FROM persistable ORDER BY int_field");
650:
651: assertNotNull(rs);
652: assertTrue(rs.next());
653: assertEquals(666, rs.getInt("int_field"));
654: assertTrue(rs.next());
655: assertEquals(888, rs.getInt("int_field"));
656: assertTrue(!rs.next());
657:
658: ConnectionSingleton.close(rs);
659: } catch (Exception e) {
660: if (rs != null)
661: ConnectionSingleton.close(rs);
662: fail(e.toString());
663: }
664: }
665: }
|