    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 $Revision: 471689 $ $Date: 2006-11-06 10:52:49 +0000 (Mon, 06 Nov 2006) $
    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(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      public void testDefaultStringToTypeConvert() {
    57
    58          // Create & Configure the Converter
    59          DateTimeConverter converter = makeConverter();
    60          converter.setUseLocaleFormat(false);
    61
    62          // Valid String --> java.sql.Date Conversion
    63          String testString = "2006-05-16";
    64          Object expected = toType(testString, "yyyy-MM-dd", null);
    65          validConversion(converter, expected, testString);
    66
    67          // Invalid String --> java.sql.Date Conversion
    68          invalidConversion(converter, "01/01/2006");
    69      }
    70
    71      /***
    72       * Test default java.sql.Date to String conversion
    73       */
    74      public void testDefaultTypeToStringConvert() {
    75
    76          // Create & Configure the Converter
    77          DateTimeConverter converter = makeConverter();
    78          converter.setUseLocaleFormat(false);
    79
    80          // Valid String --> java.sql.Date Conversion
    81          String expected  = "2006-05-16";
    82          Object testVal   = toType(expected, "yyyy-MM-dd", null);
    83          stringConversion(converter, expected, testVal);
    84
    85          Object result = converter.convert(String.class, new Integer(2));
    86          assertEquals("Default toString()", "2", result);
    87      }
    88
    89      /***
    90       * Create the Converter with no default value.
    91       * @return A new Converter
    92       */
    93      protected DateTimeConverter makeConverter() {
    94          return new SqlDateConverter();
    95      }
    96
    97      /***
    98       * Create the Converter with a default value.
    99       * @param defaultValue The default value
    100      * @return A new Converter
    101      */
    102     protected DateTimeConverter makeConverter(Object defaultValue) {
    103         return new SqlDateConverter(defaultValue);
    104     }
    105
    106     /***
    107      * Return the expected type
    108      * @return The expected type
    109      */
    110     protected Class getExpectedType() {
    111         return Date.class;
    112     }
    113
    114     /***
    115      * Convert from a Calendar to the appropriate Date type
    116      *
    117      * @param value The Calendar value to convert
    118      * @return The converted value
    119      */
    120     protected Object toType(Calendar value) {
    121         return new java.sql.Date(getTimeInMillis(value));
    122     }
    123
    124 }