1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.beanutils.converters;
19  
20  import java.sql.Time;
21  import java.util.Calendar;
22  import java.util.Locale;
23  
24  import junit.framework.TestSuite;
25  
26  /***
27   * Test Case for the {@link SqlTimeConverter} class.
28   *
29   * @version $Revision: 471689 $ $Date: 2006-11-06 10:52:49 +0000 (Mon, 06 Nov 2006) $
30   */
31  
32  public class SqlTimeConverterTestCase extends DateConverterTestBase {
33  
34      /***
35       * Construct a new Date test case.
36       * @param name Test Name
37       */
38      public SqlTimeConverterTestCase(String name) {
39          super(name);
40      }
41  
42      // ------------------------------------------------------------------------
43  
44      /***
45       * Create Test Suite
46       * @return test suite
47       */
48      public static TestSuite suite() {
49          return new TestSuite(SqlTimeConverterTestCase.class);
50      }
51  
52      // ------------------------------------------------------------------------
53  
54      /***
55       * Test Date Converter with no default value
56       */
57      public void testLocale() {
58  
59          // Re-set the default Locale to Locale.US
60          Locale defaultLocale = Locale.getDefault();
61          Locale.setDefault(Locale.US);
62  
63          String pattern = "h:mm a"; // SHORT style time format for US Locale
64  
65          // Create & Configure the Converter
66          DateTimeConverter converter = makeConverter();
67          converter.setUseLocaleFormat(true);
68  
69          // Valid String --> Type Conversion
70          String testString = "3:06 pm";
71          Object expected = toType(testString, pattern, null);
72          validConversion(converter, expected, testString);
73  
74          // Invalid Conversions
75          invalidConversion(converter, null);
76          invalidConversion(converter, "");
77          invalidConversion(converter, "13:05");
78          invalidConversion(converter, "11:05 p");
79          invalidConversion(converter, "11.05 pm");
80          invalidConversion(converter, new Integer(2));
81  
82          // Test specified Locale
83          converter.setLocale(Locale.UK);
84          invalidConversion(converter, testString);      // Test previous value now fails
85          validConversion(converter, expected, "15:06"); // UK Short style is "HH:mm"
86  
87          // Restore the default Locale
88          Locale.setDefault(defaultLocale);
89  
90      }
91  
92      /***
93       * Test default String to java.sql.Time conversion
94       */
95      public void testDefaultStringToTypeConvert() {
96  
97          // Create & Configure the Converter
98          DateTimeConverter converter = makeConverter();
99          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 }