KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > persistable > PersistableTest


1 /*
2  * Copyright (C) 2006 Methodhead Software LLC. All rights reserved.
3  *
4  * This file is part of TransferCM.
5  *
6  * TransferCM is free software; you can redistribute it and/or modify it under the
7  * terms of the GNU General Public License as published by the Free Software
8  * Foundation; either version 2 of the License, or (at your option) any later
9  * version.
10  *
11  * TransferCM is distributed in the hope that it will be useful, but WITHOUT ANY
12  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14  * details.
15  *
16  * You should have received a copy of the GNU General Public License along with
17  * TransferCM; if not, write to the Free Software Foundation, Inc., 51 Franklin St,
18  * Fifth Floor, Boston, MA 02110-1301 USA
19  */

20
21 package com.methodhead.persistable;
22
23 import java.sql.*;
24 import java.util.*;
25 import junit.framework.*;
26 import org.apache.log4j.*;
27 import com.methodhead.test.*;
28 import org.apache.commons.beanutils.*;
29 import com.methodhead.persistable.ConnectionSingleton;
30
31 public class PersistableTest extends DbTestCase {
32
33   Persistable persistable = null;
34
35   protected static class TestPersistable
36   extends
37     Persistable {
38
39     private static DynaClass dynaClass_ = null;
40
41     static {
42       DynaProperty[] dynaProperties =
43         new DynaProperty[] {
44           new DynaProperty( "string_field", String JavaDoc.class ),
45           new DynaProperty( "int_field", Integer JavaDoc.class ),
46           new DynaProperty( "boolean_field", Boolean JavaDoc.class ),
47           new DynaProperty( "double_field", Double JavaDoc.class ),
48           new DynaProperty( "date_field", java.util.Date JavaDoc.class ),
49         };
50
51       dynaClass_ =
52         new BasicDynaClass(
53           "persistable", TestPersistable.class, dynaProperties );
54     }
55
56     public TestPersistable() {
57       super( dynaClass_ );
58     }
59
60     public TestPersistable( DynaClass dynaClass ) {
61       super( dynaClass );
62     }
63   }
64
65   protected static class PersistableComparator
66   implements
67     Comparator {
68     public int compare( Object JavaDoc o1, Object JavaDoc o2 ) {
69       try {
70         Integer JavaDoc i1 = ( Integer JavaDoc )( ( ( DynaBean )o1 ).get( "int_field" ) );
71         Integer JavaDoc i2 = ( Integer JavaDoc )( ( ( DynaBean )o2 ).get( "int_field" ) );
72         return i1.compareTo( i2 );
73       }
74       catch ( PersistableException e ) {
75         return -1;
76       }
77     }
78     public boolean equals( Object JavaDoc o1, Object JavaDoc o2 ) {
79       try {
80         Integer JavaDoc i1 = ( Integer JavaDoc )( ( ( DynaBean )o1 ).get( "int_field" ) );
81         Integer JavaDoc i2 = ( Integer JavaDoc )( ( ( DynaBean )o2 ).get( "int_field" ) );
82         return i1.equals( i2 );
83       }
84       catch ( PersistableException e ) {
85         return false;
86       }
87     }
88   }
89
90   DynaClass dynaClass_ = null;
91
92   Persistable persistable_ = null;
93   Persistable persistable1_ = null;
94   Persistable persistable2_ = null;
95   Persistable persistable3_ = null;
96
97   java.util.Date JavaDoc date1_ = null;
98   java.util.Date JavaDoc date2_ = null;
99   java.util.Date JavaDoc date3_ = null;
100   java.util.Date JavaDoc date4_ = null;
101   java.util.Date JavaDoc date5_ = null;
102
103   public PersistableTest( String JavaDoc name ) {
104     super( name );
105   }
106
107   protected void setUp() {
108     //setLogLevel( Level.DEBUG );
109
try {
110       try {
111         ConnectionSingleton.runUpdate( "DROP TABLE persistable" );
112       }
113       catch ( SQLException e ) {
114       }
115
116       if ( ConnectionSingleton.getDatabaseType().equals( ConnectionSingleton.DBTYPE_SQLSERVER ) ) {
117         ConnectionSingleton.runUpdate(
118           "CREATE TABLE persistable ( " +
119           " string_field VARCHAR(32), " +
120           " int_field INT, " +
121           " boolean_field BIT, " +
122           " double_field FLOAT, " +
123           " date_field DATETIME " +
124           ")" );
125       }
126       else {
127         ConnectionSingleton.runUpdate(
128           "CREATE TABLE persistable ( " +
129           " string_field VARCHAR(32), " +
130           " int_field INT, " +
131           " boolean_field BIT, " +
132           " double_field FLOAT, " +
133           " date_field TIMESTAMP " +
134           ")" );
135       }
136
137       Calendar cal = new GregorianCalendar();
138
139       cal.set( 2003, 1, 20, 20, 20, 10 );
140       date1_ = cal.getTime();
141
142       cal.set( 2003, 2, 25, 20, 20, 10 );
143       date2_ = cal.getTime();
144
145       cal.set( 2003, 3, 25, 20, 20, 10 );
146       date3_ = cal.getTime();
147
148       cal.set( 2003, 1, 20, 20, 20, 0 );
149       date4_ = cal.getTime();
150
151       cal.set( 2003, 1, 20, 0, 0, 0 );
152       date5_ = cal.getTime();
153
154       DynaProperty[] dynaProperties =
155         new DynaProperty[] {
156           new DynaProperty( "string_field", String JavaDoc.class ),
157           new DynaProperty( "int_field", Integer JavaDoc.class ),
158           new DynaProperty( "boolean_field", Boolean JavaDoc.class ),
159           new DynaProperty( "double_field", Double JavaDoc.class ),
160           new DynaProperty( "date_field", java.util.Date JavaDoc.class ),
161         };
162
163       dynaClass_ = new BasicDynaClass( "persistable", TestPersistable.class, dynaProperties );
164
165       persistable_ = new Persistable( dynaClass_ );
166
167       persistable1_ = new Persistable( dynaClass_ );
168       persistable1_.setString( "string_field", "string_value" );
169       persistable1_.setInt( "int_field", 666 );
170       persistable1_.setBoolean( "boolean_field", true );
171       persistable1_.setDouble( "double_field", 6.66 );
172       persistable1_.setDate( "date_field", date1_ );
173       persistable1_.saveNew();
174
175       persistable2_ = new Persistable( dynaClass_ );
176       persistable2_.setString( "string_field", "string_value2" );
177       persistable2_.setInt( "int_field", 777 );
178       persistable2_.setBoolean( "boolean_field", false );
179       persistable2_.setDouble( "double_field", 0.0 );
180       persistable2_.setDate( "date_field", date2_ );
181       persistable2_.saveNew();
182
183       persistable3_ = new Persistable( dynaClass_ );
184       persistable3_.setString( "string_field", "string_value3" );
185       persistable3_.setInt( "int_field", 888 );
186       persistable3_.setBoolean( "boolean_field", true );
187       persistable3_.setDouble( "double_field", 8.88 );
188       persistable3_.setDate( "date_field", date3_ );
189       persistable3_.saveNew();
190     }
191     catch ( Exception JavaDoc e ) {
192       e.printStackTrace();
193       fail();
194     }
195   }
196
197   protected void tearDown() {
198   }
199
200   public void testGettersAndSetters() {
201
202     assertNull( persistable_.getString( "string_field" ) );
203
204     persistable_.setString( "string_field", "foo" );
205
206     assertEquals( "foo", persistable_.getString( "string_field" ) );
207     assertEquals( 0, persistable_.getInt( "int_field" ) );
208
209     persistable_.setInt( "int_field", 666 );
210
211     assertEquals( 666, persistable_.getInt( "int_field" ) );
212     assertEquals( false, persistable_.getBoolean( "boolean_field" ) );
213
214     persistable_.setBoolean( "boolean_field", true );
215
216     assertEquals( true, persistable_.getBoolean( "boolean_field" ) );
217     assertEquals( 0.00, persistable_.getDouble( "double_field" ), 0.001 );
218
219     persistable_.setDouble( "double_field", 6.66 );
220
221     assertEquals( 6.66, persistable_.getDouble( "double_field" ), 0.001 );
222     assertNull( persistable_.getDate( "date_field" ) );
223
224     persistable_.setDate( "date_field", date1_ );
225
226     assertDatesEqual( date1_, persistable_.getDate( "date_field" ) );
227   }
228
229   public void testSetAsString() {
230
231     java.util.Date JavaDoc d = null;
232
233     //
234
// string
235
//
236
persistable_.setAsString( "string_field", "string_value" );
237
238     assertEquals( "string_value", persistable_.getString( "string_field" ) );
239
240     persistable_.setAsString( "string_field", null );
241
242     assertEquals( "", persistable_.getString( "string_field" ) );
243
244     //
245
// int
246
//
247
persistable_.setAsString( "int_field", "666" );
248
249     assertEquals( 666, persistable_.getInt( "int_field" ) );
250
251     persistable_.setAsString( "int_field", null );
252
253     assertEquals( 0, persistable_.getInt( "int_field" ) );
254
255     persistable_.setAsString( "int_field", "" );
256
257     assertEquals( 0, persistable_.getInt( "int_field" ) );
258
259     //
260
// double
261
//
262
persistable_.setAsString( "double_field", "6.66" );
263
264     assertEquals( 6.66, persistable_.getDouble( "double_field" ), 0.001 );
265
266     persistable_.setAsString( "double_field", null );
267
268     assertEquals( 0.0, persistable_.getDouble( "double_field" ), 0.001 );
269
270     persistable_.setAsString( "double_field", "" );
271
272     assertEquals( 0.0, persistable_.getDouble( "double_field" ), 0.001 );
273
274     //
275
// boolean
276
//
277
persistable_.setAsString( "boolean_field", "true" );
278
279     assertEquals( true, persistable_.getBoolean( "boolean_field" ) );
280
281     persistable_.setAsString( "boolean_field", "True" );
282
283     assertEquals( true, persistable_.getBoolean( "boolean_field" ) );
284
285     persistable_.setAsString( "boolean_field", "yes" );
286
287     assertEquals( true, persistable_.getBoolean( "boolean_field" ) );
288
289     persistable_.setAsString( "boolean_field", "Yes" );
290
291     assertEquals( true, persistable_.getBoolean( "boolean_field" ) );
292
293     persistable_.setAsString( "boolean_field", "on" );
294
295     assertEquals( true, persistable_.getBoolean( "boolean_field" ) );
296
297     persistable_.setAsString( "boolean_field", "On" );
298
299     assertEquals( true, persistable_.getBoolean( "boolean_field" ) );
300
301     persistable_.setAsString( "boolean_field", "false" );
302
303     assertEquals( false, persistable_.getBoolean( "boolean_field" ) );
304
305     persistable_.setAsString( "boolean_field", "" );
306
307     assertEquals( false, persistable_.getBoolean( "boolean_field" ) );
308
309     persistable_.setAsString( "boolean_field", null );
310
311     assertEquals( false, persistable_.getBoolean( "boolean_field" ) );
312
313     //
314
// date
315
//
316
d = new java.util.Date JavaDoc();
317     persistable_.setAsString( "date_field", null );
318
319     assertDatesEqual( d, persistable_.getDate( "date_field" ) );
320
321     d = new java.util.Date JavaDoc();
322     persistable_.setAsString( "date_field", "" );
323
324     assertDatesEqual( d, persistable_.getDate( "date_field" ) );
325
326     persistable_.setAsString( "date_field", "2/20/03 8:20 PM" );
327
328     assertDatesEqual( date4_, persistable_.getDate( "date_field" ) );
329
330     persistable_.setAsString( "date_field", "2/20/2003" );
331
332     assertDatesEqual( date5_, persistable_.getDate( "date_field" ) );
333   }
334
335   public void testSetAsObject() {
336
337     //
338
// string
339
//
340
persistable_.setAsObject( "string_field", "string_value" );
341
342     assertEquals( "string_value", persistable_.getString( "string_field" ) );
343
344     persistable_.setAsObject( "string_field", null );
345
346     assertEquals( "", persistable_.getString( "string_field" ) );
347   }
348
349   public void testGetSqlLiteralString() {
350     assertEquals( "''", Persistable.getSqlLiteral( "" ) );
351     assertEquals( "'test'", Persistable.getSqlLiteral( "test" ) );
352     assertEquals( "'''test'''", Persistable.getSqlLiteral( "'test'" ) );
353   }
354
355   public void testGetSqlLiteralDate() {
356     assertEquals( "'2003-02-20 20:20:10'", Persistable.getSqlLiteral( date1_ ) );
357   }
358
359   public void testGetSqlLiteralBoolean() {
360     assertEquals( "'1'", Persistable.getSqlLiteral( new Boolean JavaDoc( true ) ) );
361     assertEquals( "'0'", Persistable.getSqlLiteral( new Boolean JavaDoc( false ) ) );
362   }
363
364   public void testGetSqlLiteralField() {
365     try {
366       persistable_.setString( "string_field", "string_value" );
367       persistable_.setInt( "int_field", 666 );
368       persistable_.setBoolean( "boolean_field", true );
369       persistable_.setDouble( "double_field", 6.66 );
370       persistable_.setDate( "date_field", date1_ );
371
372       assertEquals( "'string_value'", persistable_.getSqlLiteral( persistable_.getDynaClass().getDynaProperty( "string_field" ) ) );
373       assertEquals( "666", persistable_.getSqlLiteral( persistable_.getDynaClass().getDynaProperty( "int_field" ) ) );
374       assertEquals( "'1'", persistable_.getSqlLiteral( persistable_.getDynaClass().getDynaProperty( "boolean_field" ) ) );
375       assertEquals( "6.66", persistable_.getSqlLiteral( persistable_.getDynaClass().getDynaProperty( "double_field" ) ) );
376       assertEquals( "'2003-02-20 20:20:10'", persistable_.getSqlLiteral( persistable_.getDynaClass().getDynaProperty( "date_field" ) ) );
377
378       persistable_.setBoolean( "boolean_field", false );
379
380       assertEquals( "'0'", persistable_.getSqlLiteral( persistable_.getDynaClass().getDynaProperty( "boolean_field" ) ) );
381     }
382     catch ( PersistableException e ) {
383       fail( e.getMessage() );
384     }
385   }
386
387   public void testSaveNew() {
388     try {
389       persistable_.setString( "string_field", "foo" );
390       persistable_.setInt( "int_field", 999 );
391       persistable_.setBoolean( "boolean_field", true );
392       persistable_.setDouble( "double_field", 6.66 );
393       persistable_.setDate( "date_field", date1_ );
394       persistable_.saveNew();
395       ResultSet rs = ConnectionSingleton.runQuery( "SELECT string_field, int_field, boolean_field, double_field, date_field FROM persistable WHERE int_field=999" );
396
397       assertNotNull( rs );
398       assertTrue( rs.next() );
399       assertEquals( "foo", rs.getString( "string_field" ) );
400       assertEquals( 999, rs.getInt( "int_field" ) );
401       assertEquals( true, rs.getBoolean( "boolean_field" ) );
402       assertEquals( 6.66, rs.getDouble( "double_field" ), 0.01 );
403       assertDatesEqual( date1_, rs.getTimestamp( "date_field" ) );
404       assertTrue( !rs.next() );
405
406       ConnectionSingleton.close( rs );
407     }
408     catch ( Exception JavaDoc e ) {
409       fail( e.toString() );
410     }
411   }
412
413   public void testSave() {
414     try {
415       persistable1_.setString( "string_field", "bar" );
416       persistable1_.setBoolean( "boolean_field", true );
417       persistable1_.setDouble( "double_field", 7.77 );
418       persistable1_.setDate( "date_field", date1_ );
419       persistable1_.save( "int_field=666" );
420       ResultSet rs = ConnectionSingleton.runQuery( "SELECT string_field, int_field, boolean_field, double_field, date_field FROM persistable WHERE int_field=666" );
421
422       assertNotNull( rs );
423       assertTrue( rs.next() );
424       assertEquals( "bar", rs.getString( "string_field" ) );
425       assertEquals( true, rs.getBoolean( "boolean_field" ) );
426       assertEquals( 7.77, rs.getDouble( "double_field" ), 0.01 );
427       assertDatesEqual( date1_, rs.getTimestamp( "date_field" ) );
428       assertTrue( !rs.next() );
429
430       ConnectionSingleton.close( rs );
431     }
432     catch ( Exception JavaDoc e ) {
433       fail( e.toString() );
434     }
435   }
436
437   public void testLoad() {
438     try {
439       //
440
// load a typical row
441
//
442
persistable_.load( "int_field=666" );
443
444       assertEquals( "string_value", persistable_.getString( "string_field" ) );
445       assertEquals( 666, persistable_.getInt( "int_field" ) );
446       assertEquals( true, persistable_.getBoolean( "boolean_field" ) );
447       assertEquals( 6.66, persistable_.getDouble( "double_field" ), 0.01 );
448       assertDatesEqual( date1_, persistable_.getDate( "date_field" ) );
449
450       //
451
// load a row with a 0.0 double value (this causes problems in sql server)
452
//
453
persistable_.load( "int_field=777" );
454
455       assertEquals( 0.00, persistable_.getDouble( "double_field" ), 0.01 );
456     }
457     catch ( Exception JavaDoc e ) {
458       fail( e.toString() );
459     }
460   }
461
462   public void testLoadAll() {
463     try {
464       List l = TestPersistable.loadAll( dynaClass_, null, null );
465
466       assertNotNull( l );
467       assertEquals( 3, l.size() );
468
469       Collections.sort( l, new PersistableComparator() );
470       TestPersistable testPersistable = ( TestPersistable )l.get( 0 );
471       testPersistable = ( TestPersistable )l.get( 0 );
472
473       assertEquals( persistable1_.getString( "string_field" ), testPersistable.getString( "string_field" ) );
474       assertEquals( persistable1_.getInt( "int_field" ), testPersistable.getInt( "int_field" ) );
475       assertEquals( persistable1_.getBoolean( "boolean_field" ), testPersistable.getBoolean( "boolean_field" ) );
476       assertEquals( persistable1_.getDouble( "double_field" ), testPersistable.getDouble( "double_field" ), 0.01 );
477       assertDatesEqual( persistable1_.getDate( "date_field" ), testPersistable.getDate( "date_field" ) );
478
479       testPersistable = ( TestPersistable )l.get( 1 );
480
481       assertEquals( persistable2_.getString( "string_field" ), testPersistable.getString( "string_field" ) );
482       assertEquals( persistable2_.getInt( "int_field" ), testPersistable.getInt( "int_field" ) );
483       assertEquals( persistable2_.getBoolean( "boolean_field" ), testPersistable.getBoolean( "boolean_field" ) );
484       assertEquals( persistable2_.getDouble( "double_field" ), testPersistable.getDouble( "double_field" ), 0.01 );
485       assertDatesEqual( persistable2_.getDate( "date_field" ), testPersistable.getDate( "date_field" ) );
486
487       testPersistable = ( TestPersistable )l.get( 2 );
488
489       assertEquals( persistable3_.getString( "string_field" ), testPersistable.getString( "string_field" ) );
490       assertEquals( persistable3_.getInt( "int_field" ), testPersistable.getInt( "int_field" ) );
491       assertEquals( persistable3_.getBoolean( "boolean_field" ), testPersistable.getBoolean( "boolean_field" ) );
492       assertEquals( persistable3_.getDouble( "double_field" ), testPersistable.getDouble( "double_field" ), 0.01 );
493       assertDatesEqual( persistable3_.getDate( "date_field" ), testPersistable.getDate( "date_field" ) );
494     }
495     catch ( Exception JavaDoc e ) {
496       e.printStackTrace();
497       fail();
498     }
499   }
500
501   public void testLoadAllWhere() {
502     try {
503       List l = TestPersistable.loadAll( dynaClass_, "string_field='string_value2'", null );
504
505       assertNotNull( l );
506       assertEquals( 1, l.size() );
507
508       TestPersistable testPersistable = ( TestPersistable )l.get( 0 );
509
510       assertEquals( persistable2_.getString( "string_field" ), testPersistable.getString( "string_field" ) );
511       assertEquals( persistable2_.getInt( "int_field" ), testPersistable.getInt( "int_field" ) );
512       assertEquals( persistable2_.getBoolean( "boolean_field" ), testPersistable.getBoolean( "boolean_field" ) );
513       assertEquals( persistable2_.getDouble( "double_field" ), testPersistable.getDouble( "double_field" ), 0.01 );
514       assertDatesEqual( persistable2_.getDate( "date_field" ), testPersistable.getDate( "date_field" ) );
515     }
516     catch ( Exception JavaDoc e ) {
517       fail( e.toString() );
518     }
519   }
520
521   public void testLoadAllOrderBy() {
522     try {
523       List l = TestPersistable.loadAll( dynaClass_, null, "int_field DESC" );
524
525       assertNotNull( l );
526       assertEquals( 3, l.size() );
527
528       TestPersistable testPersistable = ( TestPersistable )l.get( 0 );
529
530       assertEquals( persistable3_.getInt( "int_field" ), testPersistable.getInt( "int_field" ) );
531
532       testPersistable = ( TestPersistable )l.get( 1 );
533
534       assertEquals( persistable2_.getInt( "int_field" ), testPersistable.getInt( "int_field" ) );
535
536       testPersistable = ( TestPersistable )l.get( 2 );
537
538       assertEquals( persistable1_.getInt( "int_field" ), testPersistable.getInt( "int_field" ) );
539     }
540     catch ( Exception JavaDoc e ) {
541       fail( e.toString() );
542     }
543   }
544
545   public void testLoadAllNonStatic() {
546     try {
547       persistable = new TestPersistable();
548       List l = persistable.loadAll( null, "int_field DESC" );
549
550       assertNotNull( l );
551       assertEquals( 3, l.size() );
552
553       TestPersistable testPersistable = ( TestPersistable )l.get( 0 );
554
555       assertEquals( persistable3_.getInt( "int_field" ), testPersistable.getInt( "int_field" ) );
556
557       testPersistable = ( TestPersistable )l.get( 1 );
558
559       assertEquals( persistable2_.getInt( "int_field" ), testPersistable.getInt( "int_field" ) );
560
561       testPersistable = ( TestPersistable )l.get( 2 );
562
563       assertEquals( persistable1_.getInt( "int_field" ), testPersistable.getInt( "int_field" ) );
564     }
565     catch ( Exception JavaDoc e ) {
566       fail( e.toString() );
567     }
568   }
569
570   public void testDeleteAll() {
571     ResultSet rs = null;
572     try {
573       Persistable.deleteAll( dynaClass_, null );
574       rs = ConnectionSingleton.runQuery( "SELECT int_field FROM persistable" );
575
576       assertNotNull( rs );
577       assertTrue( !rs.next() );
578
579       ConnectionSingleton.close( rs );
580     }
581     catch ( Exception JavaDoc e ) {
582       if ( rs != null )
583         ConnectionSingleton.close( rs );
584       fail( e.toString() );
585     }
586   }
587
588   public void testDeleteAllNonStatic() {
589     ResultSet rs = null;
590     try {
591       persistable = new TestPersistable();
592       persistable.deleteAll( null );
593       rs = ConnectionSingleton.runQuery( "SELECT int_field FROM persistable" );
594
595       assertNotNull( rs );
596       assertTrue( !rs.next() );
597
598       ConnectionSingleton.close( rs );
599     }
600     catch ( Exception JavaDoc e ) {
601       if ( rs != null )
602         ConnectionSingleton.close( rs );
603       fail( e.toString() );
604     }
605   }
606
607   public void testDeleteAllWhere() {
608     ResultSet rs = null;
609     try {
610       Persistable.deleteAll( dynaClass_, "int_field=777" );
611       rs = ConnectionSingleton.runQuery( "SELECT int_field FROM persistable ORDER BY int_field" );
612
613       assertNotNull( rs );
614       assertTrue( rs.next() );
615       assertEquals( 666, rs.getInt( "int_field" ) );
616       assertTrue( rs.next() );
617       assertEquals( 888, rs.getInt( "int_field" ) );
618       assertTrue( !rs.next() );
619
620       ConnectionSingleton.close( rs );
621     }
622     catch ( Exception JavaDoc e ) {
623       if ( rs != null )
624         ConnectionSingleton.close( rs );
625       fail( e.toString() );
626     }
627   }
628 }
629
Popular Tags