Project Report: fawkez

Packagesummary org.jcoderz.commons.util

org.jcoderz.commons.util.DbUtilServerTest

LineHitsNoteSource
1  /*
2   * $Id: DbUtilServerTest.java 1011 2008-06-16 17:57:36Z 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.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   */
510 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     {
600       Connection con = null;
610       Statement stmt = null;
62        try
63        {
640          con = getConnection();
650          stmt = con.createStatement();
66           try
67           {
680             stmt.executeUpdate("DROP TABLE tst_dbutil");
69           }
700          catch (SQLException x)
71           {
72              // ignore, table might not exist
730          }
740          stmt.executeUpdate(
75                 "CREATE TABLE tst_dbutil "
76                 + "(id number(10) primary key, name varchar2(100))");
77        }
78        finally
79        {
800          DbUtil.close(stmt);
810          DbUtil.close(con);
820       }
830    }
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     {
930       doTestDuplicateBatchUpdate(false);
940    }
95  
96 (1)   public void testDuplicateBatchUpdateLbsStatement ()
97           throws Exception
98     {
990       doTestDuplicateBatchUpdate(true);
1000    }
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     {
1100       doTestBatchWithExecute(false);
1110    }
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     {
1210       doTestBatchWithExecute(true);
1220    }
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     {
1320       doTestClearStatement(false);
1330    }
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     {
1430       doTestClearStatement(true);
1440    }
145  
146     private void doTestDuplicateBatchUpdate (boolean wrapPstmt)
147           throws Exception
148     {
1490       Connection con = null;
1500       PreparedStatement pstmt = null;
151        try
152        {
1530          con = getConnection();
1540          pstmt = con.prepareStatement(InsertRow.QUERY);
1550          if (wrapPstmt)
156           {
1570             pstmt = DbUtil.getLimitedBatchSizePreparedStatement(
158                    pstmt, TEST_BATCH_SIZE);
159           }
1600          pstmt.setInt(InsertRow.PARAM_ID, 1);
1610          pstmt.setString(InsertRow.PARAM_NAME, "hans wurscht");
1620          pstmt.addBatch();
163  
1640          final int[] countArray = pstmt.executeBatch();
1650          assertEquals("Should have one statement", 1, countArray.length);
1660          assertTrue("Should have one row updated",
167                 1 == countArray[0] // one row
168                 || Statement.SUCCESS_NO_INFO == countArray[0]
169              );
170  
1710          final int[] secondUpdate = pstmt.executeBatch();
1720          assertEquals("Should have empty count array", 0, secondUpdate.length);
173        }
174        finally
175        {
1760          DbUtil.close(pstmt);
1770          DbUtil.close(con);
1780       }
1790    }
180  
181     private void doTestBatchWithExecute (boolean wrapPstmt)
182           throws Exception
183     {
1840       Connection con = null;
1850       PreparedStatement pstmt = null;
186        try
187        {
1880          con = getConnection();
1890          pstmt = con.prepareStatement(InsertRow.QUERY);
1900          if (wrapPstmt)
191           {
1920             pstmt = DbUtil.getLimitedBatchSizePreparedStatement(
193                    pstmt, TEST_BATCH_SIZE);
194           }
1950          pstmt.setInt(InsertRow.PARAM_ID, 1);
1960          pstmt.setString(InsertRow.PARAM_NAME, "hans wurscht");
1970          pstmt.addBatch();
198  
1990          pstmt.execute();
2000          fail("Execute should throw exception when statement has batch");
201        }
2020       catch (SQLException x)
203        {
204           // expected
205        }
206        finally
207        {
2080          DbUtil.close(pstmt);
2090          DbUtil.close(con);
2100       }
211  
2120       assertEquals("Expected no rows", 0, countTable());
2130    }
214  
215     private void doTestClearStatement (boolean wrapPstmt)
216           throws Exception
217     {
2180       Connection con = null;
2190       PreparedStatement pstmt = null;
220        try
221        {
2220          con = getConnection();
2230          pstmt = con.prepareStatement(InsertRow.QUERY);
2240          if (wrapPstmt)
225           {
2260             pstmt = DbUtil.getLimitedBatchSizePreparedStatement(
227                    pstmt, TEST_BATCH_SIZE);
228           }
2290          pstmt.setInt(InsertRow.PARAM_ID, 1);
2300          pstmt.setString(InsertRow.PARAM_NAME, "hans wurscht");
2310          pstmt.addBatch();
232  
2330          pstmt.clearBatch();
234  
2350          pstmt.executeBatch();
236        }
237        finally
238        {
2390          DbUtil.close(pstmt);
2400          DbUtil.close(con);
2410       }
242  
2430       assertEquals("Expected no rows", 0, countTable());
244  
2450    }
246  
247     private int countTable ()
248           throws NamingException, SQLException
249     {
2500       Connection con = null;
2510       Statement stmt = null;
2520       ResultSet rset = null;
253        final int count;
254        try
255        {
2560          con = getConnection();
2570          stmt = con.createStatement();
2580(2)         rset = stmt.executeQuery(CountRows.QUERY);
2590          assertTrue("Empty result set", rset.next());
2600          count = rset.getInt(CountRows.RESULT_COUNT);
261        }
262        finally
263        {
2640          DbUtil.close(stmt);
2650          DbUtil.close(con);
2660       }
2670       return count;
268     }
269  
270     private Connection getConnection ()
271           throws NamingException, SQLException
272     {
2730       final Context ctx = new InitialContext();
2740       final DataSource ds = (DataSource) ctx.lookup("FIXME");
2750       return ds.getConnection();
276     }
277  
2780(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  
2860(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  }

Findings in this File

f (5) A method/constructor shouldn't explicitly throw java.lang.Exception Not required for testcode.
f (6) A method/constructor shouldn't explicitly throw java.lang.Exception Not required for testcode.
f (7) A method/constructor shouldn't explicitly throw java.lang.Exception Not required for testcode.
f (8) A method/constructor shouldn't explicitly throw java.lang.Exception Not required for testcode.
c (1) 96 : 4 Missing a Javadoc comment.
i (2) 258 : 0 Method org.jcoderz.commons.util.DbUtilServerTest.countTable() may fail to clean up stream or resource of type java.sql.ResultSet (test code) Decreased severity from 'warning' for testcode.
c (3) 278 : 4 Utility classes should not have a public or default constructor.
c (4) 286 : 4 Utility classes should not have a public or default constructor.