001: /*
002: * Copyright (C) 2005 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 20. June 2005 by Joe Walnes
011: */
012: package com.thoughtworks.acceptance;
013:
014: import com.thoughtworks.acceptance.objects.StandardObject;
015: import com.thoughtworks.xstream.XStream;
016: import com.thoughtworks.xstream.mapper.Mapper;
017: import com.thoughtworks.xstream.mapper.MapperWrapper;
018:
019: public class OmitFieldsTest extends AbstractAcceptanceTest {
020:
021: public static class Thing extends StandardObject {
022: transient String alwaysIgnore;
023: String sometimesIgnore;
024: String neverIgnore;
025: }
026:
027: public void testTransientFieldsAreOmittedByDefault() {
028: Thing in = new Thing();
029: in.alwaysIgnore = "a";
030: in.sometimesIgnore = "b";
031: in.neverIgnore = "c";
032:
033: String expectedXml = "" + "<thing>\n"
034: + " <sometimesIgnore>b</sometimesIgnore>\n"
035: + " <neverIgnore>c</neverIgnore>\n" + "</thing>";
036:
037: xstream.alias("thing", Thing.class);
038:
039: String actualXml = xstream.toXML(in);
040: assertEquals(expectedXml, actualXml);
041:
042: Thing out = (Thing) xstream.fromXML(actualXml);
043: assertEquals(null, out.alwaysIgnore);
044: assertEquals("b", out.sometimesIgnore);
045: assertEquals("c", out.neverIgnore);
046: }
047:
048: public void testAdditionalFieldsCanBeExplicitlyOmittedThroughFacade() {
049: Thing in = new Thing();
050: in.alwaysIgnore = "a";
051: in.sometimesIgnore = "b";
052: in.neverIgnore = "c";
053:
054: String expectedXml = "" //
055: + "<thing>\n" //
056: + " <neverIgnore>c</neverIgnore>\n" //
057: + "</thing>";
058:
059: xstream.alias("thing", Thing.class);
060: xstream.omitField(Thing.class, "sometimesIgnore");
061:
062: String actualXml = xstream.toXML(in);
063: assertEquals(expectedXml, actualXml);
064:
065: Thing out = (Thing) xstream.fromXML(actualXml);
066: assertEquals(null, out.alwaysIgnore);
067: assertEquals(null, out.sometimesIgnore);
068: assertEquals("c", out.neverIgnore);
069: }
070:
071: public static class DerivedThing extends Thing {
072: String derived;
073: }
074:
075: public void testInheritedFieldsCanBeExplicitlyOmittedThroughFacade() {
076: DerivedThing in = new DerivedThing();
077: in.alwaysIgnore = "a";
078: in.sometimesIgnore = "b";
079: in.neverIgnore = "c";
080: in.derived = "d";
081:
082: String expectedXml = "" + "<thing>\n"
083: + " <neverIgnore>c</neverIgnore>\n"
084: + " <derived>d</derived>\n" + "</thing>";
085:
086: xstream.alias("thing", DerivedThing.class);
087: xstream.omitField(Thing.class, "sometimesIgnore");
088:
089: String actualXml = xstream.toXML(in);
090: assertEquals(expectedXml, actualXml);
091:
092: DerivedThing out = (DerivedThing) xstream.fromXML(actualXml);
093: assertEquals(null, out.alwaysIgnore);
094: assertEquals(null, out.sometimesIgnore);
095: assertEquals("c", out.neverIgnore);
096: assertEquals("d", out.derived);
097: }
098:
099: public static class AnotherThing extends StandardObject {
100: String stuff;
101: String cheese;
102: String myStuff;
103: String myCheese;
104: }
105:
106: public void testFieldsCanBeIgnoredUsingCustomStrategy() {
107: AnotherThing in = new AnotherThing();
108: in.stuff = "a";
109: in.cheese = "b";
110: in.myStuff = "c";
111: in.myCheese = "d";
112:
113: String expectedXml = "" + "<thing>\n" + " <stuff>a</stuff>\n"
114: + " <cheese>b</cheese>\n" + "</thing>";
115:
116: class OmitFieldsWithMyPrefixMapper extends MapperWrapper {
117: public OmitFieldsWithMyPrefixMapper(Mapper wrapped) {
118: super (wrapped);
119: }
120:
121: public boolean shouldSerializeMember(Class definedIn,
122: String fieldName) {
123: return !fieldName.startsWith("my");
124: }
125: }
126:
127: xstream = new XStream() {
128: protected MapperWrapper wrapMapper(MapperWrapper next) {
129: return new OmitFieldsWithMyPrefixMapper(next);
130: }
131: };
132:
133: xstream.alias("thing", AnotherThing.class);
134:
135: String actualXml = xstream.toXML(in);
136: assertEquals(expectedXml, actualXml);
137:
138: AnotherThing out = (AnotherThing) xstream.fromXML(actualXml);
139: assertEquals("a", out.stuff);
140: assertEquals("b", out.cheese);
141: assertEquals(null, out.myStuff);
142: assertEquals(null, out.myCheese);
143: }
144:
145: public void testDeletedElementCanBeOmitted() {
146: String expectedXml = ""
147: + "<thing>\n"
148: + " <meanwhileDeletedIgnore>c</meanwhileDeletedIgnore>\n"
149: + " <sometimesIgnore>b</sometimesIgnore>\n"
150: + " <neverIgnore>c</neverIgnore>\n" + "</thing>";
151:
152: xstream.alias("thing", Thing.class);
153: xstream.omitField(Thing.class, "meanwhileDeletedIgnore");
154:
155: Thing out = (Thing) xstream.fromXML(expectedXml);
156: assertEquals("b", out.sometimesIgnore);
157: assertEquals("c", out.neverIgnore);
158: }
159:
160: public void testDeletedAttributeCanBeOmitted() {
161: String expectedXml = ""
162: + "<thing meanwhileDeletedIgnore='c'>\n"
163: + " <sometimesIgnore>b</sometimesIgnore>\n"
164: + " <neverIgnore>c</neverIgnore>\n" + "</thing>";
165:
166: xstream.alias("thing", Thing.class);
167: xstream.omitField(Thing.class, "meanwhileDeletedIgnore");
168:
169: Thing out = (Thing) xstream.fromXML(expectedXml);
170: assertEquals("b", out.sometimesIgnore);
171: assertEquals("c", out.neverIgnore);
172: }
173:
174: public void testAttributeCanBeOmitted() {
175: String expectedXml = "<thing neverIgnore=\"c\"/>";
176:
177: xstream.alias("thing", Thing.class);
178: xstream.useAttributeFor(String.class);
179: xstream.omitField(Thing.class, "sometimesIgnore");
180:
181: Thing in = new Thing();
182: in.alwaysIgnore = "a";
183: in.sometimesIgnore = "b";
184: in.neverIgnore = "c";
185: assertEquals(expectedXml, xstream.toXML(in));
186:
187: Thing out = (Thing) xstream.fromXML(expectedXml);
188: assertNull(out.sometimesIgnore);
189: assertEquals("c", out.neverIgnore);
190: }
191:
192: static class ThingAgain extends Thing {
193: String sometimesIgnore;
194:
195: void setHidden(String s) {
196: super .sometimesIgnore = s;
197: }
198: }
199:
200: // TODO: XSTR-457
201: public void todoTestAnOmittedFieldMakesADefinedInAttributeSuperfluous() {
202: ThingAgain in = new ThingAgain();
203: in.alwaysIgnore = "a";
204: in.setHidden("b");
205: in.neverIgnore = "c";
206: in.sometimesIgnore = "d";
207:
208: xstream.alias("thing", ThingAgain.class);
209: xstream.omitField(ThingAgain.class, "sometimesIgnore");
210:
211: String expectedXml = "" + "<thing>\n"
212: + " <sometimesIgnore>b</sometimesIgnore>\n"
213: + " <neverIgnore>c</neverIgnore>\n" + "</thing>";
214:
215: String actualXml = xstream.toXML(in);
216: assertEquals(expectedXml, actualXml);
217:
218: ThingAgain out = (ThingAgain) xstream.fromXML(expectedXml);
219: assertNull(out.sometimesIgnore);
220: out.alwaysIgnore = "a";
221: out.sometimesIgnore = "d";
222: assertEquals(in, out);
223: }
224: }
|