001: /*
002: * Copyright (C) 2005 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 03. October 2005 by Joerg Schaible
011: */
012: package com.thoughtworks.xstream.converters.extended;
013:
014: import com.thoughtworks.xstream.XStream;
015: import com.thoughtworks.xstream.testutil.TimeZoneChanger;
016:
017: import junit.framework.TestCase;
018:
019: import java.sql.Timestamp;
020:
021: /**
022: * @author Chung-Onn Cheong
023: * @author Jörg Schaible
024: */
025: public class ISO8601SqlTimestampConverterTest extends TestCase {
026: private ISO8601SqlTimestampConverter converter;
027:
028: protected void setUp() throws Exception {
029: super .setUp();
030: converter = new ISO8601SqlTimestampConverter();
031:
032: // Ensure that this test always run as if it were in the EST timezone.
033: // This prevents failures when running the tests in different zones.
034: // Note: 'EST' has no relevance - it was just a randomly chosen zone.
035: TimeZoneChanger.change("EST");
036: }
037:
038: protected void tearDown() throws Exception {
039: TimeZoneChanger.reset();
040: super .tearDown();
041: }
042:
043: public void testISO8601SqlTimestamp() {
044: XStream xs = new XStream();
045: xs.registerConverter(converter);
046:
047: long currentTime = System.currentTimeMillis();
048:
049: Timestamp ts1 = new Timestamp(currentTime);
050: String xmlString = xs.toXML(ts1);
051:
052: Timestamp ts2 = (Timestamp) xs.fromXML(xmlString);
053:
054: assertEquals("ISO Timestamp Converted is not the same ", ts1,
055: ts2);
056: assertEquals("Current time not equal to converted timestamp",
057: currentTime, (ts2.getTime() / 1000) * 1000
058: + ts2.getNanos() / 1000000);
059: }
060:
061: public void testISO8601SqlTimestampWith1Milli() {
062: XStream xs = new XStream();
063: xs.registerConverter(converter);
064:
065: long currentTime = (System.currentTimeMillis() / 1000 * 1000) + 1;
066:
067: Timestamp ts1 = new Timestamp(currentTime);
068: String xmlString = xs.toXML(ts1);
069:
070: Timestamp ts2 = (Timestamp) xs.fromXML(xmlString);
071:
072: assertEquals("ISO Timestamp Converted is not the same ", ts1,
073: ts2);
074: assertEquals("Current time not equal to converted timestamp",
075: currentTime, (ts2.getTime() / 1000) * 1000
076: + ts2.getNanos() / 1000000);
077: }
078:
079: public void testISO8601SqlTimestampWithNanos() {
080: XStream xs = new XStream();
081: xs.registerConverter(converter);
082:
083: Timestamp ts1 = new Timestamp(System.currentTimeMillis());
084: ts1.setNanos(987654321);
085: String xmlString = xs.toXML(ts1);
086:
087: Timestamp ts2 = (Timestamp) xs.fromXML(xmlString);
088:
089: assertEquals("ISO Timestamp Converted is not the same ", ts1,
090: ts2);
091: assertEquals("Nanos are not equal", ts1.getNanos(), ts2
092: .getNanos());
093: }
094:
095: public void testTimestampWithoutFraction() {
096: String isoFormat = "1993-02-14T13:10:30-05:00";
097: Timestamp out = (Timestamp) converter.fromString(isoFormat);
098: assertEquals("1993-02-14T13:10:30.000000000-05:00", converter
099: .toString(out));
100: }
101: }
|