root/trunk/test/java/org/jcoderz/commons/util/DbUtilTest.java

Revision 1568, 10.6 kB (checked in by amandel, 3 years ago)

Disable testcase that tests disabled functionality....

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