summaryrefslogtreecommitdiffstats
path: root/sdo-java/trunk-cts/sdo2.1/src/main/java/test/sdo21/tests/conversion/DateConversionTest.java
blob: b9f74007ad72d4cf643680c46f1600992ab666ee (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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*
 *  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.
 *  
 *  $Rev$  $Date$
 */
package test.sdo21.tests.conversion;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

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

import org.junit.BeforeClass;
import org.junit.Ignore;

import test.sdo21.CTSSuite;
import test.sdo21.framework.CTSTestCase;

import commonj.sdo.helper.DataHelper;

// TODO: Could convert to a paramatized test case and simplify this code a lot

/**
 * Set of tests for date and time related conversions. DateConversionTest
 * 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.
 */
public class DateConversionTest extends CTSTestCase {
    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});

    /**
     * Junit 4.1 will execute this once prior to test cases being executed. This
     * sets up the Calendar with current date.
     */
    @BeforeClass
    public static void setupCalender() {
        data_helper = getTestHelper().createHelperContext().getDataHelper();
        test_calendar = new GregorianCalendar();
        test_calendar.setTime(new Date(System.currentTimeMillis()));
        test_date = test_calendar.getTime();
    }

    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() {
            from_date = null;
            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) {
                throw new RuntimeException( "Failed to initialize test", e );
            }

        }

        // This method is needed because there is not a toDate(Date) method in
        // DataHelper.

        private void initializeToDate() {
            this.from_calendar = test_calendar;
            this.from_date = test_date;
            this.from_type = "toDate";
        }

        private void attemptConversion(TestType to_type) {
            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) {
            String result;

            try {
                result = (String)conversion.invoke(data_helper, parm);
                assertNotNull( result );

                compareFields(parm[0], result, compare_fields);
            } catch (Exception e) {
                e.printStackTrace();
                fail("An unexpected exception was thrown while calling " + conversion.getName()
                    + " after initializing with "
                    + this.from_type
                    + ":"
                    + e.toString());
            }

        }

        private void compareFields(Object compare_to, String output, int[] compare_fields) {
            Calendar result = data_helper.toCalendar(output);
            assertNotNull( "data_helper.toCalendar(" + output + ") should not return null", result );
            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++) {
                int expectedValue = expected.get(compare_fields[i]);
                int actualValue = result.get(compare_fields[i]);
                if (expectedValue != actualValue) {
                      throw new IllegalStateException( "Expected: [" + expectedValue + "] != Actual: [" + actualValue + "]" );
                  }
            }
        }

    }

    /**
     * testConversionsFromDay verifies each of the conversions from Day using
     * the DataHelper
     */
    @org.junit.Test
    public void testConversionsFromDay() {
        Test FromDay = new Test();
        FromDay.initialize(TO_DAY);
        FromDay.attemptConversion(TO_DAY);
    }

    /**
     * testConversionsFromDate verifies each of the conversions from Date using
     * the DataHelper
     */
    @org.junit.Test
    public void testConversionsFromDate() {
        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);
    }

    /**
     * testConversionsFromDateTime verifies each of the conversions from
     * DateTime using the DataHelper
     */
    @org.junit.Test
    public void testConversionsFromDateTime() {
        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);
    }

    /**
     * testConversionsFromDuration verifies each of the conversions from
     * Duration using the DataHelper
     */
    @org.junit.Test
    public void testConversionsFromDuration() {
        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);
    }

    /**
     * testConversionsFromMonth verifies each of the conversions from Month
     * using the DataHelper
     */
    @org.junit.Test
    public void testConversionsFromMonth() {
        Test FromMonth = new Test();

        FromMonth.initialize(TO_MONTH);

        FromMonth.attemptConversion(TO_MONTH);
    }

    /**
     * testConversionsFromMonthDay verifies each of the conversions from
     * MonthDay using the DataHelper
     */
    @org.junit.Test
    public void testConversionsFromMonthDay() {
        Test FromMonthDay = new Test();

        FromMonthDay.initialize(TO_MONTH_DAY);
        FromMonthDay.attemptConversion(TO_MONTH_DAY);
        FromMonthDay.attemptConversion(TO_MONTH);
        FromMonthDay.attemptConversion(TO_DAY);
    }

    /**
     * testConversionsFromTime verifies each of the conversions from Time using
     * the DataHelper
     */
    @org.junit.Test
    public void testConversionsFromTime() {
        Test FromTime = new Test();

        FromTime.initialize(TO_TIME);

        FromTime.attemptConversion(TO_TIME);
    }

    /**
     * testConversionsFromYear verifies each of the conversions from Year using
     * the DataHelper
     */
    @org.junit.Test
    public void testConversionsFromYear() {
        Test FromYear = new Test();

        FromYear.initialize(TO_YEAR);

        FromYear.attemptConversion(TO_YEAR);
    }

    /**
     * testConversionsFromYearMonth verifies each of the conversions from
     * YearMonth using the DataHelper
     */
    @org.junit.Test
    public void testConversionsFromYearMonth() {
        Test FromYearMonth = new Test();

        FromYearMonth.initialize(TO_YEAR_MONTH);

        FromYearMonth.attemptConversion(TO_YEAR_MONTH);
        FromYearMonth.attemptConversion(TO_MONTH);
        FromYearMonth.attemptConversion(TO_YEAR);
    }

    /**
     * testConversionsFromYearMonthDay verifies each of the conversions from
     * YearMonthDay using the DataHelper
     */
    @org.junit.Test
    public void testConversionsFromYearMonthDay() {
        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);
    }

    /**
     * testToDateFormats verifies that strings that should be recognized by
     * toDate do not result in a null Date value when passed to
     * DataHelper.toDate(String).
     */
    @org.junit.Test
    @Ignore("This individual test is effectively in the UnderReview set. See TUSCANY-1175")
    public void testToDateFormats() {
        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]));
        }
    }
}