1 package com.thoughtworks.xstream.converters.enums; 2 3 import com.thoughtworks.xstream.XStream; 4 import junit.framework.TestCase; 5 6 10 14 public class EnumConverterTest extends TestCase { 15 16 private XStream xstream; 17 18 protected void setUp() throws Exception { 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 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 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 |