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