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

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

public class XsiNil354Test extends XmlTestBase
{
    protected static class DoubleWrapper {
        public Double d;

        public DoubleWrapper() { }
        public DoubleWrapper(Double value) {
            d = value;
        }
    }

    private final XmlMapper MAPPER = newMapper();

    public void testWithDoubleAsNull() throws Exception
    {
        DoubleWrapper bean = MAPPER.readValue(
"<DoubleWrapper xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><d xsi:nil='true' /></DoubleWrapper>",
                DoubleWrapper.class);
        assertNotNull(bean);
        assertNull(bean.d);

        bean = MAPPER.readValue(
"<DoubleWrapper xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><d xsi:nil='true'>  </d></DoubleWrapper>",
                DoubleWrapper.class);
        assertNotNull(bean);
        assertNull(bean.d);
        
        // actually we should perhaps also verify there is no content but... for now, let's leave it.
    }

    public void testWithDoubleAsNonNull() throws Exception
    {
        DoubleWrapper bean = MAPPER.readValue(
                "<DoubleWrapper xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><d xsi:nil='false'>0.25</d></DoubleWrapper>",
                DoubleWrapper.class);
        assertNotNull(bean);
        assertEquals(Double.valueOf(0.25), bean.d);
    }

    public void testRootPojoAsNull() throws Exception
    {
        Point bean = MAPPER.readValue(
                "<Point xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='true' />",
                Point.class);
        assertNull(bean);
    }

    public void testRootPojoAsNonNull() throws Exception
    {
        Point bean = MAPPER.readValue(
                "<Point xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:nil='false'></Point>",
                Point.class);
        assertNotNull(bean);
    }
}

@@ -393,24 +393,376 @@
    %7D%0A    %7D%0A%0A
+    protected static class Parent %7B%0A        public Level1 level1;%0A    %7D%0A%0A    protected static class Level1 %7B%0A        public Level2 level2;%0A        public String field; // this should not be needed, but an unknown element is thrown without it%0A    %7D%0A%0A    protected static class Level2 %7B%0A        public String ignored;%0A        public String field;%0A    %7D%0A%0A
     private 
@@ -799,16 +799,16 @@
 pper();%0A
-
 %0A    pub
@@ -1363,17 +1363,9 @@
 d);%0A
-        
 %0A
+
     
@@ -2330,32 +2330,32 @@
   Point.class);%0A
-
         assertNo
@@ -2372,11 +2372,818 @@
 );%0A    %7D
+%0A%0A    public void testDoesNotAffectHierarchy() throws Exception%0A    %7B%0A        String xml = %22%3CParent xmlns:xsi=%5C%22http://www.w3.org/2001/XMLSchema-instance%5C%22%3E%22%0A                + %22%3Clevel1%3E%22%0A                + %22%3Clevel2%3E%22%0A                + %22%3Cignored xsi:nil=%5C%22true%5C%22/%3E%22%0A                + %22%3Cfield%3Etest-value%3C/field%3E%22%0A                + %22%3C/level2%3E%22%0A                + %22%3C/level1%3E%22%0A                + %22%3C/Parent%3E%22;%0A        Parent bean = MAPPER.readValue(xml, Parent.class);%0A%0A        assertNotNull(bean);%0A%0A        // this should not be set, but having an xsi:nil field before it causes it to set the next field on the wrong class%0A        assertEquals(%22test-value%22, bean.level1.field);%0A%0A        // fails because field is set on level1 instead of on level2%0A        assertEquals(%22test-value%22, bean.level1.level2.field);%0A    %7D
 %0A%7D%0A

Add test for xsi:nil hierarchy bug (#366)