1 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 date1_ = null; 53 DynaClass dynaClass_ = null; 54 55 public AutoIntKeyPersistableTest ( String 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 .class ), 110 new DynaProperty( "string_field", String .class ), 111 new DynaProperty( "int_field", Integer .class ), 112 new DynaProperty( "boolean_field", Boolean .class ), 113 new DynaProperty( "double_field", Double .class ), 114 new DynaProperty( "date_field", java.util.Date .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 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 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 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 e ) { 199 fail( e.toString() ); 200 } 201 } 202 } 203 | Popular Tags |