01: /*
02: * Copyright (C) 2007 XStream Committers.
03: * All rights reserved.
04: *
05: * The software in this package is published under the terms of the BSD
06: * style license a copy of which has been included with this distribution in
07: * the LICENSE.txt file.
08: *
09: * Created on 10. April 2007 by Guilherme Silveira
10: */
11: package com.thoughtworks.acceptance;
12:
13: import com.thoughtworks.xstream.XStream;
14: import com.thoughtworks.xstream.converters.reflection.FieldDictionary;
15: import com.thoughtworks.xstream.converters.reflection.PureJavaReflectionProvider;
16: import com.thoughtworks.xstream.converters.reflection.SortableFieldKeySorter;
17:
18: public class SortableFieldListTest extends AbstractAcceptanceTest {
19:
20: public void testSortsFieldOrderWithArray() {
21:
22: SortableFieldKeySorter sorter = new SortableFieldKeySorter();
23: sorter.registerFieldOrder(MommyBear.class, new String[] { "b",
24: "c", "a" });
25:
26: xstream = new XStream(new PureJavaReflectionProvider(
27: new FieldDictionary(sorter)));
28:
29: xstream.alias("mommy", MommyBear.class);
30: MommyBear root = new MommyBear();
31: root.c = "ccc";
32: root.b = "bbb";
33: root.a = "aaa";
34: assertBothWays(root, "<mommy>\n" + " <b>bbb</b>\n"
35: + " <c>ccc</c>\n" + " <a>aaa</a>\n" + "</mommy>");
36: }
37:
38: public void testSortsFieldOrderWhileUsingInheritance() {
39:
40: SortableFieldKeySorter sorter = new SortableFieldKeySorter();
41: sorter.registerFieldOrder(BabyBear.class, new String[] { "b",
42: "d", "c", "a" });
43:
44: xstream = new XStream(new PureJavaReflectionProvider(
45: new FieldDictionary(sorter)));
46:
47: xstream.alias("baby", BabyBear.class);
48: BabyBear root = new BabyBear();
49: root.c = "ccc";
50: root.b = "bbb";
51: root.a = "aaa";
52: root.d = "ddd";
53: assertBothWays(root, "<baby>\n" + " <b>bbb</b>\n"
54: + " <d>ddd</d>\n" + " <c>ccc</c>\n"
55: + " <a>aaa</a>\n" + "</baby>");
56: }
57:
58: public static class MommyBear {
59: protected String c, a, b;
60: }
61:
62: public static class BabyBear extends MommyBear {
63: private String d;
64: }
65:
66: }
|