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.SQLException ; 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 29 30 import junit.extensions.TestSetup; 31 import junit.framework.Test; 32 import junit.framework.TestSuite; 33 34 import org.opensubsystems.core.error.OSSException; 35 import org.opensubsystems.core.persist.db.Database; 36 import org.opensubsystems.core.persist.db.DatabaseImpl; 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.Log; 42 43 52 public final class SelectAfterInsertIntoUniqueColumnTest 53 { 54 56 59 private SelectAfterInsertIntoUniqueColumnTest( 60 ) 61 { 62 } 64 65 67 73 public static Test suite( 74 ) 75 { 76 TestSuite suite = new DatabaseTestSuite("SelectAfterInsertIntoUniqueColumnTest"); 77 suite.addTestSuite(SelectAfterInsertIntoUniqueColumnTestInternal.class); 78 TestSetup wrapper = new DatabaseTestSetup(suite); 79 80 return wrapper; 81 } 82 83 88 public static class SelectAfterInsertIntoUniqueColumnTestInternal extends DatabaseTest 89 { 90 92 95 private static Logger s_logger 96 = Log.getInstance(SelectAfterInsertIntoUniqueColumnTestInternal.class); 97 98 100 103 static 104 { 105 Database dbDatabase; 107 108 try 109 { 110 dbDatabase = DatabaseImpl.getInstance(); 111 dbDatabase.add(DatabaseTestSchema.class); 113 } 114 catch (OSSException bfeExc) 115 { 116 throw new RuntimeException ("Unexpected exception.", bfeExc); 117 } 118 } 119 120 125 public SelectAfterInsertIntoUniqueColumnTestInternal( 126 String strTestName 127 ) 128 { 129 super(strTestName); 130 } 131 132 138 public void testSelectAfterInsertIntoUniqueColumn( 139 ) throws Throwable 140 { 141 final String INSERT_VALUE = "insert into UNIQUE_COLUMN_TEST (TEST_ID) values (?)"; 142 final String SELECT_VALUE = "select count(*) from UNIQUE_COLUMN_TEST"; 143 final String DELETE_ALL = "delete from UNIQUE_COLUMN_TEST"; 144 145 PreparedStatement insertStatement = null; 146 PreparedStatement deleteStatement = null; 147 PreparedStatement selectStatement = null; 148 ResultSet rsResults = null; 149 int iDeletedCount = 0; 150 int iCounter; 151 152 try 153 { 154 m_transaction.begin(); 157 try 158 { 159 deleteStatement = m_connection.prepareStatement(DELETE_ALL); 160 iDeletedCount = DatabaseUtils.executeUpdateAndClose(deleteStatement); 161 m_transaction.commit(); 162 } 163 catch (Throwable throwable) 164 { 165 m_transaction.rollback(); 166 throw throwable; 167 } 168 finally 169 { 170 DatabaseUtils.closeStatement(deleteStatement); 171 deleteStatement = null; 172 } 173 174 assertEquals("No records should be initially in the database.", 175 0, iDeletedCount); 176 177 m_transaction.begin(); 179 try 180 { 181 try 182 { 183 for (iCounter = 1; iCounter < 6; iCounter++) 185 { 186 insertStatement = m_connection.prepareStatement(INSERT_VALUE); 187 insertStatement.setInt(1, 100 * iCounter); 188 189 insertStatement.executeUpdate(); 190 } 191 try 193 { 194 insertStatement.setInt(1, 100); 195 insertStatement.executeUpdate(); 196 fail("Database allowed to insert duplicate value into unique column."); 197 } 198 catch (SQLException sqlExc) 199 { 200 } 202 203 try 204 { 205 selectStatement = m_connection.prepareStatement(SELECT_VALUE); 206 rsResults = selectStatement.executeQuery(); 207 208 if (rsResults.next()) 209 { 210 assertEquals("Incorrect number of selected items", 211 5, rsResults.getInt(1)); 212 } 213 } 214 catch (SQLException sqleExc) 215 { 216 if (sqleExc.getMessage().indexOf("ERROR: current transaction" + 218 " is aborted, commands ignored until end of transaction block") 219 != -1) 220 { 221 s_logger.log(Level.WARNING, "Database doesn't allow execute" + 222 " additional commands in a transaction if" + 223 " one of the commands failed.", sqleExc); 224 fail("Database doesn't allow execute additional commands" + 225 " in a transaction if one of the commands failed."); 226 } 227 throw sqleExc; 228 } 229 finally 230 { 231 DatabaseUtils.closeResultSetAndStatement(rsResults, 232 selectStatement); 233 } 234 } 235 finally 236 { 237 DatabaseUtils.closeStatement(insertStatement); 238 } 239 m_transaction.commit(); 240 } 241 catch (Throwable throwable) 242 { 243 m_transaction.rollback(); 244 throw throwable; 245 } 246 } 247 finally 248 { 249 m_transaction.begin(); 251 try 252 { 253 deleteStatement = m_connection.prepareStatement(DELETE_ALL); 254 iDeletedCount = DatabaseUtils.executeUpdateAndClose(deleteStatement); 255 m_transaction.commit(); 256 257 } 258 catch (Throwable throwable) 259 { 260 m_transaction.rollback(); 261 throw throwable; 262 } 263 finally 264 { 265 DatabaseUtils.closeStatement(deleteStatement); 266 } 267 } 268 } 269 } 270 } 271 | Popular Tags |