1 21 22 package org.opensubsystems.core.persist.db.driver; 23 24 import java.sql.PreparedStatement ; 25 import java.sql.ResultSet ; 26 import java.sql.Types ; 27 import java.util.logging.Logger ; 28 29 import junit.extensions.TestSetup; 30 import junit.framework.Test; 31 import junit.framework.TestSuite; 32 33 import org.opensubsystems.core.error.OSSException; 34 import org.opensubsystems.core.persist.db.Database; 35 import org.opensubsystems.core.persist.db.DatabaseImpl; 36 import org.opensubsystems.core.persist.db.DatabaseSchemaManager; 37 import org.opensubsystems.core.persist.db.DatabaseTest; 38 import org.opensubsystems.core.persist.db.DatabaseTestSetup; 39 import org.opensubsystems.core.persist.db.DatabaseTestSuite; 40 import org.opensubsystems.core.util.DatabaseUtils; 41 import org.opensubsystems.core.util.GlobalConstants; 42 import org.opensubsystems.core.util.Log; 43 44 52 public final class GeneratedKeyTest 53 { 54 56 59 private GeneratedKeyTest( 60 ) 61 { 62 } 64 65 67 73 public static Test suite( 74 ) 75 { 76 TestSuite suite = new DatabaseTestSuite("GeneratedKeyTest"); 77 suite.addTestSuite(GeneratedKeyTestInternal.class); 78 TestSetup wrapper = new DatabaseTestSetup(suite); 79 80 return wrapper; 81 } 82 83 88 public static class GeneratedKeyTestInternal extends DatabaseTest 89 { 90 92 95 private static Logger s_logger = Log.getInstance(GeneratedKeyTest.class); 96 97 100 static 101 { 102 Database dbDatabase; 104 105 try 106 { 107 dbDatabase = DatabaseImpl.getInstance(); 108 dbDatabase.add(DatabaseTestSchema.class); 110 } 111 catch (OSSException bfeExc) 112 { 113 throw new RuntimeException ("Unexpected exception.", bfeExc); 114 } 115 } 116 117 122 public GeneratedKeyTestInternal( 123 String strTestName 124 ) 125 { 126 super(strTestName); 127 } 128 129 137 public void testGeneratedKey( 138 ) throws Throwable 139 { 140 final String INSERT_VALUE = ((DatabaseTestSchema)DatabaseSchemaManager.getInstance( 141 DatabaseTestSchema.class)).getInsertGeneratedKey(); 142 final String DELETE_VALUE = "delete from GENERATEDKEY_TEST where TEST_VALUE = ?"; 143 final String VALUE_TEST = "test value"; 144 145 PreparedStatement insertStatement = null; 146 ResultSet rsResults = null; 147 int iInsertCount; 148 149 m_transaction.begin(); 150 151 try 152 { 153 insertStatement = m_connection.prepareStatement(INSERT_VALUE); 154 insertStatement.setString(1, VALUE_TEST); 155 156 iInsertCount = insertStatement.executeUpdate(); 157 158 try 160 { 161 rsResults = insertStatement.getGeneratedKeys(); 162 rsResults.getInt(1); 163 } 164 catch (Throwable throwable) 165 { 166 s_logger.warning("Warning: Database driver " + getDataSourceName() 169 + " still doesn't support retrieval of generated keys: " 170 + throwable); 171 } 172 173 m_transaction.commit(); 174 } 175 catch (Throwable throwable) 176 { 177 m_transaction.rollback(); 178 throw throwable; 179 } 180 finally 181 { 182 DatabaseUtils.closeResultSetAndStatement(rsResults, insertStatement); 183 } 184 185 assertEquals("Exactly one record have been inserted.", 186 iInsertCount, 1); 187 188 int iDeletedCount = 0; 190 191 m_transaction.begin(); 192 193 try 194 { 195 PreparedStatement deleteStatement; 196 197 deleteStatement = m_connection.prepareStatement(DELETE_VALUE); 198 deleteStatement.setString(1, VALUE_TEST); 199 iDeletedCount = DatabaseUtils.executeUpdateAndClose(deleteStatement); 200 m_transaction.commit(); 201 202 assertEquals("Exactly one record with date data shoud have been deleted.", 203 1, iDeletedCount); 204 } 205 catch (Throwable throwable) 206 { 207 m_transaction.rollback(); 208 throw throwable; 209 } 210 } 211 212 220 public void testGeneratedKeyUsingStoredProcedure( 221 ) throws Throwable 222 { 223 final String DELETE_VALUE = "delete from GENERATEDKEY_TEST where TEST_VALUE = ?"; 224 final String VALUE_TEST = "test value"; 225 226 int iInsertCount = 0; 227 int iGeneratedKey = 0; 228 229 m_transaction.begin(); 230 231 try 232 { 233 int[] returnValues; 234 235 returnValues = ((DatabaseTestSchema)DatabaseSchemaManager.getInstance( 236 DatabaseTestSchema.class)).executeInsertGeneratedKey2( 237 m_connection, VALUE_TEST); 238 if (returnValues != null) 239 { 240 iInsertCount = returnValues[0]; 241 iGeneratedKey = returnValues[1]; 242 } 243 244 m_transaction.commit(); 245 } 246 catch (Throwable throwable) 247 { 248 m_transaction.rollback(); 249 throw throwable; 250 } 251 252 assertEquals("Exactly one record have been inserted.", 253 1, iInsertCount); 254 assertTrue("Generated key must be greater than 0", iGeneratedKey > 0); 255 s_logger.finest("Database generated and using stored procedure returned key " 256 + iGeneratedKey); 257 258 int iDeletedCount = 0; 260 261 m_transaction.begin(); 262 263 try 264 { 265 PreparedStatement deleteStatement; 266 267 deleteStatement = m_connection.prepareStatement(DELETE_VALUE); 268 deleteStatement.setString(1, VALUE_TEST); 269 iDeletedCount = DatabaseUtils.executeUpdateAndClose(deleteStatement); 270 m_transaction.commit(); 271 272 assertEquals("Exactly one record with date data shoud have been deleted.", 273 iDeletedCount, 1); 274 } 275 catch (Throwable throwable) 276 { 277 m_transaction.rollback(); 278 throw throwable; 279 } 280 } 281 282 291 public void testForeignKey( 292 ) throws Throwable 293 { 294 final String INSERT_VALUE = "insert into OWN_FK_TEST (TEST_ID,FK_ID) values (?,?)"; 295 final String UPDATE_VALUE = "update OWN_FK_TEST set FK_ID = ? where TEST_ID = ?"; 296 final String DELETE_VALUE = "delete from OWN_FK_TEST where TEST_ID = ?"; 297 298 PreparedStatement insertStatement = null; 299 PreparedStatement deleteStatement = null; 300 int iInsertCount; 301 int iDeleteCount; 302 303 m_transaction.begin(); 304 305 try 306 { 307 try 308 { 309 insertStatement = m_connection.prepareStatement(INSERT_VALUE); 310 insertStatement.setInt(1, 1); 311 insertStatement.setInt(2, 1); 312 iInsertCount = insertStatement.executeUpdate(); 313 if (GlobalConstants.ERROR_CHECKING) 314 { 315 assert iInsertCount >= 0 : "executeUpdate cannot return negative value."; 317 } 318 } 319 catch (Throwable throwable) 320 { 321 assertTrue("Not possible to insert data which FK is pointing to themselves.", false); 322 323 insertStatement = m_connection.prepareStatement(INSERT_VALUE); 324 insertStatement.setInt(1, 1); 325 insertStatement.setNull(2, Types.INTEGER); 326 iInsertCount = insertStatement.executeUpdate(); 327 328 insertStatement = m_connection.prepareStatement(UPDATE_VALUE); 329 insertStatement.setInt(1, 1); 330 insertStatement.setInt(2, 1); 331 iInsertCount = insertStatement.executeUpdate(); 332 333 } 334 335 try 337 { 338 deleteStatement = m_connection.prepareStatement(DELETE_VALUE); 339 deleteStatement.setInt(1, 1); 340 iDeleteCount = deleteStatement.executeUpdate(); 341 if (GlobalConstants.ERROR_CHECKING) 342 { 343 assert iDeleteCount >= 0 : "executeUpdate cannot return negative value."; 345 } 346 } 347 catch (Throwable throwable) 348 { 349 assertTrue("Not possible to delete data which FK is pointing to themselves.", 350 false); 351 throw throwable; 352 } 353 354 m_transaction.commit(); 355 } 356 catch (Throwable throwable) 357 { 358 m_transaction.rollback(); 359 throw throwable; 360 } 361 finally 362 { 363 DatabaseUtils.closeStatement(deleteStatement); 364 DatabaseUtils.closeStatement(insertStatement); 365 } 366 } 367 } 368 } 369 | Popular Tags |