1   package org.weda.converter;
2   
3   import java.sql.Timestamp;
4   import java.text.SimpleDateFormat;
5   import java.util.Date;
6   import junit.framework.TestCase;
7   import org.weda.converter.impl.DateConverter;
8   
9   /**
10   *
11   * @author tim
12   */
13  public class DateConverterTest extends TestCase{
14      DateConverter conv = new DateConverter();
15      
16      /** Creates a new instance of DateConverterTest */
17      public DateConverterTest(String name) {
18          super(name);
19      }
20      
21      public void test_convertToString() throws Exception {
22          checkConvertToString(conv);
23      }
24      
25      public void test_convertToDate() throws Exception {
26          checkConvertToDate(conv);
27      }
28      
29      public void checkConvertToString(ValueTypeConverter conv) throws Exception{
30          SimpleDateFormat fmt = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
31          assertEquals("01-01-2006"
32                       , conv.convert(String.class, fmt.parse("01-01-2006 00:00:00")
33                                    , "dd-MM-yyyy"));
34          assertEquals("01-01-2006 01:01:01"
35                       , conv.convert(
36                          String.class
37                          , new Timestamp(fmt.parse("01-01-2006 01:01:01").getTime())
38                          , "dd-MM-yyyy HH:mm:ss"));
39          assertEquals("01-01-2006 01:01:01"
40                       , conv.convert(
41                          String.class
42                          , new java.sql.Date(fmt.parse("01-01-2006 01:01:01").getTime())
43                          , "dd-MM-yyyy HH:mm:ss"));
44          assertEquals("01:01:01"
45                       , conv.convert(
46                          String.class
47                          , new java.sql.Time(fmt.parse("01-01-2006 01:01:01").getTime())
48                          , "HH:mm:ss"));
49      }
50      
51      public void checkConvertToDate(ValueTypeConverter conv) throws Exception {
52          SimpleDateFormat fmt = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
53          assertEquals(fmt.parse("01-01-2006 00:00:01")
54                       , conv.convert(
55                              Date.class, "01-01-2006 00:00:01"
56                              , "dd-MM-yyyy HH:mm:ss"));
57          assertEquals(new java.sql.Date(fmt.parse("01-01-2006 00:00:01").getTime())
58                       , conv.convert(
59                              java.sql.Date.class, "01-01-2006 00:00:01"
60                              , "dd-MM-yyyy HH:mm:ss"));
61          assertEquals(new Timestamp(fmt.parse("01-01-2006 00:00:01").getTime())
62                       , conv.convert(
63                              java.sql.Timestamp.class, "01-01-2006 00:00:01"
64                              , "dd-MM-yyyy HH:mm:ss"));
65          assertEquals(new java.sql.Time(fmt.parse("01-01-1970 00:00:01").getTime())
66                       , conv.convert(
67                              java.sql.Time.class, "00:00:01"
68                              , "HH:mm:ss"));       
69      }    
70  }