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 void testHandle() throws SQLException {
029            ResultSetHandler h = new KeyedHandler();
030    
031            Map results = (Map) h.handle(this.rs);
032    
033            assertNotNull(results);
034            assertEquals(ROWS, results.size());
035    
036            Iterator iter = results.keySet().iterator();
037            Map row = null;
038            while (iter.hasNext()) {
039                Object key = iter.next();
040                assertNotNull(key);
041                row = (Map) results.get(key);
042                assertNotNull(row);
043                assertEquals(COLS, row.keySet().size());
044            }
045    
046            row = (Map) results.get("1");
047            assertEquals("1", row.get("one"));
048            assertEquals("2", row.get("TWO"));
049            assertEquals("3", row.get("Three"));
050        }
051    
052        public void testColumnIndexHandle() throws SQLException {
053            ResultSetHandler h = new KeyedHandler(2);
054            Map results = (Map) h.handle(this.rs);
055    
056            assertNotNull(results);
057            assertEquals(ROWS, results.size());
058    
059            Iterator iter = results.keySet().iterator();
060            Map row = null;
061            while (iter.hasNext()) {
062                Object key = iter.next();
063                assertNotNull(key);
064                row = (Map) results.get(key);
065                assertNotNull(row);
066                assertEquals(COLS, row.keySet().size());
067            }
068    
069            row = (Map) results.get("5");
070            assertEquals("4", row.get("one"));
071            assertEquals("5", row.get("TWO"));
072            assertEquals("6", row.get("Three"));
073        }
074    
075        public void testColumnNameHandle() throws SQLException {
076            ResultSetHandler h = new KeyedHandler("three");
077            Map results = (Map) h.handle(this.rs);
078    
079            assertNotNull(results);
080            assertEquals(ROWS, results.size());
081    
082            Iterator iter = results.keySet().iterator();
083            Map row = null;
084            while (iter.hasNext()) {
085                Object key = iter.next();
086                assertNotNull(key);
087                row = (Map) results.get(key);
088                assertNotNull(row);
089                assertEquals(COLS, row.keySet().size());
090            }
091    
092            row = (Map) results.get("6");
093            assertEquals("4", row.get("one"));
094            assertEquals("5", row.get("TWO"));
095            assertEquals("6", row.get("Three"));
096        }
097    
098        public void testEmptyResultSetHandle() throws SQLException {
099            ResultSetHandler h = new KeyedHandler();
100            Map results = (Map) h.handle(this.emptyResultSet);
101            assertNotNull(results);
102            assertTrue(results.isEmpty());
103        }
104    }