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:
018: package org.apache.commons.beanutils.converters;
019:
020: import java.sql.Date;
021: import java.util.Calendar;
022:
023: import junit.framework.TestSuite;
024:
025: /**
026: * Test Case for the {@link SqlDateConverter} class.
027: *
028: * @version $Revision: 471689 $ $Date: 2006-11-06 10:52:49 +0000 (Mon, 06 Nov 2006) $
029: */
030:
031: public class SqlDateConverterTestCase extends DateConverterTestBase {
032:
033: /**
034: * Construct a new Date test case.
035: * @param name Test Name
036: */
037: public SqlDateConverterTestCase(String name) {
038: super (name);
039: }
040:
041: // ------------------------------------------------------------------------
042:
043: /**
044: * Create Test Suite
045: * @return test suite
046: */
047: public static TestSuite suite() {
048: return new TestSuite(SqlDateConverterTestCase.class);
049: }
050:
051: // ------------------------------------------------------------------------
052:
053: /**
054: * Test default String to java.sql.Date conversion
055: */
056: public void testDefaultStringToTypeConvert() {
057:
058: // Create & Configure the Converter
059: DateTimeConverter converter = makeConverter();
060: converter.setUseLocaleFormat(false);
061:
062: // Valid String --> java.sql.Date Conversion
063: String testString = "2006-05-16";
064: Object expected = toType(testString, "yyyy-MM-dd", null);
065: validConversion(converter, expected, testString);
066:
067: // Invalid String --> java.sql.Date Conversion
068: invalidConversion(converter, "01/01/2006");
069: }
070:
071: /**
072: * Test default java.sql.Date to String conversion
073: */
074: public void testDefaultTypeToStringConvert() {
075:
076: // Create & Configure the Converter
077: DateTimeConverter converter = makeConverter();
078: converter.setUseLocaleFormat(false);
079:
080: // Valid String --> java.sql.Date Conversion
081: String expected = "2006-05-16";
082: Object testVal = toType(expected, "yyyy-MM-dd", null);
083: stringConversion(converter, expected, testVal);
084:
085: Object result = converter.convert(String.class, new Integer(2));
086: assertEquals("Default toString()", "2", result);
087: }
088:
089: /**
090: * Create the Converter with no default value.
091: * @return A new Converter
092: */
093: protected DateTimeConverter makeConverter() {
094: return new SqlDateConverter();
095: }
096:
097: /**
098: * Create the Converter with a default value.
099: * @param defaultValue The default value
100: * @return A new Converter
101: */
102: protected DateTimeConverter makeConverter(Object defaultValue) {
103: return new SqlDateConverter(defaultValue);
104: }
105:
106: /**
107: * Return the expected type
108: * @return The expected type
109: */
110: protected Class getExpectedType() {
111: return Date.class;
112: }
113:
114: /**
115: * Convert from a Calendar to the appropriate Date type
116: *
117: * @param value The Calendar value to convert
118: * @return The converted value
119: */
120: protected Object toType(Calendar value) {
121: return new java.sql.Date(getTimeInMillis(value));
122: }
123:
124: }
|