KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > methodhead > aikp > AutoIntKeyPersistableTest


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.aikp;
22
23 import java.sql.*;
24 import java.util.*;
25
26 import junit.framework.*;
27
28 import com.methodhead.test.*;
29 import com.methodhead.persistable.*;
30 import org.apache.commons.beanutils.*;
31
32 public class AutoIntKeyPersistableTest extends DbTestCase {
33
34   protected static class TestClass extends AutoIntKeyPersistable {
35     public TestClass() {
36       super( null );
37     }
38
39     public TestClass(
40       DynaClass dynaClass ) {
41       super( dynaClass );
42     }
43
44     public List loadAll() {
45       return null;
46     }
47   }
48
49   AutoIntKeyPersistable impl_ = null;
50   AutoIntKeyPersistable impl1_ = null;
51   static Map fields_ = null;
52   java.util.Date JavaDoc date1_ = null;
53   DynaClass dynaClass_ = null;
54
55   public AutoIntKeyPersistableTest ( String JavaDoc name ) {
56     super( name );
57   }
58
59   protected void createData()
60   throws
61     PersistableException {
62     impl1_ = new TestClass( dynaClass_ );
63     impl1_.setString( "string_field", "string_value" );
64     impl1_.setInt( "int_field", 666 );
65     impl1_.setBoolean( "boolean_field", true );
66     impl1_.setDouble( "double_field", 6.66 );
67     impl1_.setDate( "date_field", date1_ );
68     impl1_.saveNew();
69   }
70
71   protected void setUp()
72   throws
73     SQLException {
74
75     try {
76       ConnectionSingleton.runUpdate( "DROP TABLE mh_id" );
77       ConnectionSingleton.runUpdate( "DROP TABLE persistable" );
78     }
79     catch ( SQLException e ) {
80     }
81
82     ConnectionSingleton.runUpdate( "CREATE TABLE mh_id (name VARCHAR(32), value INT)" );
83
84     if ( ConnectionSingleton.getDatabaseType().equals( ConnectionSingleton.DBTYPE_SQLSERVER ) ) {
85       ConnectionSingleton.runUpdate(
86         "CREATE TABLE persistable ( " +
87         " id INT, " +
88         " string_field VARCHAR(32), " +
89         " int_field INT, " +
90         " boolean_field BIT, " +
91         " double_field REAL, " +
92         " date_field DATETIME " +
93         ")" );
94     }
95     else {
96       ConnectionSingleton.runUpdate(
97         "CREATE TABLE persistable ( " +
98         " id INT, " +
99         " string_field VARCHAR(32), " +
100         " int_field INT, " +
101         " boolean_field BIT, " +
102         " double_field REAL, " +
103         " date_field TIMESTAMP " +
104         ")" );
105     }
106
107     DynaProperty[] dynaProperties =
108       new DynaProperty[] {
109         new DynaProperty( "id", Integer JavaDoc.class ),
110         new DynaProperty( "string_field", String JavaDoc.class ),
111         new DynaProperty( "int_field", Integer JavaDoc.class ),
112         new DynaProperty( "boolean_field", Boolean JavaDoc.class ),
113         new DynaProperty( "double_field", Double JavaDoc.class ),
114         new DynaProperty( "date_field", java.util.Date JavaDoc.class ),
115       };
116
117     dynaClass_ = new BasicDynaClass( "persistable", TestClass.class, dynaProperties );
118
119     impl_ = new TestClass( dynaClass_ );
120
121     Calendar cal = new GregorianCalendar();
122
123     cal.set( 2003, 1, 20, 20, 20, 10 );
124     date1_ = cal.getTime();
125   }
126
127   protected void tearDown() {
128   }
129
130   public void testNewKey() {
131     try {
132       AutoIntKeyPersistable impl = new TestClass();
133
134       assertEquals( new IntKey( 1 ), impl.newKey( "name1" ) );
135       assertEquals( new IntKey( 2 ), impl.newKey( "name1" ) );
136       assertEquals( new IntKey( 1 ), impl.newKey( "name2" ) );
137     }
138     catch ( Exception JavaDoc e ) {
139       fail( e.toString() );
140     }
141   }
142
143   public void testSaveNew() {
144     ResultSet rs = null;
145     try {
146       AutoIntKeyPersistable impl = new TestClass( dynaClass_ );
147       impl.setString( "string_field", "string_value" );
148       impl.setInt( "int_field", 666 );
149       impl.setBoolean( "boolean_field", true );
150       impl.setDouble( "double_field", 6.66 );
151       impl.setDate( "date_field", date1_ );
152       impl.saveNew();
153
154       rs = ConnectionSingleton.runQuery( "SELECT string_field, int_field, boolean_field, double_field, date_field FROM persistable WHERE id=" + impl.getInt( "id" ) );
155
156       assertNotNull( rs );
157       assertTrue( rs.next() );
158       assertEquals( "string_value", rs.getString( "string_field" ) );
159       assertEquals( 666, rs.getInt( "int_field" ) );
160       assertEquals( true, rs.getBoolean( "boolean_field" ) );
161       assertEquals( 6.66, rs.getDouble( "double_field" ), 0.01 );
162       assertDatesEqual( date1_, rs.getTimestamp( "date_field" ) );
163       assertTrue( !rs.next() );
164
165       ConnectionSingleton.close( rs );
166     }
167     catch ( Exception JavaDoc e ) {
168       if ( rs != null )
169         ConnectionSingleton.close( rs );
170       e.printStackTrace();
171       fail();
172     }
173   }
174
175   public void testLoad() {
176     try {
177       createData();
178       impl_.load( "int_field=666" );
179
180       assertEquals( impl1_.getInt( "id" ), impl_.getInt( "id" ) );
181     }
182     catch ( Exception JavaDoc e ) {
183       fail( e.toString() );
184     }
185   }
186
187   public void testLoadAll() {
188     try {
189       createData();
190       List l = AutoIntKeyPersistable.loadAll( dynaClass_, null, null );
191
192       assertEquals( 1, l.size() );
193
194       impl_ = ( AutoIntKeyPersistable )l.get( 0 );
195
196       assertEquals( new IntKey( 1 ), impl_.getKey() );
197     }
198     catch ( Exception JavaDoc e ) {
199       fail( e.toString() );
200     }
201   }
202 }
203
Popular Tags