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
22 import junit.framework.Test;
23 import junit.framework.TestSuite;
24 import junit.textui.TestRunner;
25 import net.sf.ezmorph.MorphException;
26 import net.sf.ezmorph.Morpher;
27
28
29
30
31 public class DateMorpherTest extends AbstractObjectMorpherTestCase
32 {
33 public static void main( String[] args )
34 {
35 TestRunner.run( suite() );
36 }
37
38 public static Test suite()
39 {
40 TestSuite suite = new TestSuite( DateMorpherTest.class );
41 suite.setName( "DateMorpher Tests" );
42 return suite;
43 }
44
45 private DateMorpher anotherMorpher;
46 private DateMorpher anotherMorpherWithDefaultValue;
47 private DateMorpher morpher;
48 private DateMorpher morpherWithDefaultValue;
49
50 public DateMorpherTest( String name )
51 {
52 super( name );
53 }
54
55
56
57 public void testConstructor_illegalFormats()
58 {
59 try{
60 new DateMorpher( null );
61 fail( "Should have thrown a MorphException" );
62 }
63 catch( MorphException expected ){
64
65 }
66
67 try{
68 new DateMorpher( new String[0] );
69 fail( "Should have thrown a MorphException" );
70 }
71 catch( MorphException expected ){
72
73 }
74
75 try{
76 new DateMorpher( null,false );
77 fail( "Should have thrown a MorphException" );
78 }
79 catch( MorphException expected ){
80
81 }
82
83 try{
84 new DateMorpher( new String[0], false );
85 fail( "Should have thrown a MorphException" );
86 }
87 catch( MorphException expected ){
88
89 }
90 }
91
92 public void testMorph_canNotParseDate()
93 {
94 DateMorpher morpher = new DateMorpher( new String[] { "dd/MM/yyyy" } );
95 try{
96 morpher.morph( "01-01-1976" );
97 fail( "Should ve have thrown a MorphException" );
98 }
99 catch( MorphException expected ){
100
101 }
102 }
103
104 public void testMorph_matchFirstPattern()
105 {
106 Date expected = getUnixEpoch();
107 Date actual = (Date) morpher.morph( "1970-1-1" );
108 assertEquals( expected, actual );
109 }
110
111 public void testMorph_matchSecondPattern()
112 {
113 Date expected = getUnixEpoch();
114 Date actual = (Date) morpher.morph( "1/1/1970" );
115 assertEquals( expected, actual );
116 }
117
118 public void testMorph_noConversion()
119 {
120 Date expected = new Date();
121 Date actual = (Date) morpher.morph( expected );
122 assertEquals( expected, actual );
123 }
124
125 public void testMorph_notSupported()
126 {
127 try{
128 morpher.morph( new Object[0] );
129 fail( "Should have thrown a MorphException" );
130 }
131 catch( MorphException expected ){
132
133 }
134 }
135
136 public void testMorph_null()
137 {
138 assertNull( morpher.morph( null ) );
139 }
140
141 public void testMorph_useDefault()
142 {
143 Date expected = new Date();
144 morpher.setDefaultValue( expected );
145 morpher.setUseDefault( true );
146 Date actual = (Date) morpher.morph( "BOGUS" );
147 assertEquals( expected, actual );
148 }
149
150 protected Morpher getAnotherMorpher()
151 {
152 return anotherMorpher;
153 }
154
155 protected Morpher getAnotherMorpherWithDefaultValue()
156 {
157 return anotherMorpherWithDefaultValue;
158 }
159
160 protected Morpher getMorpher()
161 {
162 return morpher;
163 }
164
165 protected Morpher getMorpherWithDefaultValue()
166 {
167 return morpherWithDefaultValue;
168 }
169
170 protected void setUp() throws Exception
171 {
172 morpher = new DateMorpher( new String[] { "yyyy-MM-dd", "MM/dd/yyyy" } );
173 morpherWithDefaultValue = new DateMorpher( new String[] { "yyyy-MM-dd", "MM/dd/yyyy" },
174 new Date() );
175 anotherMorpher = new DateMorpher( new String[] { "yyyy-MM-dd", "MM/dd/yyyy" } );
176 anotherMorpherWithDefaultValue = new DateMorpher(
177 new String[] { "yyyy-MM-dd", "MM/dd/yyyy" }, getUnixEpoch() );
178 }
179
180 private Date getUnixEpoch()
181 {
182 Calendar c = Calendar.getInstance();
183 c.set( Calendar.YEAR, 1970 );
184 c.set( Calendar.MONTH, 0 );
185 c.set( Calendar.DAY_OF_MONTH, 1 );
186 c.set( Calendar.HOUR_OF_DAY, 0 );
187 c.set( Calendar.MINUTE, 0 );
188 c.set( Calendar.SECOND, 0 );
189 c.set( Calendar.MILLISECOND, 0 );
190 return c.getTime();
191 }
192 }