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.Timestamp;
21  import java.util.Calendar;
22  import java.util.Locale;
23  
24  import junit.framework.TestSuite;
25  
26  /***
27   * Test Case for the {@link SqlTimestampConverter} class.
28   *
29   * @version $Revision: 471689 $ $Date: 2006-11-06 10:52:49 +0000 (Mon, 06 Nov 2006) $
30   */
31  
32  public class SqlTimestampConverterTestCase extends DateConverterTestBase {
33  
34      /***
35       * Construct a new Date test case.
36       * @param name Test Name
37       */
38      public SqlTimestampConverterTestCase(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(SqlTimestampConverterTestCase.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 = "M/d/yy h:mm a"; // SHORT style Date & 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/21/06 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 pm");
78          invalidConversion(converter, "11:05 p");
79          invalidConversion(converter, "11.05 pm");
80          invalidConversion(converter, new Integer(2));
81  
82          // Restore the default Locale
83          Locale.setDefault(defaultLocale);
84  
85      }
86  
87      /***
88       * Test default String to java.sql.Timestamp conversion
89       */
90      public void testDefaultStringToTypeConvert() {
91  
92          // Create & Configure the Converter
93          DateTimeConverter converter = makeConverter();
94          converter.setUseLocaleFormat(false);
95  
96          // Valid String --> java.sql.Timestamp Conversion
97          String testString = "2006-10-23 15:36:01.0";
98          Object expected = toType(testString, "yyyy-MM-dd HH:mm:ss.S", null);
99          validConversion(converter, expected, testString);
100 
101         // Invalid String --> java.sql.Timestamp Conversion
102         invalidConversion(converter, "2006/09/21 15:36:01.0");
103         invalidConversion(converter, "2006-10-22");
104         invalidConversion(converter, "15:36:01");
105 
106     }
107 
108     /***
109      * Create the Converter with no default value.
110      * @return A new Converter
111      */
112     protected DateTimeConverter makeConverter() {
113         return new SqlTimestampConverter();
114     }
115     
116     /***
117      * Create the Converter with a default value.
118      * @param defaultValue The default value
119      * @return A new Converter
120      */
121     protected DateTimeConverter makeConverter(Object defaultValue) {
122         return new SqlTimestampConverter(defaultValue);
123     }
124 
125     /***
126      * Return the expected type
127      * @return The expected type
128      */
129     protected Class getExpectedType() {
130         return Timestamp.class;
131     }
132 
133     /***
134      * Convert from a Calendar to the appropriate Date type
135      * 
136      * @param value The Calendar value to convert
137      * @return The converted value
138      */
139     protected Object toType(Calendar value) {
140         return new Timestamp(getTimeInMillis(value));
141     }
142 
143 }