summaryrefslogtreecommitdiffstats
path: root/sdo-java/trunk/impl/src/test/java/org/apache/tuscany/sdo/test/DateConversionTestCase.java
blob: 86808f3b56b095742cd8c5cece89aefd050b748a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
/**
 *
 *  Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 */
package org.apache.tuscany.sdo.test;

import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import junit.framework.TestCase;

import commonj.sdo.helper.DataHelper;

// DateConversionTestCase insures that the DataHelper conversions accurately
// retain the information in the specified fields (e.g. month, day or year).
// It also provides coverage for the DataHelper API.
// Note that toDate is called each time Test.initialize() is called.

public class DateConversionTestCase extends TestCase
{
    private static Calendar test_calendar;
    private static Date test_date;
    private static DataHelper data_helper;
    
    private static final TestType TO_DATE_TIME = new TestType("toDateTime", new int [] {Calendar.YEAR, Calendar.MONTH, 
            Calendar.DAY_OF_MONTH, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND});
    private static final TestType TO_DURATION = new TestType("toDuration", new int [] {Calendar.YEAR, Calendar.MONTH, 
            Calendar.DAY_OF_MONTH, Calendar.HOUR_OF_DAY, Calendar.MINUTE, Calendar.SECOND, Calendar.MILLISECOND});
    private static final TestType TO_TIME = new TestType("toTime", new int [] {Calendar.HOUR_OF_DAY, Calendar.MINUTE, 
            Calendar.SECOND, Calendar.MILLISECOND});
    private static final TestType TO_DAY = new TestType("toDay", new int[] {Calendar.DAY_OF_MONTH});
    private static final TestType TO_MONTH = new TestType("toMonth", new int[] {Calendar.MONTH});
    private static final TestType TO_MONTH_DAY = new TestType("toMonthDay", new int[] {Calendar.MONTH, Calendar.DAY_OF_MONTH});
    private static final TestType TO_YEAR = new TestType("toYear", new int[] {Calendar.YEAR});
    private static final TestType TO_YEAR_MONTH= new TestType("toYearMonth",  new int[] {Calendar.YEAR, Calendar.MONTH});
    private static final TestType TO_YEAR_MONTH_DAY = new TestType("toYearMonthDay",  new int[] {Calendar.YEAR, 
            Calendar.MONTH, Calendar.DAY_OF_MONTH});    
    
    public DateConversionTestCase() throws Exception
    {
        data_helper = DataHelper.INSTANCE;
        test_calendar = new GregorianCalendar();
        test_calendar.setTime(new Date(System.currentTimeMillis()));
        test_date = test_calendar.getTime();  
    }
    
    //Test for TUSCANY-1514
    public void testNullStringToDate() throws Exception {
    	Date result = data_helper.toDate(null);
    	assertEquals(null, result);
    }
    
    private static class TestType
    {
        private static final Class[] DATE_CLASS_ARRAY = {Date.class};
        private static final Class[] CALENDAR_CLASS_ARRAY = {Calendar.class};
        
        Method date_method;
        Method calendar_method;
        int [] compare_fields;  
        
        public TestType (String method_name, int [] compare_fields)
        {
            try
            {
                this.date_method = DataHelper.class.getMethod(method_name, DATE_CLASS_ARRAY);
            }
            catch (NoSuchMethodException e)
            {
                this.date_method = null;
            }
                
            this.compare_fields = compare_fields;    
            try
            {
                this.calendar_method = DataHelper.class.getMethod(method_name, CALENDAR_CLASS_ARRAY);
            }
            catch (NoSuchMethodException e)
            {
                this.calendar_method = null;
            }

        }
        
        public Method getDateMethod()
        {
            return this.date_method;
        }
        
        public Method getCalendarMethod()
        {
            return this.calendar_method;
        }
    }
    
    private static class Test
    {
        String from_type;
        Date from_date;    
        Calendar from_calendar;
        Class expected_exception;
        
        public Test ()
        {
            this.from_date = null;
            this.from_calendar = null;
            expected_exception = null;
        }
        
        public void initialize (TestType from_type)
        {            
            this.from_type = from_type.getDateMethod().getName();
            
            try
            {    
                String date_string = (String) from_type.getDateMethod().invoke(data_helper, new Object[] {test_date});
                
                this.from_date = data_helper.toDate(date_string);
                date_string = (String) from_type.getCalendarMethod().invoke(data_helper, new Object[] {test_calendar});
                this.from_calendar = data_helper.toCalendar(date_string);
            }
            catch (Exception e)
            {
                this.from_date = null;
                this.from_calendar = null;
            }
            
        }
        
        // This method is needed because there is not a toDate(Date) method in DataHelper.
        
        public void initializeToDate()
        {
            this.from_calendar = test_calendar;
            this.from_date = test_date;
            this.from_type = "toDate";
        }
        
        public void attemptConversion (TestType to_type) throws Exception
        {
            executeConversion(to_type.getDateMethod(), new Object[] {this.from_date}, to_type.compare_fields);
            executeConversion(to_type.getCalendarMethod(), new Object[] {this.from_calendar}, to_type.compare_fields);
        }
        
        private void executeConversion (Method conversion, Object[] parm, int [] compare_fields) throws Exception
        {
            String result;
            
            try
            {
                result = (String) conversion.invoke(data_helper, parm);
            }
            catch (Exception e)
            {
                System.err.println("An unexpected exception was thrown while calling " + conversion.getName() + 
                        " after initializing with " + this.from_type + ".");
                throw e;
            }

             assertTrue("The expected value did not result when calling " + conversion.getName() + 
                    " after initializing with " + this.from_type + ".", compareFields(parm[0], result, compare_fields));
        }
        
        private boolean compareFields(Object compare_to, String output, int [] compare_fields)
        {
            Calendar result = data_helper.toCalendar(output);
            Calendar expected;
            
            if (compare_to instanceof Calendar)
                expected = (GregorianCalendar) test_calendar;
            else
            {
                expected = new GregorianCalendar();
                expected.setTime((Date) test_date);
            }
            
            for (int i = 0; i < compare_fields.length; i++)
            {
                if (expected.get(compare_fields[i]) != result.get(compare_fields[i]))
                {
                   System.err.println("Failed: - expected '" + expected.get(compare_fields[i]) + "' got '" + result.get(compare_fields[i]) + "' for output: " + output);
                   return false;
                }
            }
            return true;
        }
        
    }

    public void testConversionsFromDay() throws Exception
    {
        Test FromDay = new Test();
        
        FromDay.initialize(TO_DAY);
        
        FromDay.attemptConversion(TO_DAY);
    }

    public void testConversionsFromDate() throws Exception
    {
        Test FromDate = new Test();
        
        FromDate.initializeToDate();
        
        FromDate.attemptConversion(TO_DATE_TIME);
        FromDate.attemptConversion(TO_DURATION);
        FromDate.attemptConversion(TO_TIME);
        FromDate.attemptConversion(TO_DAY);
        FromDate.attemptConversion(TO_MONTH);
        FromDate.attemptConversion(TO_MONTH_DAY);
        FromDate.attemptConversion(TO_YEAR);
        FromDate.attemptConversion(TO_YEAR_MONTH);
        FromDate.attemptConversion(TO_YEAR_MONTH_DAY);           
    }
  
    public void testConversionsFromDateTime() throws Exception
    {
        Test FromDateTime = new Test();
        
        FromDateTime.initialize(TO_DATE_TIME);

        FromDateTime.attemptConversion(TO_DATE_TIME);
        FromDateTime.attemptConversion(TO_DURATION);
        FromDateTime.attemptConversion(TO_TIME);
        FromDateTime.attemptConversion(TO_DAY);
        FromDateTime.attemptConversion(TO_MONTH);
        FromDateTime.attemptConversion(TO_MONTH_DAY);
        FromDateTime.attemptConversion(TO_YEAR);
        FromDateTime.attemptConversion(TO_YEAR_MONTH);
        FromDateTime.attemptConversion(TO_YEAR_MONTH_DAY);            
    }
    
    public void testConversionsFromDuration() throws Exception
    {
        Test FromDuration = new Test();
        
        FromDuration.initialize(TO_DURATION);

        FromDuration.attemptConversion(TO_DURATION);
        FromDuration.attemptConversion(TO_DATE_TIME);
        FromDuration.attemptConversion(TO_TIME);
        FromDuration.attemptConversion(TO_DAY);
        FromDuration.attemptConversion(TO_MONTH);
        FromDuration.attemptConversion(TO_MONTH_DAY);
        FromDuration.attemptConversion(TO_YEAR);
        FromDuration.attemptConversion(TO_YEAR_MONTH);
        FromDuration.attemptConversion(TO_YEAR_MONTH_DAY);                   
    }
    
    public void testConversionsFromMonth() throws Exception
    {
        Test FromMonth = new Test();
        
        FromMonth.initialize(TO_MONTH);

        FromMonth.attemptConversion(TO_MONTH);           
    } 
    
    public void testConversionsFromMonthDay() throws Exception
    {
        Test FromMonthDay = new Test();
        
        FromMonthDay.initialize(TO_MONTH_DAY);
        FromMonthDay.attemptConversion(TO_MONTH_DAY);
        FromMonthDay.attemptConversion(TO_MONTH);
        FromMonthDay.attemptConversion(TO_DAY);       
    } 

    public void testConversionsFromTime() throws Exception
    {
        Test FromTime = new Test();
        
        FromTime.initialize(TO_TIME);

        FromTime.attemptConversion(TO_TIME);     
    } 

    public void testConversionsFromYear() throws Exception
    {
        Test FromYear = new Test();

        FromYear.initialize(TO_YEAR);

        FromYear.attemptConversion(TO_YEAR);         
    } 
 
    public void testConversionsFromYearMonth() throws Exception
    {
        Test FromYearMonth = new Test();
        
        FromYearMonth.initialize(TO_YEAR_MONTH);

        FromYearMonth.attemptConversion(TO_YEAR_MONTH);
        FromYearMonth.attemptConversion(TO_MONTH);
        FromYearMonth.attemptConversion(TO_YEAR);       
    } 
    
    public void testConversionsFromYearMonthDay() throws Exception
    {
        Test FromYearMonthDay = new Test();
        
        FromYearMonthDay.initialize(TO_YEAR_MONTH_DAY);

        FromYearMonthDay.attemptConversion(TO_YEAR_MONTH_DAY);
        FromYearMonthDay.attemptConversion(TO_YEAR_MONTH);
        FromYearMonthDay.attemptConversion(TO_MONTH_DAY);
        FromYearMonthDay.attemptConversion(TO_YEAR);        
        FromYearMonthDay.attemptConversion(TO_MONTH);
        FromYearMonthDay.attemptConversion(TO_DAY);      
    } 

    // Ensure that strings that should be recognized by toDate do not 
    // result in a null Date value.
    
    public void testToDateFormats() throws Exception
    {
        String[] validStrings = 
        {
            "2006-03-31T03:30:45.123Z",
            "-2006-03-31T03:30:45.1Z",
            "2006-03-31T03:30:45Z",
            "2006-03-31T03:30:45.123",
            "2006-03-31T03:30:45.1",
            "-2006-03-31T03:30:45",
            "2006-03-31T03:30:45.123 EDT",
            "2006-03-31T03:30:45.1 EDT",
            "2006-03-31T03:30:45 EDT",
            "---05 PST",
            "---04",
            "--12 GMT",
            "--12",
            "--08-08 EST",
            "--08-08",
            "1976-08-08 PDT",
            "1976-08-08",
            "88-12 CST",
            "1988-12",
            "2005 CDT",
            "1999",
            "P2006Y 08M 10D T 12H 24M 07S",
            "P2006Y 10D T 12H",
            "-P2006Y 08M 10D T 07S.2",
            "P08M 10D T 07H",
            "-P 04M 10DT12H 24S.88",
            "PT12H"
        };
        
        for (int i = 0; i < validStrings.length; i++)
        {
           assertNotNull("DataHelper.toData() should not return null for '" + validStrings[i] + "'.", 
                   data_helper.toDate(validStrings[i]));
        }

    }
    
    public void testDateTime(){
      // a small bolt on test case resulting from a fix for JIRA TUSCANY-1044
      String date = DataHelper.INSTANCE.toDateTime(DataHelper.INSTANCE.toCalendar("2007-02-04T00:00:00.200Z"));
      assertEquals("2007-02-04T00:00:00.200Z", date);
  }

}