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.handlers;
18  
19  import static org.mockito.Mockito.mock;
20  
21  import java.sql.SQLException;
22  import java.util.Map;
23  import java.util.Map.Entry;
24  
25  import org.apache.commons.dbutils.BaseTestCase;
26  import org.apache.commons.dbutils.ResultSetHandler;
27  import org.apache.commons.dbutils.RowProcessor;
28  
29  public class KeyedHandlerTest extends BaseTestCase {
30  
31      public void testColumnIndexHandle() throws SQLException {
32          final ResultSetHandler<Map<String,Map<String,Object>>> h = new KeyedHandler<>(2);
33          final Map<String,Map<String,Object>> results = h.handle(this.rs);
34  
35          assertNotNull(results);
36          assertEquals(ROWS, results.size());
37  
38          Map<String,Object> row = null;
39          for(final Entry<String, Map<String, Object>> entry : results.entrySet())
40          {
41              final Object key = entry.getKey();
42              assertNotNull(key);
43              row = entry.getValue();
44              assertNotNull(row);
45              assertEquals(COLS, row.size());
46          }
47          row = results.get("5");
48          assertEquals("4", row.get("one"));
49          assertEquals("5", row.get("TWO"));
50          assertEquals("SIX", row.get("Three"));
51      }
52  
53      public void testColumnNameHandle() throws SQLException {
54          final ResultSetHandler<Map<Integer,Map<String,Object>>> h = new KeyedHandler<>("intTest");
55          final Map<Integer,Map<String,Object>> results = h.handle(this.rs);
56  
57          assertNotNull(results);
58          assertEquals(ROWS, results.size());
59  
60          Map<String,Object> row = null;
61          for(final Entry<Integer, Map<String, Object>> entry : results.entrySet())
62          {
63              final Object key = entry.getKey();
64              assertNotNull(key);
65              row = entry.getValue();
66              assertNotNull(row);
67              assertEquals(COLS, row.size());
68          }
69          row = results.get(Integer.valueOf(3));
70          assertEquals("4", row.get("one"));
71          assertEquals("5", row.get("TWO"));
72          assertEquals("SIX", row.get("Three"));
73      }
74  
75      public void testEmptyResultSetHandle() throws SQLException {
76          final ResultSetHandler<Map<String,Map<String,Object>>> h = new KeyedHandler<>();
77          final Map<String,Map<String,Object>> results = h.handle(this.emptyResultSet);
78          assertNotNull(results);
79          assertTrue(results.isEmpty());
80      }
81  
82      public void testHandle() throws SQLException {
83          final ResultSetHandler<Map<String,Map<String,Object>>> h = new KeyedHandler<>();
84  
85          final Map<String,Map<String,Object>> results = h.handle(this.rs);
86  
87          assertNotNull(results);
88          assertEquals(ROWS, results.size());
89  
90          Map<String,Object> row = null;
91          for(final Entry<String, Map<String, Object>> entry : results.entrySet())
92          {
93              final Object key = entry.getKey();
94              assertNotNull(key);
95              row = entry.getValue();
96              assertNotNull(row);
97              assertEquals(COLS, row.size());
98          }
99          row = results.get("1");
100         assertEquals("1", row.get("one"));
101         assertEquals("2", row.get("TWO"));
102         assertEquals("THREE", row.get("Three"));
103     }
104 
105     public void testInjectedRowProcess() throws Exception {
106         final RowProcessor mockProc = mock(RowProcessor.class);
107         final ResultSetHandler<Map<String,Map<String,Object>>> h = new KeyedHandler<>(mockProc);
108         final Map<String,Map<String,Object>> results = h.handle(this.rs);
109 
110         assertNotNull(results);
111         assertEquals(ROWS, results.size());
112 
113         Map<String,Object> row = null;
114         for(final Entry<String, Map<String, Object>> entry : results.entrySet())
115         {
116             row = entry.getValue();
117             assertNotNull(row);
118             assertTrue(row.isEmpty());
119             assertEquals(0, row.size());
120         }
121     }
122 }