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 = new SimpleDateFormat(
039: "EEE MMM dd HH:mm:ss zzz yyyy", Locale.US);
040:
041: /**
042: * Constructor for BasicRowProcessorTest.
043: * @param name
044: */
045: public BasicRowProcessorTest(String name) {
046: super (name);
047: }
048:
049: public void testToArray() throws SQLException {
050:
051: int rowCount = 0;
052: Object[] a = null;
053: while (this .rs.next()) {
054: a = processor.toArray(this .rs);
055: assertEquals(COLS, a.length);
056: rowCount++;
057: }
058:
059: assertEquals(ROWS, rowCount);
060: assertEquals("4", a[0]);
061: assertEquals("5", a[1]);
062: assertEquals("6", a[2]);
063: }
064:
065: public void testToBean() throws SQLException, ParseException {
066:
067: int rowCount = 0;
068: TestBean b = null;
069: while (this .rs.next()) {
070: b = (TestBean) processor.toBean(this .rs, TestBean.class);
071: assertNotNull(b);
072: rowCount++;
073: }
074:
075: assertEquals(ROWS, rowCount);
076: assertEquals("4", b.getOne());
077: assertEquals("5", b.getTwo());
078: assertEquals("6", b.getThree());
079: assertEquals("not set", b.getDoNotSet());
080: assertEquals(3, b.getIntTest());
081: assertEquals(new Integer(4), b.getIntegerTest());
082: assertEquals(null, b.getNullObjectTest());
083: assertEquals(0, b.getNullPrimitiveTest());
084: // test date -> string handling
085: assertNotNull(b.getNotDate());
086: assertTrue(!"not a date".equals(b.getNotDate()));
087: datef.parse(b.getNotDate());
088: }
089:
090: public void testToBeanList() throws SQLException, ParseException {
091:
092: List list = processor.toBeanList(this .rs, TestBean.class);
093: assertNotNull(list);
094: assertEquals(ROWS, list.size());
095:
096: TestBean b = (TestBean) list.get(1);
097:
098: assertEquals("4", b.getOne());
099: assertEquals("5", b.getTwo());
100: assertEquals("6", b.getThree());
101: assertEquals("not set", b.getDoNotSet());
102: assertEquals(3, b.getIntTest());
103: assertEquals(new Integer(4), b.getIntegerTest());
104: assertEquals(null, b.getNullObjectTest());
105: assertEquals(0, b.getNullPrimitiveTest());
106: // test date -> string handling
107: assertNotNull(b.getNotDate());
108: assertTrue(!"not a date".equals(b.getNotDate()));
109: datef.parse(b.getNotDate());
110: }
111:
112: public void testToMap() throws SQLException {
113:
114: int rowCount = 0;
115: Map m = null;
116: while (this .rs.next()) {
117: m = processor.toMap(this .rs);
118: assertNotNull(m);
119: assertEquals(COLS, m.keySet().size());
120: rowCount++;
121: }
122:
123: assertEquals(ROWS, rowCount);
124: assertEquals("4", m.get("One")); // case shouldn't matter
125: assertEquals("5", m.get("two"));
126: assertEquals("6", m.get("THREE"));
127: }
128:
129: }
|