001: /*
002: * Copyright (C) 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 18. April 2006 by Mauro Talevi
011: */
012: package com.thoughtworks.acceptance;
013:
014: import com.thoughtworks.acceptance.objects.StandardObject;
015: import com.thoughtworks.xstream.core.JVM;
016:
017: import java.text.DecimalFormatSymbols;
018: import java.util.Locale;
019:
020: public class XmlFriendlyTest extends AbstractAcceptanceTest {
021:
022: public static class WithDollarCharField extends StandardObject {
023: String $field;
024: String field$;
025: String fi$eld;
026: String fi$$eld;
027: }
028:
029: public void testSupportsFieldsWithDollarChar() {
030: xstream.alias("dollar", WithDollarCharField.class);
031:
032: WithDollarCharField in = new WithDollarCharField();
033: in.$field = "a";
034: in.field$ = "b";
035: in.fi$eld = "c";
036: in.fi$$eld = "d";
037:
038: String expected = "" + "<dollar>\n"
039: + " <_-field>a</_-field>\n"
040: + " <field_->b</field_->\n"
041: + " <fi_-eld>c</fi_-eld>\n"
042: + " <fi_-_-eld>d</fi_-_-eld>\n" + "</dollar>";
043: assertBothWays(in, expected);
044: }
045:
046: public static class WithUnderscoreCharField extends StandardObject {
047: String _field;
048: String field_;
049: String fi_eld;
050: }
051:
052: public void testSupportsFieldsWithUnderscoreChar() {
053: xstream.alias("underscore", WithUnderscoreCharField.class);
054:
055: WithUnderscoreCharField in = new WithUnderscoreCharField();
056: in._field = "a";
057: in.field_ = "b";
058: in.fi_eld = "c";
059:
060: String expected = "" + "<underscore>\n"
061: + " <__field>a</__field>\n"
062: + " <field__>b</field__>\n"
063: + " <fi__eld>c</fi__eld>\n" + "</underscore>";
064: assertBothWays(in, expected);
065: }
066:
067: public static class WithDoubleUnderscoreCharField extends
068: StandardObject {
069: String __field;
070: String field__;
071: String fi__eld;
072: }
073:
074: public void testSupportsFieldsWithDoubleUnderscoreChar() {
075: xstream
076: .alias("underscore",
077: WithDoubleUnderscoreCharField.class);
078:
079: WithDoubleUnderscoreCharField in = new WithDoubleUnderscoreCharField();
080: in.__field = "a";
081: in.field__ = "b";
082: in.fi__eld = "c";
083:
084: String expected = "" + "<underscore>\n"
085: + " <____field>a</____field>\n"
086: + " <field____>b</field____>\n"
087: + " <fi____eld>c</fi____eld>\n" + "</underscore>";
088: assertBothWays(in, expected);
089: }
090:
091: public static class WithDollarAndUnderscoreCharField extends
092: StandardObject {
093: String $_$field;
094: String field$_$;
095: String fi_$_eld;
096: String fi_$$_eld;
097: String fi$__$eld;
098: }
099:
100: public void testSupportsFieldsWithDollarAndUnderScoreChar() {
101: xstream.alias("dollar", WithDollarAndUnderscoreCharField.class);
102:
103: WithDollarAndUnderscoreCharField in = new WithDollarAndUnderscoreCharField();
104: in.$_$field = "a";
105: in.field$_$ = "b";
106: in.fi_$_eld = "c";
107: in.fi_$$_eld = "d";
108: in.fi$__$eld = "e";
109:
110: String expected = "" + "<dollar>\n"
111: + " <_-___-field>a</_-___-field>\n"
112: + " <field_-___->b</field_-___->\n"
113: + " <fi___-__eld>c</fi___-__eld>\n"
114: + " <fi___-_-__eld>d</fi___-_-__eld>\n"
115: + " <fi_-_____-eld>e</fi_-_____-eld>\n" + "</dollar>";
116: assertBothWays(in, expected);
117: }
118:
119: public static class __ {
120: public static class A_B extends StandardObject {
121: private int x;
122:
123: public A_B(int x) {
124: this .x = x;
125: }
126:
127: }
128: }
129:
130: public void testSupportsUnderscoreInShortClassName() {
131: assertBothWays(
132: new __.A_B(3),
133: ""
134: + "<com.thoughtworks.acceptance.XmlFriendlyTest_-_____-A__B>\n"
135: + " <x>3</x>\n"
136: + "</com.thoughtworks.acceptance.XmlFriendlyTest_-_____-A__B>");
137: }
138:
139: public void testSlashRSlashSlashSlashN() {
140: String before = "\r\\\n";
141: String xml = xstream.toXML(before);
142: assertEquals(before, xstream.fromXML(xml));
143: }
144:
145: public void testCanDealWithUtfText() {
146: assertBothWays("J\u00F6rg", "<string>J\u00F6rg</string>");
147: }
148:
149: public void testCanDealWithNullCharactersInText() {
150: assertBothWays("X\0Y", "<string>X�Y</string>");
151: }
152:
153: public void testEscapesXmlUnfriendlyChars() {
154: assertBothWays("<", "<string><</string>");
155: assertBothWays(">", "<string>></string>");
156: assertBothWays("<>", "<string><></string>");
157: assertBothWays("<=", "<string><=</string>");
158: assertBothWays(">=", "<string>>=</string>");
159: assertBothWays("&", "<string>&</string>");
160: assertBothWays("'", "<string>'</string>");
161: assertBothWays("\"", "<string>"</string>");
162: }
163:
164: public void testDecimalFormatSymbols() {
165: final String xml;
166: if (!JVM.is14()) {
167: xml = "<java.text.DecimalFormatSymbols serialization=\"custom\">\n"
168: + " <java.text.DecimalFormatSymbols>\n"
169: + " <default>\n"
170: + " <decimalSeparator>,</decimalSeparator>\n"
171: + " <digit>#</digit>\n"
172: + " <exponential>E</exponential>\n"
173: + " <groupingSeparator>.</groupingSeparator>\n"
174: + " <minusSign>-</minusSign>\n"
175: + " <monetarySeparator>,</monetarySeparator>\n"
176: + " <patternSeparator>;</patternSeparator>\n"
177: + " <perMill>\u2030</perMill>\n"
178: + " <percent>%</percent>\n"
179: + " <serialVersionOnStream>1</serialVersionOnStream>\n"
180: + " <zeroDigit>0</zeroDigit>\n"
181: + " <NaN>\ufffd</NaN>\n"
182: + " <currencySymbol>DM</currencySymbol>\n"
183: + " <infinity>\u221e</infinity>\n"
184: + " <intlCurrencySymbol>DEM</intlCurrencySymbol>\n"
185: + " </default>\n"
186: + " </java.text.DecimalFormatSymbols>\n"
187: + "</java.text.DecimalFormatSymbols>";
188: } else if (!JVM.is16()) {
189: xml = "<java.text.DecimalFormatSymbols serialization=\"custom\">\n"
190: + " <java.text.DecimalFormatSymbols>\n"
191: + " <default>\n"
192: + " <decimalSeparator>,</decimalSeparator>\n"
193: + " <digit>#</digit>\n"
194: + " <exponential>E</exponential>\n"
195: + " <groupingSeparator>.</groupingSeparator>\n"
196: + " <minusSign>-</minusSign>\n"
197: + " <monetarySeparator>,</monetarySeparator>\n"
198: + " <patternSeparator>;</patternSeparator>\n"
199: + " <perMill>\u2030</perMill>\n"
200: + " <percent>%</percent>\n"
201: + " <serialVersionOnStream>2</serialVersionOnStream>\n"
202: + " <zeroDigit>0</zeroDigit>\n"
203: + " <NaN>\ufffd</NaN>\n"
204: + " <currencySymbol>\u20ac</currencySymbol>\n"
205: + " <infinity>\u221e</infinity>\n"
206: + " <intlCurrencySymbol>EUR</intlCurrencySymbol>\n"
207: + " <locale>de_DE</locale>\n"
208: + " </default>\n"
209: + " </java.text.DecimalFormatSymbols>\n"
210: + "</java.text.DecimalFormatSymbols>";
211: } else {
212: xml = "<java.text.DecimalFormatSymbols serialization=\"custom\">\n"
213: + " <java.text.DecimalFormatSymbols>\n"
214: + " <default>\n"
215: + " <decimalSeparator>,</decimalSeparator>\n"
216: + " <digit>#</digit>\n"
217: + " <exponential>E</exponential>\n"
218: + " <groupingSeparator>.</groupingSeparator>\n"
219: + " <minusSign>-</minusSign>\n"
220: + " <monetarySeparator>,</monetarySeparator>\n"
221: + " <patternSeparator>;</patternSeparator>\n"
222: + " <perMill>\u2030</perMill>\n"
223: + " <percent>%</percent>\n"
224: + " <serialVersionOnStream>3</serialVersionOnStream>\n"
225: + " <zeroDigit>0</zeroDigit>\n"
226: + " <NaN>\ufffd</NaN>\n"
227: + " <currencySymbol>\u20ac</currencySymbol>\n"
228: + " <exponentialSeparator>E</exponentialSeparator>\n"
229: + " <infinity>\u221e</infinity>\n"
230: + " <intlCurrencySymbol>EUR</intlCurrencySymbol>\n"
231: + " <locale>de_DE</locale>\n"
232: + " </default>\n"
233: + " </java.text.DecimalFormatSymbols>\n"
234: + "</java.text.DecimalFormatSymbols>";
235: }
236: final DecimalFormatSymbols format = new DecimalFormatSymbols(
237: Locale.GERMANY);
238: assertBothWays(format, xml);
239: }
240:
241: }
|