| 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.lang.reflect.InvocationHandler; |
| 36 | | | import java.lang.reflect.Method; |
| 37 | | | import java.lang.reflect.Proxy; |
| 38 | | | import java.sql.Connection; |
| 39 | | | import java.sql.PreparedStatement; |
| 40 | | | import java.sql.ResultSet; |
| 41 | | | import java.sql.Statement; |
| 42 | | | import java.sql.Timestamp; |
| 43 | | | import java.sql.Types; |
| 44 | | | import java.util.Arrays; |
| 45 | | | import junit.framework.TestCase; |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | | @author |
| 50 | | | |
| 51 | 100 | | public class DbUtilTest |
| 52 | | | extends TestCase |
| 53 | | | { |
| 54 | | | private static final int TEST_BATCH_SIZE = 10; |
| 55 | | | private static final int TEST_LOOPS = 50; |
| 56 | | | |
| 57 | | | private static final String WRONG_TYPE_CODE = "Wrong type code"; |
| 58 | | | private static final String WRONG_METHOD_CALLED |
| 59 | | | = "Wrong method called on prepared statement"; |
| 60 | | | |
| 61 | | | |
| 62 | | | |
| 63 | | | |
| 64 | | | @throws |
| 65 | | | |
| 66 | | | public void testCloseObject () |
| 67 | | | throws Exception |
| 68 | | | { |
| 69 | 70 | | final Class[] testInterfaces = { |
| 70 | | | Statement.class, |
| 71 | | | Connection.class, |
| 72 | | | ResultSet.class |
| 73 | | | }; |
| 74 | | | |
| 75 | 100 | | for (int i = 0; i < testInterfaces.length; i++) |
| 76 | | | { |
| 77 | 100 | | final TestInvocationHandler invHandler = new TestInvocationHandler(); |
| 78 | 75 | | final Object obj = Proxy.newProxyInstance( |
| 79 | | | Statement.class.getClassLoader(), |
| 80 | | | new Class[] {testInterfaces[i]}, |
| 81 | | | invHandler); |
| 82 | 100 | | if (obj instanceof Statement) |
| 83 | | | { |
| 84 | 100 | | DbUtil.close((Statement) obj); |
| 85 | | | } |
| 86 | 100 | | else if (obj instanceof Connection) |
| 87 | | | { |
| 88 | 100 | | DbUtil.close((Connection) obj); |
| 89 | | | } |
| 90 | | | else |
| 91 | | | { |
| 92 | 100 | | DbUtil.close((ResultSet) obj); |
| 93 | | | } |
| 94 | 100 | | assertTrue("Close should have been called on the Statement object", |
| 95 | | | invHandler.isCloseCalled()); |
| 96 | | | } |
| 97 | 100 | | } |
| 98 | | | |
| 99 | | | |
| 100 | | | |
| 101 | | | @throws |
| 102 | | | |
| 103 | | | public void testParameterSetters () |
| 104 | | | throws Exception |
| 105 | | | { |
| 106 | 100 | | final TestInvocationHandler invHandler = new TestInvocationHandler(); |
| 107 | 71 | | final PreparedStatement pstmt |
| 108 | | | = (PreparedStatement) Proxy.newProxyInstance( |
| 109 | | | PreparedStatement.class.getClassLoader(), |
| 110 | | | new Class[] {PreparedStatement.class}, |
| 111 | | | invHandler); |
| 112 | | | |
| 113 | 100 | | final String s1 = "Hello World"; |
| 114 | 100 | | DbUtil.setCharOrNull(pstmt, 0, s1); |
| 115 | 100 | | assertEquals(WRONG_METHOD_CALLED, |
| 116 | | | "setString", invHandler.getLastMethod()); |
| 117 | 100 | | DbUtil.setVarcharOrNull(pstmt, 0, s1); |
| 118 | 100 | | assertEquals(WRONG_METHOD_CALLED, |
| 119 | | | "setString", invHandler.getLastMethod()); |
| 120 | 100 | | DbUtil.setClobOrNull(pstmt, 0, s1); |
| 121 | 100 | | assertEquals(WRONG_METHOD_CALLED, |
| 122 | | | "setCharacterStream", invHandler.getLastMethod()); |
| 123 | | | |
| 124 | 100 | | final Integer i = new Integer(0); |
| 125 | 100 | | DbUtil.setIntegerOrNull(pstmt, 0, i); |
| 126 | 100 | | assertEquals(WRONG_METHOD_CALLED, |
| 127 | | | "setInt", invHandler.getLastMethod()); |
| 128 | | | |
| 129 | 100 | | final Long l = new Long(0); |
| 130 | 100 | | DbUtil.setLongOrNull(pstmt, 0, l); |
| 131 | 100 | | assertEquals(WRONG_METHOD_CALLED, |
| 132 | | | "setLong", invHandler.getLastMethod()); |
| 133 | | | |
| 134 | 100 | | final Timestamp tstamp = new Timestamp(0); |
| 135 | 100 | | DbUtil.setTimestampOrNull(pstmt, 0, tstamp); |
| 136 | 100 | | assertEquals(WRONG_METHOD_CALLED, |
| 137 | | | "setTimestamp", invHandler.getLastMethod()); |
| 138 | 100 | | } |
| 139 | | | |
| 140 | | | |
| 141 | | | |
| 142 | | | @throws |
| 143 | | | |
| 144 | | | public void testSetNullParameters () |
| 145 | | | throws Exception |
| 146 | | | { |
| 147 | 100 | | final TestInvocationHandler invHandler = new TestInvocationHandler(); |
| 148 | 71 | | final PreparedStatement pstmt |
| 149 | | | = (PreparedStatement) Proxy.newProxyInstance( |
| 150 | | | PreparedStatement.class.getClassLoader(), |
| 151 | | | new Class[] {PreparedStatement.class}, |
| 152 | | | invHandler); |
| 153 | 100 | | DbUtil.setCharOrNull(pstmt, 0, null); |
| 154 | 100 | | assertEquals(WRONG_TYPE_CODE, invHandler.getTypeCode(), Types.CHAR); |
| 155 | 100 | | DbUtil.setClobOrNull(pstmt, 0, null); |
| 156 | 100 | | assertEquals(WRONG_TYPE_CODE, invHandler.getTypeCode(), Types.CLOB); |
| 157 | 100 | | DbUtil.setIntegerOrNull(pstmt, 0, null); |
| 158 | 100 | | assertEquals(WRONG_TYPE_CODE, invHandler.getTypeCode(), Types.INTEGER); |
| 159 | 100 | | DbUtil.setLongOrNull(pstmt, 0, null); |
| 160 | 100 | | assertEquals(WRONG_TYPE_CODE, invHandler.getTypeCode(), Types.INTEGER); |
| 161 | 100 | | DbUtil.setTimestampOrNull(pstmt, 0, null); |
| 162 | 100 | | assertEquals(WRONG_TYPE_CODE, |
| 163 | | | invHandler.getTypeCode(), Types.TIMESTAMP); |
| 164 | 100 | | DbUtil.setVarcharOrNull(pstmt, 0, null); |
| 165 | 100 | | assertEquals(WRONG_TYPE_CODE, invHandler.getTypeCode(), Types.VARCHAR); |
| 166 | 100 | | } |
| 167 | | | |
| 168 | | | |
| 169 | | | |
| 170 | | | |
| 171 | | | public void testCloseNullObjects () |
| 172 | | | { |
| 173 | | | try |
| 174 | | | { |
| 175 | 100 | | final Statement stmt = null; |
| 176 | 100 | | DbUtil.close(stmt); |
| 177 | | | |
| 178 | 100 | | final Connection con = null; |
| 179 | 100 | | DbUtil.close(con); |
| 180 | | | |
| 181 | 100 | | final ResultSet rset = null; |
| 182 | 100 | | DbUtil.close(rset); |
| 183 | | | } |
| 184 | 0 | | catch (Exception x) |
| 185 | | | { |
| 186 | 0 | | fail("No exception expected when closing a null object"); |
| 187 | 100 | | } |
| 188 | 100 | | } |
| 189 | | | |
| 190 | | (1) | public void xtestLimitedBatchSizePreparedStatement () |
| 191 | | | throws Exception |
| 192 | | | { |
| 193 | 0 | | final PreparedStatementHandler pstmtHandler |
| 194 | | | = new PreparedStatementHandler(); |
| 195 | 0 | | final PreparedStatement pstmt |
| 196 | | | = (PreparedStatement) Proxy.newProxyInstance( |
| 197 | | | PreparedStatement.class.getClassLoader(), |
| 198 | | | new Class[] {PreparedStatement.class}, |
| 199 | | | pstmtHandler); |
| 200 | 0 | | final PreparedStatement wrappedPstmt |
| 201 | | | = DbUtil.getLimitedBatchSizePreparedStatement( |
| 202 | | | pstmt, TEST_BATCH_SIZE); |
| 203 | | | |
| 204 | 0 | | int batchCount = 0; |
| 205 | 0 | | for (int i = 0; i < TEST_LOOPS; i++) |
| 206 | | | { |
| 207 | 0 | | wrappedPstmt.addBatch(); |
| 208 | 0 | | batchCount++; |
| 209 | 0 | | if (batchCount == TEST_BATCH_SIZE + 1) |
| 210 | | | { |
| 211 | 0 | | assertTrue("Batch update not called", |
| 212 | | | pstmtHandler.isExecuteBatchCalled()); |
| 213 | 0 | | assertEquals("Wrong batch count", |
| 214 | | | pstmtHandler.getLastUpdatedCount(), TEST_BATCH_SIZE); |
| 215 | 0 | | batchCount = 1; |
| 216 | | | } |
| 217 | | | } |
| 218 | 0 | | final int[] updateCount = wrappedPstmt.executeBatch(); |
| 219 | 0 | | assertEquals("Wrong update counts.", TEST_LOOPS, updateCount.length); |
| 220 | 0 | | for (int i = 0; i < updateCount.length; i++) |
| 221 | | | { |
| 222 | 0 | | assertEquals( |
| 223 | | | "Wrong update count", Statement.SUCCESS_NO_INFO, updateCount[i]); |
| 224 | | | } |
| 225 | 0 | | } |
| 226 | | | |
| 227 | 100 | | private static final class TestInvocationHandler |
| 228 | | | implements InvocationHandler |
| 229 | | | { |
| 230 | 100 | | private boolean mCloseCalled = false; |
| 231 | | | private String mLastMethod; |
| 232 | 100 | | private int mTypeCode = 0; |
| 233 | | | |
| 234 | | | public Object invoke (Object proxy, Method method, Object[] args) |
| 235 | | | throws Throwable |
| 236 | | | { |
| 237 | 100 | (2) | if (method.getName().equals("close")) |
| 238 | | | { |
| 239 | 100 | | mCloseCalled = true; |
| 240 | | | } |
| 241 | 100 | (3) | else if (method.getName().equals("setNull")) |
| 242 | | | { |
| 243 | 100 | (4) | final Integer arg2 = (Integer) args[1]; |
| 244 | 100 | | mTypeCode = arg2.intValue(); |
| 245 | | | } |
| 246 | 100 | | mLastMethod = method.getName(); |
| 247 | 100 | | return null; |
| 248 | | | } |
| 249 | | | |
| 250 | | | public boolean isCloseCalled () |
| 251 | | | { |
| 252 | 100 | | return mCloseCalled; |
| 253 | | | } |
| 254 | | | |
| 255 | | | public String getLastMethod () |
| 256 | | | { |
| 257 | 100 | | return mLastMethod; |
| 258 | | | } |
| 259 | | | |
| 260 | | | public int getTypeCode () |
| 261 | | | { |
| 262 | 100 | | return mTypeCode; |
| 263 | | | } |
| 264 | | | } |
| 265 | | | |
| 266 | 0 | | private static final class PreparedStatementHandler |
| 267 | | | implements InvocationHandler |
| 268 | | | { |
| 269 | 0 | | private int mBatchCount = 0; |
| 270 | 0 | | private boolean mExecuteBatchCalled = false; |
| 271 | 0 | | private boolean mClearBatchCalled = true; |
| 272 | 0 | | private int mLastUpdated = 0; |
| 273 | | | |
| 274 | | | {@inheritDoc} |
| 275 | | | public Object invoke (Object proxy, Method method, Object[] args) |
| 276 | | | throws Throwable |
| 277 | | | { |
| 278 | | | final Object result; |
| 279 | | | |
| 280 | 0 | (5) | if (method.getName().equals("addBatch")) |
| 281 | | | { |
| 282 | 0 | | mBatchCount++; |
| 283 | 0 | | result = null; |
| 284 | | | } |
| 285 | 0 | (6) | else if (method.getName().equals("executeBatch")) |
| 286 | | | { |
| 287 | 0 | | mExecuteBatchCalled = true; |
| 288 | 0 | | mLastUpdated = mBatchCount; |
| 289 | 0 | | mBatchCount = 0; |
| 290 | 0 | | result = new int[mLastUpdated]; |
| 291 | 0 | | Arrays.fill((int[]) result, Statement.SUCCESS_NO_INFO); |
| 292 | | | } |
| 293 | 0 | (7) | else if (method.getName().equals("clearBatch")) |
| 294 | | | { |
| 295 | 0 | | mClearBatchCalled = true; |
| 296 | 0 | | result = null; |
| 297 | | | } |
| 298 | | | else |
| 299 | | | { |
| 300 | 0 | | throw new UnsupportedOperationException(method.getName()); |
| 301 | | | } |
| 302 | | | |
| 303 | 0 | | return result; |
| 304 | | | } |
| 305 | | | |
| 306 | | | |
| 307 | | | @return |
| 308 | | | |
| 309 | | | public boolean isExecuteBatchCalled () |
| 310 | | | { |
| 311 | 0 | | return mExecuteBatchCalled; |
| 312 | | | } |
| 313 | | | |
| 314 | | | |
| 315 | | | |
| 316 | | | |
| 317 | | | public void resetExecuteBatchCalled () |
| 318 | | | { |
| 319 | 0 | | mExecuteBatchCalled = false; |
| 320 | 0 | | } |
| 321 | | | |
| 322 | | | |
| 323 | | | @return |
| 324 | | | |
| 325 | | | public int getLastUpdatedCount () |
| 326 | | | { |
| 327 | 0 | | return mLastUpdated; |
| 328 | | | } |
| 329 | | | |
| 330 | | | |
| 331 | | | @return |
| 332 | | | |
| 333 | | | public boolean isClearBatchCalled () |
| 334 | | | { |
| 335 | 0 | | return mClearBatchCalled; |
| 336 | | | } |
| 337 | | | |
| 338 | | | |
| 339 | | | |
| 340 | | | |
| 341 | | | public void resetClearBatchCalled () |
| 342 | | | { |
| 343 | 0 | | mClearBatchCalled = false; |
| 344 | 0 | | } |
| 345 | | | } |
| 346 | | | } |