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.converters.ConversionException;
015: import com.thoughtworks.xstream.testutil.TimeZoneChanger;
016:
017: import junit.framework.TestCase;
018:
019: import org.joda.time.DateTime;
020:
021: import java.util.ArrayList;
022: import java.util.Calendar;
023: import java.util.Collections;
024: import java.util.List;
025: import java.util.TimeZone;
026:
027: public class ISO8601GregorianCalendarConverterTest extends TestCase {
028:
029: private ISO8601GregorianCalendarConverter converter;
030:
031: protected void setUp() throws Exception {
032: super .setUp();
033: converter = new ISO8601GregorianCalendarConverter();
034:
035: // Ensure that this test always run as if it were in the EST timezone.
036: // This prevents failures when running the tests in different zones.
037: // Note: 'EST' has no relevance - it was just a randomly chosen zone.
038: TimeZoneChanger.change("EST");
039: }
040:
041: protected void tearDown() throws Exception {
042: TimeZoneChanger.reset();
043: super .tearDown();
044: }
045:
046: public void testRetainsDetailDownToMillisecondLevel() {
047: // setup
048: Calendar in = Calendar.getInstance();
049:
050: // execute
051: String text = converter.toString(in);
052: Calendar out = (Calendar) converter.fromString(text);
053:
054: // verify
055: assertEquals(in, out);
056: }
057:
058: public void testSavedTimeIsInUTC() {
059: Calendar in = Calendar.getInstance();
060: final String iso8601;
061: iso8601 = new DateTime(in).toString();
062: String converterXML = converter.toString(in);
063: assertEquals(iso8601, converterXML);
064:
065: Calendar out = (Calendar) converter.fromString(converterXML);
066: assertEquals(in, out);
067: }
068:
069: public void testCanLoadTimeInDifferentTimeZone() {
070: Calendar in = Calendar.getInstance();
071: String converterXML = converter.toString(in);
072:
073: TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
074: Calendar timeInBerlin = Calendar.getInstance();
075: timeInBerlin.setTime(in.getTime());
076: Calendar out = (Calendar) converter.fromString(converterXML);
077: assertEquals(timeInBerlin, out);
078: }
079:
080: public void testIsThreadSafe() throws InterruptedException {
081: final List results = Collections
082: .synchronizedList(new ArrayList());
083: final ISO8601GregorianCalendarConverter converter = new ISO8601GregorianCalendarConverter();
084: final Object monitor = new Object();
085: final int numberOfCallsPerThread = 20;
086: final int numberOfThreads = 20;
087:
088: // spawn some concurrent threads, that hammer the converter
089: Runnable runnable = new Runnable() {
090: public void run() {
091: for (int i = 0; i < numberOfCallsPerThread; i++) {
092: try {
093: converter.fromString("1993-02-14T13:10:30");
094: results.add("PASS");
095: } catch (ConversionException e) {
096: results.add("FAIL");
097: } finally {
098: synchronized (monitor) {
099: monitor.notifyAll();
100: }
101: }
102: }
103: }
104: };
105: for (int i = 0; i < numberOfThreads; i++) {
106: new Thread(runnable).start();
107: }
108:
109: // wait for all results
110: while (results.size() < numberOfThreads
111: * numberOfCallsPerThread) {
112: synchronized (monitor) {
113: monitor.wait(100);
114: }
115: }
116:
117: assertTrue("Nothing succeded", results.contains("PASS"));
118: assertFalse("At least one attempt failed", results
119: .contains("FAIL"));
120: }
121: }
|