KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > converters > enums > EnumConverterTest


1 package com.thoughtworks.xstream.converters.enums;
2
3 import com.thoughtworks.xstream.XStream;
4 import junit.framework.TestCase;
5
6 // ***** READ THIS *****
7
// This class will only compile with JDK 1.5.0 or above as it test Java enums.
8
// If you are using an earlier version of Java, just don't try to build this class. XStream should work fine without it.
9

10 /**
11  * @author Joe Walnes
12  * @author Bryan Coleman
13  */

14 public class EnumConverterTest extends TestCase {
15
16     private XStream xstream;
17
18     protected void setUp() throws Exception JavaDoc {
19         super.setUp();
20         xstream = new XStream();
21         xstream.alias("simple", SimpleEnum.class);
22         xstream.alias("polymorphic", PolymorphicEnum.class);
23     }
24
25     public void testRepresentsEnumAsSingleStringValue() {
26         String JavaDoc expectedXml = "<simple>GREEN</simple>";
27         SimpleEnum in = SimpleEnum.GREEN;
28         assertEquals(expectedXml, xstream.toXML(in));
29         assertEquals(in, xstream.fromXML(expectedXml));
30     }
31
32     public void testRepresentsPolymorphicEnumAsSingleStringValue() {
33         String JavaDoc expectedXml = "<polymorphic>B</polymorphic>";
34         PolymorphicEnum in = PolymorphicEnum.B;
35         assertEquals(expectedXml, xstream.toXML(in));
36         assertEquals(in, xstream.fromXML(expectedXml));
37     }
38
39     public void testDeserializedEnumIsTheSameNotJustEqual() {
40         assertSame(SimpleEnum.GREEN, xstream.fromXML(xstream.toXML(SimpleEnum.GREEN)));
41         assertSame(PolymorphicEnum.B, xstream.fromXML(xstream.toXML(PolymorphicEnum.B)));
42     }
43
44     public void testResolvesSpecializedPolymorphicEnum() {
45         PolymorphicEnum in;
46         PolymorphicEnum out;
47
48         in = PolymorphicEnum.A;
49         out = (PolymorphicEnum) xstream.fromXML(xstream.toXML(in));
50         assertEquals("apple", out.fruit());
51
52         in = PolymorphicEnum.B;
53         out = (PolymorphicEnum) xstream.fromXML(xstream.toXML(in));
54         assertEquals("banana", out.fruit());
55     }
56
57 }
58
Popular Tags