| 1 |
package test.encoding; |
| 2 |
|
| 3 |
import junit.framework.TestCase; |
| 4 |
import org.apache.axis.components.encoding.XMLEncoder; |
| 5 |
import org.apache.axis.components.encoding.XMLEncoderFactory; |
| 6 |
|
| 7 |
import java.io.UnsupportedEncodingException; |
| 8 |
|
| 9 |
/** |
| 10 |
* Tests for the new XMLEncoder components. |
| 11 |
* Some of the tests are convoluted; that is to make diagnosis of faults easy, even |
| 12 |
* with JUnit's XML reporting intervening in the process. |
| 13 |
*/ |
| 14 |
public class EncodingTest extends TestCase { |
| 15 |
private static final String GERMAN_UMLAUTS = " Some text \u00df with \u00fc special \u00f6 chars \u00e4."; |
| 16 |
private static final String XML_SPECIAL_CHARS = "< > \" &"; |
| 17 |
private static final String ENCODED_XML_SPECIAL_CHARS = "< > " &"; |
| 18 |
private static final String SUPPORT_CHARS_LESS_HEX_20 = "\t\r\n"; |
| 19 |
private static final String ENCODED_SUPPORT_CHARS_LESS_HEX_20 = "	
"; |
| 20 |
private static final String INVALID_XML_STRING = "Invalid XML String \u0000"; |
| 21 |
private static final String FRENCH_ACCENTS="\u00e0\u00e2\u00e4\u00e7\u00e8\u00e9\u00ea\u00eb\u00ee\u00ef\u00f4\u00f6\u00f9\u00fb\u00fc"; |
| 22 |
|
| 23 |
public EncodingTest(String s) { |
| 24 |
super(s); |
| 25 |
} |
| 26 |
|
| 27 |
public void testEncodingFailure() throws Exception { |
| 28 |
// now it should return a default encoder |
| 29 |
assertTrue( XMLEncoderFactory.getEncoder("XYZ") != null ); |
| 30 |
|
| 31 |
assertInvalidStringsDetected(INVALID_XML_STRING); |
| 32 |
//run through the first 32 chars |
| 33 |
for(int i=0;i<31;i++) { |
| 34 |
char c=(char)i; |
| 35 |
//ignore legit whitespace |
| 36 |
if ("\t\n\r".indexOf(c) == -1) { |
| 37 |
//verify the others are caught |
| 38 |
String s=(new Character(c)).toString(); |
| 39 |
assertInvalidStringsDetected(s); |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
/** |
| 45 |
* try a string through the two encoders we have, verify it is invalid |
| 46 |
* @param invalidXmlString string we expect to fail |
| 47 |
* @throws Exception |
| 48 |
*/ |
| 49 |
private void assertInvalidStringsDetected(String invalidXmlString) throws Exception { |
| 50 |
assertInvalidStringsDetected(XMLEncoderFactory.ENCODING_UTF_16,invalidXmlString); |
| 51 |
assertInvalidStringsDetected(XMLEncoderFactory.ENCODING_UTF_8, invalidXmlString); |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
/** |
| 57 |
* try a string through the two encoders we have, verify it is invalid |
| 58 |
* @param encoderChoice name of the encoder to use |
| 59 |
* @param invalidXmlString string we expect to fail |
| 60 |
*/ |
| 61 |
private void assertInvalidStringsDetected(String encoderChoice, String invalidXmlString) throws Exception { |
| 62 |
try { |
| 63 |
XMLEncoder encoder = XMLEncoderFactory.getEncoder(encoderChoice); |
| 64 |
encoder.encode(invalidXmlString); |
| 65 |
fail("A UnsupportedEncodingException should have been thrown."); |
| 66 |
} catch (IllegalArgumentException expected) { |
| 67 |
// expected |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
public void testUTF8() throws Exception { |
| 72 |
XMLEncoder encoder = XMLEncoderFactory.getEncoder(XMLEncoderFactory.ENCODING_UTF_8); |
| 73 |
String encodedUmlauts = encoder.encode(GERMAN_UMLAUTS); |
| 74 |
|
| 75 |
assertEquals(XMLEncoderFactory.ENCODING_UTF_8, encoder.getEncoding()); |
| 76 |
assertEquals(GERMAN_UMLAUTS, encodedUmlauts); |
| 77 |
verifyCommonAssertions(encoder); |
| 78 |
} |
| 79 |
|
| 80 |
public void testUTF16() throws Exception { |
| 81 |
XMLEncoder encoder = XMLEncoderFactory.getEncoder(XMLEncoderFactory.ENCODING_UTF_16); |
| 82 |
String encodedUmlauts = encoder.encode(GERMAN_UMLAUTS); |
| 83 |
|
| 84 |
assertEquals(XMLEncoderFactory.ENCODING_UTF_16, encoder.getEncoding()); |
| 85 |
assertEquals(GERMAN_UMLAUTS, encodedUmlauts); |
| 86 |
//verifyCommonAssertions(encoder); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
public void test2UTF8() throws Exception { |
| 91 |
XMLEncoder encoder = XMLEncoderFactory.getEncoder(XMLEncoderFactory.ENCODING_UTF_8); |
| 92 |
String encodedAccents = encoder.encode(FRENCH_ACCENTS); |
| 93 |
|
| 94 |
assertEquals(XMLEncoderFactory.ENCODING_UTF_8, encoder.getEncoding()); |
| 95 |
assertEquals(FRENCH_ACCENTS, encodedAccents); |
| 96 |
verifyCommonAssertions(encoder); |
| 97 |
} |
| 98 |
|
| 99 |
public void test2UTF16() throws Exception { |
| 100 |
XMLEncoder encoder = XMLEncoderFactory.getEncoder(XMLEncoderFactory.ENCODING_UTF_16); |
| 101 |
String encodedAccents = encoder.encode(FRENCH_ACCENTS); |
| 102 |
|
| 103 |
assertEquals(XMLEncoderFactory.ENCODING_UTF_16, encoder.getEncoding()); |
| 104 |
assertEquals(FRENCH_ACCENTS, encodedAccents); |
| 105 |
//verifyCommonAssertions(encoder); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* assertions here hold for either encoder |
| 110 |
* @param encoder |
| 111 |
*/ |
| 112 |
private void verifyCommonAssertions(XMLEncoder encoder) { |
| 113 |
String encodedXMLChars = encoder.encode(XML_SPECIAL_CHARS); |
| 114 |
assertEquals(ENCODED_XML_SPECIAL_CHARS, encodedXMLChars); |
| 115 |
//assert that the whitespace chars are not touched |
| 116 |
verifyUntouched(encoder, "\t"); |
| 117 |
verifyUntouched(encoder, "\n"); |
| 118 |
verifyUntouched(encoder, "\r"); |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* verify that the support chars are not touched. This is done on a char by |
| 123 |
* char basis for easier debugging. One debug problem there is that |
| 124 |
* ant's XML logger also encodes the strings, making diagnosing |
| 125 |
* the defect from an error report trickier than normal. |
| 126 |
* @param encoder |
| 127 |
*/ |
| 128 |
private void verifyUntouched(XMLEncoder encoder, String source) { |
| 129 |
for(int i=0;i<source.length();i++) { |
| 130 |
char c = source.charAt(i); |
| 131 |
Character ch = new Character(c); |
| 132 |
String xmlString = ch.toString(); |
| 133 |
String encoded= encoder.encode(xmlString); |
| 134 |
assertEquals("Char " +(int) c + " was encoded as " + hexDump(encoded), |
| 135 |
xmlString,encoded); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
private String hexDump(String source) { |
| 140 |
StringBuffer out=new StringBuffer(source.length()*5); |
| 141 |
for (int i = 0; i < source.length(); i++) { |
| 142 |
char c = source.charAt(i); |
| 143 |
out.append("0x"); |
| 144 |
out.append(Integer.toHexString(c)); |
| 145 |
out.append(" "); |
| 146 |
} |
| 147 |
return new String(out); |
| 148 |
|
| 149 |
} |
| 150 |
public static void main(String[] args) { |
| 151 |
junit.textui.TestRunner.run(EncodingTest.class); |
| 152 |
} |
| 153 |
} |