01: /*
02: * Copyright (C) 2008 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 04. January 2008 by Joerg Schaible
10: */
11: package com.thoughtworks.acceptance.annotations;
12:
13: import java.util.HashMap;
14:
15: import com.thoughtworks.acceptance.AbstractAcceptanceTest;
16: import com.thoughtworks.xstream.XStream;
17: import com.thoughtworks.xstream.annotations.XStreamConverter;
18: import com.thoughtworks.xstream.annotations.XStreamConverters;
19: import com.thoughtworks.xstream.converters.collections.MapConverter;
20: import com.thoughtworks.xstream.mapper.Mapper;
21:
22: /**
23: * Tests for using annotations for classes.
24: *
25: * @author Chung-Onn, Cheong
26: * @author Jörg Schaible
27: */
28: public class ClassConverterTest extends AbstractAcceptanceTest {
29:
30: @Override
31: protected XStream createXStream() {
32: XStream xstream = super .createXStream();
33: xstream.autodetectAnnotations(true);
34: return xstream;
35: }
36:
37: protected void setUp() throws Exception {
38: super .setUp();
39: xstream.alias("my-map", MyMap.class);
40: xstream.processAnnotations(MyMap.class);
41: }
42:
43: public void testAnnotationForConvertersWithParameters() {
44: final MyMap value = new MyMap();
45: value.put("key1", "value1");
46: String expected = "" + "<my-map>\n" + " <entry>\n"
47: + " <string>key1</string>\n"
48: + " <string>value1</string>\n" + " </entry>\n"
49: + "</my-map>";
50: assertBothWays(value, expected);
51: }
52:
53: @XStreamConverters({@XStreamConverter(MyMapConverter.class)})
54: public static class MyMap extends HashMap<String, Object> {
55:
56: }
57:
58: public static class MyMapConverter extends MapConverter {
59:
60: public MyMapConverter(Mapper classMapper) {
61: super (classMapper);
62: }
63:
64: public boolean canConvert(Class type) {
65: return type.equals(MyMap.class);
66: }
67:
68: }
69: }
|