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 DBProcReturnInsertedRowsCountTest 48 { 49 51 54 private DBProcReturnInsertedRowsCountTest( 55 ) 56 { 57 } 59 60 62 68 public static Test suite( 69 ) 70 { 71 TestSuite suite = new DatabaseTestSuite("DBProcReturnInsertedRowsCountTest"); 72 suite.addTestSuite(DBProcReturnInsertedRowsCountTestInternal.class); 73 TestSetup wrapper = new DatabaseTestSetup(suite); 74 75 return wrapper; 76 } 77 78 83 public static class DBProcReturnInsertedRowsCountTestInternal 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 DBProcReturnInsertedRowsCountTestInternal( 111 String strTestName 112 ) 113 { 114 super(strTestName); 115 } 116 117 122 public void testDBProcReturnInsertRowsCount( 123 ) throws Throwable 124 { 125 final String DELETE_VALUE = "delete from GENERATEDKEY_TEST where TEST_VALUE = ?"; 126 final String VALUE_TEST = "test value"; 127 128 int iInsertCount = 0; 129 int iInsertedRows = 0; 130 131 PreparedStatement deleteStatement = null; 132 133 m_transaction.begin(); 134 135 try 136 { 137 try 138 { 139 int[] returnValues; 140 returnValues = ((DatabaseTestSchema)DatabaseSchemaManager.getInstance( 143 DatabaseTestSchema.class)).executeInsertRow( 144 m_connection, VALUE_TEST); 145 if (returnValues != null) 146 { 147 iInsertCount = returnValues[0]; 149 iInsertedRows = returnValues[1]; 151 } 152 153 m_transaction.commit(); 154 } 155 catch (Throwable throwable) 156 { 157 m_transaction.rollback(); 158 throw throwable; 159 } 160 161 assertEquals("DBS does not support returning number of processed rows using" + 162 " [ RowCounter = insertStatement.executeUpdate() ]. " + 163 " Number of inserted rows is incorrect.", 1, iInsertCount); 164 assertTrue("DBS does not support stored procedures or stored procedure" + 167 " does not support returning number of affected rows. Number" + 168 " of inserted rows returned from stored procedure must be" + 169 " greater than 0.", 170 iInsertedRows > 0); 171 172 int iDeletedCount = 0; 173 174 m_transaction.begin(); 175 176 try 177 { 178 deleteStatement = m_connection.prepareStatement(DELETE_VALUE); 179 deleteStatement.setString(1, VALUE_TEST); 180 iDeletedCount = DatabaseUtils.executeUpdateAndClose(deleteStatement); 181 m_transaction.commit(); 182 183 assertEquals("Exactly one record with date data shoud have been deleted.", 184 iDeletedCount, 1); 185 } 186 catch (Throwable throwable) 187 { 188 m_transaction.rollback(); 189 throw throwable; 190 } 191 } 192 finally 193 { 194 m_transaction.begin(); 196 try 197 { 198 deleteStatement = m_connection.prepareStatement(DELETE_VALUE); 200 deleteStatement.setString(1, VALUE_TEST); 201 DatabaseUtils.executeUpdateAndClose(deleteStatement); 202 m_transaction.commit(); 203 } 204 catch (Throwable throwable) 205 { 206 m_transaction.rollback(); 207 throw throwable; 208 } 209 finally 210 { 211 DatabaseUtils.closeStatement(deleteStatement); 212 } 213 } 214 215 } 216 } 217 } 218 | Popular Tags |