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.sql.SQLException;
20  import java.text.DateFormat;
21  import java.text.ParseException;
22  import java.text.SimpleDateFormat;
23  import java.util.HashMap;
24  import java.util.Iterator;
25  import java.util.List;
26  import java.util.Locale;
27  import java.util.Map;
28  
29  /**
30   * Test the BasicRowProcessor class.
31   */
32  public class BasicRowProcessorTest extends BaseTestCase {
33  
34      private static final RowProcessor processor = new BasicRowProcessor();
35  
36      /**
37       * Format that matches Date.toString().
38       * Sun Mar 14 15:19:15 MST 2004
39       */
40      private static final DateFormat datef =
41          new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
42  
43      public void testPutAllContainsKeyAndRemove() throws Exception {
44          final Map<String, Object> test = new HashMap<>(3);
45          test.put("fiRst", "thing");
46          test.put("seCond", "another");
47          test.put("thIrd", "more");
48          final Map<String, Object> brpMap = BasicRowProcessor.createCaseInsensitiveHashMap(3);
49          brpMap.putAll(test);
50  
51          assertEquals(test, brpMap);
52          assertTrue(brpMap.containsKey("fiRst"));
53          assertTrue(brpMap.containsKey("first"));
54  
55          brpMap.remove("first");
56          assertFalse(brpMap.containsKey("first"));
57      }
58  
59      public void testToArray() throws SQLException {
60  
61          Object[] a;
62          assertTrue(this.rs.next());
63          a = processor.toArray(this.rs);
64          assertEquals(COLS, a.length);
65          assertEquals("1", a[0]);
66          assertEquals("2", a[1]);
67          assertEquals("THREE", a[2]);
68  
69          assertTrue(this.rs.next());
70          a = processor.toArray(this.rs);
71          assertEquals(COLS, a.length);
72  
73          assertEquals("4", a[0]);
74          assertEquals("5", a[1]);
75          assertEquals("SIX", a[2]);
76  
77          assertFalse(this.rs.next());
78      }
79  
80      public void testToBean() throws SQLException, ParseException {
81  
82          assertTrue(this.rs.next());
83          TestBean row = processor.toBean(this.rs, TestBean.class);
84          assertEquals("1", row.getOne());
85          assertEquals("2", row.getTwo());
86          assertEquals(TestBean.Ordinal.THREE, row.getThree());
87          assertEquals("not set", row.getDoNotSet());
88  
89          assertTrue(this.rs.next());
90          row = processor.toBean(this.rs, TestBean.class);
91  
92          assertEquals("4", row.getOne());
93          assertEquals("5", row.getTwo());
94          assertEquals(TestBean.Ordinal.SIX, row.getThree());
95          assertEquals("not set", row.getDoNotSet());
96          assertEquals(3, row.getIntTest());
97          assertEquals(Integer.valueOf(4), row.getIntegerTest());
98          assertEquals(null, row.getNullObjectTest());
99          assertEquals(0, row.getNullPrimitiveTest());
100         // test date -> string handling
101         assertNotNull(row.getNotDate());
102         assertTrue(!"not a date".equals(row.getNotDate()));
103         assertTrue(row.getNotDate().endsWith("789456123"));
104 
105         assertFalse(this.rs.next());
106 
107     }
108 
109     public void testToBeanList() throws SQLException, ParseException {
110 
111         final List<TestBean> list = processor.toBeanList(this.rs, TestBean.class);
112         assertNotNull(list);
113         assertEquals(ROWS, list.size());
114 
115         TestBean b = list.get(0);
116         assertEquals("1", b.getOne());
117         assertEquals("2", b.getTwo());
118         assertEquals(TestBean.Ordinal.THREE, b.getThree());
119         assertEquals("not set", b.getDoNotSet());
120         datef.parse(b.getNotDate());
121 
122         b = list.get(1);
123         assertEquals("4", b.getOne());
124         assertEquals("5", b.getTwo());
125         assertEquals(TestBean.Ordinal.SIX, b.getThree());
126         assertEquals("not set", b.getDoNotSet());
127         assertEquals(3, b.getIntTest());
128         assertEquals(Integer.valueOf(4), b.getIntegerTest());
129         assertEquals(null, b.getNullObjectTest());
130         assertEquals(0, b.getNullPrimitiveTest());
131         // test date -> string handling
132         assertNotNull(b.getNotDate());
133         assertTrue(!"not a date".equals(b.getNotDate()));
134         assertTrue(b.getNotDate().endsWith("789456123"));
135     }
136 
137     public void testToMap() throws SQLException {
138 
139         assertTrue(this.rs.next());
140         Map<String, Object> m = processor.toMap(this.rs);
141         assertEquals(COLS, m.size());
142         assertEquals("1", m.get("one"));
143         assertEquals("2", m.get("TWO"));
144         assertEquals("THREE", m.get("Three"));
145 
146         assertTrue(this.rs.next());
147         m = processor.toMap(this.rs);
148         assertEquals(COLS, m.size());
149 
150         assertEquals("4", m.get("One")); // case shouldn't matter
151         assertEquals("5", m.get("two"));
152         assertEquals("SIX", m.get("THREE"));
153 
154         assertFalse(this.rs.next());
155     }
156 
157     public void testToMapOrdering() throws SQLException {
158 
159         assertTrue(this.rs.next());
160         final Map<String, Object> m = processor.toMap(this.rs);
161 
162         final Iterator<String> itr = m.keySet().iterator();
163         assertEquals("one", itr.next());
164         assertEquals("two", itr.next());
165         assertEquals("three", itr.next());
166         assertEquals("notInBean", itr.next());
167         assertEquals("intTest", itr.next());
168         assertEquals("integerTest", itr.next());
169         assertEquals("nullObjectTest", itr.next());
170         assertEquals("nullPrimitiveTest", itr.next());
171         assertEquals("notDate", itr.next());
172         assertEquals("columnProcessorDoubleTest", itr.next());
173         assertEquals("11", itr.next());
174 
175         assertFalse(itr.hasNext());
176     }
177 }