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