1 package com.thoughtworks.xstream.converters.basic; 2 3 import com.thoughtworks.xstream.converters.ConversionException; 4 import junit.framework.TestCase; 5 6 import java.util.ArrayList ; 7 import java.util.Collections ; 8 import java.util.Date ; 9 import java.util.List ; 10 11 public class DateConverterTest extends TestCase { 12 13 private DateConverter converter; 14 15 protected void setUp() throws Exception { 16 super.setUp(); 17 converter = new DateConverter(); 18 } 19 20 public void testRetainsDetailDownToMillisecondLevel() { 21 Date in = new Date (); 23 24 String text = converter.toString(in); 26 Date out = (Date ) converter.fromString(text); 27 28 assertEquals(in, out); 30 assertEquals(in.toString(), out.toString()); 31 assertEquals(in.getTime(), out.getTime()); 32 } 33 34 public void testUnmarshallsOldXStreamDatesThatLackMillisecond() { 35 String oldStyleText = "2004-02-22 15:16:04PM"; 37 38 Date out = (Date ) converter.fromString(oldStyleText); 40 41 assertEquals("2004-02-22 15:16:04.0 PM", converter.toString(out)); 43 } 44 45 public void testIsThreadSafe() throws InterruptedException { 46 final List results = Collections.synchronizedList(new ArrayList ()); 47 final DateConverter converter = new DateConverter(); 48 final Object monitor = new Object (); 49 final int numberOfCallsPerThread = 20; 50 final int numberOfThreads = 20; 51 52 Runnable runnable = new Runnable () { 54 public void run() { 55 for (int i = 0; i < numberOfCallsPerThread; i++) { 56 try { 57 converter.fromString("2004-02-22 15:16:04.0 PM"); 58 results.add("PASS"); 59 } catch (ConversionException e) { 60 results.add("FAIL"); 61 } finally { 62 synchronized (monitor) { 63 monitor.notifyAll(); 64 } 65 } 66 } 67 } 68 }; 69 for (int i = 0; i < numberOfThreads; i++) { 70 new Thread (runnable).start(); 71 } 72 73 while (results.size() < numberOfThreads * numberOfCallsPerThread) { 75 synchronized (monitor) { 76 monitor.wait(); 77 } 78 } 79 80 assertTrue("Nothing suceeded", results.contains("PASS")); 81 assertFalse("At least one attempt failed", results.contains("FAIL")); 82 } 83 } 84 | Popular Tags |