KickJava   Java API By Example, From Geeks To Geeks.

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


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 import com.thoughtworks.acceptance.objects.Software;
7
8 public class CustomMapperTest extends AbstractAcceptanceTest {
9
10     /**
11      * A sample mapper strips the underscore prefix of fieldnames in the XML
12      */

13     private static class FieldPrefixStrippingMapper extends MapperWrapper {
14         public FieldPrefixStrippingMapper(ClassMapper wrapped) {
15             super(wrapped);
16         }
17
18         public String JavaDoc serializedMember(Class JavaDoc type, String JavaDoc memberName) {
19             if (memberName.startsWith("_")) {
20                 // _blah -> blah
21
memberName = memberName.substring(1); // chop off leading char (the underscore)
22
} else if (memberName.startsWith("my")) {
23                 // myBlah -> blah
24
memberName = memberName.substring(2, 3).toLowerCase() + memberName.substring(3);
25             }
26             return super.serializedMember(type, memberName);
27         }
28
29         public String JavaDoc realMember(Class JavaDoc type, String JavaDoc serialized) {
30             String JavaDoc fieldName = super.realMember(type, serialized);
31             // Not very efficient or elegant, but enough to get the point across.
32
// Luckily the CachingMapper will ensure this is only ever called once per field per class.
33
try {
34                 type.getDeclaredField("_" + fieldName);
35                 return "_" + fieldName;
36             } catch (NoSuchFieldException JavaDoc e) {
37                 try {
38                     String JavaDoc myified = "my" + fieldName.substring(0, 1).toUpperCase() + fieldName.substring(1);
39                     type.getDeclaredField(myified);
40                     return myified;
41                 } catch (NoSuchFieldException JavaDoc e2) {
42                     return fieldName;
43                 }
44             }
45         }
46     }
47
48     public static class ThingWithStupidNamingConventions extends StandardObject {
49         String JavaDoc _firstName;
50         String JavaDoc lastName;
51         int myAge;
52
53         public ThingWithStupidNamingConventions(String JavaDoc firstname, String JavaDoc lastname, int age) {
54             _firstName = firstname;
55             this.lastName = lastname;
56             myAge = age;
57         }
58     }
59
60     public void testUserDefinedMappingCanAlterFieldName() {
61         xstream = new XStream() {
62             protected MapperWrapper wrapMapper(MapperWrapper next) {
63                 return new FieldPrefixStrippingMapper(next);
64             }
65         };
66         xstream.alias("thing", ThingWithStupidNamingConventions.class);
67
68         ThingWithStupidNamingConventions in = new ThingWithStupidNamingConventions("Joe", "Walnes", 10);
69         String JavaDoc expectedXml = ""
70                 + "<thing>\n"
71                 + " <firstName>Joe</firstName>\n" // look, no underscores!
72
+ " <lastName>Walnes</lastName>\n"
73                 + " <age>10</age>\n"
74                 + "</thing>";
75
76         assertBothWays(in, expectedXml);
77     }
78
79     private static class PackageStrippingMapper extends MapperWrapper {
80         public PackageStrippingMapper(ClassMapper wrapped) {
81             super(wrapped);
82         }
83
84         public String JavaDoc serializedClass(Class JavaDoc type) {
85             return type.getName().replaceFirst(".*\\.", "");
86         }
87     }
88     
89     public void testStripsPackagesUponDeserialization() {
90         // obviously this isn't deserializable!
91
xstream = new XStream() {
92             protected MapperWrapper wrapMapper(MapperWrapper next) {
93                 return new PackageStrippingMapper(next);
94             }
95         };
96
97         // NOTE: no aliases defined!
98

99         String JavaDoc expectedXml = "" +
100                 "<Software>\n" +
101                 " <vendor>ms</vendor>\n" +
102                 " <name>word</name>\n" +
103                 "</Software>";
104         assertEquals(expectedXml, xstream.toXML(new Software("ms", "word")));
105     }
106 }
107
Popular Tags