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    *      https://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.beanutils2.sql.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 org.apache.commons.beanutils2.converters.AbstractDateConverterTest;
27  import org.junit.jupiter.api.Test;
28  
29  /**
30   * Test Case for the {@link SqlTimestampConverter} class.
31   */
32  public class SqlTimestampConverterTest extends AbstractDateConverterTest<Timestamp> {
33  
34      /**
35       * Gets the expected type
36       *
37       * @return The expected type
38       */
39      @Override
40      protected Class<Timestamp> getExpectedType() {
41          return Timestamp.class;
42      }
43  
44      private boolean isUSFormatWithComma() {
45          // BEANUTILS-495 workaround - sometimes Java 9 expects "," in date even if
46          // the format is set to lenient
47          final DateFormat loc = DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.US);
48          return loc.format(new Date()).contains(",");
49      }
50  
51      /**
52       * Create the Converter with no default value.
53       *
54       * @return A new Converter
55       */
56      @Override
57      protected SqlTimestampConverter makeConverter() {
58          return new SqlTimestampConverter();
59      }
60  
61      /**
62       * Create the Converter with a default value.
63       *
64       * @param defaultValue The default value
65       * @return A new Converter
66       */
67      @Override
68      protected SqlTimestampConverter makeConverter(final Timestamp defaultValue) {
69          return new SqlTimestampConverter(defaultValue);
70      }
71  
72      /**
73       * Test default String to java.sql.Timestamp conversion
74       */
75      @Override
76      @Test
77      public void testDefaultStringToTypeConvert() {
78          // Create & Configure the Converter
79          final SqlTimestampConverter converter = makeConverter();
80          converter.setUseLocaleFormat(false);
81  
82          // Valid String --> java.sql.Timestamp Conversion
83          final String testString = "2006-10-23 15:36:01.0";
84          final Object expected = toType(testString, "yyyy-MM-dd HH:mm:ss.S", null);
85          validConversion(converter, expected, testString);
86  
87          // Invalid String --> java.sql.Timestamp Conversion
88          invalidConversion(converter, "2006/09/21 15:36:01.0");
89          invalidConversion(converter, "2006-10-22");
90          invalidConversion(converter, "15:36:01");
91      }
92  
93      /**
94       * Test Date Converter with no default value
95       */
96      @Override
97      @Test
98      public void testLocale() {
99          // Re-set the default Locale to Locale.US
100         final Locale defaultLocale = Locale.getDefault();
101         Locale.setDefault(Locale.US);
102         isUSFormatWithComma();
103 
104         // Create & Configure the Converter
105         final SqlTimestampConverter converter = makeConverter();
106         converter.setUseLocaleFormat(true);
107 
108         String pattern; // SHORT style Date & Time format for US Locale
109         String testString;
110         if (isUSFormatWithComma()) {
111             pattern = "M/d/yy, h:mm a";
112             testString = "3/21/06, 3:06 PM";
113         } else {
114             // More regular pattern for Java 8 and earlier
115             pattern = "M/d/yy h:mm a";
116             testString = "3/21/06 3:06 PM";
117         }
118 
119         // Valid String --> Type Conversion
120         final Object expected = toType(testString, pattern, null);
121         validConversion(converter, expected, testString);
122 
123         // Invalid Conversions
124         invalidConversion(converter, null);
125         invalidConversion(converter, "");
126         invalidConversion(converter, "13:05 pm");
127         invalidConversion(converter, "11:05 p");
128         invalidConversion(converter, "11.05 pm");
129         invalidConversion(converter, Integer.valueOf(2));
130 
131         // Restore the default Locale
132         Locale.setDefault(defaultLocale);
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     @Override
142     protected Timestamp toType(final Calendar value) {
143         return new Timestamp(getTimeInMillis(value));
144     }
145 
146 }