1 package com.thoughtworks.xstream.converters.extended; 2 3 import com.thoughtworks.xstream.converters.ConversionException; 4 import com.thoughtworks.xstream.testutil.TimeZoneChanger; 5 import junit.framework.TestCase; 6 7 import java.util.ArrayList ; 8 import java.util.Collections ; 9 import java.util.Date ; 10 import java.util.List ; 11 12 public class ISO8601DateConverterTest extends TestCase { 13 14 private ISO8601DateConverter converter; 15 16 protected void setUp() throws Exception { 17 super.setUp(); 18 converter = new ISO8601DateConverter(); 19 20 TimeZoneChanger.change("EST"); 24 } 25 26 protected void tearDown() throws Exception { 27 TimeZoneChanger.reset(); 28 super.tearDown(); 29 } 30 31 public void testRetainsDetailDownToMillisecondLevel() { 32 Date in = new Date (); 34 35 String text = converter.toString(in); 37 Date out = (Date ) converter.fromString(text); 38 39 assertEquals(in, out); 41 assertEquals(in.toString(), out.toString()); 42 assertEquals(in.getTime(), out.getTime()); 43 } 44 45 public void TODOtestUnmarshallsISOFormat() { String isoFormat = "1993-02-14T13:10:30"; 50 Date out = (Date ) converter.fromString(isoFormat); 52 assertEquals("1993-02-14T13:10:30.000Z", converter.toString(out)); 54 } 55 56 public void testIsThreadSafe() throws InterruptedException { 57 final List results = Collections.synchronizedList(new ArrayList ()); 58 final ISO8601DateConverter converter = new ISO8601DateConverter(); 59 final Object monitor = new Object (); 60 final int numberOfCallsPerThread = 20; 61 final int numberOfThreads = 20; 62 63 Runnable runnable = new Runnable () { 65 public void run() { 66 for (int i = 0; i < numberOfCallsPerThread; i++) { 67 try { 68 converter.fromString("1993-02-14T13:10:30"); 69 results.add("PASS"); 70 } catch (ConversionException e) { 71 results.add("FAIL"); 72 } finally { 73 synchronized (monitor) { 74 monitor.notifyAll(); 75 } 76 } 77 } 78 } 79 }; 80 for (int i = 0; i < numberOfThreads; i++) { 81 new Thread (runnable).start(); 82 } 83 84 while (results.size() < numberOfThreads * numberOfCallsPerThread) { 86 synchronized (monitor) { 87 monitor.wait(100); 88 } 89 } 90 91 assertTrue("Nothing succeded", results.contains("PASS")); 92 assertFalse("At least one attempt failed", results.contains("FAIL")); 93 } 94 } 95 | Popular Tags |