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 anInteger = new Integer (2); 13 char anChar = 'a'; 14 Character anCharacter = new Character ('w'); 15 boolean anBool = true; 16 Boolean anBoolean = new Boolean (false); 17 byte aByte = 4; 18 Byte aByteClass = new Byte ("5"); 19 short aShort = 6; 20 Short aShortClass = new Short ("7"); 21 float aFloat = 8f; 22 Float aFloatClass = new Float ("9"); 23 long aLong = 10; 24 Long aLongClass = new Long ("11"); 25 String anString = new String ("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 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 normal = "normal"; 58 transient String trans = "transient"; 59 final String fin = "final"; 60 static String stat = "stat"; 61 } 62 63 public void testDoesNotSerializeTransientOrStaticFields() { 64 TypesOfFields fields = new TypesOfFields(); 65 String 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 xml = xstream.toXML(fields); 75 assertEquals(expected, xml); 76 77 } 78 79 } 80 | Popular Tags |