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 SameColumnTest 49 { 50 52 55 private SameColumnTest( 56 ) 57 { 58 } 60 61 63 69 public static Test suite( 70 ) 71 { 72 TestSuite suite = new DatabaseTestSuite("SameColumnTest"); 73 suite.addTestSuite(SameColumnTestInternal.class); 74 TestSetup wrapper = new DatabaseTestSetup(suite); 75 76 return wrapper; 77 } 78 79 84 public static class SameColumnTestInternal 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 SameColumnTestInternal( 112 String strTestName 113 ) 114 { 115 super(strTestName); 116 } 117 118 124 public void testSameColumSelect( 125 ) throws Throwable 126 { 127 final String INSERT_VALUE1 = "insert into SAME_TEST1 (ID) values (?)"; 128 final String INSERT_VALUE2 = "insert into SAME_TEST2 (ID) values (?)"; 129 final String SELECT_VALUE = "select SAME_TEST1.ID,SAME_TEST2.ID" + 130 " from SAME_TEST1, SAME_TEST2"; 131 final String DELETE_VALUE1 = "delete from SAME_TEST1"; 132 final String DELETE_VALUE2 = "delete from SAME_TEST2"; 133 134 final int VALUE1 = 1; 136 final int VALUE2 = 2; 137 138 PreparedStatement insertStatement = null; 139 ResultSet rsResults = null; 140 int iInsertCount; 141 142 m_transaction.begin(); 144 try 145 { 146 try 147 { 148 insertStatement = m_connection.prepareStatement(INSERT_VALUE1); 149 insertStatement.setInt(1, VALUE1); 150 iInsertCount = insertStatement.executeUpdate(); 151 if (GlobalConstants.ERROR_CHECKING) 152 { 153 assert iInsertCount >= 0 : "executeUpdate cannot return negative value."; 155 } 156 157 insertStatement = m_connection.prepareStatement(INSERT_VALUE2); 158 insertStatement.setInt(1, VALUE2); 159 iInsertCount = insertStatement.executeUpdate(); 160 if (GlobalConstants.ERROR_CHECKING) 161 { 162 assert iInsertCount >= 0 : "executeUpdate cannot return negative value."; 164 } 165 } 166 catch (Throwable throwable) 167 { 168 m_transaction.rollback(); 169 throw throwable; 170 } 171 finally 172 { 173 DatabaseUtils.closeResultSetAndStatement(rsResults, insertStatement); 174 } 175 m_transaction.commit(); 176 177 PreparedStatement selectStatement = null; 179 180 selectStatement = m_connection.prepareStatement(SELECT_VALUE); 181 rsResults = selectStatement.executeQuery(); 182 183 assertTrue("No result in select", rsResults.next()); 184 assertEquals("The first returned value is not correct", VALUE1, rsResults.getInt(1)); 185 assertEquals("The second returned value is not correct", VALUE2, rsResults.getInt(2)); 186 187 } 188 finally 189 { 190 m_transaction.begin(); 192 try 193 { 194 PreparedStatement deleteStatement; 195 196 deleteStatement = m_connection.prepareStatement(DELETE_VALUE1); 197 deleteStatement.execute(); 198 deleteStatement = m_connection.prepareStatement(DELETE_VALUE2); 199 deleteStatement.execute(); 200 201 m_transaction.commit(); 202 } 203 catch (Throwable throwable) 204 { 205 m_transaction.rollback(); 206 throw throwable; 207 } 208 } 209 } 210 } 211 } 212 | Popular Tags |