001: /*
002: * Copyright (C) 2006, 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 01. December 2006 by Joerg Schaible
010: */
011: package com.thoughtworks.acceptance.annotations;
012:
013: import com.thoughtworks.acceptance.AbstractAcceptanceTest;
014: import com.thoughtworks.xstream.InitializationException;
015: import com.thoughtworks.xstream.XStream;
016: import com.thoughtworks.xstream.annotations.XStreamAlias;
017: import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
018: import com.thoughtworks.xstream.annotations.XStreamImplicit;
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.List;
022: import java.util.Map;
023:
024: /**
025: * Test for annotations mapping implicit collections.
026: *
027: * @author Lucio Benfante
028: * @author Jörg Schaible
029: */
030: public class ImplicitCollectionTest extends AbstractAcceptanceTest {
031:
032: @Override
033: protected XStream createXStream() {
034: XStream xstream = super .createXStream();
035: xstream.autodetectAnnotations(true);
036: return xstream;
037: }
038:
039: public void testAnnotation() {
040: String expected = "" + "<root>\n" + " <string>one</string>\n"
041: + " <string>two</string>\n" + "</root>";
042: ImplicitRootOne implicitRoot = new ImplicitRootOne();
043: implicitRoot.getValues().add("one");
044: implicitRoot.getValues().add("two");
045: assertBothWays(implicitRoot, expected);
046: }
047:
048: public void testAnnotationWithItemFieldName() {
049: String expected = "" + "<root>\n" + " <value>one</value>\n"
050: + " <value>two</value>\n" + "</root>";
051: ImplicitRootTwo implicitRoot = new ImplicitRootTwo();
052: implicitRoot.getValues().add("one");
053: implicitRoot.getValues().add("two");
054: assertBothWays(implicitRoot, expected);
055: }
056:
057: public void testAnnotationFailsForInvalidFieldType() {
058: try {
059: xstream.processAnnotations(InvalidImplicitRoot.class);
060: fail("Thrown " + InitializationException.class.getName()
061: + " expected");
062: } catch (final InitializationException e) {
063: assertTrue(e.getMessage().indexOf("\"value\"") > 0);
064: }
065: }
066:
067: @XStreamAlias("root")
068: public static class ImplicitRootOne {
069: @XStreamImplicit()
070: private List<String> values = new ArrayList<String>();
071:
072: public List<String> getValues() {
073: return values;
074: }
075:
076: public void setValues(List<String> values) {
077: this .values = values;
078: }
079: }
080:
081: @XStreamAlias("root")
082: public static class ImplicitRootTwo {
083: @XStreamImplicit(itemFieldName="value")
084: private List<String> values = new ArrayList<String>();
085:
086: public List<String> getValues() {
087: return values;
088: }
089:
090: public void setValues(List<String> values) {
091: this .values = values;
092: }
093: }
094:
095: @XStreamAlias("root")
096: public static class InvalidImplicitRoot {
097: @XStreamImplicit(itemFieldName="outch")
098: private String value;
099:
100: public String getValue() {
101: return value;
102: }
103:
104: public void setValue(String value) {
105: this .value = value;
106: }
107: }
108:
109: @XStreamAlias("implicit")
110: public static class ImplicitParameterizedType {
111: @XStreamImplicit(itemFieldName="line")
112: private ArrayList<ArrayList<Point>> signatureLines;
113: }
114:
115: @XStreamAlias("point")
116: public static class Point {
117: @XStreamAsAttribute
118: private int x;
119: @XStreamAsAttribute
120: private int y;
121:
122: public Point(int x, int y) {
123: this .x = x;
124: this .y = y;
125: }
126: }
127:
128: public void testAnnotationHandlesParameterizedTypes() {
129: String xml = "" + "<implicit>\n" + " <line>\n"
130: + " <point x=\"33\" y=\"11\"/>\n" + " </line>\n"
131: + "</implicit>";
132: ImplicitParameterizedType root = new ImplicitParameterizedType();
133: root.signatureLines = new ArrayList<ArrayList<Point>>();
134: root.signatureLines.add(new ArrayList<Point>());
135: root.signatureLines.get(0).add(new Point(33, 11));
136: assertBothWays(root, xml);
137: }
138:
139: @XStreamAlias("type")
140: public static class ParametrizedTypeIsInterface {
141: @XStreamImplicit()
142: private ArrayList<Map> list = new ArrayList<Map>();
143: }
144:
145: public void testWorksForTypesThatAreInterfaces() {
146: ParametrizedTypeIsInterface type = new ParametrizedTypeIsInterface();
147: type.list = new ArrayList<Map>();
148: type.list.add(new HashMap());
149: String xml = "" //
150: + "<type>\n" //
151: + " <map/>\n" //
152: + "</type>";
153: assertBothWays(type, xml);
154: }
155:
156: @XStreamAlias("untyped")
157: private static class Untyped {
158: @XStreamImplicit
159: private List list = new ArrayList();
160:
161: public Untyped() {
162: list.add("1");
163: }
164: }
165:
166: public void testCanHandleUntypedCollections() {
167: Untyped untyped = new Untyped();
168: String xml = "" //
169: + "<untyped>\n" //
170: + " <string>1</string>\n" //
171: + "</untyped>";
172: assertBothWays(untyped, xml);
173: }
174: }
|