KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > converters > extended > ISO8601DateConverterTest


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 JavaDoc;
8 import java.util.Collections JavaDoc;
9 import java.util.Date JavaDoc;
10 import java.util.List JavaDoc;
11
12 public class ISO8601DateConverterTest extends TestCase {
13
14     private ISO8601DateConverter converter;
15
16     protected void setUp() throws Exception JavaDoc {
17         super.setUp();
18         converter = new ISO8601DateConverter();
19         
20         // Ensure that this test always run as if it were in the EST timezone.
21
// This prevents failures when running the tests in different zones.
22
// Note: 'EST' has no relevance - it was just a randomly chosen zone.
23
TimeZoneChanger.change("EST");
24     }
25
26     protected void tearDown() throws Exception JavaDoc {
27         TimeZoneChanger.reset();
28         super.tearDown();
29     }
30
31     public void testRetainsDetailDownToMillisecondLevel() {
32         // setup
33
Date JavaDoc in = new Date JavaDoc();
34
35         // execute
36
String JavaDoc text = converter.toString(in);
37         Date JavaDoc out = (Date JavaDoc) converter.fromString(text);
38
39         // verify
40
assertEquals(in, out);
41         assertEquals(in.toString(), out.toString());
42         assertEquals(in.getTime(), out.getTime());
43     }
44
45     // Note: this test assumes that your are in the GMT timezone
46
// if not - simply set your computers' timezone to be in GMT
47
public void TODOtestUnmarshallsISOFormat() { // TODO: Temporarily disabled as the results vary between timezone
48
// setup
49
String JavaDoc isoFormat = "1993-02-14T13:10:30";
50         // execute
51
Date JavaDoc out = (Date JavaDoc) converter.fromString(isoFormat);
52         // verify
53
assertEquals("1993-02-14T13:10:30.000Z", converter.toString(out));
54     }
55
56     public void testIsThreadSafe() throws InterruptedException JavaDoc {
57         final List JavaDoc results = Collections.synchronizedList(new ArrayList JavaDoc());
58         final ISO8601DateConverter converter = new ISO8601DateConverter();
59         final Object JavaDoc monitor = new Object JavaDoc();
60         final int numberOfCallsPerThread = 20;
61         final int numberOfThreads = 20;
62
63         // spawn some concurrent threads, that hammer the converter
64
Runnable JavaDoc runnable = new Runnable JavaDoc() {
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 JavaDoc(runnable).start();
82         }
83
84         // wait for all results
85
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