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

import java.math.BigDecimal;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonRootName;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonUnwrapped;

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

// Tests copied from databind "JDKNumberDeserTest" (only a small subset)
public class NumberDeserWithXMLTest extends XmlTestBase
{
    // [databind#2644]
    @JsonRootName("Root")
    static class NodeRoot2644 {
        public String type;

        @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXTERNAL_PROPERTY, property = "type")
        @JsonSubTypes(value = {
                @JsonSubTypes.Type(value = NodeParent2644.class, name = "NodeParent")
        })
        public Node2644 node;
    }

    public static class NodeParent2644 extends Node2644 { }

    public static abstract class Node2644 {
        @JsonProperty("amount")
        BigDecimal val;

        public BigDecimal getVal() {
            return val;
        }

        public void setVal(BigDecimal val) {
            this.val = val;
        }
    }

    // [databind#2784]
    static class BigDecimalHolder2784 {
        public BigDecimal value;
    }

    @JsonRootName("Nested")
    static class NestedBigDecimalHolder2784 {
        @JsonUnwrapped
        public BigDecimalHolder2784 holder;
    }

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

    private final XmlMapper MAPPER = newMapper();

    // [databind#2644]
    public void testBigDecimalSubtypes() throws Exception
    {
        ObjectMapper mapper = mapperBuilder()
                .registerSubtypes(NodeParent2644.class)
                .build();
        NodeRoot2644 root = mapper.readValue("<Root>\n"
                +"<type>NodeParent</type>"
                +"<node><amount>9999999999999999.99</amount></node>\n"
                +"</Root>\n",
                NodeRoot2644.class
        );

        assertEquals(new BigDecimal("9999999999999999.99"), root.node.getVal());
    }

    // [databind#2784]
    public void testBigDecimalUnwrapped() throws Exception
    {
        // mapper.enable(DeserializationFeature.USE_BIG_DECIMAL_FOR_FLOATS);
        final String DOC = "<Nested><value>5.00</value></Nested>";
        NestedBigDecimalHolder2784 result = MAPPER.readValue(DOC, NestedBigDecimalHolder2784.class);
        assertEquals(new BigDecimal("5.00"), result.holder.value);
    }
}

@@ -1469,126 +1469,546 @@
     
-@JsonRootName(%22Nested%22)%0A    static class NestedBigDecimalHolder2784 %7B%0A        @JsonUnwrapped%0A        public BigDecimal
+static class DoubleHolder2784 %7B%0A        public Double value;%0A    %7D%0A%0A    static class FloatHolder2784 %7B%0A        public Float value;%0A    %7D%0A%0A    @JsonRootName(%22Nested%22)%0A    static class NestedBigDecimalHolder2784 %7B%0A        @JsonUnwrapped%0A        public BigDecimalHolder2784 holder;%0A    %7D%0A%0A    @JsonRootName(%22Nested%22)%0A    static class NestedDoubleHolder2784 %7B%0A        @JsonUnwrapped%0A        public DoubleHolder2784 holder;%0A    %7D%0A%0A    @JsonRootName(%22Nested%22)%0A    static class NestedFloatHolder2784 %7B%0A        @JsonUnwrapped%0A        public Float
 Hold
@@ -3223,11 +3223,628 @@
 );%0A    %7D
+%0A%0A    public void testDoubleUnwrapped() throws Exception%0A    %7B%0A        final String DOC = %22%3CNested%3E%3Cvalue%3E125.123456789%3C/value%3E%3C/Nested%3E%22;%0A        NestedDoubleHolder2784 result = MAPPER.readValue(DOC, NestedDoubleHolder2784.class);%0A        assertEquals(Double.parseDouble(%22125.123456789%22), result.holder.value);%0A    %7D%0A%0A    public void testFloatUnwrapped() throws Exception%0A    %7B%0A        final String DOC = %22%3CNested%3E%3Cvalue%3E125.123%3C/value%3E%3C/Nested%3E%22;%0A        NestedFloatHolder2784 result = MAPPER.readValue(DOC, NestedFloatHolder2784.class);%0A        assertEquals(Float.parseFloat(%22125.123%22), result.holder.value);%0A    %7D
 %0A%7D%0A

add tests for doubles/floats (#534)