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

import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

public class EmptyStringValueTest extends XmlTestBase
{
    static class Name {
        public String first;
        public String last;

        public Name() { }
        public Name(String f, String l) {
            first = f;
            last = l;
        }
    }

    static class Names {
        public List<Name> names = new ArrayList<Name>();
    }

    // [dataformat-xml#25]
    static class EmptyStrings25
    {
        @JacksonXmlProperty(isAttribute=true)
        public String a = "NOT SET";
        public String b = "NOT SET";
    }

    /*
    /**********************************************************
    /* Test methods
    /**********************************************************
     */

    private final XmlMapper MAPPER = newMapper();

    public void testEmptyString162() throws Exception
    {
        Name name = MAPPER.readValue("<name><first>Ryan</first><last></last></name>",
                Name.class);
        assertNotNull(name);
        assertEquals("Ryan", name.first);
        assertEquals("", name.last);
    }

    public void testEmptyElement() throws Exception
    {
        final String XML = "<name><first/><last></last></name>";

        // Default settings (since 2.12): empty element does NOT become `null`:
        Name name = MAPPER.readValue(XML, Name.class);
        assertNotNull(name);
        assertEquals("", name.first);
        assertEquals("", name.last);

        // but can be changed
        XmlMapper mapper2 = XmlMapper.builder()
                .enable(FromXmlParser.Feature.EMPTY_ELEMENT_AS_NULL)
                .build();
        name = mapper2.readValue(XML, Name.class);
        assertNotNull(name);
        assertNull(name.first);
        assertEquals("", name.last);
    }

    public void testEmptyStringElement() throws Exception
    {
        // then with empty element
        StringBean bean = MAPPER.readValue("<StringBean><text></text></StringBean>", StringBean.class);
        assertNotNull(bean);
        // empty String or null?
        // As per [dataformat-xml#162], really should be "", not null:
        assertEquals("", bean.text);
//        assertNull(bean.text);
    }

    // [dataformat-xml#25]
    public void testEmptyStringFromElemAndAttr() throws Exception
    {
        EmptyStrings25 ob = MAPPER.readValue("<EmptyString a=''><b /></EmptyString>",
                EmptyStrings25.class);
        assertNotNull(ob);
        assertEquals("", ob.a);
        assertEquals("", ob.b);
    }
}

@@ -98,16 +98,123 @@
 .List;%0A%0A
+import com.fasterxml.jackson.annotation.JsonCreator;%0Aimport com.fasterxml.jackson.annotation.JsonProperty;%0A
 import c
@@ -938,24 +938,354 @@
 ET%22;%0A    %7D%0A%0A
+    // %5Bdataformat-xml#427%5D%0A    static class Stuff427 %7B%0A        String str;%0A%0A        @JsonCreator(mode = JsonCreator.Mode.DELEGATING)%0A        public Stuff427(String s) %7B str = s; %7D%0A    %7D%0A%0A    static class Product427 %7B%0A        Stuff427 stuff;%0A%0A        public Product427(@JsonProperty(%22stuff%22) Stuff427 s) %7B stuff = s; %7D%0A    %7D%0A    %0A
     /*%0A    /
@@ -3203,11 +3203,351 @@
 );%0A    %7D
+%0A%0A    // %5Bdataformat-xml#427%5D%0A    public void testEmptyIssue427() throws Exception%0A    %7B%0A        String xml = %22%3Cproduct%3E%3Cstuff%3E%3C/stuff%3E%3C/product%3E%22;%0A%0A        Product427 product = MAPPER.readValue(xml, Product427.class);%0A%0A        assertNotNull(product);%0A        assertNotNull(product.stuff);%0A        assertEquals(%22%22, product.stuff.str);%0A    %7D
 %0A%7D%0A

Add passing test for #427