View Javadoc
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.text.DateFormat;
22  import java.util.Calendar;
23  import java.util.Date;
24  import java.util.Locale;
25  
26  import junit.framework.TestSuite;
27  
28  /**
29   * Test Case for the {@link SqlTimestampConverter} class.
30   *
31   * @version $Id$
32   */
33  
34  public class SqlTimestampConverterTestCase extends DateConverterTestBase {
35  
36      /**
37       * Construct a new Date test case.
38       * @param name Test Name
39       */
40      public SqlTimestampConverterTestCase(final String name) {
41          super(name);
42      }
43  
44      // ------------------------------------------------------------------------
45  
46      /**
47       * Create Test Suite
48       * @return test suite
49       */
50      public static TestSuite suite() {
51          return new TestSuite(SqlTimestampConverterTestCase.class);
52      }
53  
54      // ------------------------------------------------------------------------
55  
56      private boolean isUSFormatWithComma() {
57          // BEANUTILS-495 workaround - sometimes Java 9 expects "," in date even if
58          // the format is set to lenient
59          DateFormat loc = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.US);
60          return loc.format(new Date()).contains(",");
61      }
62  
63      /**
64       * Test Date Converter with no default value
65       */
66      @Override
67      public void testLocale() {
68  
69          // Re-set the default Locale to Locale.US
70          final Locale defaultLocale = Locale.getDefault();
71          Locale.setDefault(Locale.US);
72          isUSFormatWithComma();
73  
74  
75          // Create & Configure the Converter
76          final DateTimeConverter converter = makeConverter();
77          converter.setUseLocaleFormat(true);
78  
79          String pattern; // SHORT style Date & Time format for US Locale
80          String testString;
81          if (isUSFormatWithComma()) {
82              pattern = "M/d/yy, h:mm a";
83              testString = "3/21/06, 3:06 PM";
84          } else {
85              // More regular pattern for Java 8 and earlier
86              pattern = "M/d/yy h:mm a";
87              testString = "3/21/06 3:06 PM";
88          }
89  
90  
91          // Valid String --> Type Conversion
92          final Object expected = toType(testString, pattern, null);
93          validConversion(converter, expected, testString);
94  
95          // Invalid Conversions
96          invalidConversion(converter, null);
97          invalidConversion(converter, "");
98          invalidConversion(converter, "13:05 pm");
99          invalidConversion(converter, "11:05 p");
100         invalidConversion(converter, "11.05 pm");
101         invalidConversion(converter, new Integer(2));
102 
103         // Restore the default Locale
104         Locale.setDefault(defaultLocale);
105 
106     }
107 
108     /**
109      * Test default String to java.sql.Timestamp conversion
110      */
111     @Override
112     public void testDefaultStringToTypeConvert() {
113 
114         // Create & Configure the Converter
115         final DateTimeConverter converter = makeConverter();
116         converter.setUseLocaleFormat(false);
117 
118         // Valid String --> java.sql.Timestamp Conversion
119         final String testString = "2006-10-23 15:36:01.0";
120         final Object expected = toType(testString, "yyyy-MM-dd HH:mm:ss.S", null);
121         validConversion(converter, expected, testString);
122 
123         // Invalid String --> java.sql.Timestamp Conversion
124         invalidConversion(converter, "2006/09/21 15:36:01.0");
125         invalidConversion(converter, "2006-10-22");
126         invalidConversion(converter, "15:36:01");
127 
128     }
129 
130     /**
131      * Create the Converter with no default value.
132      * @return A new Converter
133      */
134     @Override
135     protected DateTimeConverter makeConverter() {
136         return new SqlTimestampConverter();
137     }
138 
139     /**
140      * Create the Converter with a default value.
141      * @param defaultValue The default value
142      * @return A new Converter
143      */
144     @Override
145     protected DateTimeConverter makeConverter(final Object defaultValue) {
146         return new SqlTimestampConverter(defaultValue);
147     }
148 
149     /**
150      * Return the expected type
151      * @return The expected type
152      */
153     @Override
154     protected Class<?> getExpectedType() {
155         return Timestamp.class;
156     }
157 
158     /**
159      * Convert from a Calendar to the appropriate Date type
160      *
161      * @param value The Calendar value to convert
162      * @return The converted value
163      */
164     @Override
165     protected Object toType(final Calendar value) {
166         return new Timestamp(getTimeInMillis(value));
167     }
168 
169 }