KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thoughtworks > xstream > converters > reflection > ReflectionConverterTest


1 package com.thoughtworks.xstream.converters.reflection;
2
3 import com.thoughtworks.acceptance.StandardObject;
4 import com.thoughtworks.xstream.XStream;
5 import com.thoughtworks.xstream.io.xml.XppDriver;
6 import junit.framework.TestCase;
7
8 public class ReflectionConverterTest extends TestCase {
9
10     public class World extends StandardObject {
11         int anInt = 1;
12         Integer JavaDoc anInteger = new Integer JavaDoc(2);
13         char anChar = 'a';
14         Character JavaDoc anCharacter = new Character JavaDoc('w');
15         boolean anBool = true;
16         Boolean JavaDoc anBoolean = new Boolean JavaDoc(false);
17         byte aByte = 4;
18         Byte JavaDoc aByteClass = new Byte JavaDoc("5");
19         short aShort = 6;
20         Short JavaDoc aShortClass = new Short JavaDoc("7");
21         float aFloat = 8f;
22         Float JavaDoc aFloatClass = new Float JavaDoc("9");
23         long aLong = 10;
24         Long JavaDoc aLongClass = new Long JavaDoc("11");
25         String JavaDoc anString = new String JavaDoc("XStream programming!");
26     }
27
28     public void testSerializesAllPrimitiveFieldsInACustomObject() {
29         World world = new World();
30
31         XStream xstream = new XStream(new XppDriver());
32         xstream.alias("world", World.class);
33
34         String JavaDoc expected =
35                 "<world>\n" +
36                 " <aByte>4</aByte>\n" +
37                 " <aByteClass>5</aByteClass>\n" +
38                 " <aFloat>8.0</aFloat>\n" +
39                 " <aFloatClass>9.0</aFloatClass>\n" +
40                 " <aLong>10</aLong>\n" +
41                 " <aLongClass>11</aLongClass>\n" +
42                 " <aShort>6</aShort>\n" +
43                 " <aShortClass>7</aShortClass>\n" +
44                 " <anBool>true</anBool>\n" +
45                 " <anBoolean>false</anBoolean>\n" +
46                 " <anChar>a</anChar>\n" +
47                 " <anCharacter>w</anCharacter>\n" +
48                 " <anInt>1</anInt>\n" +
49                 " <anInteger>2</anInteger>\n" +
50                 " <anString>XStream programming!</anString>\n" +
51                 "</world>";
52
53         assertEquals(expected, xstream.toXML(world));
54     }
55
56     static class TypesOfFields extends StandardObject {
57         String JavaDoc normal = "normal";
58         transient String JavaDoc trans = "transient";
59         final String JavaDoc fin = "final";
60         static String JavaDoc stat = "stat";
61     }
62
63     public void testDoesNotSerializeTransientOrStaticFields() {
64         TypesOfFields fields = new TypesOfFields();
65         String JavaDoc expected = "" +
66                 "<types>\n" +
67                 " <fin>final</fin>\n" +
68                 " <normal>normal</normal>\n" +
69                 "</types>";
70
71         XStream xstream = new XStream(new XppDriver());
72         xstream.alias("types", TypesOfFields.class);
73
74         String JavaDoc xml = xstream.toXML(fields);
75         assertEquals(expected, xml);
76
77     }
78
79 }
80
Popular Tags