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.util.Collection;
21  import java.util.HashMap;
22  import java.util.LinkedList;
23  import java.util.Map;
24  
25  import org.junit.Test;
26  
27  public final class BaseResultSetHandlerTest extends BaseTestCase {
28  
29      private static final class ToMapCollectionHandler
30          extends BaseResultSetHandler<Collection<Map<String, Object>>> {
31  
32          @Override
33          protected Collection<Map<String, Object>> handle() throws SQLException {
34              final Collection<Map<String, Object>> result = new LinkedList<>();
35  
36              while (next()) {
37                  final Map<String, Object> current = new HashMap<>();
38  
39                  for (int i = 1; i <= getMetaData().getColumnCount(); i++) {
40                      current.put(getMetaData().getColumnName(i), getObject(i));
41                  }
42  
43                  result.add(current);
44              }
45  
46              return result;
47          }
48  
49      }
50  
51      @Test
52      public void handleWithoutExplicitResultSetInvocation() throws Exception {
53          final Collection<Map<String, Object>> result = new ToMapCollectionHandler().handle(createMockResultSet());
54  
55          assertFalse(result.isEmpty());
56  
57          for (final Map<String, Object> current : result) {
58              assertTrue(current.containsKey("one"));
59              assertTrue(current.containsKey("two"));
60              assertTrue(current.containsKey("three"));
61              assertTrue(current.containsKey("notInBean"));
62              assertTrue(current.containsKey("intTest"));
63              assertTrue(current.containsKey("integerTest"));
64              assertTrue(current.containsKey("nullObjectTest"));
65              assertTrue(current.containsKey("nullPrimitiveTest"));
66              assertTrue(current.containsKey("notDate"));
67              assertTrue(current.containsKey("columnProcessorDoubleTest"));
68          }
69      }
70  
71  }