/*
 * Copyright (C) 2011 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 30. July 2011 by Joerg Schaible
 */
package com.thoughtworks.acceptance.annotations;

import com.thoughtworks.acceptance.AbstractAcceptanceTest;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import com.thoughtworks.xstream.annotations.XStreamImplicit;


/**
 * Test for annotations mapping implicit arrays.
 * 
 * @author J&ouml;rg Schaible
 */
public class ImplicitArrayTest extends AbstractAcceptanceTest {

    @Override
    protected XStream createXStream() {
        XStream xstream = super.createXStream();
        xstream.autodetectAnnotations(true);
        return xstream;
    }

    public void testAnnotation() {
        String expected = ""
            + "<root>\n"
            + "  <string>one</string>\n"
            + "  <string>two</string>\n"
            + "</root>";
        ImplicitRootOne implicitRoot = new ImplicitRootOne();
        implicitRoot.values = new String[]{
            "one", "two"
        };
        assertBothWays(implicitRoot, expected);
    }

    public void testAnnotationWithItemFieldName() {
        String expected = ""
            + "<root>\n"
            + "  <value>one</value>\n"
            + "  <value>two</value>\n"
            + "</root>";
        ImplicitRootTwo implicitRoot = new ImplicitRootTwo();
        implicitRoot.values = new String[]{
            "one", "two"
        };
        assertBothWays(implicitRoot, expected);
    }

    @XStreamAlias("root")
    public static class ImplicitRootOne {
        @XStreamImplicit()
        String[] values;
    }

    @XStreamAlias("root")
    public static class ImplicitRootTwo {
        @XStreamImplicit(itemFieldName = "value")
        String[] values;
    }

    @XStreamAlias("component")
    public static class ParameterizedComponentType {
        @XStreamImplicit(itemFieldName = "line")
        private Point[][] signatureLines;
    }

    @XStreamAlias("point")
    public static class Point {
        @XStreamAsAttribute
        private int x;
        @XStreamAsAttribute
        private int y;

        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
    }

    public void testAnnotationHandlesParameterizedComponentTypes() {
        String xml = ""
            + "<component>\n"
            + "  <line>\n"
            + "    <point x=\"33\" y=\"11\"/>\n"
            + "  </line>\n"
            + "</component>";
        ParameterizedComponentType root = new ParameterizedComponentType();
        root.signatureLines = new Point[][] {
            new Point[] { new Point(33, 11) }
        };
        assertBothWays(root, xml);
    }

    @XStreamAlias("primitives")
    public static class PrimitiveArrays {
        @XStreamImplicit()
        private char[] chars;
    }

    public void testWorksForTypesThatArePrimitiveArrays() {
        PrimitiveArrays type = new PrimitiveArrays();
        type.chars = new char[]{'f', 'o', 'o'};
        String xml = "" //
            + "<primitives>\n" // 
            + "  <char>f</char>\n" //
            + "  <char>o</char>\n" //
            + "  <char>o</char>\n" //
            + "</primitives>";
        assertBothWays(type, xml);
    }
}

@@ -2953,32 +2953,842 @@
 oot, xml);%0A    %7D
+%0A    %0A    @XStreamAlias(%22point3d%22)%0A    public static class Point3D extends Point %7B%0A        @XStreamAsAttribute%0A    %09private int z;%0A%0A%09%09public Point3D(int x, int y, int z) %7B%0A    %09%09super(x,y);%0A%09%09%09this.z = z;%0A    %09%7D%0A    %7D%0A    %0A    @XStreamAlias(%22root%22)%0A    public static class VariantArray %7B%0A        @XStreamImplicit%0A    %09private Point%5B%5D points;%0A    %7D%0A    %0A    public void testCanHandleVariantArrays()%0A%09%7B%0A%09%09VariantArray array = new VariantArray();%0A%09%09array.points = new Point%5B%5D %7B%0A%09%09%09new Point(1, 2), %0A%09%09%09new Point3D(3, 4, 5), %0A%09%09%09new Point(6, 7)%0A%09%09%7D;%0A        String xml = %22%22 //%0A            + %22%3Croot%3E%5Cn%22 // %0A            + %22  %3Cpoint x=%5C%221%5C%22 y=%5C%222%5C%22/%3E%5Cn%22%0A            + %22  %3Cpoint3d x=%5C%223%5C%22 y=%5C%224%5C%22 z=%5C%225%5C%22/%3E%5Cn%22%0A            + %22  %3Cpoint x=%5C%226%5C%22 y=%5C%227%5C%22/%3E%5Cn%22%0A            + %22%3C/root%3E%22;%0A        assertBothWays(array, xml);%0A%09%7D
 %0A%0A    @XStreamAl

Add test for implicit variant array.