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