Project Report: fawkez

Packagesummary org.jcoderz.commons.util

org.jcoderz.commons.util.DbUtilTest

LineHitsNoteSource
1  /*
2   * $Id: DbUtilTest.java 1568 2009-11-01 17:46:14Z amandel $
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.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   * Tests the DbUtil class.
49   * @author Albrecht Messner
50   */
51100 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      * 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     {
6970       final Class[] testInterfaces = {
70              Statement.class,
71              Connection.class,
72              ResultSet.class
73           };
74  
75100       for (int i = 0; i < testInterfaces.length; i++)
76        {
77100          final TestInvocationHandler invHandler = new TestInvocationHandler();
7875          final Object obj = Proxy.newProxyInstance(
79                 Statement.class.getClassLoader(),
80                 new Class[] {testInterfaces[i]},
81                 invHandler);
82100          if (obj instanceof Statement)
83           {
84100             DbUtil.close((Statement) obj);
85           }
86100          else if (obj instanceof Connection)
87           {
88100             DbUtil.close((Connection) obj);
89           }
90           else
91           {
92100             DbUtil.close((ResultSet) obj);
93           }
94100          assertTrue("Close should have been called on the Statement object",
95                 invHandler.isCloseCalled());
96        }
97100    }
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     {
106100       final TestInvocationHandler invHandler = new TestInvocationHandler();
10771       final PreparedStatement pstmt
108              = (PreparedStatement) Proxy.newProxyInstance(
109                 PreparedStatement.class.getClassLoader(),
110                 new Class[] {PreparedStatement.class},
111                 invHandler);
112  
113100       final String s1 = "Hello World";
114100       DbUtil.setCharOrNull(pstmt, 0, s1);
115100       assertEquals(WRONG_METHOD_CALLED,
116              "setString", invHandler.getLastMethod());
117100       DbUtil.setVarcharOrNull(pstmt, 0, s1);
118100       assertEquals(WRONG_METHOD_CALLED,
119              "setString", invHandler.getLastMethod());
120100       DbUtil.setClobOrNull(pstmt, 0, s1);
121100       assertEquals(WRONG_METHOD_CALLED,
122              "setCharacterStream", invHandler.getLastMethod());
123  
124100       final Integer i = new Integer(0);
125100       DbUtil.setIntegerOrNull(pstmt, 0, i);
126100       assertEquals(WRONG_METHOD_CALLED,
127              "setInt", invHandler.getLastMethod());
128  
129100       final Long l = new Long(0);
130100       DbUtil.setLongOrNull(pstmt, 0, l);
131100       assertEquals(WRONG_METHOD_CALLED,
132              "setLong", invHandler.getLastMethod());
133  
134100       final Timestamp tstamp = new Timestamp(0);
135100       DbUtil.setTimestampOrNull(pstmt, 0, tstamp);
136100       assertEquals(WRONG_METHOD_CALLED,
137              "setTimestamp", invHandler.getLastMethod());
138100    }
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     {
147100       final TestInvocationHandler invHandler = new TestInvocationHandler();
14871       final PreparedStatement pstmt
149              = (PreparedStatement) Proxy.newProxyInstance(
150                 PreparedStatement.class.getClassLoader(),
151                 new Class[] {PreparedStatement.class},
152                 invHandler);
153100       DbUtil.setCharOrNull(pstmt, 0, null);
154100       assertEquals(WRONG_TYPE_CODE, invHandler.getTypeCode(), Types.CHAR);
155100       DbUtil.setClobOrNull(pstmt, 0, null);
156100       assertEquals(WRONG_TYPE_CODE, invHandler.getTypeCode(), Types.CLOB);
157100       DbUtil.setIntegerOrNull(pstmt, 0, null);
158100       assertEquals(WRONG_TYPE_CODE, invHandler.getTypeCode(), Types.INTEGER);
159100       DbUtil.setLongOrNull(pstmt, 0, null);
160100       assertEquals(WRONG_TYPE_CODE, invHandler.getTypeCode(), Types.INTEGER);
161100       DbUtil.setTimestampOrNull(pstmt, 0, null);
162100       assertEquals(WRONG_TYPE_CODE,
163              invHandler.getTypeCode(), Types.TIMESTAMP);
164100       DbUtil.setVarcharOrNull(pstmt, 0, null);
165100       assertEquals(WRONG_TYPE_CODE, invHandler.getTypeCode(), Types.VARCHAR);
166100    }
167  
168     /**
169      * Tests closing of a null java.sql.Statement, Connection and ResultSet.
170      */
171     public void testCloseNullObjects ()
172     {
173        try
174        {
175100          final Statement stmt = null;
176100          DbUtil.close(stmt);
177  
178100          final Connection con = null;
179100          DbUtil.close(con);
180  
181100          final ResultSet rset = null;
182100          DbUtil.close(rset);
183        }
1840       catch (Exception x)
185        {
1860          fail("No exception expected when closing a null object");
187100       }
188100    }
189  
190 (1)   public void xtestLimitedBatchSizePreparedStatement ()
191           throws Exception
192     {
1930       final PreparedStatementHandler pstmtHandler
194              = new PreparedStatementHandler();
1950       final PreparedStatement pstmt
196              = (PreparedStatement) Proxy.newProxyInstance(
197                 PreparedStatement.class.getClassLoader(),
198                 new Class[] {PreparedStatement.class},
199                 pstmtHandler);
2000       final PreparedStatement wrappedPstmt
201              = DbUtil.getLimitedBatchSizePreparedStatement(
202                 pstmt, TEST_BATCH_SIZE);
203  
2040       int batchCount = 0;
2050       for (int i = 0; i < TEST_LOOPS; i++)
206        {
2070          wrappedPstmt.addBatch();
2080          batchCount++;
2090          if (batchCount == TEST_BATCH_SIZE + 1)
210           {
2110             assertTrue("Batch update not called",
212                    pstmtHandler.isExecuteBatchCalled());
2130             assertEquals("Wrong batch count",
214                    pstmtHandler.getLastUpdatedCount(), TEST_BATCH_SIZE);
2150             batchCount = 1;
216           }
217        }
2180       final int[] updateCount = wrappedPstmt.executeBatch();
2190       assertEquals("Wrong update counts.", TEST_LOOPS, updateCount.length);
2200       for (int i = 0; i < updateCount.length; i++)
221        {
2220          assertEquals(
223                 "Wrong update count", Statement.SUCCESS_NO_INFO, updateCount[i]);
224        }
2250    }
226  
227100    private static final class TestInvocationHandler
228           implements InvocationHandler
229     {
230100       private boolean mCloseCalled = false;
231        private String mLastMethod;
232100       private int mTypeCode = 0;
233  
234        public Object invoke (Object proxy, Method method, Object[] args)
235              throws Throwable
236        {
237100(2)         if (method.getName().equals("close"))
238           {
239100             mCloseCalled = true;
240           }
241100(3)         else if (method.getName().equals("setNull"))
242           {
243100(4)            final Integer arg2 = (Integer) args[1];
244100             mTypeCode = arg2.intValue();
245           }
246100          mLastMethod = method.getName();
247100          return null;
248        }
249  
250        public boolean isCloseCalled ()
251        {
252100          return mCloseCalled;
253        }
254  
255        public String getLastMethod ()
256        {
257100          return mLastMethod;
258        }
259  
260        public int getTypeCode ()
261        {
262100          return mTypeCode;
263        }
264     }
265  
2660    private static final class PreparedStatementHandler
267           implements InvocationHandler
268     {
2690       private int mBatchCount = 0;
2700       private boolean mExecuteBatchCalled = false;
2710       private boolean mClearBatchCalled = true;
2720       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  
2800(5)         if (method.getName().equals("addBatch"))
281           {
2820             mBatchCount++;
2830             result = null;
284           }
2850(6)         else if (method.getName().equals("executeBatch"))
286           {
2870             mExecuteBatchCalled = true;
2880             mLastUpdated = mBatchCount;
2890             mBatchCount = 0;
2900             result = new int[mLastUpdated];
2910             Arrays.fill((int[]) result, Statement.SUCCESS_NO_INFO);
292           }
2930(7)         else if (method.getName().equals("clearBatch"))
294           {
2950             mClearBatchCalled = true;
2960             result = null;
297           }
298           else
299           {
3000             throw new UnsupportedOperationException(method.getName());
301           }
302  
3030          return result;
304        }
305  
306        /**
307         * @return Returns the executeBatchCalled.
308         */
309        public boolean isExecuteBatchCalled ()
310        {
3110          return mExecuteBatchCalled;
312        }
313  
314        /**
315         *
316         */
317        public void resetExecuteBatchCalled ()
318        {
3190          mExecuteBatchCalled = false;
3200       }
321  
322        /**
323         * @return Returns the batchCount.
324         */
325        public int getLastUpdatedCount ()
326        {
3270          return mLastUpdated;
328        }
329  
330        /**
331         * @return Returns the clearBatchCalled.
332         */
333        public boolean isClearBatchCalled ()
334        {
3350          return mClearBatchCalled;
336        }
337  
338        /**
339         *
340         */
341        public void resetClearBatchCalled ()
342        {
3430          mClearBatchCalled = false;
3440       }
345     }
346  }

Findings in this File

f (8) A method/constructor shouldn't explicitly throw java.lang.Exception Not required for testcode.
c (1) 190 : 4 Missing a Javadoc comment.
i (2) 237 : 0 method org.jcoderz.commons.util.DbUtilTest$TestInvocationHandler.invoke(Object, Method, Object[]) makes literal string comparisons passing the literal as an argument (test code)
i (3) 241 : 0 method org.jcoderz.commons.util.DbUtilTest$TestInvocationHandler.invoke(Object, Method, Object[]) makes literal string comparisons passing the literal as an argument (test code)
i (4) 243 : 0 method org.jcoderz.commons.util.DbUtilTest$TestInvocationHandler.invoke(Object, Method, Object[]) accesses list or array with constant index (test code) Decreased severity from 'warning' for testcode.
i (5) 280 : 0 method org.jcoderz.commons.util.DbUtilTest$PreparedStatementHandler.invoke(Object, Method, Object[]) makes literal string comparisons passing the literal as an argument (test code)
i (6) 285 : 0 method org.jcoderz.commons.util.DbUtilTest$PreparedStatementHandler.invoke(Object, Method, Object[]) makes literal string comparisons passing the literal as an argument (test code)
i (7) 293 : 0 method org.jcoderz.commons.util.DbUtilTest$PreparedStatementHandler.invoke(Object, Method, Object[]) makes literal string comparisons passing the literal as an argument (test code)