package com.thoughtworks.acceptance;

import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.converters.SingleValueConverter;

import java.text.DecimalFormat;
import java.text.ParseException;

public class CustomConverterTest extends AbstractAcceptanceTest {
    
    private final class DoubleConverter implements SingleValueConverter {
        private final DecimalFormat formatter;

        private DoubleConverter() {
            formatter = new DecimalFormat("#,###,##0");
        }

        public boolean canConvert(Class type) {
            return type == double.class || type == Double.class;
        }

        public String toString(Object obj) {
            return this.formatter.format(obj);
        }

        public Object fromString(String str) {
            try {
                // the formatter will chose the most appropriate format ... Long
                return this.formatter.parseObject(str);
            } catch (ParseException e) {
                throw new ConversionException(e);
            }
        }
    }

    public static class DoubleWrapper {
        Double d;

        public DoubleWrapper(double d) {
            super();
            this.d = new Double(d);
        }
    }

    public void testWrongObjectTypeReturned() {
        xstream.alias("dw", DoubleWrapper.class);
        xstream.registerConverter(new DoubleConverter());

        String xml = "" +
            "<dw>\n" +
            "  <d>-92.000.000</d>\n" +
            "</dw>";

        try {
            xstream.fromXML(xml);
            fail("Thrown " + ConversionException.class.getName() + " expected");
        } catch (final ConversionException e) {
            assertTrue(e.getMessage().indexOf(Long.class.getName()) > 0);
        }
    }
}

@@ -1190,14 +1190,29 @@
     
-super(
+this.d = new Double(d
 );%0A 
@@ -1220,37 +1220,85 @@
     
+  %7D%0A
       
-this.d = new Double(d);
+  %0A        protected DoubleWrapper() %7B%0A            // JDK 1.3 issue
 %0A   

Make JDK 1.3 happy.