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 ; 9 import java.io.IOException ; 10 import java.sql.Timestamp ; 11 import java.util.TimeZone ; 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 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 .class); 36 xstream.registerConverter(new SqlTimestampConverter()); 37 38 TimeZone.setDefault(TimeZone.getTimeZone("GMT")); 39 assertBothWays(new Timestamp (1234), 40 "<sql-timestamp>1970-01-01 00:00:01.234</sql-timestamp>"); 41 } 42 43 public void testFile() throws IOException { 44 xstream.alias("file", File .class); 45 xstream.registerConverter(new FileConverter()); 46 47 File absFile = File.createTempFile("bleh", ".tmp"); 49 absFile.deleteOnExit(); 50 assertTrue(absFile.isAbsolute()); 51 String expectedXml = "<file>" + absFile.getPath() + "</file>"; 52 assertFilesBothWay(absFile, expectedXml); 53 54 File relFile = new File ("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 f, String expectedXml) { 63 String resultXml = xstream.toXML(f); 64 assertEquals(expectedXml, resultXml); 65 Object resultObj = xstream.fromXML(resultXml); 66 assertEquals(File .class, resultObj.getClass()); 67 assertEquals(f, resultObj); 68 assertEquals(f.getAbsolutePath(), ((File )resultObj).getAbsolutePath()); 71 assertEquals(f.isAbsolute(), ((File )resultObj).isAbsolute()); } 73 } 74 | Popular Tags |