FileDocCategorySizeDatePackage
EncodingTest.javaAPI DocApache Axis 1.46084Sat Apr 22 18:57:28 BST 2006test.encoding

EncodingTest

public class EncodingTest extends TestCase
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.

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
Constructors Summary
public EncodingTest(String s)


       
        super(s);
    
Methods Summary
private voidassertInvalidStringsDetected(java.lang.String invalidXmlString)
try a string through the two encoders we have, verify it is invalid

param
invalidXmlString string we expect to fail
throws
Exception

        assertInvalidStringsDetected(XMLEncoderFactory.ENCODING_UTF_16,invalidXmlString);
        assertInvalidStringsDetected(XMLEncoderFactory.ENCODING_UTF_8, invalidXmlString);
    
private voidassertInvalidStringsDetected(java.lang.String encoderChoice, java.lang.String 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

        try {
            XMLEncoder encoder = XMLEncoderFactory.getEncoder(encoderChoice);
            encoder.encode(invalidXmlString);
            fail("A UnsupportedEncodingException should have been thrown.");
        } catch (IllegalArgumentException expected) {
            // expected
        }
    
private java.lang.StringhexDump(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 voidmain(java.lang.String[] args)

        junit.textui.TestRunner.run(EncodingTest.class);
    
public voidtest2UTF16()

        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 voidtest2UTF8()

        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 voidtestEncodingFailure()

        // 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 voidtestUTF16()

        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 voidtestUTF8()

        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 voidverifyCommonAssertions(org.apache.axis.components.encoding.XMLEncoder encoder)
assertions here hold for either encoder

param
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 voidverifyUntouched(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.

param
encoder

        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);
        }