1 21 22 package org.opensubsystems.core.persist.db.driver; 23 24 import java.sql.PreparedStatement ; 25 26 import junit.extensions.TestSetup; 27 import junit.framework.Test; 28 import junit.framework.TestSuite; 29 30 import org.opensubsystems.core.error.OSSException; 31 import org.opensubsystems.core.persist.db.Database; 32 import org.opensubsystems.core.persist.db.DatabaseImpl; 33 import org.opensubsystems.core.persist.db.DatabaseSchemaManager; 34 import org.opensubsystems.core.persist.db.DatabaseTest; 35 import org.opensubsystems.core.persist.db.DatabaseTestSetup; 36 import org.opensubsystems.core.persist.db.DatabaseTestSuite; 37 import org.opensubsystems.core.util.DatabaseUtils; 38 39 47 public final class UpdateDBProcWithoutOutputTest 48 { 49 51 54 private UpdateDBProcWithoutOutputTest( 55 ) 56 { 57 } 59 60 62 68 public static Test suite( 69 ) 70 { 71 TestSuite suite = new DatabaseTestSuite("UpdateDBProcWithoutOutputTest"); 72 suite.addTestSuite(UpdateDBProcWithoutOutputTestInternal.class); 73 TestSetup wrapper = new DatabaseTestSetup(suite); 74 75 return wrapper; 76 } 77 78 83 public static class UpdateDBProcWithoutOutputTestInternal extends DatabaseTest 84 { 85 88 static 89 { 90 Database dbDatabase; 92 93 try 94 { 95 dbDatabase = DatabaseImpl.getInstance(); 96 dbDatabase.add(DatabaseTestSchema.class); 98 } 99 catch (OSSException bfeExc) 100 { 101 throw new RuntimeException ("Unexpected exception.", bfeExc); 102 } 103 } 104 105 110 public UpdateDBProcWithoutOutputTestInternal( 111 String strTestName 112 ) 113 { 114 super(strTestName); 115 } 116 117 123 public void testUpdateDBProcWithoutOutputParam( 124 ) throws Throwable 125 { 126 final String INSERT_VALUE = "insert into TRANSACTION_TEST (TEST_ID,TEST_VALUE)" + 127 " values (?,?)"; 128 final String DELETE_VALUE = "delete from TRANSACTION_TEST where TEST_VALUE in (?, ?)"; 129 final String DELETE_ALL = "delete from TRANSACTION_TEST"; 130 final String OLD_VALUE_TEST = "test_value"; 131 final String NEW_VALUE_TEST = "test_value_updated"; 132 133 PreparedStatement insertStatement = null; 134 PreparedStatement deleteStatement = null; 135 int iInsertCount = 0; 136 int iUpdateCount = 0; 137 int iDeletedCount = 0; 138 139 try 140 { 141 m_transaction.begin(); 144 try 145 { 146 deleteStatement = m_connection.prepareStatement(DELETE_ALL); 147 148 iDeletedCount = DatabaseUtils.executeUpdateAndClose(deleteStatement); 149 150 m_transaction.commit(); 151 } 152 catch (Throwable throwable) 153 { 154 m_transaction.rollback(); 155 throw throwable; 156 } 157 158 assertEquals("No records should be initially in the database.", 159 0, iDeletedCount); 160 161 m_transaction.begin(); 163 try 164 { 165 try 166 { 167 insertStatement = m_connection.prepareStatement(INSERT_VALUE); 168 insertStatement.setInt(1, 100); 169 insertStatement.setString(2, OLD_VALUE_TEST); 170 171 iInsertCount = insertStatement.executeUpdate(); 172 } 173 finally 174 { 175 DatabaseUtils.closeStatement(insertStatement); 176 } 177 m_transaction.commit(); 178 } 179 catch (Throwable throwable) 180 { 181 m_transaction.rollback(); 182 throw throwable; 183 } 184 assertEquals("Exactly one record have been inserted.", 185 iInsertCount, 1); 186 187 188 m_transaction.begin(); 190 try 191 { 192 iUpdateCount = ((DatabaseTestSchema)DatabaseSchemaManager.getInstance( 193 DatabaseTestSchema.class)).executeUpdateTestValue( 194 m_connection, 195 OLD_VALUE_TEST, 196 NEW_VALUE_TEST); 197 m_transaction.commit(); 198 199 } 200 catch (Throwable throwable) 201 { 202 m_transaction.rollback(); 203 throw throwable; 204 } 205 assertEquals("Exactly one record have been updated.", 206 1, iUpdateCount); 207 208 } 209 finally 210 { 211 m_transaction.begin(); 213 try 214 { 215 deleteStatement = m_connection.prepareStatement(DELETE_VALUE); 216 deleteStatement.setString(1, OLD_VALUE_TEST); 217 deleteStatement.setString(2, NEW_VALUE_TEST); 218 iDeletedCount = DatabaseUtils.executeUpdateAndClose(deleteStatement); 219 m_transaction.commit(); 220 221 } 222 catch (Throwable throwable) 223 { 224 m_transaction.rollback(); 225 throw throwable; 226 } 227 finally 228 { 229 DatabaseUtils.closeStatement(deleteStatement); 230 } 231 assertEquals("Exactly 1 record with data shoud have been deleted.", 232 1, iDeletedCount); 233 } 234 } 235 } 236 } 237 | Popular Tags |