/*
 * Copyright (C) 2005 Joe Walnes.
 * Copyright (C) 2006, 2007, 2008 XStream Committers.
 * All rights reserved.
 *
 * The software in this package is published under the terms of the BSD
 * style license a copy of which has been included with this distribution in
 * the LICENSE.txt file.
 * 
 * Created on 18. March 2005 by Joe Walnes
 */
package com.thoughtworks.xstream.converters.enums;

import com.thoughtworks.xstream.XStream;
import junit.framework.TestCase;

import java.util.Arrays;


// ***** READ THIS *****
// This class will only compile with JDK 1.5.0 or above as it test Java enums.
// If you are using an earlier version of Java, just don't try to build this class. XStream
// should work fine without it.

/**
 * @author Joe Walnes
 * @author Bryan Coleman
 */
public class EnumConverterTest extends TestCase {

    private XStream xstream;

    protected void setUp() throws Exception {
        super.setUp();
        xstream = new XStream();
        xstream.alias("simple", SimpleEnum.class);
        xstream.alias("polymorphic", PolymorphicEnum.class);
    }

    public void testRepresentsEnumAsSingleStringValue() {
        String expectedXml = "<simple>GREEN</simple>";
        SimpleEnum in = SimpleEnum.GREEN;
        assertEquals(expectedXml, xstream.toXML(in));
        assertEquals(in, xstream.fromXML(expectedXml));
    }

    public void testRepresentsPolymorphicEnumAsSingleStringValue() {
        String expectedXml = "<polymorphic>B</polymorphic>";
        PolymorphicEnum in = PolymorphicEnum.B;
        assertEquals(expectedXml, xstream.toXML(in));
        assertEquals(in, xstream.fromXML(expectedXml));
    }

    public void testDeserializedEnumIsTheSameNotJustEqual() {
        assertSame(SimpleEnum.GREEN, xstream.fromXML(xstream.toXML(SimpleEnum.GREEN)));
        assertSame(PolymorphicEnum.B, xstream.fromXML(xstream.toXML(PolymorphicEnum.B)));
    }

    public void testResolvesSpecializedPolymorphicEnum() {
        PolymorphicEnum in;
        PolymorphicEnum out;

        in = PolymorphicEnum.A;
        out = (PolymorphicEnum)xstream.fromXML(xstream.toXML(in));
        assertEquals("apple", out.fruit());

        in = PolymorphicEnum.B;
        out = (PolymorphicEnum)xstream.fromXML(xstream.toXML(in));
        assertEquals("banana", out.fruit());
    }

    static class Bowl {
        Fruit fruit;
    }

    public void testSupportsDefaultImplementationToBeAnEnum() {
        xstream.alias("bowl", Bowl.class);
        xstream.addDefaultImplementation(PolymorphicEnum.class, Fruit.class);
        String expectedXml = "" // force format
            + "<bowl>\n"
            + "  <fruit>B</fruit>\n"
            + "</bowl>";
        Bowl in = new Bowl();
        in.fruit = PolymorphicEnum.B;
        assertEquals(expectedXml, xstream.toXML(in));
        Bowl out = (Bowl)xstream.fromXML(expectedXml);
        assertSame(in.fruit, PolymorphicEnum.B);
    }

    public void testEnumsAreImmutable() {
        SimpleEnum[] in = new SimpleEnum[]{SimpleEnum.GREEN, SimpleEnum.GREEN};
        String expectedXml = ""
            + "<simple-array>\n"
            + "  <simple>GREEN</simple>\n"
            + "  <simple>GREEN</simple>\n"
            + "</simple-array>";
        assertEquals(expectedXml, xstream.toXML(in));
        assertTrue(Arrays.equals(in, (SimpleEnum[])xstream.fromXML(expectedXml)));
    }
}

@@ -61,14 +61,8 @@
 2007
-, 2008
  XSt
@@ -461,35 +461,8 @@
 e;%0A%0A
-import java.util.Arrays;%0A%0A%0A
 // *
@@ -652,19 +652,16 @@
  XStream
-%0A//
  should 
@@ -2026,32 +2026,33 @@
 PolymorphicEnum)
+ 
 xstream.fromXML(
@@ -2179,16 +2179,17 @@
 hicEnum)
+ 
 xstream.
@@ -2272,1060 +2272,6 @@
  %7D%0A%0A
-    static class Bowl %7B%0A        Fruit fruit;%0A    %7D%0A%0A    public void testSupportsDefaultImplementationToBeAnEnum() %7B%0A        xstream.alias(%22bowl%22, Bowl.class);%0A        xstream.addDefaultImplementation(PolymorphicEnum.class, Fruit.class);%0A        String expectedXml = %22%22 // force format%0A            + %22%3Cbowl%3E%5Cn%22%0A            + %22  %3Cfruit%3EB%3C/fruit%3E%5Cn%22%0A            + %22%3C/bowl%3E%22;%0A        Bowl in = new Bowl();%0A        in.fruit = PolymorphicEnum.B;%0A        assertEquals(expectedXml, xstream.toXML(in));%0A        Bowl out = (Bowl)xstream.fromXML(expectedXml);%0A        assertSame(in.fruit, PolymorphicEnum.B);%0A    %7D%0A%0A    public void testEnumsAreImmutable() %7B%0A        SimpleEnum%5B%5D in = new SimpleEnum%5B%5D%7BSimpleEnum.GREEN, SimpleEnum.GREEN%7D;%0A        String expectedXml = %22%22%0A            + %22%3Csimple-array%3E%5Cn%22%0A            + %22  %3Csimple%3EGREEN%3C/simple%3E%5Cn%22%0A            + %22  %3Csimple%3EGREEN%3C/simple%3E%5Cn%22%0A            + %22%3C/simple-array%3E%22;%0A        assertEquals(expectedXml, xstream.toXML(in));%0A        assertTrue(Arrays.equals(in, (SimpleEnum%5B%5D)xstream.fromXML(expectedXml)));%0A    %7D%0A
 %7D%0A

Revert last version.