1 21 22 package org.opensubsystems.core.persist.db.driver; 23 24 import java.sql.PreparedStatement ; 25 import java.sql.ResultSet ; 26 27 import junit.extensions.TestSetup; 28 import junit.framework.Test; 29 import junit.framework.TestSuite; 30 31 import org.opensubsystems.core.error.OSSException; 32 import org.opensubsystems.core.persist.db.Database; 33 import org.opensubsystems.core.persist.db.DatabaseImpl; 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 import org.opensubsystems.core.util.GlobalConstants; 39 40 48 public final class SetNullColumnTest 49 { 50 52 55 private SetNullColumnTest( 56 ) 57 { 58 } 60 61 63 69 public static Test suite( 70 ) 71 { 72 TestSuite suite = new DatabaseTestSuite("SetNullColumnTest"); 73 suite.addTestSuite(SetNullColumnTestInternal.class); 74 TestSetup wrapper = new DatabaseTestSetup(suite); 75 76 return wrapper; 77 } 78 79 84 public static class SetNullColumnTestInternal extends DatabaseTest 85 { 86 89 static 90 { 91 Database dbDatabase; 93 94 try 95 { 96 dbDatabase = DatabaseImpl.getInstance(); 97 dbDatabase.add(DatabaseTestSchema.class); 99 } 100 catch (OSSException bfeExc) 101 { 102 throw new RuntimeException ("Unexpected exception.", bfeExc); 103 } 104 } 105 106 111 public SetNullColumnTestInternal( 112 String strTestName 113 ) 114 { 115 super(strTestName); 116 } 117 118 126 public void testInsertSelectEmptyString( 127 ) throws Throwable 128 { 129 final String INSERT_VALUE = "insert into NULL_COLUMN_TEST (NAME) values (?)"; 130 final String SELECT_VALUE = "select NAME from NULL_COLUMN_TEST"; 131 final String DELETE_VALUE = "delete from NULL_COLUMN_TEST"; 132 133 PreparedStatement insertStatement = null; 134 ResultSet rsResults = null; 135 int iInsertCount; 136 137 String testValue = ""; 138 boolean testResult = false; 139 m_transaction.begin(); 141 try 142 { 143 try 144 { 145 insertStatement = m_connection.prepareStatement(INSERT_VALUE); 147 insertStatement.setString(1, ""); 148 iInsertCount = insertStatement.executeUpdate(); 149 if (GlobalConstants.ERROR_CHECKING) 150 { 151 assert iInsertCount >= 0 : "executeUpdate cannot return negative value."; 153 } 154 m_transaction.commit(); 155 } 156 catch (Throwable throwable) 157 { 158 m_transaction.rollback(); 159 throw throwable; 160 } 161 finally 162 { 163 DatabaseUtils.closeResultSetAndStatement(rsResults, insertStatement); 164 rsResults = null; 165 } 166 167 PreparedStatement selectStatement = null; 169 170 try 171 { 172 selectStatement = m_connection.prepareStatement(SELECT_VALUE); 173 rsResults = selectStatement.executeQuery(); 174 175 assertTrue("No result in select", rsResults.next()); 176 testValue = rsResults.getString(1); 177 assertNotNull("Inserted string shoul be not null or empty string" + 178 " returned as null", testValue); 179 testResult = rsResults.wasNull(); 180 assertFalse("Inserted record shoul be not treated as null", testResult); 181 } 182 finally 183 { 184 DatabaseUtils.closeResultSetAndStatement(rsResults, selectStatement); 185 rsResults = null; 186 } 187 } 188 finally 189 { 190 m_transaction.begin(); 192 try 193 { 194 PreparedStatement deleteStatement; 195 196 deleteStatement = m_connection.prepareStatement(DELETE_VALUE); 197 deleteStatement.execute(); 198 m_transaction.commit(); 199 } 200 catch (Throwable throwable) 201 { 202 m_transaction.rollback(); 203 throw throwable; 204 } 205 } 206 } 207 } 208 } 209 | Popular Tags |