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 39 47 public final class AddBatchTest 48 { 49 51 54 private AddBatchTest( 55 ) 56 { 57 } 59 60 62 68 public static Test suite( 69 ) 70 { 71 TestSuite suite = new DatabaseTestSuite("AddBatchTest"); 72 suite.addTestSuite(AddBatchTestInternal.class); 73 TestSetup wrapper = new DatabaseTestSetup(suite); 74 75 return wrapper; 76 } 77 78 83 public static class AddBatchTestInternal 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 AddBatchTestInternal( 111 String strTestName 112 ) 113 { 114 super(strTestName); 115 } 116 117 122 public void testAddBatch( 123 ) throws Throwable 124 { 125 final String INSERT_VALUE = "insert into NULL_COLUMN_TEST (NAME) values (?)"; 126 final String SELECT_VALUE = "select count(NAME) from NULL_COLUMN_TEST where " + 127 "NAME like 'test_batch_%'"; 128 final String DELETE_VALUE = "delete from NULL_COLUMN_TEST"; 129 130 PreparedStatement insertStatement = null; 131 ResultSet rsResults = null; 132 int[] arrInsertCount = new int[10]; 133 134 int testValue = 0; 135 136 m_transaction.begin(); 138 try 139 { 140 try 141 { 142 insertStatement = m_connection.prepareStatement(INSERT_VALUE); 143 for (int iIndex = 1; iIndex < 11; iIndex++) 145 { 146 insertStatement.setString(1, "test_batch_" + iIndex); 147 insertStatement.addBatch(); 148 } 149 arrInsertCount = insertStatement.executeBatch(); 150 m_transaction.commit(); 151 } 152 catch (Throwable throwable) 153 { 154 m_transaction.rollback(); 155 throw throwable; 156 } 157 finally 158 { 159 DatabaseUtils.closeStatement(insertStatement); 160 rsResults = null; 161 } 162 163 assertEquals("Number of inserted records is incorrect", 10, arrInsertCount.length); 164 165 PreparedStatement selectStatement = null; 167 168 try 169 { 170 selectStatement = m_connection.prepareStatement(SELECT_VALUE); 171 rsResults = selectStatement.executeQuery(); 172 173 assertTrue("No result in select", rsResults.next()); 174 testValue = rsResults.getInt(1); 175 assertEquals("Incorrect number of specified records selected", 10, testValue); 176 } 177 finally 178 { 179 DatabaseUtils.closeResultSetAndStatement(rsResults, selectStatement); 180 rsResults = null; 181 } 182 } 183 finally 184 { 185 m_transaction.begin(); 187 try 188 { 189 PreparedStatement deleteStatement; 190 191 deleteStatement = m_connection.prepareStatement(DELETE_VALUE); 192 deleteStatement.execute(); 193 m_transaction.commit(); 194 } 195 catch (Throwable throwable) 196 { 197 m_transaction.rollback(); 198 throw throwable; 199 } 200 } 201 } 202 } 203 } 204 | Popular Tags |