001: /*
002: * Copyright (C) 2004, 2005, 2006 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 22. July 2004 by Joe Walnes
011: */
012: package com.thoughtworks.acceptance;
013:
014: import com.thoughtworks.acceptance.objects.StandardObject;
015: import com.thoughtworks.xstream.XStream;
016: import com.thoughtworks.xstream.io.xml.XStream11XmlFriendlyReplacer;
017: import com.thoughtworks.xstream.io.xml.XppDriver;
018:
019: public class XStream11XmlFriendlyTest extends AbstractAcceptanceTest {
020:
021: protected XStream createXStream() {
022: return new XStream(new XppDriver(
023: new XStream11XmlFriendlyReplacer())) {
024:
025: protected boolean useXStream11XmlFriendlyMapper() {
026: return true;
027: }
028:
029: };
030: }
031:
032: public static class WithDollarCharField extends StandardObject {
033: String $field;
034: String field$;
035: String fi$eld;
036: String fi$$eld;
037: }
038:
039: public void testSupportsFieldsWithDollarChar() {
040: xstream.alias("dollar", WithDollarCharField.class);
041:
042: WithDollarCharField in = new WithDollarCharField();
043: in.$field = "a";
044: in.field$ = "b";
045: in.fi$eld = "c";
046: in.fi$$eld = "d";
047:
048: String expected11 = ""
049: + "<dollar>\n"
050: + " <_DOLLAR_field>a</_DOLLAR_field>\n"
051: + " <field_DOLLAR_>b</field_DOLLAR_>\n"
052: + " <fi_DOLLAR_eld>c</fi_DOLLAR_eld>\n"
053: + " <fi_DOLLAR__DOLLAR_eld>d</fi_DOLLAR__DOLLAR_eld>\n"
054: + "</dollar>";
055:
056: String expected12 = "" + "<dollar>\n"
057: + " <_-field>a</_-field>\n"
058: + " <field_->b</field_->\n"
059: + " <fi_-eld>c</fi_-eld>\n"
060: + " <fi_-_-eld>d</fi_-_-eld>\n" + "</dollar>";
061:
062: assertWithAsymmetricalXml(in, expected11, expected12);
063: }
064:
065: public static class WithUnderscoreCharField extends StandardObject {
066: String _field;
067: String field_;
068: String fi_eld;
069: String fi__eld;
070: }
071:
072: public void testSupportsFieldsWithUnderscoreChar() {
073: xstream.alias("underscore", WithUnderscoreCharField.class);
074:
075: WithUnderscoreCharField in = new WithUnderscoreCharField();
076: in._field = "a";
077: in.field_ = "b";
078: in.fi_eld = "c";
079: in.fi__eld = "d";
080:
081: String expected11 = "" + "<underscore>\n"
082: + " <__field>a</__field>\n"
083: + " <field__>b</field__>\n"
084: + " <fi__eld>c</fi__eld>\n"
085: + " <fi____eld>d</fi____eld>\n" + "</underscore>";
086:
087: assertWithAsymmetricalXml(in, expected11, expected11);
088: }
089:
090: public void testSupportsAliasWithDashChar() {
091: xstream.alias("under-score", WithUnderscoreCharField.class);
092:
093: WithUnderscoreCharField in = new WithUnderscoreCharField();
094: in._field = "a";
095: in.field_ = "b";
096: in.fi_eld = "c";
097: in.fi__eld = "d";
098:
099: String expected11 = "" + "<under-score>\n"
100: + " <__field>a</__field>\n"
101: + " <field__>b</field__>\n"
102: + " <fi__eld>c</fi__eld>\n"
103: + " <fi____eld>d</fi____eld>\n" + "</under-score>";
104:
105: assertWithAsymmetricalXml(in, expected11, expected11);
106: }
107:
108: public static class A_B extends StandardObject {
109: private int x;
110:
111: public A_B(int x) {
112: this .x = x;
113: }
114:
115: }
116:
117: public void testSupportsUnderscoreInShortClassName() {
118: String expected11 = ""
119: + "<com.thoughtworks.acceptance.XStream11XmlFriendlyTest-A_B>\n"
120: + " <x>3</x>\n"
121: + "</com.thoughtworks.acceptance.XStream11XmlFriendlyTest-A_B>";
122:
123: String expected12 = ""
124: + "<com.thoughtworks.acceptance.XStream11XmlFriendlyTest_-A__B>\n"
125: + " <x>3</x>\n"
126: + "</com.thoughtworks.acceptance.XStream11XmlFriendlyTest_-A__B>";
127:
128: assertWithAsymmetricalXml(new A_B(3), expected11, expected12);
129: }
130:
131: }
|