001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.dbutils.handlers;
018:
019: import java.sql.SQLException;
020: import java.util.Iterator;
021: import java.util.Map;
022:
023: import org.apache.commons.dbutils.BaseTestCase;
024: import org.apache.commons.dbutils.ResultSetHandler;
025:
026: public class KeyedHandlerTest extends BaseTestCase {
027:
028: public KeyedHandlerTest(String name) {
029: super (name);
030: }
031:
032: public void testHandle() throws SQLException {
033: ResultSetHandler h = new KeyedHandler();
034:
035: Map results = (Map) h.handle(this .rs);
036:
037: assertNotNull(results);
038: assertEquals(ROWS, results.size());
039:
040: Iterator iter = results.keySet().iterator();
041: Map row = null;
042: while (iter.hasNext()) {
043: Object key = iter.next();
044: assertNotNull(key);
045: row = (Map) results.get(key);
046: assertNotNull(row);
047: assertEquals(COLS, row.keySet().size());
048: }
049:
050: row = (Map) results.get("1");
051: assertEquals("1", row.get("one"));
052: assertEquals("2", row.get("TWO"));
053: assertEquals("3", row.get("Three"));
054: }
055:
056: public void testColumnIndexHandle() throws SQLException {
057: ResultSetHandler h = new KeyedHandler(2);
058: Map results = (Map) h.handle(this .rs);
059:
060: assertNotNull(results);
061: assertEquals(ROWS, results.size());
062:
063: Iterator iter = results.keySet().iterator();
064: Map row = null;
065: while (iter.hasNext()) {
066: Object key = iter.next();
067: assertNotNull(key);
068: row = (Map) results.get(key);
069: assertNotNull(row);
070: assertEquals(COLS, row.keySet().size());
071: }
072:
073: row = (Map) results.get("5");
074: assertEquals("4", row.get("one"));
075: assertEquals("5", row.get("TWO"));
076: assertEquals("6", row.get("Three"));
077: }
078:
079: public void testColumnNameHandle() throws SQLException {
080: ResultSetHandler h = new KeyedHandler("three");
081: Map results = (Map) h.handle(this .rs);
082:
083: assertNotNull(results);
084: assertEquals(ROWS, results.size());
085:
086: Iterator iter = results.keySet().iterator();
087: Map row = null;
088: while (iter.hasNext()) {
089: Object key = iter.next();
090: assertNotNull(key);
091: row = (Map) results.get(key);
092: assertNotNull(row);
093: assertEquals(COLS, row.keySet().size());
094: }
095:
096: row = (Map) results.get("6");
097: assertEquals("4", row.get("one"));
098: assertEquals("5", row.get("TWO"));
099: 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: }
|