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.sql.SQLException;
20  import java.text.DateFormat;
21  import java.text.ParseException;
22  import java.text.SimpleDateFormat;
23  import java.util.List;
24  import java.util.Locale;
25  import java.util.Map;
26  
27  /**
28   * Test the BasicRowProcessor class.
29   */
30  public class BasicRowProcessorTest extends BaseTestCase {
31  
32      private static final RowProcessor processor = new BasicRowProcessor();
33      
34      /**
35       * Format that matches Date.toString().
36       * Sun Mar 14 15:19:15 MST 2004
37       */ 
38      private static final DateFormat datef =
39          new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
40  
41      /**
42       * Constructor for BasicRowProcessorTest.
43       * @param name
44       */
45      public BasicRowProcessorTest(String name) {
46          super(name);
47      }
48  
49      public void testToArray() throws SQLException {
50  
51          int rowCount = 0;
52          Object[] a = null;
53          while (this.rs.next()) {
54              a = processor.toArray(this.rs);
55              assertEquals(COLS, a.length);
56              rowCount++;
57          }
58  
59          assertEquals(ROWS, rowCount);
60          assertEquals("4", a[0]);
61          assertEquals("5", a[1]);
62          assertEquals("6", a[2]);
63      }
64  
65      public void testToBean() throws SQLException, ParseException {
66  
67          int rowCount = 0;
68          TestBean b = null;
69          while (this.rs.next()) {
70              b = (TestBean) processor.toBean(this.rs, TestBean.class);
71              assertNotNull(b);
72              rowCount++;
73          }
74  
75          assertEquals(ROWS, rowCount);
76          assertEquals("4", b.getOne());
77          assertEquals("5", b.getTwo());
78          assertEquals("6", b.getThree());
79          assertEquals("not set", b.getDoNotSet());
80          assertEquals(3, b.getIntTest());
81          assertEquals(new Integer(4), b.getIntegerTest());
82          assertEquals(null, b.getNullObjectTest());
83          assertEquals(0, b.getNullPrimitiveTest());
84          // test date -> string handling
85          assertNotNull(b.getNotDate());
86          assertTrue(!"not a date".equals(b.getNotDate()));
87          datef.parse(b.getNotDate());
88      }
89  
90      public void testToBeanList() throws SQLException, ParseException {
91  
92          List list = processor.toBeanList(this.rs, TestBean.class);
93          assertNotNull(list);
94          assertEquals(ROWS, list.size());
95  
96          TestBean b = (TestBean) list.get(1);
97  
98          assertEquals("4", b.getOne());
99          assertEquals("5", b.getTwo());
100         assertEquals("6", b.getThree());
101         assertEquals("not set", b.getDoNotSet());
102         assertEquals(3, b.getIntTest());
103         assertEquals(new Integer(4), b.getIntegerTest());
104         assertEquals(null, b.getNullObjectTest());
105         assertEquals(0, b.getNullPrimitiveTest());
106         // test date -> string handling
107         assertNotNull(b.getNotDate());
108         assertTrue(!"not a date".equals(b.getNotDate()));
109         datef.parse(b.getNotDate());
110     }
111 
112     public void testToMap() throws SQLException {
113 
114         int rowCount = 0;
115         Map m = null;
116         while (this.rs.next()) {
117             m = processor.toMap(this.rs);
118             assertNotNull(m);
119             assertEquals(COLS, m.keySet().size());
120             rowCount++;
121         }
122 
123         assertEquals(ROWS, rowCount);
124         assertEquals("4", m.get("One")); // case shouldn't matter
125         assertEquals("5", m.get("two"));
126         assertEquals("6", m.get("THREE"));
127     }
128 
129 }