package com.thoughtworks.acceptance;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.mapper.Mapper;
import com.thoughtworks.xstream.mapper.MapperWrapper;

public class OmitFieldsTest extends AbstractAcceptanceTest {

    public static class Thing extends StandardObject {
        transient String alwaysIgnore;
        String sometimesIgnore;
        String neverIgnore;
    }

    public void testTransientFieldsAreOmittedByDefault() {
        Thing in = new Thing();
        in.alwaysIgnore = "a";
        in.sometimesIgnore = "b";
        in.neverIgnore = "c";

        String expectedXml = "" +
                "<thing>\n" +
                "  <sometimesIgnore>b</sometimesIgnore>\n" +
                "  <neverIgnore>c</neverIgnore>\n" +
                "</thing>";

        xstream.alias("thing", Thing.class);

        String actualXml = xstream.toXML(in);
        assertEquals(expectedXml, actualXml);

        Thing out = (Thing) xstream.fromXML(actualXml);
        assertEquals(null, out.alwaysIgnore);
        assertEquals("b", out.sometimesIgnore);
        assertEquals("c", out.neverIgnore);
    }

    public void testAdditionalFieldsCanBeExplicitlyOmittedThroughFacade() {
        Thing in = new Thing();
        in.alwaysIgnore = "a";
        in.sometimesIgnore = "b";
        in.neverIgnore = "c";

        String expectedXml = "" +
                "<thing>\n" +
                "  <neverIgnore>c</neverIgnore>\n" +
                "</thing>";

        xstream.alias("thing", Thing.class);
        xstream.omitField(Thing.class, "sometimesIgnore");

        String actualXml = xstream.toXML(in);
        assertEquals(expectedXml, actualXml);

        Thing out = (Thing) xstream.fromXML(actualXml);
        assertEquals(null, out.alwaysIgnore);
        assertEquals(null, out.sometimesIgnore);
        assertEquals("c", out.neverIgnore);
    }

    public static class AnotherThing extends StandardObject {
        String stuff;
        String cheese;
        String myStuff;
        String myCheese;
    }

    public void testFieldsCanBeIgnoredUsingCustomStrategy() {
        AnotherThing in = new AnotherThing();
        in.stuff = "a";
        in.cheese = "b";
        in.myStuff = "c";
        in.myCheese = "d";

        String expectedXml = "" +
                "<thing>\n" +
                "  <stuff>a</stuff>\n" +
                "  <cheese>b</cheese>\n" +
                "</thing>";

        class OmitFieldsWithMyPrefixMapper extends MapperWrapper {
            public OmitFieldsWithMyPrefixMapper(Mapper wrapped) {
                super(wrapped);
            }

            public boolean shouldSerializeMember(Class definedIn, String fieldName) {
                return !fieldName.startsWith("my");
            }
        }

        xstream = new XStream() {
            protected MapperWrapper wrapMapper(MapperWrapper next) {
                return new OmitFieldsWithMyPrefixMapper(next);
            }
        };

        xstream.alias("thing", AnotherThing.class);

        String actualXml = xstream.toXML(in);
        assertEquals(expectedXml, actualXml);

        AnotherThing out = (AnotherThing) xstream.fromXML(actualXml);
        assertEquals("a", out.stuff);
        assertEquals("b", out.cheese);
        assertEquals(null, out.myStuff);
        assertEquals(null, out.myCheese);
    }
}

@@ -1933,32 +1933,1041 @@
 ore);%0D%0A    %7D%0D%0A%0D%0A
+    public static class DerivedThing extends Thing %7B%0D%0A        String derived;%0D%0A    %7D%0D%0A%0D%0A    public void testInheritedFieldsCanBeExplicitlyOmittedThroughFacade() %7B%0D%0A        DerivedThing in = new DerivedThing();%0D%0A        in.alwaysIgnore = %22a%22;%0D%0A        in.sometimesIgnore = %22b%22;%0D%0A        in.neverIgnore = %22c%22;%0D%0A        in.derived = %22d%22;%0D%0A%0D%0A        String expectedXml = %22%22 +%0D%0A                %22%3Cthing%3E%5Cn%22 +%0D%0A                %22  %3Cderived%3Ed%3C/derived%3E%5Cn%22 +%0D%0A                %22  %3CneverIgnore%3Ec%3C/neverIgnore%3E%5Cn%22 +%0D%0A                %22%3C/thing%3E%22;%0D%0A%0D%0A        xstream.alias(%22thing%22, DerivedThing.class);%0D%0A        xstream.omitField(Thing.class, %22sometimesIgnore%22);%0D%0A%0D%0A        String actualXml = xstream.toXML(in);%0D%0A        assertEquals(expectedXml, actualXml);%0D%0A%0D%0A        DerivedThing out = (DerivedThing) xstream.fromXML(actualXml);%0D%0A        assertEquals(null, out.alwaysIgnore);%0D%0A        assertEquals(null, out.sometimesIgnore);%0D%0A        assertEquals(%22c%22, out.neverIgnore);%0D%0A        assertEquals(%22d%22, out.derived);%0D%0A    %7D%0D%0A%0D%0A
     public stati

Test to omit inherited fields.