View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.dbutils;
18  
19  import java.math.BigInteger;
20  import java.sql.ResultSet;
21  import java.sql.ResultSetMetaData;
22  import java.sql.Timestamp;
23  import java.util.Date;
24  
25  import junit.framework.TestCase;
26  
27  /**
28   * BaseTestCase is the base class for all test cases as well as the "all tests"
29   * runner.
30   */
31  public class BaseTestCase extends TestCase {
32  
33      private static final String[] columnNames =
34          {
35          "one",
36          "two",
37          "three",
38          "notInBean",
39          "intTest",
40          "integerTest",
41          "nullObjectTest",
42          "nullPrimitiveTest",
43          "notDate",
44          "columnProcessorDoubleTest",
45          null
46      };
47  
48      /**
49       * The number of columns in the MockResultSet.
50       */
51      protected static final int COLS = columnNames.length;
52  
53      protected static final ResultSetMetaData metaData =
54          MockResultSetMetaData.create(columnNames);
55  
56      /**
57       * A Timestamp for test purposes having 9 decimals
58       */
59      static final Timestamp ts789456123;
60  
61      static {
62          ts789456123 = new Timestamp(new Date().getTime());
63          ts789456123.setNanos(789456123);
64      }
65  
66      private static final Object[] row1 =
67          {
68          "1",
69          "2",
70          "THREE",
71          "  notInBean  ",
72          Integer.valueOf(1),
73          Integer.valueOf(2),
74          null,
75          null,
76          new Date(),
77          BigInteger.valueOf(13),
78          null
79      };
80  
81      private static final Object[] row2 =
82          {
83          "4",
84          "5",
85          "SIX",
86          "  notInBean  ",
87          Integer.valueOf(3),
88          Integer.valueOf(4),
89          null,
90          null,
91          ts789456123,
92          BigInteger.valueOf(13),
93          null
94      };
95  
96      private static final Object[][] rows = { row1, row2 };
97  
98      /**
99       * The number of rows in the MockResultSet.
100      */
101     protected static final int ROWS = rows.length;
102 
103     /**
104      * The ResultSet all test methods will use.
105      */
106     protected ResultSet rs = null;
107 
108     /**
109      * A ResultSet with 0 rows.
110      */
111     protected ResultSet emptyResultSet = null;
112 
113     /**
114      * Creates a freshly initialized ResultSet.
115      */
116     protected ResultSet createMockResultSet() {
117         return MockResultSet.create(metaData, rows);
118     }
119 
120     /**
121      * This is called before each test method so ResultSet will be fresh each
122      * time.
123      * @see junit.framework.TestCase#setUp()
124      */
125     @Override
126     protected void setUp() throws Exception {
127         super.setUp();
128 
129         rs = this.createMockResultSet();
130         emptyResultSet = MockResultSet.create(metaData, null);
131     }
132 
133     // Test which allows Eclipse to be run on full project (avoids no tests found)
134     // check that the rows are valid for the column definition
135     public void testCheckDataSizes() {
136         assertEquals("Row 1 must contain correct number of columns", columnNames.length, row1.length);
137         assertEquals("Row 1 must contain correct number of columns", columnNames.length, row2.length);
138     }
139 
140     public void testResultSets() throws Exception {
141         assertFalse("emptyResultSet should be empty", emptyResultSet.next());
142         // fails in SqlNullCheckedResultSetTest assertTrue("rs should not be empty", rs.next());
143     }
144 }