01: /*
02: * Copyright (C) 2006, 2007 XStream Committers.
03: * All rights reserved.
04: *
05: * The software in this package is published under the terms of the BSD
06: * style license a copy of which has been included with this distribution in
07: * the LICENSE.txt file.
08: *
09: * Created on 17. March 2006 by Joerg Schaible
10: */
11: package com.thoughtworks.acceptance;
12:
13: import com.thoughtworks.xstream.converters.ConversionException;
14: import com.thoughtworks.xstream.converters.SingleValueConverter;
15:
16: import java.text.DecimalFormat;
17: import java.text.ParseException;
18:
19: public class CustomConverterTest extends AbstractAcceptanceTest {
20:
21: private final class DoubleConverter implements SingleValueConverter {
22: private final DecimalFormat formatter;
23:
24: private DoubleConverter() {
25: formatter = new DecimalFormat("#,###,##0");
26: }
27:
28: public boolean canConvert(Class type) {
29: return type == double.class || type == Double.class;
30: }
31:
32: public String toString(Object obj) {
33: return this .formatter.format(obj);
34: }
35:
36: public Object fromString(String str) {
37: try {
38: // the formatter will chose the most appropriate format ... Long
39: return this .formatter.parseObject(str);
40: } catch (ParseException e) {
41: throw new ConversionException(e);
42: }
43: }
44: }
45:
46: public static class DoubleWrapper {
47: Double d;
48:
49: public DoubleWrapper(double d) {
50: this .d = new Double(d);
51: }
52:
53: protected DoubleWrapper() {
54: // JDK 1.3 issue
55: }
56: }
57:
58: public void testWrongObjectTypeReturned() {
59: xstream.alias("dw", DoubleWrapper.class);
60: xstream.registerConverter(new DoubleConverter());
61:
62: String xml = "" + "<dw>\n" + " <d>-92.000.000</d>\n" + "</dw>";
63:
64: try {
65: xstream.fromXML(xml);
66: fail("Thrown " + ConversionException.class.getName()
67: + " expected");
68: } catch (final ConversionException e) {
69: assertTrue(e.getMessage().indexOf(Long.class.getName()) > 0);
70: }
71: }
72: }
|