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 12. April 2005 by Joe Walnes
011: */
012: package com.thoughtworks.xstream.converters.javabean;
013:
014: import junit.framework.TestCase;
015:
016: import java.util.Comparator;
017:
018: import com.thoughtworks.acceptance.objects.StandardObject;
019: import com.thoughtworks.xstream.XStream;
020:
021: public class JavaBeanConverterTest extends TestCase {
022:
023: // Different JDK versions deliver properties in different order - so sort them!
024: static class StringComparator implements Comparator {
025:
026: public int compare(Object o1, Object o2) {
027: return ((String) o1).compareToIgnoreCase((String) o2);
028: }
029:
030: }
031:
032: public static class World extends StandardObject {
033:
034: int anInt = 1;
035: Integer anInteger = new Integer(2);
036: char aChar = 'a';
037: Character aCharacter = new Character('w');
038: boolean aBool = true;
039: Boolean aBoolean = new Boolean(false);
040: byte aByte = 4;
041: Byte aByteClass = new Byte("5");
042: short aShort = 6;
043: Short aShortClass = new Short("7");
044: float aFloat = 8f;
045: Float aFloatClass = new Float("9");
046: long aLong = 10;
047: Long aLongClass = new Long("11");
048: String aString = new String("XStream programming!");
049:
050: public byte getAByte() {
051: return aByte;
052: }
053:
054: public void setAByte(byte byte1) {
055: aByte = byte1;
056: }
057:
058: public Byte getAByteClass() {
059: return aByteClass;
060: }
061:
062: public void setAByteClass(Byte byteClass) {
063: aByteClass = byteClass;
064: }
065:
066: public float getAFloat() {
067: return aFloat;
068: }
069:
070: public void setAFloat(float float1) {
071: aFloat = float1;
072: }
073:
074: public Float getAFloatClass() {
075: return aFloatClass;
076: }
077:
078: public void setAFloatClass(Float floatClass) {
079: aFloatClass = floatClass;
080: }
081:
082: public long getALong() {
083: return aLong;
084: }
085:
086: public void setALong(long long1) {
087: aLong = long1;
088: }
089:
090: public Long getALongClass() {
091: return aLongClass;
092: }
093:
094: public void setALongClass(Long longClass) {
095: aLongClass = longClass;
096: }
097:
098: public boolean isABool() {
099: return aBool;
100: }
101:
102: public void setABool(boolean aBool) {
103: this .aBool = aBool;
104: }
105:
106: public Boolean getABoolean() {
107: return aBoolean;
108: }
109:
110: public void setABoolean(Boolean aBoolean) {
111: this .aBoolean = aBoolean;
112: }
113:
114: public char getAChar() {
115: return aChar;
116: }
117:
118: public void setAChar(char aChar) {
119: this .aChar = aChar;
120: }
121:
122: public Character getACharacter() {
123: return aCharacter;
124: }
125:
126: public void setACharacter(Character aCharacter) {
127: this .aCharacter = aCharacter;
128: }
129:
130: public int getAnInt() {
131: return anInt;
132: }
133:
134: public void setAnInt(int anInt) {
135: this .anInt = anInt;
136: }
137:
138: public Integer getAnInteger() {
139: return anInteger;
140: }
141:
142: public void setAnInteger(Integer anInteger) {
143: this .anInteger = anInteger;
144: }
145:
146: public String getAString() {
147: return aString;
148: }
149:
150: public void setAString(String aString) {
151: this .aString = aString;
152: }
153:
154: public short getAShort() {
155: return aShort;
156: }
157:
158: public void setAShort(short short1) {
159: aShort = short1;
160: }
161:
162: public Short getAShortClass() {
163: return aShortClass;
164: }
165:
166: public void setAShortClass(Short shortClass) {
167: aShortClass = shortClass;
168: }
169: }
170:
171: public void testSerializesAllPrimitiveFieldsInACustomObject() {
172: World world = new World();
173:
174: XStream xstream = new XStream();
175: xstream.registerConverter(
176: new JavaBeanConverter(xstream.getMapper(),
177: new BeanProvider(new StringComparator())),
178: XStream.PRIORITY_VERY_LOW);
179: xstream.alias("world", World.class);
180:
181: String expected = "" + "<world>\n" + " <ABool>true</ABool>\n"
182: + " <ABoolean>false</ABoolean>\n"
183: + " <AByte>4</AByte>\n"
184: + " <AByteClass>5</AByteClass>\n"
185: + " <AChar>a</AChar>\n"
186: + " <ACharacter>w</ACharacter>\n"
187: + " <AFloat>8.0</AFloat>\n"
188: + " <AFloatClass>9.0</AFloatClass>\n"
189: + " <ALong>10</ALong>\n"
190: + " <ALongClass>11</ALongClass>\n"
191: + " <anInt>1</anInt>\n"
192: + " <anInteger>2</anInteger>\n"
193: + " <AShort>6</AShort>\n"
194: + " <AShortClass>7</AShortClass>\n"
195: + " <AString>XStream programming!</AString>\n"
196: + "</world>";
197:
198: String result = xstream.toXML(world);
199:
200: assertEquals(expected, result);
201: }
202:
203: /**
204: * Only normal and trans are serializable properties, the field modifiers do not matter
205: */
206: public static class TypesOfFields extends StandardObject {
207: String normal = "normal";
208:
209: transient String trans = "transient";
210:
211: final String fin = "final";
212:
213: static String stat = "stat";
214:
215: public static String getStat() {
216: return stat;
217: }
218:
219: public static void setStat(String stat) {
220: TypesOfFields.stat = stat;
221: }
222:
223: public String getFin() {
224: return fin;
225: }
226:
227: public String getNormal() {
228: return normal;
229: }
230:
231: public void setNormal(String normal) {
232: this .normal = normal;
233: }
234:
235: public String getTrans() {
236: return trans;
237: }
238:
239: public void setTrans(String trans) {
240: this .trans = trans;
241: }
242: }
243:
244: public void testDoesNotSerializeStaticFields() {
245: TypesOfFields fields = new TypesOfFields();
246: String expected = "" + "<types>\n"
247: + " <normal>normal</normal>\n"
248: + " <trans>transient</trans>\n" + "</types>";
249:
250: XStream xstream = new XStream();
251: xstream.registerConverter(
252: new JavaBeanConverter(xstream.getMapper(),
253: new BeanProvider(new StringComparator())), -20);
254: xstream.alias("types", TypesOfFields.class);
255:
256: String xml = xstream.toXML(fields);
257: assertEquals(expected, xml);
258:
259: }
260:
261: public static class SimpleBean extends StandardObject {
262: private Object member;
263:
264: public Object getMember() {
265: return this .member;
266: }
267:
268: public void setMember(Object member) {
269: this .member = member;
270: }
271: }
272:
273: public void testSupportsTypeAlias() {
274: SimpleBean innerBean = new SimpleBean();
275: SimpleBean bean = new SimpleBean();
276: bean.setMember(innerBean);
277: innerBean.setMember("foo");
278:
279: String expected = "" + "<bean>\n"
280: + " <member class=\"bean\">\n"
281: + " <member class=\"string\">foo</member>\n"
282: + " </member>\n" + "</bean>";
283:
284: XStream xstream = new XStream();
285: xstream.registerConverter(new JavaBeanConverter(xstream
286: .getMapper()), XStream.PRIORITY_VERY_LOW);
287: xstream.alias("bean", SimpleBean.class);
288:
289: String xml = xstream.toXML(bean);
290: assertEquals(expected, xml);
291: }
292:
293: public void testDoesNotSerializeOmittedFields() {
294: TypesOfFields fields = new TypesOfFields();
295: String expected = "" + "<types>\n"
296: + " <normal>normal</normal>\n" + "</types>";
297:
298: XStream xstream = new XStream();
299: xstream.registerConverter(new JavaBeanConverter(xstream
300: .getMapper()), XStream.PRIORITY_VERY_LOW);
301: xstream.alias("types", TypesOfFields.class);
302: xstream.omitField(TypesOfFields.class, "trans");
303: xstream.omitField(TypesOfFields.class, "foo");
304:
305: String xml = xstream.toXML(fields);
306: assertEquals(expected, xml);
307: }
308:
309: public void testDoesNotDeserializeOmittedFields() {
310: TypesOfFields fields = new TypesOfFields();
311: String xml = "" + "<types>\n" + " <normal>normal</normal>\n"
312: + " <foo>bar</foo>\n" + "</types>";
313:
314: XStream xstream = new XStream();
315: xstream.registerConverter(new JavaBeanConverter(xstream
316: .getMapper()), XStream.PRIORITY_VERY_LOW);
317: xstream.alias("types", TypesOfFields.class);
318: xstream.omitField(TypesOfFields.class, "foo");
319:
320: TypesOfFields unmarshalledFields = (TypesOfFields) xstream
321: .fromXML(xml);
322: assertEquals(fields, unmarshalledFields);
323: }
324:
325: static class Person {
326: private String fName;
327: private String lName;
328:
329: public Person() {
330: // Bean constructor
331: }
332:
333: public Person(String firstName, String lastName) {
334: this .fName = firstName;
335: this .lName = lastName;
336: }
337:
338: public String getFirstName() {
339: return fName;
340: }
341:
342: public void setFirstName(String name) {
343: fName = name;
344: }
345:
346: public String getLastName() {
347: return lName;
348: }
349:
350: public void setLastName(String name) {
351: lName = name;
352: }
353: }
354:
355: static class Man extends Person {
356:
357: public Man() {
358: // Bean constructor
359: super ();
360: }
361:
362: public Man(String firstName, String lastName) {
363: super (firstName, lastName);
364: }
365:
366: }
367:
368: public void testDoesNotSerializeOmittedInheritedFields() {
369: XStream xstream = new XStream();
370: xstream.registerConverter(new JavaBeanConverter(xstream
371: .getMapper()), XStream.PRIORITY_VERY_LOW);
372: xstream.omitField(Person.class, "lastName");
373: xstream.alias("man", Man.class);
374:
375: Man man = new Man("John", "Doe");
376: String expected = "" + "<man>\n"
377: + " <firstName>John</firstName>\n" + "</man>";
378:
379: assertEquals(expected, xstream.toXML(man));
380: }
381:
382: public void testUseAliasInheritedFields() {
383: XStream xstream = new XStream();
384: xstream.registerConverter(
385: new JavaBeanConverter(xstream.getMapper(),
386: new BeanProvider(new StringComparator())),
387: XStream.PRIORITY_VERY_LOW);
388: xstream.aliasField("first-name", Person.class, "firstName");
389: xstream.aliasField("last-name", Person.class, "lastName");
390: xstream.alias("man", Man.class);
391:
392: Man man = new Man("John", "Doe");
393: String expected = "" + "<man>\n"
394: + " <first-name>John</first-name>\n"
395: + " <last-name>Doe</last-name>\n" + "</man>";
396:
397: assertEquals(expected, xstream.toXML(man));
398: }
399: }
|