KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > acceptance > ExtendedTypesTest


1 package com.thoughtworks.acceptance;
2
3 import com.thoughtworks.xstream.converters.extended.ColorConverter;
4 import com.thoughtworks.xstream.converters.extended.FileConverter;
5 import com.thoughtworks.xstream.converters.extended.SqlTimestampConverter;
6
7 import java.awt.*;
8 import java.io.File JavaDoc;
9 import java.io.IOException JavaDoc;
10 import java.sql.Timestamp JavaDoc;
11 import java.util.TimeZone JavaDoc;
12
13 public class ExtendedTypesTest extends AbstractAcceptanceTest {
14
15     public void testAwtColor() {
16
17         xstream.alias("awt-color", Color.class);
18         xstream.registerConverter(new ColorConverter());
19
20         Color color = new Color(0, 10, 20, 30);
21
22         String JavaDoc expected = "" +
23                 "<awt-color>\n" +
24                 " <red>0</red>\n" +
25                 " <green>10</green>\n" +
26                 " <blue>20</blue>\n" +
27                 " <alpha>30</alpha>\n" +
28                 "</awt-color>";
29
30         assertBothWays(color, expected);
31     }
32
33     public void testSqlTimestamp() {
34
35         xstream.alias("sql-timestamp", Timestamp JavaDoc.class);
36         xstream.registerConverter(new SqlTimestampConverter());
37
38         TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
39         assertBothWays(new Timestamp JavaDoc(1234),
40                 "<sql-timestamp>1970-01-01 00:00:01.234</sql-timestamp>");
41     }
42
43     public void testFile() throws IOException JavaDoc {
44         xstream.alias("file", File JavaDoc.class);
45         xstream.registerConverter(new FileConverter());
46
47         // using temp file to avoid os specific or directory layout issues
48
File JavaDoc absFile = File.createTempFile("bleh", ".tmp");
49         absFile.deleteOnExit();
50         assertTrue(absFile.isAbsolute());
51         String JavaDoc expectedXml = "<file>" + absFile.getPath() + "</file>";
52         assertFilesBothWay(absFile, expectedXml);
53
54         // test a relative file now
55
File JavaDoc relFile = new File JavaDoc("bloh.tmp");
56         relFile.deleteOnExit();
57         assertFalse(relFile.isAbsolute());
58         expectedXml = "<file>" + relFile.getPath() + "</file>";
59         assertFilesBothWay(relFile, expectedXml);
60     }
61
62     private void assertFilesBothWay(File JavaDoc f, String JavaDoc expectedXml) {
63         String JavaDoc resultXml = xstream.toXML(f);
64         assertEquals(expectedXml, resultXml);
65         Object JavaDoc resultObj = xstream.fromXML(resultXml);
66         assertEquals(File JavaDoc.class, resultObj.getClass());
67         assertEquals(f, resultObj);
68         // now comes the part that fails without a specific converter
69
// in the case of a relative file, this will work, because we run the comparison test from the same working directory
70
assertEquals(f.getAbsolutePath(), ((File JavaDoc)resultObj).getAbsolutePath());
71         assertEquals(f.isAbsolute(), ((File JavaDoc)resultObj).isAbsolute()); // needed because File's equals method only compares the path getAttribute, at least in the win32 implementation
72
}
73 }
74
Popular Tags