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