01: /*
02: * Copyright (C) 2004, 2005 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 22. November 2004 by Mauro Talevi
11: */
12: package com.thoughtworks.xstream.converters.extended;
13:
14: import com.thoughtworks.xstream.testutil.TimeZoneChanger;
15:
16: import junit.framework.TestCase;
17:
18: import java.text.ParseException;
19: import java.text.SimpleDateFormat;
20: import java.util.Calendar;
21: import java.util.Date;
22:
23: public class ISO8601DateConverterTest extends TestCase {
24:
25: private ISO8601DateConverter converter;
26:
27: protected void setUp() throws Exception {
28: super .setUp();
29: converter = new ISO8601DateConverter();
30:
31: // Ensure that this test always run as if it were in the EST timezone.
32: // This prevents failures when running the tests in different zones.
33: // Note: 'EST' has no relevance - it was just a randomly chosen zone.
34: TimeZoneChanger.change("EST");
35: }
36:
37: protected void tearDown() throws Exception {
38: TimeZoneChanger.reset();
39: super .tearDown();
40: }
41:
42: public void testUnmashallsInCorrectTimeZone() {
43: // setup
44: Date in = new Date();
45:
46: // execute
47: String text = converter.toString(in);
48: Date out = (Date) converter.fromString(text);
49:
50: // verify
51: assertEquals(in, out);
52: assertEquals(in.toString(), out.toString());
53: assertEquals(in.getTime(), out.getTime());
54: }
55:
56: public void testUnmarshallsISOFormatInUTC() throws ParseException {
57: // setup
58: String isoFormat = "1993-02-14T13:10:30-05:00";
59: String simpleFormat = "1993-02-14 13:10:30EST";
60: // execute
61: Date out = (Date) converter.fromString(isoFormat);
62: Date control = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
63: .parse(simpleFormat);
64: // verify for EST
65: assertEquals("Sun Feb 14 13:10:30 EST 1993", out.toString());
66: assertEquals(control, out);
67: }
68:
69: public void testUnmarshallsISOFormatInLocalTime() {
70: // setup
71: String isoFormat = "1993-02-14T13:10:30";
72: // execute
73: Date out = (Date) converter.fromString(isoFormat);
74: // verify for EST
75: Calendar calendar = Calendar.getInstance();
76: calendar.set(1993, 1, 14, 13, 10, 30);
77: assertEquals(calendar.getTime().toString(), out.toString());
78: }
79: }
|