1 21 22 package org.opensubsystems.core.persist.db.driver; 23 24 import java.sql.PreparedStatement ; 25 import java.sql.ResultSet ; 26 import java.util.logging.Logger ; 27 28 import junit.extensions.TestSetup; 29 import junit.framework.Test; 30 import junit.framework.TestSuite; 31 32 import org.opensubsystems.core.error.OSSException; 33 import org.opensubsystems.core.persist.db.Database; 34 import org.opensubsystems.core.persist.db.DatabaseImpl; 35 import org.opensubsystems.core.persist.db.DatabaseSchemaManager; 36 import org.opensubsystems.core.persist.db.DatabaseTest; 37 import org.opensubsystems.core.persist.db.DatabaseTestSetup; 38 import org.opensubsystems.core.persist.db.DatabaseTestSuite; 39 import org.opensubsystems.core.util.DatabaseUtils; 40 import org.opensubsystems.core.util.Log; 41 42 50 public final class PreparedStatementTest 51 { 52 54 57 private PreparedStatementTest( 58 ) 59 { 60 } 62 63 65 71 public static Test suite( 72 ) 73 { 74 TestSuite suite = new DatabaseTestSuite("PreparedStatementTest"); 75 suite.addTestSuite(PreparedStatementTestInternal.class); 76 TestSetup wrapper = new DatabaseTestSetup(suite); 77 78 return wrapper; 79 } 80 81 86 public static class PreparedStatementTestInternal extends DatabaseTest 87 { 88 90 93 private static Logger s_logger = Log.getInstance(PreparedStatementTest.class); 94 95 98 static 99 { 100 Database dbDatabase; 102 103 try 104 { 105 dbDatabase = DatabaseImpl.getInstance(); 106 dbDatabase.add(DatabaseTestSchema.class); 108 } 109 catch (OSSException bfeExc) 110 { 111 throw new RuntimeException ("Unexpected exception.", bfeExc); 112 } 113 } 114 115 120 public PreparedStatementTestInternal( 121 String strTestName 122 ) 123 { 124 super(strTestName); 125 } 126 127 135 public void testRepeatedTransactionInsert( 136 ) throws Throwable 137 { 138 final String INSERT_VALUE = ((DatabaseTestSchema)DatabaseSchemaManager.getInstance( 139 DatabaseTestSchema.class)).getInsertGeneratedKey(); 140 final String DELETE_VALUE = "delete from GENERATEDKEY_TEST where TEST_VALUE = ?"; 141 final String VALUE_TEST = "insert test value"; 142 final int REPEAT_COUNT = 530; 144 145 PreparedStatement insertStatement = null; 146 ResultSet rsResults = null; 147 int iInsertCount; 148 int iIndex; 149 150 for (iIndex = 0; iIndex < REPEAT_COUNT; iIndex++) 151 { 152 m_transaction.begin(); 153 try 154 { 155 try 156 { 157 insertStatement = m_connection.prepareStatement(INSERT_VALUE); 158 insertStatement.setString(1, VALUE_TEST); 159 160 iInsertCount = insertStatement.executeUpdate(); 161 } 162 finally 163 { 164 DatabaseUtils.closeResultSetAndStatement(rsResults, insertStatement); 165 } 166 m_transaction.commit(); 167 } 168 catch (Throwable throwable) 169 { 170 m_transaction.rollback(); 171 throw throwable; 172 } 173 174 assertEquals("Exactly one record have been inserted.", 175 iInsertCount, 1); 176 } 177 s_logger.finest("Inserted " + iIndex + " records, each in separate transaction."); 178 179 int iDeletedCount = 0; 181 182 m_transaction.begin(); 183 try 184 { 185 PreparedStatement deleteStatement; 186 187 deleteStatement = m_connection.prepareStatement(DELETE_VALUE); 188 deleteStatement.setString(1, VALUE_TEST); 189 iDeletedCount = DatabaseUtils.executeUpdateAndClose(deleteStatement); 190 m_transaction.commit(); 191 192 assertEquals("Exactly " + iIndex + " records with data shoud have been deleted.", 193 iDeletedCount, iIndex); 194 195 s_logger.finest("Deleted " + iIndex + " records, all in one transaction."); 196 } 197 catch (Throwable throwable) 198 { 199 m_transaction.rollback(); 200 throw throwable; 201 } 202 } 203 } 204 } 205 | Popular Tags |