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 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 assertTrue( XMLEncoderFactory.getEncoder("XYZ") != null ); 30 31 assertInvalidStringsDetected(INVALID_XML_STRING); 32 for(int i=0;i<31;i++) { 34 char c=(char)i; 35 if ("\t\n\r".indexOf(c) == -1) { 37 String s=(new Character (c)).toString(); 39 assertInvalidStringsDetected(s); 40 } 41 } 42 } 43 44 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 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 } 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 } 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 } 107 108 112 private void verifyCommonAssertions(XMLEncoder encoder) { 113 String encodedXMLChars = encoder.encode(XML_SPECIAL_CHARS); 114 assertEquals(ENCODED_XML_SPECIAL_CHARS, encodedXMLChars); 115 verifyUntouched(encoder, "\t"); 117 verifyUntouched(encoder, "\n"); 118 verifyUntouched(encoder, "\r"); 119 } 120 121 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 } 154 | Popular Tags |