1 20 21 package com.methodhead.persistable; 22 23 import java.sql.*; 24 import java.util.*; 25 import junit.framework.*; 26 import com.methodhead.test.*; 27 import com.methodhead.persistable.ConnectionSingleton; 28 import org.apache.commons.beanutils.*; 29 30 public class KeyedPersistableTest extends DbTestCase { 31 32 KeyedPersistable impl_ = null; 33 KeyedPersistable impl1_ = null; 34 static Map fields_ = null; 35 java.util.Date date1_ = null; 36 java.util.Date date2_ = null; 37 DynaClass dynaClass_ = null; 38 39 40 private static class TestKey 41 implements 42 Key { 43 44 public boolean equals( Object o ) { 45 if ( o == null ) 46 return false; 47 48 if ( !getClass().isInstance( o ) ) 49 return false; 50 51 return true; 52 } 53 54 public String getWhereClause() { 55 return "int_field=666"; 56 } 57 58 public void setProperties( 59 Persistable persistable ) { 60 persistable.set( "int_field", new Integer ( 666 ) ); 61 } 62 63 public void setKeyValue( 64 Persistable persistable ) { 65 } 67 } 68 69 public static class TestKeyFactory 70 implements 71 KeyFactory { 72 public Key newInstance() { 73 return new TestKey(); 74 } 75 } 76 77 public KeyedPersistableTest( String name ) { 78 super( name ); 79 } 80 81 protected void createData() { 82 impl1_ = new KeyedPersistable( dynaClass_ ); 83 impl1_.setString( "string_field", "string_value" ); 84 impl1_.setBoolean( "boolean_field", true ); 85 impl1_.setDouble( "double_field", 6.66 ); 86 impl1_.setDate( "date_field", date1_ ); 87 impl1_.saveNew( new TestKey() ); 88 } 89 90 protected void setUp() { 91 try { 92 try { 93 ConnectionSingleton.runUpdate( "DROP TABLE persistable" ); 94 } 95 catch ( SQLException e ) { 96 } 97 98 if ( ConnectionSingleton.getDatabaseType().equals( ConnectionSingleton.DBTYPE_SQLSERVER ) ) { 99 ConnectionSingleton.runUpdate( 100 "CREATE TABLE persistable ( " + 101 " string_field VARCHAR(32), " + 102 " int_field INT, " + 103 " boolean_field BIT, " + 104 " double_field FLOAT, " + 105 " date_field DATETIME " + 106 ")" ); 107 } 108 else { 109 ConnectionSingleton.runUpdate( 110 "CREATE TABLE persistable ( " + 111 " string_field VARCHAR(32), " + 112 " int_field INT, " + 113 " boolean_field BIT, " + 114 " double_field FLOAT, " + 115 " date_field TIMESTAMP " + 116 ")" ); 117 } 118 119 DynaProperty[] dynaProperties = 120 new DynaProperty[] { 121 new DynaProperty( "string_field", String .class ), 122 new DynaProperty( "int_field", Integer .class ), 123 new DynaProperty( "boolean_field", Boolean .class ), 124 new DynaProperty( "double_field", Double .class ), 125 new DynaProperty( "date_field", java.util.Date .class ), 126 }; 127 128 dynaClass_ = new BasicDynaClass( "persistable", KeyedPersistable.class, dynaProperties ); 129 130 impl_ = new KeyedPersistable( dynaClass_ ); 131 132 Calendar cal = new GregorianCalendar(); 133 134 cal.set( 2003, 1, 20, 20, 20, 10 ); 135 date1_ = cal.getTime(); 136 137 cal.set( 2003, 2, 20, 20, 20, 10 ); 138 date2_ = cal.getTime(); 139 } 140 catch ( Exception e ) { 141 fail( e.getMessage() ); 142 } 143 } 144 145 protected void tearDown() { 146 } 147 148 public void testSaveNew() { 149 ResultSet rs = null; 150 try { 151 impl_.setString( "string_field", "string_value" ); 152 impl_.setBoolean( "boolean_field", true ); 153 impl_.setDouble( "double_field", 6.66 ); 154 impl_.setDate( "date_field", date1_ ); 155 impl_.saveNew( new TestKey() ); 156 rs = ConnectionSingleton.runQuery( "SELECT string_field, int_field, boolean_field, double_field, date_field FROM persistable WHERE int_field=666" ); 157 158 assertNotNull( rs ); 159 assertTrue( rs.next() ); 160 assertEquals( "string_value", rs.getString( "string_field" ) ); 161 assertEquals( 666, rs.getInt( "int_field" ) ); 162 assertEquals( true, rs.getBoolean( "boolean_field" ) ); 163 assertEquals( 6.66, rs.getDouble( "double_field" ), 0.01 ); 164 assertDatesEqual( date1_, rs.getTimestamp( "date_field" ) ); 165 assertTrue( !rs.next() ); 166 167 ConnectionSingleton.close( rs ); 168 } 169 catch ( Exception e ) { 170 if ( rs != null ) 171 ConnectionSingleton.close( rs ); 172 e.printStackTrace(); 173 fail(); 174 } 175 } 176 177 public void testLoad() { 178 try { 179 createData(); 180 impl_.load( new TestKey() ); 181 182 assertEquals( "string_value", impl_.getString( "string_field" ) ); 183 assertEquals( 666, impl_.getInt( "int_field" ) ); 184 assertEquals( true, impl_.getBoolean( "boolean_field" ) ); 185 assertEquals( 6.66, impl_.getDouble( "double_field" ), 0.01 ); 186 assertDatesEqual( date1_, impl_.getDate( "date_field" ) ); 187 } 188 catch ( Exception e ) { 189 fail( e.toString() ); 190 } 191 } 192 193 public void testLoadWhere() { 194 try { 195 createData(); 196 impl_.load( "int_field=666", new TestKeyFactory() ); 197 198 assertEquals( new TestKey(), impl_.key_ ); 199 } 200 catch ( Exception e ) { 201 fail( e.getMessage() ); 202 } 203 } 204 205 public void testLoadAll() { 206 try { 207 createData(); 208 List l = KeyedPersistable.loadAll( 209 dynaClass_, 210 null, 211 null, 212 new TestKeyFactory() ); 213 214 assertEquals( 1, l.size() ); 215 216 impl_ = ( KeyedPersistable )l.get( 0 ); 217 218 assertNotNull( impl_.key_ ); 219 assertEquals( "int_field=666", impl_.key_.getWhereClause() ); 220 } 221 catch ( Exception e ) { 222 fail( e.getMessage() ); 223 } 224 } 225 226 public void testSave() { 227 ResultSet rs = null; 228 try { 229 createData(); 230 impl_.load( new TestKey() ); 231 impl_.setString( "string_field", "string_value2" ); 232 impl_.setBoolean( "boolean_field", false ); 233 impl_.setDouble( "double_field", 7.77 ); 234 impl_.setDate( "date_field", date2_ ); 235 impl_.save(); 236 rs = ConnectionSingleton.runQuery( "SELECT string_field, int_field, boolean_field, double_field, date_field FROM persistable WHERE int_field=666" ); 237 238 assertNotNull( rs ); 239 assertTrue( rs.next() ); 240 assertEquals( "string_value2", rs.getString( "string_field" ) ); 241 assertEquals( 666, rs.getInt( "int_field" ) ); 242 assertEquals( false, rs.getBoolean( "boolean_field" ) ); 243 assertEquals( 7.77, rs.getDouble( "double_field" ), 0.01 ); 244 assertDatesEqual( date2_, rs.getTimestamp( "date_field" ) ); 245 assertTrue( !rs.next() ); 246 247 ConnectionSingleton.close( rs ); 248 } 249 catch ( Exception e ) { 250 if ( rs != null ) 251 ConnectionSingleton.close( rs ); 252 fail( e.getMessage() ); 253 } 254 } 255 256 public void testDelete() { 257 ResultSet rs = null; 258 try { 259 createData(); 260 impl_.load( new TestKey() ); 261 impl_.delete(); 262 rs = ConnectionSingleton.runQuery( "SELECT int_field FROM persistable WHERE int_field=666" ); 263 264 assertNotNull( rs ); 265 assertTrue( !rs.next() ); 266 267 ConnectionSingleton.close( rs ); 268 } 269 catch ( Exception e ) { 270 if ( rs != null ) 271 ConnectionSingleton.close( rs ); 272 fail( e.toString() ); 273 } 274 } 275 } 276 | Popular Tags |