You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by st...@apache.org on 2003/10/05 09:10:02 UTC

cvs commit: ws-axis/java/test/encoding EncodingTest.java

stevel      2003/10/05 00:10:02

  Modified:    java/test/encoding EncodingTest.java
  Log:
  having got the package tests to run, they were failing all day with an error, that forced me to resort to hex dumps of strings.
  Turns out we were transposing \n and \r in the new (post Axis1.1) encoder.
  
  Revision  Changes    Path
  1.4       +54 -17    ws-axis/java/test/encoding/EncodingTest.java
  
  Index: EncodingTest.java
  ===================================================================
  RCS file: /home/cvs/ws-axis/java/test/encoding/EncodingTest.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- EncodingTest.java	15 Aug 2003 23:14:27 -0000	1.3
  +++ EncodingTest.java	5 Oct 2003 07:10:02 -0000	1.4
  @@ -7,15 +7,17 @@
   import java.io.UnsupportedEncodingException;
   
   /**
  - * Tests for the new XMLEncoder components
  + * 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 {
  -    public static final String GERMAN_UMLAUTS = " Some text \u00df with \u00fc special \u00f6 chars \u00e4.";
  -    public static final String XML_SPECIAL_CHARS = "< > \" &";
  -    public static final String ENCODED_XML_SPECIAL_CHARS = "&lt; &gt; &quot; &amp;";
  -    public static final String SUPPORT_CHARS_LESS_HEX_20 = "\t \r \n";
  -    public static final String ENCODED_SUPPORT_CHARS_LESS_HEX_20 = "&#x9; &#xd; &#xa;";
  -    public static final String INVALID_XML_STRING = "Invalid XML String \u0000";
  +    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 = "&lt; &gt; &quot; &amp;";
  +    private static final String SUPPORT_CHARS_LESS_HEX_20 = "\t\r\n";
  +    private static final String ENCODED_SUPPORT_CHARS_LESS_HEX_20 = "&#x9;&#xd;&#xa;";
  +    private static final String INVALID_XML_STRING = "Invalid XML String \u0000";
   
       public EncodingTest(String s) {
           super(s);
  @@ -33,13 +35,12 @@
           for(int i=0;i<31;i++) {
               char c=(char)i;
               //ignore legit whitespace
  -            if ("\t\n\r".indexOf(c) == 1) {
  +            if ("\t\n\r".indexOf(c) == -1) {
                   //verify the others are caught
                   String s=(new Character(c)).toString();
                   assertInvalidStringsDetected(s);
               }
           }
  -        assertInvalidStringsDetected("foo");
       }
   
       /**
  @@ -72,29 +73,65 @@
       public void testUTF8() throws Exception {
           XMLEncoder encoder = XMLEncoderFactory.getEncoder(XMLEncoderFactory.ENCODING_UTF_8);
           String encodedUmlauts = encoder.encode(GERMAN_UMLAUTS);
  -        String encodedXMLChars = encoder.encode(XML_SPECIAL_CHARS);
   
           assertEquals(XMLEncoderFactory.ENCODING_UTF_8, encoder.getEncoding());
  -        assertEquals(ENCODED_XML_SPECIAL_CHARS, encodedXMLChars);
           assertEquals(GERMAN_UMLAUTS, new String(encodedUmlauts.getBytes(), XMLEncoderFactory.ENCODING_UTF_8));
  -        //assert that the whitespace chars are not touched
  -        assertEquals(SUPPORT_CHARS_LESS_HEX_20, encoder.encode(SUPPORT_CHARS_LESS_HEX_20));
  +        verifyCommonAssertions(encoder);
       }
   
       public void testUTF16() throws Exception {
  -        // this test needs to be improved, currently is does not test a lot
           XMLEncoder encoder = XMLEncoderFactory.getEncoder(XMLEncoderFactory.ENCODING_UTF_16);
           String encodedUmlauts = encoder.encode(GERMAN_UMLAUTS);
  -        String encodedXMLChars = encoder.encode(XML_SPECIAL_CHARS);
   
           assertEquals(XMLEncoderFactory.ENCODING_UTF_16, encoder.getEncoding());
  -        assertEquals(ENCODED_XML_SPECIAL_CHARS, encodedXMLChars);
           // java uses UTF-16 internally, should be equal
           assertEquals(GERMAN_UMLAUTS, encodedUmlauts);
  +        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
  -        assertEquals(SUPPORT_CHARS_LESS_HEX_20, encoder.encode(SUPPORT_CHARS_LESS_HEX_20));
  +        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);
       }