| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons.util; |
|---|
| 34 | |
|---|
| 35 | import java.sql.Connection; |
|---|
| 36 | import java.sql.PreparedStatement; |
|---|
| 37 | import java.sql.ResultSet; |
|---|
| 38 | import java.sql.SQLException; |
|---|
| 39 | import java.sql.Statement; |
|---|
| 40 | import javax.naming.Context; |
|---|
| 41 | import javax.naming.InitialContext; |
|---|
| 42 | import javax.naming.NamingException; |
|---|
| 43 | import javax.sql.DataSource; |
|---|
| 44 | import org.jcoderz.commons.ServerTestCase; |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * Tests the LimitedBatchSizePreparedStatement in the DbUtil class. |
|---|
| 49 | * @author Albrecht Messner |
|---|
| 50 | */ |
|---|
| 51 | public class DbUtilServerTest |
|---|
| 52 | extends ServerTestCase |
|---|
| 53 | { |
|---|
| 54 | private static final int TEST_BATCH_SIZE = 10; |
|---|
| 55 | |
|---|
| 56 | /** {@inheritDoc} */ |
|---|
| 57 | protected void setUp () |
|---|
| 58 | throws Exception |
|---|
| 59 | { |
|---|
| 60 | Connection con = null; |
|---|
| 61 | Statement stmt = null; |
|---|
| 62 | try |
|---|
| 63 | { |
|---|
| 64 | con = getConnection(); |
|---|
| 65 | stmt = con.createStatement(); |
|---|
| 66 | try |
|---|
| 67 | { |
|---|
| 68 | stmt.executeUpdate("DROP TABLE tst_dbutil"); |
|---|
| 69 | } |
|---|
| 70 | catch (SQLException x) |
|---|
| 71 | { |
|---|
| 72 | // ignore, table might not exist |
|---|
| 73 | } |
|---|
| 74 | stmt.executeUpdate( |
|---|
| 75 | "CREATE TABLE tst_dbutil " |
|---|
| 76 | + "(id number(10) primary key, name varchar2(100))"); |
|---|
| 77 | } |
|---|
| 78 | finally |
|---|
| 79 | { |
|---|
| 80 | DbUtil.close(stmt); |
|---|
| 81 | DbUtil.close(con); |
|---|
| 82 | } |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | /** |
|---|
| 86 | * Test what happens when we call executeBatch without having a SQL |
|---|
| 87 | * statement in the prepared statement. |
|---|
| 88 | * @throws Exception if the testcase fails |
|---|
| 89 | */ |
|---|
| 90 | public void testDuplicateBatchUpdate () |
|---|
| 91 | throws Exception |
|---|
| 92 | { |
|---|
| 93 | doTestDuplicateBatchUpdate(false); |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | public void testDuplicateBatchUpdateLbsStatement () |
|---|
| 97 | throws Exception |
|---|
| 98 | { |
|---|
| 99 | doTestDuplicateBatchUpdate(true); |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | /** |
|---|
| 103 | * Add a batch to a prepared statement and then call <code>execute</code> |
|---|
| 104 | * instead of <code>executeBatch</code>. |
|---|
| 105 | * @throws Exception if the testcase fails |
|---|
| 106 | */ |
|---|
| 107 | public void testBatchWithExecute () |
|---|
| 108 | throws Exception |
|---|
| 109 | { |
|---|
| 110 | doTestBatchWithExecute(false); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | /** |
|---|
| 114 | * Add a batch to a prepared statement and then call <code>execute</code> |
|---|
| 115 | * instead of <code>executeBatch</code>. |
|---|
| 116 | * @throws Exception if the testcase fails |
|---|
| 117 | */ |
|---|
| 118 | public void testBatchWithExecuteLbsStatement () |
|---|
| 119 | throws Exception |
|---|
| 120 | { |
|---|
| 121 | doTestBatchWithExecute(true); |
|---|
| 122 | } |
|---|
| 123 | |
|---|
| 124 | /** |
|---|
| 125 | * Run addBatch, clearBatch and then executeBatch, check that no rows |
|---|
| 126 | * are created. |
|---|
| 127 | * @throws Exception if the testcase fails |
|---|
| 128 | */ |
|---|
| 129 | public void testClearBatch () |
|---|
| 130 | throws Exception |
|---|
| 131 | { |
|---|
| 132 | doTestClearStatement(false); |
|---|
| 133 | } |
|---|
| 134 | |
|---|
| 135 | /** |
|---|
| 136 | * Run addBatch, clearBatch and then executeBatch, check that no rows |
|---|
| 137 | * are created. |
|---|
| 138 | * @throws Exception if the testcase fails |
|---|
| 139 | */ |
|---|
| 140 | public void testClearBatchLbsStatement () |
|---|
| 141 | throws Exception |
|---|
| 142 | { |
|---|
| 143 | doTestClearStatement(true); |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | private void doTestDuplicateBatchUpdate (boolean wrapPstmt) |
|---|
| 147 | throws Exception |
|---|
| 148 | { |
|---|
| 149 | Connection con = null; |
|---|
| 150 | PreparedStatement pstmt = null; |
|---|
| 151 | try |
|---|
| 152 | { |
|---|
| 153 | con = getConnection(); |
|---|
| 154 | pstmt = con.prepareStatement(InsertRow.QUERY); |
|---|
| 155 | if (wrapPstmt) |
|---|
| 156 | { |
|---|
| 157 | pstmt = DbUtil.getLimitedBatchSizePreparedStatement( |
|---|
| 158 | pstmt, TEST_BATCH_SIZE); |
|---|
| 159 | } |
|---|
| 160 | pstmt.setInt(InsertRow.PARAM_ID, 1); |
|---|
| 161 | pstmt.setString(InsertRow.PARAM_NAME, "hans wurscht"); |
|---|
| 162 | pstmt.addBatch(); |
|---|
| 163 | |
|---|
| 164 | final int[] countArray = pstmt.executeBatch(); |
|---|
| 165 | assertEquals("Should have one statement", 1, countArray.length); |
|---|
| 166 | assertTrue("Should have one row updated", |
|---|
| 167 | 1 == countArray[0] // one row |
|---|
| 168 | || Statement.SUCCESS_NO_INFO == countArray[0] |
|---|
| 169 | ); |
|---|
| 170 | |
|---|
| 171 | final int[] secondUpdate = pstmt.executeBatch(); |
|---|
| 172 | assertEquals("Should have empty count array", 0, secondUpdate.length); |
|---|
| 173 | } |
|---|
| 174 | finally |
|---|
| 175 | { |
|---|
| 176 | DbUtil.close(pstmt); |
|---|
| 177 | DbUtil.close(con); |
|---|
| 178 | } |
|---|
| 179 | } |
|---|
| 180 | |
|---|
| 181 | private void doTestBatchWithExecute (boolean wrapPstmt) |
|---|
| 182 | throws Exception |
|---|
| 183 | { |
|---|
| 184 | Connection con = null; |
|---|
| 185 | PreparedStatement pstmt = null; |
|---|
| 186 | try |
|---|
| 187 | { |
|---|
| 188 | con = getConnection(); |
|---|
| 189 | pstmt = con.prepareStatement(InsertRow.QUERY); |
|---|
| 190 | if (wrapPstmt) |
|---|
| 191 | { |
|---|
| 192 | pstmt = DbUtil.getLimitedBatchSizePreparedStatement( |
|---|
| 193 | pstmt, TEST_BATCH_SIZE); |
|---|
| 194 | } |
|---|
| 195 | pstmt.setInt(InsertRow.PARAM_ID, 1); |
|---|
| 196 | pstmt.setString(InsertRow.PARAM_NAME, "hans wurscht"); |
|---|
| 197 | pstmt.addBatch(); |
|---|
| 198 | |
|---|
| 199 | pstmt.execute(); |
|---|
| 200 | fail("Execute should throw exception when statement has batch"); |
|---|
| 201 | } |
|---|
| 202 | catch (SQLException x) |
|---|
| 203 | { |
|---|
| 204 | // expected |
|---|
| 205 | } |
|---|
| 206 | finally |
|---|
| 207 | { |
|---|
| 208 | DbUtil.close(pstmt); |
|---|
| 209 | DbUtil.close(con); |
|---|
| 210 | } |
|---|
| 211 | |
|---|
| 212 | assertEquals("Expected no rows", 0, countTable()); |
|---|
| 213 | } |
|---|
| 214 | |
|---|
| 215 | private void doTestClearStatement (boolean wrapPstmt) |
|---|
| 216 | throws Exception |
|---|
| 217 | { |
|---|
| 218 | Connection con = null; |
|---|
| 219 | PreparedStatement pstmt = null; |
|---|
| 220 | try |
|---|
| 221 | { |
|---|
| 222 | con = getConnection(); |
|---|
| 223 | pstmt = con.prepareStatement(InsertRow.QUERY); |
|---|
| 224 | if (wrapPstmt) |
|---|
| 225 | { |
|---|
| 226 | pstmt = DbUtil.getLimitedBatchSizePreparedStatement( |
|---|
| 227 | pstmt, TEST_BATCH_SIZE); |
|---|
| 228 | } |
|---|
| 229 | pstmt.setInt(InsertRow.PARAM_ID, 1); |
|---|
| 230 | pstmt.setString(InsertRow.PARAM_NAME, "hans wurscht"); |
|---|
| 231 | pstmt.addBatch(); |
|---|
| 232 | |
|---|
| 233 | pstmt.clearBatch(); |
|---|
| 234 | |
|---|
| 235 | pstmt.executeBatch(); |
|---|
| 236 | } |
|---|
| 237 | finally |
|---|
| 238 | { |
|---|
| 239 | DbUtil.close(pstmt); |
|---|
| 240 | DbUtil.close(con); |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | assertEquals("Expected no rows", 0, countTable()); |
|---|
| 244 | |
|---|
| 245 | } |
|---|
| 246 | |
|---|
| 247 | private int countTable () |
|---|
| 248 | throws NamingException, SQLException |
|---|
| 249 | { |
|---|
| 250 | Connection con = null; |
|---|
| 251 | Statement stmt = null; |
|---|
| 252 | ResultSet rset = null; |
|---|
| 253 | final int count; |
|---|
| 254 | try |
|---|
| 255 | { |
|---|
| 256 | con = getConnection(); |
|---|
| 257 | stmt = con.createStatement(); |
|---|
| 258 | rset = stmt.executeQuery(CountRows.QUERY); |
|---|
| 259 | assertTrue("Empty result set", rset.next()); |
|---|
| 260 | count = rset.getInt(CountRows.RESULT_COUNT); |
|---|
| 261 | } |
|---|
| 262 | finally |
|---|
| 263 | { |
|---|
| 264 | DbUtil.close(stmt); |
|---|
| 265 | DbUtil.close(con); |
|---|
| 266 | } |
|---|
| 267 | return count; |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | private Connection getConnection () |
|---|
| 271 | throws NamingException, SQLException |
|---|
| 272 | { |
|---|
| 273 | final Context ctx = new InitialContext(); |
|---|
| 274 | final DataSource ds = (DataSource) ctx.lookup("FIXME"); |
|---|
| 275 | return ds.getConnection(); |
|---|
| 276 | } |
|---|
| 277 | |
|---|
| 278 | private static final class InsertRow |
|---|
| 279 | { |
|---|
| 280 | static final String QUERY |
|---|
| 281 | = "INSERT INTO tst_dbutil (id, name) VALUES (?, ?)"; |
|---|
| 282 | static final int PARAM_ID = 1; |
|---|
| 283 | static final int PARAM_NAME = 2; |
|---|
| 284 | } |
|---|
| 285 | |
|---|
| 286 | private static final class CountRows |
|---|
| 287 | { |
|---|
| 288 | static final String QUERY = "SELECT count(*) FROM tst_dbutil"; |
|---|
| 289 | static final int RESULT_COUNT = 1; |
|---|
| 290 | } |
|---|
| 291 | } |
|---|