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; 018 019 import java.sql.SQLException; 020 import java.text.DateFormat; 021 import java.text.ParseException; 022 import java.text.SimpleDateFormat; 023 import java.util.List; 024 import java.util.Locale; 025 import java.util.Map; 026 027 /** 028 * Test the BasicRowProcessor class. 029 */ 030 public class BasicRowProcessorTest extends BaseTestCase { 031 032 private static final RowProcessor processor = new BasicRowProcessor(); 033 034 /** 035 * Format that matches Date.toString(). 036 * Sun Mar 14 15:19:15 MST 2004 037 */ 038 private static final DateFormat datef = 039 new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US); 040 041 public void testToArray() throws SQLException { 042 043 Object[] a = null; 044 assertTrue(this.rs.next()); 045 a = processor.toArray(this.rs); 046 assertEquals(COLS, a.length); 047 assertEquals("1", a[0]); 048 assertEquals("2", a[1]); 049 assertEquals("3", a[2]); 050 051 assertTrue(this.rs.next()); 052 a = processor.toArray(this.rs); 053 assertEquals(COLS, a.length); 054 055 assertEquals("4", a[0]); 056 assertEquals("5", a[1]); 057 assertEquals("6", a[2]); 058 059 assertFalse(this.rs.next()); 060 } 061 062 public void testToBean() throws SQLException, ParseException { 063 064 TestBean row = null; 065 assertTrue(this.rs.next()); 066 row = (TestBean) processor.toBean(this.rs, TestBean.class); 067 assertEquals("1", row.getOne()); 068 assertEquals("2", row.getTwo()); 069 assertEquals("3", row.getThree()); 070 assertEquals("not set", row.getDoNotSet()); 071 072 assertTrue(this.rs.next()); 073 row = (TestBean) processor.toBean(this.rs, TestBean.class); 074 075 assertEquals("4", row.getOne()); 076 assertEquals("5", row.getTwo()); 077 assertEquals("6", row.getThree()); 078 assertEquals("not set", row.getDoNotSet()); 079 assertEquals(3, row.getIntTest()); 080 assertEquals(new Integer(4), row.getIntegerTest()); 081 assertEquals(null, row.getNullObjectTest()); 082 assertEquals(0, row.getNullPrimitiveTest()); 083 // test date -> string handling 084 assertNotNull(row.getNotDate()); 085 assertTrue(!"not a date".equals(row.getNotDate())); 086 datef.parse(row.getNotDate()); 087 088 assertFalse(this.rs.next()); 089 090 } 091 092 public void testToBeanList() throws SQLException, ParseException { 093 094 List list = processor.toBeanList(this.rs, TestBean.class); 095 assertNotNull(list); 096 assertEquals(ROWS, list.size()); 097 098 TestBean b = (TestBean) list.get(0); 099 assertEquals("1", b.getOne()); 100 assertEquals("2", b.getTwo()); 101 assertEquals("3", b.getThree()); 102 assertEquals("not set", b.getDoNotSet()); 103 104 b = (TestBean) list.get(1); 105 assertEquals("4", b.getOne()); 106 assertEquals("5", b.getTwo()); 107 assertEquals("6", b.getThree()); 108 assertEquals("not set", b.getDoNotSet()); 109 assertEquals(3, b.getIntTest()); 110 assertEquals(new Integer(4), b.getIntegerTest()); 111 assertEquals(null, b.getNullObjectTest()); 112 assertEquals(0, b.getNullPrimitiveTest()); 113 // test date -> string handling 114 assertNotNull(b.getNotDate()); 115 assertTrue(!"not a date".equals(b.getNotDate())); 116 datef.parse(b.getNotDate()); 117 } 118 119 public void testToMap() throws SQLException { 120 121 assertTrue(this.rs.next()); 122 Map m = processor.toMap(this.rs); 123 assertEquals(COLS, m.keySet().size()); 124 assertEquals("1", m.get("one")); 125 assertEquals("2", m.get("TWO")); 126 assertEquals("3", m.get("Three")); 127 128 assertTrue(this.rs.next()); 129 m = processor.toMap(this.rs); 130 assertEquals(COLS, m.keySet().size()); 131 132 assertEquals("4", m.get("One")); // case shouldn't matter 133 assertEquals("5", m.get("two")); 134 assertEquals("6", m.get("THREE")); 135 136 assertFalse(this.rs.next()); 137 } 138 139 }