package com.fasterxml.jackson.dataformat.xml.failing;

import java.io.*;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.*;

import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
import com.fasterxml.jackson.dataformat.xml.annotation.*;

public class TestUnwrappedLists extends XmlTestBase
{
    static class Value {
        public String v;

        public Value() { }
        public Value(String str) { v = str; }
    }

    @XmlRootElement(name="list")
    @JsonRootName("list")
    static class WrappedList {
        @XmlElementWrapper(name="WRAP")
        @JacksonXmlElementWrapper(localName = "WRAP")
        public Value[] value;
    }

    @XmlRootElement(name="list")
    @JsonRootName("list")
    static class UnwrappedList {
//        @XmlElementWrapper(name="")
        @JacksonXmlElementWrapper(localName = "")
        public Value[] value;
    }
    
    /*
    /**********************************************************************
    /* Unit tests
    /**********************************************************************
     */

    public void testWrappedLists() throws Exception
    {
        XmlMapper mapper = new XmlMapper();
        WrappedList list = new WrappedList();
        list.value = new Value[] { new Value("a"), new Value("b") };

        String json = mapper.writeValueAsString(list);
        
        System.out.println("Wrapped   == "+json);
        
//      withJAXB(list);

        assertEquals("<list><WRAP><value><v>a</v></value><value><v>b</v></value></WRAP></list>", json);
    }
    
    public void testUnwrappedLists() throws Exception
    {
        XmlMapper mapper = new XmlMapper();
        UnwrappedList list = new UnwrappedList();
        list.value = new Value[] { new Value("c"), new Value("d") };

        String json = mapper.writeValueAsString(list);
        
        System.out.println("Unwrapped == "+json);

//        withJAXB(list);
        
        assertEquals("<list><value><v>c</v></value><value><v>d</v></value></list>", json);
    }

    void withJAXB(Object ob) throws Exception
    {
        JAXBContext jc = JAXBContext.newInstance(ob.getClass());
        Marshaller m = jc.createMarshaller();
        System.out.print("JAXB      -> ");
        StringWriter sw = new StringWriter();
        m.marshal(ob, sw);
        String xml = sw.toString();
        if (xml.indexOf("<?xml") == 0) {
            xml = xml.substring(xml.indexOf("?>")+2);
        }
        System.out.println(xml);
    }
}

@@ -1425,24 +1425,62 @@
 ue(%22b%22) %7D;%0A%0A
+        // First, serialize:%0A        %0A
         Stri
@@ -1526,76 +1526,8 @@
 t);%0A
-        %0A        System.out.println(%22Wrapped   == %22+json);%0A        %0A
 //  
@@ -1546,17 +1546,16 @@
 (list);%0A
-%0A
         
@@ -1645,24 +1645,244 @@
 st%3E%22, json);
+%0A%0A        // then deserialize back%0A        WrappedList output = mapper.readValue(json, WrappedList.class);%0A        assertNotNull(output);%0A        assertNotNull(output.value);%0A        assertEquals(2, output.value.length);
 %0A    %7D%0A    %0A
@@ -2219,17 +2219,16 @@
 +json);%0A
-%0A
 //      
@@ -2241,33 +2241,24 @@
 JAXB(list);%0A
-        %0A
         asse
@@ -2335,16 +2335,245 @@
 , json);
+%0A%0A        // then deserialize back%0A        UnwrappedList output = mapper.readValue(json, UnwrappedList.class);%0A        assertNotNull(output);%0A        assertNotNull(output.value);%0A        assertEquals(2, output.value.length);%0A    
 %0A    %7D%0A%0A
@@ -3025,16 +3025,16 @@
 n(xml);%0A
- 
+%0A
    %7D%0A%7D%0A

add tests for unwrapped-deserialization (failing so far)