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      public void testToArray() throws SQLException {
42  
43          Object[] a = null;
44          assertTrue(this.rs.next());
45          a = processor.toArray(this.rs);
46          assertEquals(COLS, a.length);
47          assertEquals("1", a[0]);
48          assertEquals("2", a[1]);
49          assertEquals("3", a[2]);
50              
51          assertTrue(this.rs.next());
52          a = processor.toArray(this.rs);
53          assertEquals(COLS, a.length);
54  
55          assertEquals("4", a[0]);
56          assertEquals("5", a[1]);
57          assertEquals("6", a[2]);
58              
59          assertFalse(this.rs.next());
60      }
61  
62      public void testToBean() throws SQLException, ParseException {
63  
64          TestBean row = null;
65          assertTrue(this.rs.next());
66          row = (TestBean) processor.toBean(this.rs, TestBean.class);
67          assertEquals("1", row.getOne());
68          assertEquals("2", row.getTwo());
69          assertEquals("3", row.getThree());
70          assertEquals("not set", row.getDoNotSet());
71              
72          assertTrue(this.rs.next());
73          row = (TestBean) processor.toBean(this.rs, TestBean.class);
74  
75          assertEquals("4", row.getOne());
76          assertEquals("5", row.getTwo());
77          assertEquals("6", row.getThree());
78          assertEquals("not set", row.getDoNotSet());
79          assertEquals(3, row.getIntTest());
80          assertEquals(new Integer(4), row.getIntegerTest());
81          assertEquals(null, row.getNullObjectTest());
82          assertEquals(0, row.getNullPrimitiveTest());
83          // test date -> string handling
84          assertNotNull(row.getNotDate());
85          assertTrue(!"not a date".equals(row.getNotDate()));
86          datef.parse(row.getNotDate());
87          
88          assertFalse(this.rs.next());
89          
90      }
91  
92      public void testToBeanList() throws SQLException, ParseException {
93  
94          List list = processor.toBeanList(this.rs, TestBean.class);
95          assertNotNull(list);
96          assertEquals(ROWS, list.size());
97  
98          TestBean b = (TestBean) list.get(0);
99          assertEquals("1", b.getOne());
100         assertEquals("2", b.getTwo());
101         assertEquals("3", b.getThree());
102         assertEquals("not set", b.getDoNotSet());
103         
104         b = (TestBean) list.get(1);
105         assertEquals("4", b.getOne());
106         assertEquals("5", b.getTwo());
107         assertEquals("6", b.getThree());
108         assertEquals("not set", b.getDoNotSet());
109         assertEquals(3, b.getIntTest());
110         assertEquals(new Integer(4), b.getIntegerTest());
111         assertEquals(null, b.getNullObjectTest());
112         assertEquals(0, b.getNullPrimitiveTest());
113         // test date -> string handling
114         assertNotNull(b.getNotDate());
115         assertTrue(!"not a date".equals(b.getNotDate()));
116         datef.parse(b.getNotDate());
117     }
118 
119     public void testToMap() throws SQLException {
120 
121         assertTrue(this.rs.next());
122         Map m = processor.toMap(this.rs);
123         assertEquals(COLS, m.keySet().size());
124         assertEquals("1", m.get("one"));
125         assertEquals("2", m.get("TWO"));
126         assertEquals("3", m.get("Three"));
127             
128         assertTrue(this.rs.next());
129         m = processor.toMap(this.rs);
130         assertEquals(COLS, m.keySet().size());
131 
132         assertEquals("4", m.get("One")); // case shouldn't matter
133         assertEquals("5", m.get("two"));
134         assertEquals("6", m.get("THREE"));
135             
136         assertFalse(this.rs.next());
137     }
138 
139 }