KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > acceptance > OmitFieldsTest


1 package com.thoughtworks.acceptance;
2
3 import com.thoughtworks.xstream.XStream;
4 import com.thoughtworks.xstream.alias.ClassMapper;
5 import com.thoughtworks.xstream.mapper.MapperWrapper;
6
7 public class OmitFieldsTest extends AbstractAcceptanceTest {
8
9     public static class Thing extends StandardObject {
10         transient String JavaDoc alwaysIgnore;
11         String JavaDoc sometimesIgnore;
12         String JavaDoc neverIgnore;
13     }
14
15     public void testTransientFieldsAreOmittedByDefault() {
16         Thing in = new Thing();
17         in.alwaysIgnore = "a";
18         in.sometimesIgnore = "b";
19         in.neverIgnore = "c";
20
21         String JavaDoc expectedXml = "" +
22                 "<thing>\n" +
23                 " <sometimesIgnore>b</sometimesIgnore>\n" +
24                 " <neverIgnore>c</neverIgnore>\n" +
25                 "</thing>";
26
27         xstream.alias("thing", Thing.class);
28
29         String JavaDoc actualXml = xstream.toXML(in);
30         assertEquals(expectedXml, actualXml);
31
32         Thing out = (Thing) xstream.fromXML(actualXml);
33         assertEquals(null, out.alwaysIgnore);
34         assertEquals("b", out.sometimesIgnore);
35         assertEquals("c", out.neverIgnore);
36     }
37
38     public void testAdditionalFieldsCanBeExplicitlyOmittedThroughFacade() {
39         Thing in = new Thing();
40         in.alwaysIgnore = "a";
41         in.sometimesIgnore = "b";
42         in.neverIgnore = "c";
43
44         String JavaDoc expectedXml = "" +
45                 "<thing>\n" +
46                 " <neverIgnore>c</neverIgnore>\n" +
47                 "</thing>";
48
49         xstream.alias("thing", Thing.class);
50         xstream.omitField(Thing.class, "sometimesIgnore");
51
52         String JavaDoc actualXml = xstream.toXML(in);
53         assertEquals(expectedXml, actualXml);
54
55         Thing out = (Thing) xstream.fromXML(actualXml);
56         assertEquals(null, out.alwaysIgnore);
57         assertEquals(null, out.sometimesIgnore);
58         assertEquals("c", out.neverIgnore);
59     }
60
61     public static class AnotherThing extends StandardObject {
62         String JavaDoc stuff;
63         String JavaDoc cheese;
64         String JavaDoc myStuff;
65         String JavaDoc myCheese;
66     }
67
68     public void testFieldsCanBeIgnoredUsingCustomStrategy() {
69         AnotherThing in = new AnotherThing();
70         in.stuff = "a";
71         in.cheese = "b";
72         in.myStuff = "c";
73         in.myCheese = "d";
74
75         String JavaDoc expectedXml = "" +
76                 "<thing>\n" +
77                 " <stuff>a</stuff>\n" +
78                 " <cheese>b</cheese>\n" +
79                 "</thing>";
80
81         class OmitFieldsWithMyPrefixMapper extends MapperWrapper {
82             public OmitFieldsWithMyPrefixMapper(ClassMapper wrapped) {
83                 super(wrapped);
84             }
85
86             public boolean shouldSerializeMember(Class JavaDoc definedIn, String JavaDoc fieldName) {
87                 return !fieldName.startsWith("my");
88             }
89         }
90
91         xstream = new XStream() {
92             protected MapperWrapper wrapMapper(MapperWrapper next) {
93                 return new OmitFieldsWithMyPrefixMapper(next);
94             }
95         };
96
97         xstream.alias("thing", AnotherThing.class);
98
99         String JavaDoc actualXml = xstream.toXML(in);
100         assertEquals(expectedXml, actualXml);
101
102         AnotherThing out = (AnotherThing) xstream.fromXML(actualXml);
103         assertEquals("a", out.stuff);
104         assertEquals("b", out.cheese);
105         assertEquals(null, out.myStuff);
106         assertEquals(null, out.myCheese);
107     }
108 }
109
Popular Tags