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.handlers;
18  
19  import java.sql.SQLException;
20  import java.util.Iterator;
21  import java.util.Map;
22  
23  import org.apache.commons.dbutils.BaseTestCase;
24  import org.apache.commons.dbutils.ResultSetHandler;
25  
26  public class KeyedHandlerTest extends BaseTestCase {
27  
28      public KeyedHandlerTest(String name) {
29          super(name);
30      }
31  
32      public void testHandle() throws SQLException {
33          ResultSetHandler h = new KeyedHandler();
34  
35          Map results = (Map) h.handle(this.rs);
36  
37          assertNotNull(results);
38          assertEquals(ROWS, results.size());
39  
40          Iterator iter = results.keySet().iterator();
41          Map row = null;
42          while (iter.hasNext()) {
43              Object key = iter.next();
44              assertNotNull(key);
45              row = (Map) results.get(key);
46              assertNotNull(row);
47              assertEquals(COLS, row.keySet().size());
48          }
49  
50          row = (Map) results.get("1");
51          assertEquals("1", row.get("one"));
52          assertEquals("2", row.get("TWO"));
53          assertEquals("3", row.get("Three"));
54      }
55  
56      public void testColumnIndexHandle() throws SQLException {
57          ResultSetHandler h = new KeyedHandler(2);
58          Map results = (Map) h.handle(this.rs);
59  
60          assertNotNull(results);
61          assertEquals(ROWS, results.size());
62  
63          Iterator iter = results.keySet().iterator();
64          Map row = null;
65          while (iter.hasNext()) {
66              Object key = iter.next();
67              assertNotNull(key);
68              row = (Map) results.get(key);
69              assertNotNull(row);
70              assertEquals(COLS, row.keySet().size());
71          }
72  
73          row = (Map) results.get("5");
74          assertEquals("4", row.get("one"));
75          assertEquals("5", row.get("TWO"));
76          assertEquals("6", row.get("Three"));
77      }
78  
79      public void testColumnNameHandle() throws SQLException {
80          ResultSetHandler h = new KeyedHandler("three");
81          Map results = (Map) h.handle(this.rs);
82  
83          assertNotNull(results);
84          assertEquals(ROWS, results.size());
85  
86          Iterator iter = results.keySet().iterator();
87          Map row = null;
88          while (iter.hasNext()) {
89              Object key = iter.next();
90              assertNotNull(key);
91              row = (Map) results.get(key);
92              assertNotNull(row);
93              assertEquals(COLS, row.keySet().size());
94          }
95  
96          row = (Map) results.get("6");
97          assertEquals("4", row.get("one"));
98          assertEquals("5", row.get("TWO"));
99          assertEquals("6", row.get("Three"));
100     }
101 
102     public void testEmptyResultSetHandle() throws SQLException {
103         ResultSetHandler h = new KeyedHandler();
104         Map results = (Map) h.handle(this.emptyResultSet);
105         assertNotNull(results);
106         assertTrue(results.isEmpty());
107     }
108 }