Fields Summary |
---|
private static final String | GERMAN_UMLAUTS |
private static final String | XML_SPECIAL_CHARS |
private static final String | ENCODED_XML_SPECIAL_CHARS |
private static final String | SUPPORT_CHARS_LESS_HEX_20 |
private static final String | ENCODED_SUPPORT_CHARS_LESS_HEX_20 |
private static final String | INVALID_XML_STRING |
private static final String | FRENCH_ACCENTS |
Methods Summary |
---|
private void | assertInvalidStringsDetected(java.lang.String invalidXmlString)try a string through the two encoders we have, verify it is invalid
assertInvalidStringsDetected(XMLEncoderFactory.ENCODING_UTF_16,invalidXmlString);
assertInvalidStringsDetected(XMLEncoderFactory.ENCODING_UTF_8, invalidXmlString);
|
private void | assertInvalidStringsDetected(java.lang.String encoderChoice, java.lang.String invalidXmlString)try a string through the two encoders we have, verify it is invalid
try {
XMLEncoder encoder = XMLEncoderFactory.getEncoder(encoderChoice);
encoder.encode(invalidXmlString);
fail("A UnsupportedEncodingException should have been thrown.");
} catch (IllegalArgumentException expected) {
// expected
}
|
private java.lang.String | hexDump(java.lang.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(java.lang.String[] args)
junit.textui.TestRunner.run(EncodingTest.class);
|
public void | test2UTF16()
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);
|
public void | test2UTF8()
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 | testEncodingFailure()
// 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);
}
}
|
public void | testUTF16()
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 | testUTF8()
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);
|
private void | verifyCommonAssertions(org.apache.axis.components.encoding.XMLEncoder encoder)assertions here hold for either 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");
|
private void | verifyUntouched(org.apache.axis.components.encoding.XMLEncoder encoder, java.lang.String source)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.
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);
}
|