01: /*
02: * Copyright (C) 2004 Joe Walnes.
03: * Copyright (C) 2006, 2007 XStream Committers.
04: * All rights reserved.
05: *
06: * The software in this package is published under the terms of the BSD
07: * style license a copy of which has been included with this distribution in
08: * the LICENSE.txt file.
09: *
10: * Created on 03. April 2004 by Joe Walnes
11: */
12: package com.thoughtworks.xstream.core;
13:
14: import com.thoughtworks.acceptance.AbstractAcceptanceTest;
15: import com.thoughtworks.acceptance.objects.StandardObject;
16: import com.thoughtworks.xstream.XStream;
17:
18: import java.util.ArrayList;
19: import java.util.List;
20:
21: public class ReferenceByXPathMarshallingStrategyTest extends
22: AbstractAcceptanceTest {
23:
24: protected void setUp() throws Exception {
25: super .setUp();
26: xstream.alias("thing", Thing.class);
27: }
28:
29: public static class Thing extends StandardObject {
30: private String name;
31:
32: public Thing() {
33: }
34:
35: public Thing(String name) {
36: this .name = name;
37: }
38: }
39:
40: public void testStoresReferencesUsingRelativeXPath() {
41: xstream.setMode(XStream.XPATH_RELATIVE_REFERENCES);
42:
43: Thing a = new Thing("a");
44: Thing b = new Thing("b");
45: Thing c = b;
46:
47: List list = new ArrayList();
48: list.add(a);
49: list.add(b);
50: list.add(c);
51:
52: String expected = "" + "<list>\n" + " <thing>\n"
53: + " <name>a</name>\n" + " </thing>\n"
54: + " <thing>\n" + " <name>b</name>\n"
55: + " </thing>\n"
56: + " <thing reference=\"../thing[2]\"/>\n" + // xpath
57: "</list>";
58:
59: assertBothWays(list, expected);
60: }
61:
62: public void testStoresReferencesUsingAbsoluteXPath() {
63: xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
64:
65: Thing a = new Thing("a");
66: Thing b = new Thing("b");
67: Thing c = b;
68:
69: List list = new ArrayList();
70: list.add(a);
71: list.add(b);
72: list.add(c);
73:
74: String expected = "" + "<list>\n" + " <thing>\n"
75: + " <name>a</name>\n" + " </thing>\n"
76: + " <thing>\n" + " <name>b</name>\n"
77: + " </thing>\n"
78: + " <thing reference=\"/list/thing[2]\"/>\n" + // xpath
79: "</list>";
80:
81: assertBothWays(list, expected);
82: }
83: }
|