01: /*
02: * Copyright (C) 2005 Joe Walnes.
03: * Copyright (C) 2006, 2007, 2008 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 18. March 2005 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.converters.enums;
13:
14: import com.thoughtworks.xstream.XStream;
15: import junit.framework.TestCase;
16:
17: // ***** READ THIS *****
18: // This class will only compile with JDK 1.5.0 or above as it test Java enums.
19: // If you are using an earlier version of Java, just don't try to build this class. XStream should work fine without it.
20:
21: /**
22: * @author Joe Walnes
23: * @author Bryan Coleman
24: */
25: public class EnumConverterTest extends TestCase {
26:
27: private XStream xstream;
28:
29: protected void setUp() throws Exception {
30: super .setUp();
31: xstream = new XStream();
32: xstream.alias("simple", SimpleEnum.class);
33: xstream.alias("polymorphic", PolymorphicEnum.class);
34: }
35:
36: public void testRepresentsEnumAsSingleStringValue() {
37: String expectedXml = "<simple>GREEN</simple>";
38: SimpleEnum in = SimpleEnum.GREEN;
39: assertEquals(expectedXml, xstream.toXML(in));
40: assertEquals(in, xstream.fromXML(expectedXml));
41: }
42:
43: public void testRepresentsPolymorphicEnumAsSingleStringValue() {
44: String expectedXml = "<polymorphic>B</polymorphic>";
45: PolymorphicEnum in = PolymorphicEnum.B;
46: assertEquals(expectedXml, xstream.toXML(in));
47: assertEquals(in, xstream.fromXML(expectedXml));
48: }
49:
50: public void testDeserializedEnumIsTheSameNotJustEqual() {
51: assertSame(SimpleEnum.GREEN, xstream.fromXML(xstream
52: .toXML(SimpleEnum.GREEN)));
53: assertSame(PolymorphicEnum.B, xstream.fromXML(xstream
54: .toXML(PolymorphicEnum.B)));
55: }
56:
57: public void testResolvesSpecializedPolymorphicEnum() {
58: PolymorphicEnum in;
59: PolymorphicEnum out;
60:
61: in = PolymorphicEnum.A;
62: out = (PolymorphicEnum) xstream.fromXML(xstream.toXML(in));
63: assertEquals("apple", ((Fruit) out).fruit()); // see Bug ID: 6522780
64:
65: in = PolymorphicEnum.B;
66: out = (PolymorphicEnum) xstream.fromXML(xstream.toXML(in));
67: assertEquals("banana", ((Fruit) out).fruit()); // see Bug ID: 6522780
68: }
69:
70: }
|