001: /*
002: * Copyright (C) 2007 XStream Committers.
003: * All rights reserved.
004: *
005: * The software in this package is published under the terms of the BSD
006: * style license a copy of which has been included with this distribution in
007: * the LICENSE.txt file.
008: *
009: * Created on 14. May 2007 by Guilherme Silveira
010: */
011: package com.thoughtworks.acceptance;
012:
013: import java.util.ArrayList;
014: import java.util.Collection;
015: import java.util.HashSet;
016: import java.util.List;
017:
018: import com.thoughtworks.xstream.builder.XStreamBuilder;
019: import com.thoughtworks.xstream.builder.processor.TypeConfigProcessor;
020: import com.thoughtworks.xstream.converters.Converter;
021: import com.thoughtworks.xstream.converters.MarshallingContext;
022: import com.thoughtworks.xstream.converters.UnmarshallingContext;
023: import com.thoughtworks.xstream.io.HierarchicalStreamReader;
024: import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
025:
026: /**
027: * @author Guilherme Silveira
028: */
029: public class XStreamBuilderTest extends AbstractBuilderAcceptanceTest {
030:
031: public void testSupportsBuildStyleWithAlias() {
032: XStreamBuilder builder = new XStreamBuilder() {
033: {
034: handle(Office.class).with(alias("office"));
035: }
036: };
037:
038: Office office = new Office("Rua Vergueiro");
039: String expected = "<office>\n <address>Rua Vergueiro</address>\n</office>";
040: assertBothWays(builder.buildXStream(), office, expected);
041: }
042:
043: public static class Office {
044: private String address;
045:
046: public Office(String address) {
047: this .address = address;
048: }
049: }
050:
051: public void testHandleCorrectlyFieldAliases() {
052:
053: XStreamBuilder builder = new XStreamBuilder() {
054: {
055: handle(Office.class)
056: .with(
057: new TypeConfigProcessor[] {
058: alias("office"),
059: field("address").with(
060: as("logradouro")) });
061: }
062: };
063:
064: Office office = new Office("Rua Vergueiro");
065: String expected = "<office>\n <logradouro>Rua Vergueiro</logradouro>\n</office>";
066: assertBothWays(builder.buildXStream(), office, expected);
067:
068: }
069:
070: public void testHandleCorrectlyFieldOmmission() {
071:
072: XStreamBuilder builder = new XStreamBuilder() {
073: {
074: handle(Office.class).with(
075: new TypeConfigProcessor[] { alias("office"),
076: ignores("address")
077: // TODO "decision: could be" field("address").with(ignored())
078: });
079: }
080: };
081:
082: Office office = new Office("Rua Vergueiro");
083: String expected = "<office/>";
084: assertBothWays(builder.buildXStream(), office, expected);
085:
086: }
087:
088: public static class CollectionContainer {
089: Collection collection;
090: }
091:
092: public void testHandleCorrectlyDefaultImplementations() {
093:
094: XStreamBuilder builder = new XStreamBuilder() {
095: {
096: handle(Collection.class).with(
097: implementedBy(HashSet.class));
098: handle(CollectionContainer.class).with(alias("cc"));
099: }
100: };
101:
102: CollectionContainer root = new CollectionContainer();
103: root.collection = new HashSet();
104: String expected = "<cc>\n <collection/>\n</cc>";
105:
106: assertBothWays(builder.buildXStream(), root, expected);
107:
108: }
109:
110: public static class DoNothingConverter implements Converter {
111: private final Class support;
112:
113: public DoNothingConverter(Class aClass) {
114: this .support = aClass;
115: }
116:
117: public void marshal(Object source,
118: HierarchicalStreamWriter writer,
119: MarshallingContext context) {
120: writer.startNode("wow");
121: writer.endNode();
122: }
123:
124: public Object unmarshal(HierarchicalStreamReader reader,
125: UnmarshallingContext context) {
126: try {
127: return support.newInstance();
128: } catch (InstantiationException e) {
129: throw new RuntimeException(e);
130: } catch (IllegalAccessException e) {
131: throw new RuntimeException(e);
132: }
133: }
134:
135: public boolean canConvert(Class type) {
136: return support.equals(type);
137: }
138: }
139:
140: public void testHandleCorrectlyConverterRegistrations() {
141:
142: XStreamBuilder builder = new XStreamBuilder() {
143: {
144: handle(CollectionContainer.class).with(alias("cc"));
145: register(converter(new DoNothingConverter(
146: CollectionContainer.class)));
147: }
148: };
149:
150: CollectionContainer root = new CollectionContainer();
151: root.collection = new HashSet();
152: String expected = "<cc>\n <wow/>\n</cc>";
153:
154: assertBothWays(builder.buildXStream(), root, expected);
155:
156: }
157:
158: static class Home {
159: }
160:
161: public void testHandleCorrectlyAbsoluteReferences() {
162:
163: XStreamBuilder builder = new XStreamBuilder() {
164: {
165: with(absoluteReferences());
166: handle(Home.class).with(alias("home"));
167: }
168: };
169:
170: List root = new ArrayList();
171: root.add(new Home());
172: root.add(root.get(0));
173: String expected = "<list>\n <home/>\n <home reference=\"/list/home\"/>\n</list>";
174:
175: assertBothWays(builder.buildXStream(), root, expected);
176:
177: }
178:
179: public void testHandleCorrectlyIdReferences() {
180:
181: XStreamBuilder builder = new XStreamBuilder() {
182: {
183: with(idReferences());
184: handle(Home.class).with(alias("home"));
185: }
186: };
187:
188: List root = new ArrayList();
189: root.add(new Home());
190: root.add(root.get(0));
191: String expected = "<list id=\"1\">\n <home id=\"2\"/>\n <home reference=\"2\"/>\n</list>";
192:
193: assertBothWays(builder.buildXStream(), root, expected);
194:
195: }
196:
197: public void testHandleCorrectlyNoReferences() {
198:
199: XStreamBuilder builder = new XStreamBuilder() {
200: {
201: with(noReferences());
202: handle(Home.class).with(alias("home"));
203: }
204: };
205:
206: List root = new ArrayList();
207: root.add(new Home());
208: root.add(root.get(0));
209: String expected = "<list>\n <home/>\n <home/>\n</list>";
210:
211: assertBothWays(builder.buildXStream(), root, expected);
212:
213: }
214:
215: }
|