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.Timestamp;
021: import java.util.Calendar;
022: import java.util.Locale;
023:
024: import junit.framework.TestSuite;
025:
026: /**
027: * Test Case for the {@link SqlTimestampConverter} class.
028: *
029: * @version $Revision: 471689 $ $Date: 2006-11-06 10:52:49 +0000 (Mon, 06 Nov 2006) $
030: */
031:
032: public class SqlTimestampConverterTestCase extends
033: DateConverterTestBase {
034:
035: /**
036: * Construct a new Date test case.
037: * @param name Test Name
038: */
039: public SqlTimestampConverterTestCase(String name) {
040: super (name);
041: }
042:
043: // ------------------------------------------------------------------------
044:
045: /**
046: * Create Test Suite
047: * @return test suite
048: */
049: public static TestSuite suite() {
050: return new TestSuite(SqlTimestampConverterTestCase.class);
051: }
052:
053: // ------------------------------------------------------------------------
054:
055: /**
056: * Test Date Converter with no default value
057: */
058: public void testLocale() {
059:
060: // Re-set the default Locale to Locale.US
061: Locale defaultLocale = Locale.getDefault();
062: Locale.setDefault(Locale.US);
063:
064: String pattern = "M/d/yy h:mm a"; // SHORT style Date & Time format for US Locale
065:
066: // Create & Configure the Converter
067: DateTimeConverter converter = makeConverter();
068: converter.setUseLocaleFormat(true);
069:
070: // Valid String --> Type Conversion
071: String testString = "3/21/06 3:06 pm";
072: Object expected = toType(testString, pattern, null);
073: validConversion(converter, expected, testString);
074:
075: // Invalid Conversions
076: invalidConversion(converter, null);
077: invalidConversion(converter, "");
078: invalidConversion(converter, "13:05 pm");
079: invalidConversion(converter, "11:05 p");
080: invalidConversion(converter, "11.05 pm");
081: invalidConversion(converter, new Integer(2));
082:
083: // Restore the default Locale
084: Locale.setDefault(defaultLocale);
085:
086: }
087:
088: /**
089: * Test default String to java.sql.Timestamp conversion
090: */
091: public void testDefaultStringToTypeConvert() {
092:
093: // Create & Configure the Converter
094: DateTimeConverter converter = makeConverter();
095: converter.setUseLocaleFormat(false);
096:
097: // Valid String --> java.sql.Timestamp Conversion
098: String testString = "2006-10-23 15:36:01.0";
099: Object expected = toType(testString, "yyyy-MM-dd HH:mm:ss.S",
100: null);
101: validConversion(converter, expected, testString);
102:
103: // Invalid String --> java.sql.Timestamp Conversion
104: invalidConversion(converter, "2006/09/21 15:36:01.0");
105: invalidConversion(converter, "2006-10-22");
106: invalidConversion(converter, "15:36:01");
107:
108: }
109:
110: /**
111: * Create the Converter with no default value.
112: * @return A new Converter
113: */
114: protected DateTimeConverter makeConverter() {
115: return new SqlTimestampConverter();
116: }
117:
118: /**
119: * Create the Converter with a default value.
120: * @param defaultValue The default value
121: * @return A new Converter
122: */
123: protected DateTimeConverter makeConverter(Object defaultValue) {
124: return new SqlTimestampConverter(defaultValue);
125: }
126:
127: /**
128: * Return the expected type
129: * @return The expected type
130: */
131: protected Class getExpectedType() {
132: return Timestamp.class;
133: }
134:
135: /**
136: * Convert from a Calendar to the appropriate Date type
137: *
138: * @param value The Calendar value to convert
139: * @return The converted value
140: */
141: protected Object toType(Calendar value) {
142: return new Timestamp(getTimeInMillis(value));
143: }
144:
145: }
|