/*
 * Copyright (C) 2007 XStream Committers.
 * All rights reserved.
 *
 * The software in this package is published under the terms of the BSD
 * style license a copy of which has been included with this distribution in
 * the LICENSE.txt file.
 * 
 * Created on 06. November 2007 by Joerg Schaible
 */
package com.thoughtworks.acceptance;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.converters.basic.BooleanConverter;
import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
import com.thoughtworks.xstream.converters.reflection.ReflectionConverter;


/**
 * @author J&ouml;rg Schaible
 */
public class LocalConverterTest extends AbstractAcceptanceTest {

    public static class MultiBoolean {
        private boolean bool;
        private boolean speech;
        private boolean bit;

        private MultiBoolean() {
            this(false, false, false);
        }

        public MultiBoolean(boolean bool, boolean speech, boolean bit) {
            this.bool = bool;
            this.speech = speech;
            this.bit = bit;
        }

    }

    protected void setUp() throws Exception {
        super.setUp();
        xstream.alias("mbool", MultiBoolean.class);
        xstream.registerConverter(new ReflectionConverter(
            xstream.getMapper(), new PureJavaReflectionProvider()), XStream.PRIORITY_VERY_LOW);
    }

    public void testCanBeAppliedToIndividualFields() {
        MultiBoolean multiBool = new MultiBoolean(true, true, true);
        String xml = ""
            + "<mbool>\n"
            + "  <bool>true</bool>\n"
            + "  <speech>yes</speech>\n"
            + "  <bit>1</bit>\n"
            + "</mbool>";

        xstream.registerLocalConverter(MultiBoolean.class, "speech", BooleanConverter.YES_NO);
        xstream.registerLocalConverter(MultiBoolean.class, "bit", BooleanConverter.BINARY);
        assertBothWays(multiBool, xml);
    }
}

@@ -376,16 +376,81 @@
 Stream;%0A
+import com.thoughtworks.xstream.converters.SingleValueConverter;%0A
 import c
@@ -1997,10 +1997,1387 @@
 ;%0A    %7D%0A
+    %0A        public static class SymbolParameter %7B%0A        private int type;%0A        private int color;%0A        private int width;%0A%0A        public SymbolParameter() %7B%0A        %7D%0A%0A        public SymbolParameter(int type, int color, int width) %7B%0A            this.type = type;%0A            this.color = color;%0A            this.width = width;%0A        %7D%0A%0A    %7D%0A%0A    public static class HexNumberConverter implements SingleValueConverter %7B%0A        public boolean canConvert(Class type) %7B%0A            return type.equals(int.class) %7C%7C type.equals(Integer.class);%0A        %7D%0A%0A        public Object fromString(String value) %7B%0A            return Integer.parseInt(value, 16);%0A        %7D%0A%0A        public String toString(Object obj) %7B%0A            return Integer.toHexString((Integer)obj);%0A        %7D%0A    %7D%0A%0A    // TODO (XSTR-481)%0A    public void todoTestCanBeUsedForAttributeValue() %7B%0A        SymbolParameter multiBool = new SymbolParameter(1, 0xff00ff, 100);%0A        String xml = %22%22%0A                + %22%3Cparam color=%5C%22ff00ff%5C%22%3E%5Cn%22%0A                + %22  %3Ctype%3E1%3C/type%3E%5Cn%22%0A                + %22  %3Cwidth%3E100%3C/width%3E%5Cn%22%0A                + %22%3C/param%3E%22;%0A%0A        xstream.alias(%22param%22, SymbolParameter.class);%0A        xstream.useAttributeFor(%22color%22, int.class);%0A        xstream.registerLocalConverter(SymbolParameter.class, %22color%22, new HexNumberConverter());%0A        assertBothWays(multiBool, xml);%0A    %7D%0A
 %7D%0A

Prepare unit test for XSTR-481.