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 static org.junit.jupiter.api.Assertions.assertEquals;
21
22 import java.sql.Date;
23 import java.util.Calendar;
24
25 import org.apache.commons.beanutils2.converters.AbstractDateConverterTest;
26 import org.apache.commons.beanutils2.converters.DateTimeConverter;
27 import org.junit.jupiter.api.Test;
28
29 /**
30 * Test Case for the {@link SqlDateConverter} class.
31 */
32 public class SqlDateConverterTest extends AbstractDateConverterTest<Date> {
33
34 /**
35 * Gets the expected type
36 *
37 * @return The expected type
38 */
39 @Override
40 protected Class<Date> getExpectedType() {
41 return Date.class;
42 }
43
44 /**
45 * Create the Converter with no default value.
46 *
47 * @return A new Converter
48 */
49 @Override
50 protected SqlDateConverter makeConverter() {
51 return new SqlDateConverter();
52 }
53
54 /**
55 * Create the Converter with a default value.
56 *
57 * @param defaultValue The default value
58 * @return A new Converter
59 */
60 @Override
61 protected SqlDateConverter makeConverter(final Date defaultValue) {
62 return new SqlDateConverter(defaultValue);
63 }
64
65 /**
66 * Test default String to java.sql.Date conversion
67 */
68 @Override
69 @Test
70 public void testDefaultStringToTypeConvert() {
71
72 // Create & Configure the Converter
73 final SqlDateConverter converter = makeConverter();
74 converter.setUseLocaleFormat(false);
75
76 // Valid String --> java.sql.Date Conversion
77 final String testString = "2006-05-16";
78 final Object expected = toType(testString, "yyyy-MM-dd", null);
79 validConversion(converter, expected, testString);
80
81 // Invalid String --> java.sql.Date Conversion
82 invalidConversion(converter, "01/01/2006");
83 }
84
85 /**
86 * Test default java.sql.Date to String conversion
87 */
88 @Test
89 public void testDefaultTypeToStringConvert() {
90
91 // Create & Configure the Converter
92 final DateTimeConverter<Date> converter = makeConverter();
93 converter.setUseLocaleFormat(false);
94
95 // Valid String --> java.sql.Date Conversion
96 final String expected = "2006-05-16";
97 final Object testVal = toType(expected, "yyyy-MM-dd", null);
98 stringConversion(converter, expected, testVal);
99
100 final Object result = converter.convert(String.class, Integer.valueOf(2));
101 assertEquals("2", result, "Default toString()");
102 }
103
104 /**
105 * Convert from a Calendar to the appropriate Date type
106 *
107 * @param value The Calendar value to convert
108 * @return The converted value
109 */
110 @Override
111 protected Date toType(final Calendar value) {
112 return new Date(getTimeInMillis(value));
113 }
114
115 }