1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.ezmorph.object;
18
19 import java.util.Calendar;
20 import java.util.Date;
21 import java.util.HashMap;
22 import java.util.Map;
23
24 import junit.framework.Test;
25 import junit.framework.TestSuite;
26 import junit.textui.TestRunner;
27 import net.sf.ezmorph.MorphException;
28 import net.sf.ezmorph.Morpher;
29
30
31
32
33 public class MapToDateMorpherTest extends AbstractObjectMorpherTestCase
34 {
35 public static void main( String[] args )
36 {
37 TestRunner.run( suite() );
38 }
39
40 public static Test suite()
41 {
42 TestSuite suite = new TestSuite( MapToDateMorpherTest.class );
43 suite.setName( "MapToDateMorpher Tests" );
44 return suite;
45 }
46
47 private MapToDateMorpher anotherMorpher;
48 private MapToDateMorpher anotherMorpherWithDefaultValue;
49 private MapToDateMorpher morpher;
50 private MapToDateMorpher morpherWithDefaultValue;
51
52 public MapToDateMorpherTest( String name )
53 {
54 super( name );
55 }
56
57
58
59 public void testMorph()
60 {
61 Map map = new HashMap();
62 map.put( "year", new Integer( 2007 ) );
63 map.put( "month", new Integer( 5 ) );
64 map.put( "day", new Integer( 17 ) );
65 map.put( "hour", new Integer( 12 ) );
66 map.put( "minutes", new Integer( 13 ) );
67 map.put( "seconds", new Integer( 14 ) );
68 map.put( "milliseconds", new Integer( 150 ) );
69
70 Date date = (Date) morpher.morph( map );
71 assertNotNull( date );
72 Calendar c = Calendar.getInstance();
73 c.setTime( date );
74 assertEquals( 2007, c.get( Calendar.YEAR ) );
75 assertEquals( 5, c.get( Calendar.MONTH ) );
76 assertEquals( 17, c.get( Calendar.DATE ) );
77 assertEquals( 12, c.get( Calendar.HOUR_OF_DAY ) );
78 assertEquals( 13, c.get( Calendar.MINUTE ) );
79 assertEquals( 14, c.get( Calendar.SECOND ) );
80 assertEquals( 150, c.get( Calendar.MILLISECOND ) );
81 }
82
83 public void testMorph_noConversion()
84 {
85 Date expected = new Date();
86 Date actual = (Date) morpher.morph( expected );
87 assertEquals( expected, actual );
88 }
89
90 public void testMorph_notSupported()
91 {
92 try{
93 morpher.morph( new Object[0] );
94 fail( "Should have thrown a MorphException" );
95 }
96 catch( MorphException expected ){
97
98 }
99 }
100
101 public void testMorph_null()
102 {
103 assertNull( morpher.morph( null ) );
104 }
105
106 public void testMorph_useDefault()
107 {
108 Date expected = new Date();
109 morpher.setDefaultValue( expected );
110 morpher.setUseDefault( true );
111 Date actual = (Date) morpher.morph( new HashMap() );
112 assertEquals( expected, actual );
113 }
114
115 protected Morpher getAnotherMorpher()
116 {
117 return anotherMorpher;
118 }
119
120 protected Morpher getAnotherMorpherWithDefaultValue()
121 {
122 return anotherMorpherWithDefaultValue;
123 }
124
125 protected Morpher getMorpher()
126 {
127 return morpher;
128 }
129
130 protected Morpher getMorpherWithDefaultValue()
131 {
132 return morpherWithDefaultValue;
133 }
134
135 protected void setUp() throws Exception
136 {
137 morpher = new MapToDateMorpher();
138 morpherWithDefaultValue = new MapToDateMorpher( new Date() );
139 anotherMorpher = new MapToDateMorpher();
140 anotherMorpherWithDefaultValue = new MapToDateMorpher( getUnixEpoch() );
141 }
142
143 private Date getUnixEpoch()
144 {
145 Calendar c = Calendar.getInstance();
146 c.set( Calendar.YEAR, 1970 );
147 c.set( Calendar.MONTH, 0 );
148 c.set( Calendar.DAY_OF_MONTH, 1 );
149 c.set( Calendar.HOUR_OF_DAY, 0 );
150 c.set( Calendar.MINUTE, 0 );
151 c.set( Calendar.SECOND, 0 );
152 c.set( Calendar.MILLISECOND, 0 );
153 return c.getTime();
154 }
155 }