| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 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 | | | |
| 49 | | | @author |
| 50 | | | |
| 51 | 0 | | 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 | 0 | | Connection con = null; |
| 61 | 0 | | Statement stmt = null; |
| 62 | | | try |
| 63 | | | { |
| 64 | 0 | | con = getConnection(); |
| 65 | 0 | | stmt = con.createStatement(); |
| 66 | | | try |
| 67 | | | { |
| 68 | 0 | | stmt.executeUpdate("DROP TABLE tst_dbutil"); |
| 69 | | | } |
| 70 | 0 | | catch (SQLException x) |
| 71 | | | { |
| 72 | | | |
| 73 | 0 | | } |
| 74 | 0 | | stmt.executeUpdate( |
| 75 | | | "CREATE TABLE tst_dbutil " |
| 76 | | | + "(id number(10) primary key, name varchar2(100))"); |
| 77 | | | } |
| 78 | | | finally |
| 79 | | | { |
| 80 | 0 | | DbUtil.close(stmt); |
| 81 | 0 | | DbUtil.close(con); |
| 82 | 0 | | } |
| 83 | 0 | | } |
| 84 | | | |
| 85 | | | |
| 86 | | | |
| 87 | | | |
| 88 | | | @throws |
| 89 | | | |
| 90 | | | public void testDuplicateBatchUpdate () |
| 91 | | | throws Exception |
| 92 | | | { |
| 93 | 0 | | doTestDuplicateBatchUpdate(false); |
| 94 | 0 | | } |
| 95 | | | |
| 96 | | (1) | public void testDuplicateBatchUpdateLbsStatement () |
| 97 | | | throws Exception |
| 98 | | | { |
| 99 | 0 | | doTestDuplicateBatchUpdate(true); |
| 100 | 0 | | } |
| 101 | | | |
| 102 | | | |
| 103 | | | <code></code> |
| 104 | | | <code></code> |
| 105 | | | @throws |
| 106 | | | |
| 107 | | | public void testBatchWithExecute () |
| 108 | | | throws Exception |
| 109 | | | { |
| 110 | 0 | | doTestBatchWithExecute(false); |
| 111 | 0 | | } |
| 112 | | | |
| 113 | | | |
| 114 | | | <code></code> |
| 115 | | | <code></code> |
| 116 | | | @throws |
| 117 | | | |
| 118 | | | public void testBatchWithExecuteLbsStatement () |
| 119 | | | throws Exception |
| 120 | | | { |
| 121 | 0 | | doTestBatchWithExecute(true); |
| 122 | 0 | | } |
| 123 | | | |
| 124 | | | |
| 125 | | | |
| 126 | | | |
| 127 | | | @throws |
| 128 | | | |
| 129 | | | public void testClearBatch () |
| 130 | | | throws Exception |
| 131 | | | { |
| 132 | 0 | | doTestClearStatement(false); |
| 133 | 0 | | } |
| 134 | | | |
| 135 | | | |
| 136 | | | |
| 137 | | | |
| 138 | | | @throws |
| 139 | | | |
| 140 | | | public void testClearBatchLbsStatement () |
| 141 | | | throws Exception |
| 142 | | | { |
| 143 | 0 | | doTestClearStatement(true); |
| 144 | 0 | | } |
| 145 | | | |
| 146 | | | private void doTestDuplicateBatchUpdate (boolean wrapPstmt) |
| 147 | | | throws Exception |
| 148 | | | { |
| 149 | 0 | | Connection con = null; |
| 150 | 0 | | PreparedStatement pstmt = null; |
| 151 | | | try |
| 152 | | | { |
| 153 | 0 | | con = getConnection(); |
| 154 | 0 | | pstmt = con.prepareStatement(InsertRow.QUERY); |
| 155 | 0 | | if (wrapPstmt) |
| 156 | | | { |
| 157 | 0 | | pstmt = DbUtil.getLimitedBatchSizePreparedStatement( |
| 158 | | | pstmt, TEST_BATCH_SIZE); |
| 159 | | | } |
| 160 | 0 | | pstmt.setInt(InsertRow.PARAM_ID, 1); |
| 161 | 0 | | pstmt.setString(InsertRow.PARAM_NAME, "hans wurscht"); |
| 162 | 0 | | pstmt.addBatch(); |
| 163 | | | |
| 164 | 0 | | final int[] countArray = pstmt.executeBatch(); |
| 165 | 0 | | assertEquals("Should have one statement", 1, countArray.length); |
| 166 | 0 | | assertTrue("Should have one row updated", |
| 167 | | | 1 == countArray[0] |
| 168 | | | || Statement.SUCCESS_NO_INFO == countArray[0] |
| 169 | | | ); |
| 170 | | | |
| 171 | 0 | | final int[] secondUpdate = pstmt.executeBatch(); |
| 172 | 0 | | assertEquals("Should have empty count array", 0, secondUpdate.length); |
| 173 | | | } |
| 174 | | | finally |
| 175 | | | { |
| 176 | 0 | | DbUtil.close(pstmt); |
| 177 | 0 | | DbUtil.close(con); |
| 178 | 0 | | } |
| 179 | 0 | | } |
| 180 | | | |
| 181 | | | private void doTestBatchWithExecute (boolean wrapPstmt) |
| 182 | | | throws Exception |
| 183 | | | { |
| 184 | 0 | | Connection con = null; |
| 185 | 0 | | PreparedStatement pstmt = null; |
| 186 | | | try |
| 187 | | | { |
| 188 | 0 | | con = getConnection(); |
| 189 | 0 | | pstmt = con.prepareStatement(InsertRow.QUERY); |
| 190 | 0 | | if (wrapPstmt) |
| 191 | | | { |
| 192 | 0 | | pstmt = DbUtil.getLimitedBatchSizePreparedStatement( |
| 193 | | | pstmt, TEST_BATCH_SIZE); |
| 194 | | | } |
| 195 | 0 | | pstmt.setInt(InsertRow.PARAM_ID, 1); |
| 196 | 0 | | pstmt.setString(InsertRow.PARAM_NAME, "hans wurscht"); |
| 197 | 0 | | pstmt.addBatch(); |
| 198 | | | |
| 199 | 0 | | pstmt.execute(); |
| 200 | 0 | | fail("Execute should throw exception when statement has batch"); |
| 201 | | | } |
| 202 | 0 | | catch (SQLException x) |
| 203 | | | { |
| 204 | | | |
| 205 | | | } |
| 206 | | | finally |
| 207 | | | { |
| 208 | 0 | | DbUtil.close(pstmt); |
| 209 | 0 | | DbUtil.close(con); |
| 210 | 0 | | } |
| 211 | | | |
| 212 | 0 | | assertEquals("Expected no rows", 0, countTable()); |
| 213 | 0 | | } |
| 214 | | | |
| 215 | | | private void doTestClearStatement (boolean wrapPstmt) |
| 216 | | | throws Exception |
| 217 | | | { |
| 218 | 0 | | Connection con = null; |
| 219 | 0 | | PreparedStatement pstmt = null; |
| 220 | | | try |
| 221 | | | { |
| 222 | 0 | | con = getConnection(); |
| 223 | 0 | | pstmt = con.prepareStatement(InsertRow.QUERY); |
| 224 | 0 | | if (wrapPstmt) |
| 225 | | | { |
| 226 | 0 | | pstmt = DbUtil.getLimitedBatchSizePreparedStatement( |
| 227 | | | pstmt, TEST_BATCH_SIZE); |
| 228 | | | } |
| 229 | 0 | | pstmt.setInt(InsertRow.PARAM_ID, 1); |
| 230 | 0 | | pstmt.setString(InsertRow.PARAM_NAME, "hans wurscht"); |
| 231 | 0 | | pstmt.addBatch(); |
| 232 | | | |
| 233 | 0 | | pstmt.clearBatch(); |
| 234 | | | |
| 235 | 0 | | pstmt.executeBatch(); |
| 236 | | | } |
| 237 | | | finally |
| 238 | | | { |
| 239 | 0 | | DbUtil.close(pstmt); |
| 240 | 0 | | DbUtil.close(con); |
| 241 | 0 | | } |
| 242 | | | |
| 243 | 0 | | assertEquals("Expected no rows", 0, countTable()); |
| 244 | | | |
| 245 | 0 | | } |
| 246 | | | |
| 247 | | | private int countTable () |
| 248 | | | throws NamingException, SQLException |
| 249 | | | { |
| 250 | 0 | | Connection con = null; |
| 251 | 0 | | Statement stmt = null; |
| 252 | 0 | | ResultSet rset = null; |
| 253 | | | final int count; |
| 254 | | | try |
| 255 | | | { |
| 256 | 0 | | con = getConnection(); |
| 257 | 0 | | stmt = con.createStatement(); |
| 258 | 0 | (2) | rset = stmt.executeQuery(CountRows.QUERY); |
| 259 | 0 | | assertTrue("Empty result set", rset.next()); |
| 260 | 0 | | count = rset.getInt(CountRows.RESULT_COUNT); |
| 261 | | | } |
| 262 | | | finally |
| 263 | | | { |
| 264 | 0 | | DbUtil.close(stmt); |
| 265 | 0 | | DbUtil.close(con); |
| 266 | 0 | | } |
| 267 | 0 | | return count; |
| 268 | | | } |
| 269 | | | |
| 270 | | | private Connection getConnection () |
| 271 | | | throws NamingException, SQLException |
| 272 | | | { |
| 273 | 0 | | final Context ctx = new InitialContext(); |
| 274 | 0 | | final DataSource ds = (DataSource) ctx.lookup("FIXME"); |
| 275 | 0 | | return ds.getConnection(); |
| 276 | | | } |
| 277 | | | |
| 278 | 0 | (3) | 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 | 0 | (4) | 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 | | | } |