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.util.Date;
23  
24  import junit.framework.TestCase;
25  
26  /**
27   * BaseTestCase is the base class for all test cases as well as the "all tests"
28   * runner.
29   */
30  public class BaseTestCase extends TestCase {
31  
32      private static final String[] columnNames =
33          new String[] {
34              "one",
35              "two",
36              "three",
37              "notInBean",
38              "intTest",
39              "integerTest",
40              "nullObjectTest",
41              "nullPrimitiveTest",
42              "notDate",
43              "columnProcessorDoubleTest" };
44  
45      /**
46       * The number of columns in the MockResultSet.
47       */
48      protected static final int COLS = columnNames.length;
49  
50      protected static final ResultSetMetaData metaData =
51          MockResultSetMetaData.create(columnNames);
52  
53      private static final Object[] row1 =
54          new Object[] {
55              "1",
56              "2",
57              "3",
58              "  notInBean  ",
59              Integer.valueOf(1),
60              Integer.valueOf(2),
61              null,
62              null,
63              new Date(),
64              BigInteger.valueOf(13)};
65  
66      private static final Object[] row2 =
67          new Object[] {
68              "4",
69              "5",
70              "6",
71              "  notInBean  ",
72              Integer.valueOf(3),
73              Integer.valueOf(4),
74              null,
75              null,
76              new Date(),
77              BigInteger.valueOf(13)};
78  
79      private static final Object[][] rows = new Object[][] { row1, row2 };
80  
81      /**
82       * The number of rows in the MockResultSet.
83       */
84      protected static final int ROWS = rows.length;
85  
86      /**
87       * The ResultSet all test methods will use.
88       */
89      protected ResultSet rs = null;
90  
91      /**
92       * A ResultSet with 0 rows.
93       */
94      protected ResultSet emptyResultSet = null;
95  
96      /**
97       * This is called before each test method so ResultSet will be fresh each
98       * time.
99       * @see junit.framework.TestCase#setUp()
100      */
101     @Override
102     protected void setUp() throws Exception {
103         super.setUp();
104 
105         rs = this.createMockResultSet();
106         emptyResultSet = MockResultSet.create(metaData, null);
107     }
108 
109     /**
110      * Creates a freshly initialized ResultSet.
111      */
112     protected ResultSet createMockResultSet() {
113         return MockResultSet.create(metaData, rows);
114     }
115 
116     // Test which allows Eclipse to be run on full project (avoids no tests found)
117     // check that the rows are valid for the column definition
118     public void testCheckDataSizes() {
119         assertEquals("Row 1 must contain correct number of columns", columnNames.length, row1.length);
120         assertEquals("Row 1 must contain correct number of columns", columnNames.length, row2.length);
121     }
122 
123     public void testResultSets() throws Exception {
124         assertFalse("emptyResultSet should be empty", emptyResultSet.next());
125         // fails in SqlNullCheckedResultSetTest assertTrue("rs should not be empty", rs.next());
126     }
127 }