CVS Update: xmlpull-api-v1/src/java/tests/org/xmlpull/v1/tests

Aleksander Andrzej Slominski <[email protected]> Tue, 25 Feb 2003 15:08:12 -0500 (EST)
Newsgroups gmane.text.xml.xmlpull.devel
Message-ID <[email protected]>
aslom       03/02/25 15:08:12

  Modified:    addons/java/sax2_driver/src/org/xmlpull/v1/sax2 Driver.java
               src/java/tests/org/xmlpull/v1/tests PackageTests.java
                        TestSerializeWithNs.java
  Log:
  updated tests to check and report support for optinal formatting
  properties and features  of serializer
  
  Revision  Changes    Path
  1.3       +1 -3      xmlpull-api-v1/addons/java/sax2_driver/src/org/xmlpull/v1/sax2/Driver.java
  
  Index: Driver.java
  ===================================================================
  RCS file: /l/extreme/cvspub/xmlpull-api-v1/addons/java/sax2_driver/src/org/xmlpull/v1/sax2/Driver.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -b -t -w -r1.2 -r1.3
  --- Driver.java	18 Feb 2003 19:48:11 -0000	1.2
  +++ Driver.java	25 Feb 2003 20:08:12 -0000	1.3
  @@ -287,6 +287,7 @@
                           errorHandler.fatalError(saxException);
                           return;
                       }
  +                    // NOTE: replace with Connection to run in J2ME environment
                       try {
                           URL url = new URL(systemId);
                           stream = url.openStream();
  @@ -465,6 +466,3 @@
       }
   
   }
  -
  -
  -
  
  
  
  1.16      +4 -0      xmlpull-api-v1/src/java/tests/org/xmlpull/v1/tests/PackageTests.java
  
  Index: PackageTests.java
  ===================================================================
  RCS file: /l/extreme/cvspub/xmlpull-api-v1/src/java/tests/org/xmlpull/v1/tests/PackageTests.java,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -b -t -w -r1.15 -r1.16
  --- PackageTests.java	7 Sep 2002 20:39:44 -0000	1.15
  +++ PackageTests.java	25 Feb 2003 20:08:12 -0000	1.16
  @@ -72,6 +72,10 @@
   
       private static StringBuffer notes = new StringBuffer();
       public static void addNote(String note) {
  +        if(PackageTests.runnigAllTests() == false) {
  +            System.out.print(note);
  +            System.out.flush();
  +        }
           notes.append(note);
       }
   
  
  
  
  1.13      +218 -29   xmlpull-api-v1/src/java/tests/org/xmlpull/v1/tests/TestSerializeWithNs.java
  
  Index: TestSerializeWithNs.java
  ===================================================================
  RCS file: /l/extreme/cvspub/xmlpull-api-v1/src/java/tests/org/xmlpull/v1/tests/TestSerializeWithNs.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -b -t -w -r1.12 -r1.13
  --- TestSerializeWithNs.java	12 Oct 2002 22:17:59 -0000	1.12
  +++ TestSerializeWithNs.java	25 Feb 2003 20:08:12 -0000	1.13
  @@ -341,6 +341,201 @@
       private boolean serializerLineSeparatorSupported;
       private boolean serializerUseApostropheSupported;
   
  +
  +    /**
  +     * Test optional support pretty printing
  +     */
  +    public void testUseApostrophe() throws Exception {
  +        XmlSerializer ser = factory.newSerializer();
  +        try {
  +            ser.setFeature(FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE, true);
  +        } catch(Exception ex) {
  +            // ignore test if optional property not supported
  +            return;
  +        }
  +        PackageTests.addNote(
  +            "* feature "+FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE+" is supported\n");
  +
  +        boolean useApost = ser.getFeature(FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE);
  +        assertEquals(true, useApost);
  +        checkAttributeQuot(true, ser);
  +
  +        ser.setFeature(FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE, false);
  +        useApost = ser.getFeature(FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE);
  +        assertEquals(false, useApost);
  +
  +        checkAttributeQuot(false, ser);
  +        useApost = ser.getFeature(FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE);
  +        assertEquals(false, useApost);
  +
  +        ser.setFeature(FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE, true);
  +        useApost = ser.getFeature(FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE);
  +        assertEquals(true, useApost);
  +        checkAttributeQuot(true, ser);
  +
  +        checkAttributeQuotMix(ser);
  +    }
  +
  +    /**
  +     * Check that attribute was quoted correctly
  +     */
  +    private void checkAttributeQuot(boolean useApostrophe, XmlSerializer ser) throws Exception {
  +        StringWriter sw = new StringWriter();
  +        ser.setOutput(sw);
  +
  +        ser.startTag("", "test");
  +        ser.attribute(null, "att", "value");
  +        ser.endTag("", "test");
  +        ser.endDocument();
  +
  +        String s = sw.toString();
  +        if(useApostrophe) {
  +            assertTrue("use apostrophe for attribute value", s.indexOf("'value'") !=-1);
  +        } else {
  +            assertTrue("use quotation for attribute value", s.indexOf("\"value\"") !=-1);
  +        }
  +
  +        // some validaiton of serialized XML
  +        XmlPullParser pp = factory.newPullParser();
  +        pp.setInput(new StringReader(s));
  +        pp.nextTag();
  +        pp.require(pp.START_TAG, null, "test");
  +        assertEquals("value", pp.getAttributeValue(pp.NO_NAMESPACE, "att"));
  +        pp.nextTag();
  +        pp.require(pp.END_TAG, null, "test");
  +    }
  +
  +    /**
  +     * Check that attribute quotations can be changed _during_ serialization
  +     */
  +    private void checkAttributeQuotMix(XmlSerializer ser) throws Exception {
  +        StringWriter sw = new StringWriter();
  +        ser.setOutput(sw);
  +
  +        ser.startTag("", "test");
  +        ser.attribute(null, "att", "value");
  +        ser.setFeature(FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE, true);
  +        ser.attribute(null, "attA", "valueA");
  +        ser.setFeature(FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE, false);
  +        ser.attribute(null, "attQ", "valueQ");
  +        ser.endTag("", "test");
  +        ser.endDocument();
  +
  +        String s = sw.toString();
  +        assertTrue("use apostrophe for attribute value", s.indexOf("'valueA'") !=-1);
  +        assertTrue("use apostrophe for attribute value", s.indexOf("\"valueQ\"") !=-1);
  +
  +        // some validaiton of serialized XML
  +        XmlPullParser pp = factory.newPullParser();
  +        pp.setInput(new StringReader(s));
  +        pp.nextTag();
  +        pp.require(pp.START_TAG, null, "test");
  +        assertEquals("value", pp.getAttributeValue(pp.NO_NAMESPACE, "att"));
  +        assertEquals("valueA", pp.getAttributeValue(pp.NO_NAMESPACE, "attA"));
  +        assertEquals("valueQ", pp.getAttributeValue(pp.NO_NAMESPACE, "attQ"));
  +        pp.nextTag();
  +        pp.require(pp.END_TAG, null, "test");
  +    }
  +
  +
  +    public void testIndentation() throws Exception {
  +        XmlSerializer ser = factory.newSerializer();
  +        try {
  +            ser.setProperty(PROPERTY_SERIALIZER_INDENTATION, " ");
  +        } catch(Exception ex) {
  +            // ignore test if optional property not supported
  +            return;
  +        }
  +        PackageTests.addNote("* property "+PROPERTY_SERIALIZER_INDENTATION+" is supported\n");
  +
  +        StringWriter sw = new StringWriter();
  +        ser.setOutput(sw);
  +
  +        ser.startTag("", "S1");
  +        ser.startTag("", "S2");
  +        ser.text("T");
  +        ser.endTag("", "S2");
  +        ser.startTag("", "M2");
  +        ser.startTag("", "M3");
  +        ser.endTag("", "M3");
  +        ser.endTag("", "M2");
  +        ser.endTag("", "S1");
  +        ser.endDocument();
  +
  +        String xml = sw.toString();
  +        //System.out.println(getClass()+" xml="+xml);
  +        checkFormatting(" ", 0, "\n", "<S1", xml);
  +        checkFormatting(" ", 1, "\n", "<S2", xml);
  +        checkFormatting("T", 1, null, "</S2", xml); //special case set that no indent but content
  +        checkFormatting(" ", 1, "\n", "<M2", xml);
  +        checkFormatting(" ", 2, "\n", "<M3", xml);
  +        checkFormatting(" ", 1, "\n", "</M2", xml);
  +        checkFormatting(" ", 0, "\n", "</S1", xml);
  +
  +        //TODO check if line separators property is supported ...
  +    }
  +
  +    private void checkFormatting(String indent, int level, String lineSeparator,
  +                                 String s, String xml) throws Exception {
  +        // check that s is on output XML
  +        int pos = xml.indexOf(s);
  +        assertTrue(pos >= 0);
  +        // check that indent string is used at level
  +        for (int i = 0; i < level; i++)
  +        {
  +            for (int j = indent.length() - 1; j >= 0 ; j--)
  +            {
  +                --pos;
  +                if(pos < 0) {
  +                    fail("not enough indent for "+printable(s)+" in "+printable(xml));
  +                }
  +                char indentCh = indent.charAt(j);
  +                char ch = xml.charAt(pos);
  +                assertEquals(
  +                    "expected indentation character '"+printable(indent)+"'"
  +                        +" pos="+pos+" s='"+printable(s)
  +                        +"' xml="+printable(xml),
  +                    printable(indentCh), printable(ch));
  +            }
  +        }
  +        // check that indent is of exact size and line ending is as expected
  +        if(pos > 0) {
  +            --pos;
  +            char ch = xml.charAt(pos);
  +            if(lineSeparator != null) {
  +                for (int i = lineSeparator.length() - 1; i >=0 ; i--)
  +                {
  +                    char lineSepCh = lineSeparator.charAt(i);
  +                    assertEquals(
  +                        "expected end of line at pos="+pos+" s='"+printable(s)
  +                            +"' xml="+printable(xml),
  +                        printable(lineSepCh), printable(ch));
  +                    --pos;
  +                    ch = xml.charAt(pos);
  +
  +                }
  +            } else {
  +                char indentCh = indent.charAt(indent.length() - 1);
  +                assertTrue(
  +                    "expected character that is different from '"+printable(indent)+"'"
  +                        +" used for indentation "
  +                        +"pos="+pos+" s="+printable(s)+" xml="+printable(xml),
  +                    indentCh != ch);
  +            }
  +        }
  +    }
  +
  +    public void testLineSeparator() throws Exception {
  +        XmlSerializer ser = factory.newSerializer();
  +        try {
  +            ser.setProperty(PROPERTY_SERIALIZER_LINE_SEPARATOR, "\n");
  +        } catch(Exception ex) {
  +            // ignore test if optional property not supported
  +            return;
  +        }
  +        PackageTests.addNote("* property "+PROPERTY_SERIALIZER_LINE_SEPARATOR+" is supported\n");
  +    }
  +
       /** generate SOAP 1.2 envelope
        try to use indentation
   
  @@ -365,12 +560,7 @@
               try {
                   ser.setFeature(FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE,
                                  attvalueUseApostrophe.booleanValue());
  -                if(!serializerUseApostropheSupported) {
  -                    PackageTests.addNote(
  -                        "* feature "
  -                            +FEATURE_SERIALIZER_ATTVALUE_USE_APOSTROPHE+" is supported\n");
                       serializerUseApostropheSupported = true;
  -                }
               } catch(Exception ex) {
                   // ignore if optional feature not supported
               }
  @@ -378,12 +568,7 @@
           if(indentation !=null) {
               try {
                   ser.setProperty(PROPERTY_SERIALIZER_INDENTATION, indentation);
  -                if(!serializerIndentationSupported) {
  -                    PackageTests.addNote(
  -                        "* property "
  -                            +PROPERTY_SERIALIZER_INDENTATION+" is supported\n");
                       serializerIndentationSupported = true;
  -                }
               } catch(Exception ex) {
                   // ignore if optional property not supported
               }
  @@ -391,12 +576,7 @@
           if(lineSeparator !=null) {
               try {
                   ser.setProperty(PROPERTY_SERIALIZER_LINE_SEPARATOR, lineSeparator);
  -                if(!serializerLineSeparatorSupported) {
  -                    PackageTests.addNote(
  -                        "* property "
  -                            +PROPERTY_SERIALIZER_LINE_SEPARATOR+" is supported\n");
                       serializerLineSeparatorSupported = true;
  -                }
               } catch(Exception ex) {
                   // ignore if optional property not supported
               }
  @@ -452,7 +632,7 @@
       //setPrefix check that prefix is not duplicated ...
   
       public void testSetPrefix() throws Exception {
  -        //TODO      redeclaring defult namespace
  +        //TODO check redeclaring defult namespace
   
   
           checkTestSetPrefixSoap(SOAP12);
  @@ -465,32 +645,41 @@
           String generated = generateSoapEnvelope("", "n", "m");
           //System.err.println(getClass()+" generated="+generated);
   
  -        // 1 is for one extra namespace must be added to declare attrbute mustUnderstan in SOAP-ENV namespace
  -        checkTestSetPrefixSoap(generated, 1);
  +        // 1 is for one extra namespace must be added to declare xmlns namespace
  +        //    for attrbute mustUnderstan in SOAP-ENV namespace
  +        checkTestSetPrefixSoap(generated, 1,false);
   
   
  -        checkTestSetPrefixSoap(generateSoapEnvelope("", null, "m"),1);
  +        checkTestSetPrefixSoap(generateSoapEnvelope("", null, "m"),1,false);
   
           //check optional pretty printing
           checkTestSetPrefixSoap(generateSoapEnvelope("env", "n", "m", Boolean.FALSE, null, null));
           checkTestSetPrefixSoap(generateSoapEnvelope("env", "n", "m", Boolean.TRUE, null, null));
  -        checkTestSetPrefixSoap(generateSoapEnvelope("env", "n", "m", null, " ", null));
  -        checkTestSetPrefixSoap(generateSoapEnvelope("env", "n", "m", null, "\t", null));
  -        checkTestSetPrefixSoap(generateSoapEnvelope("env", "n", "m", null, "    ", null));
  +        checkTestSetPrefixSoap(generateSoapEnvelope("env", "n", "m", null, " ", null), true);
  +        checkTestSetPrefixSoap(generateSoapEnvelope("env", "n", "m", null, "\t", null), true);
  +        checkTestSetPrefixSoap(generateSoapEnvelope("env", "n", "m", null, "    ", null), true);
           String s = generateSoapEnvelope("env", "n", "m", Boolean.TRUE, " ", "\n");
           //System.out.println(getClass()+" envelope="+generateSoapEnvelope("", "n", "m"));
  -        checkTestSetPrefixSoap(s);
  +        checkTestSetPrefixSoap(s, true);
       }
   
       private void checkTestSetPrefixSoap(String soapEnvelope) throws Exception {
  -        checkTestSetPrefixSoap(soapEnvelope, 0);
  +        checkTestSetPrefixSoap(soapEnvelope, 0, false);
  +    }
  +
  +    private void checkTestSetPrefixSoap(String soapEnvelope, boolean indented) throws Exception {
  +        checkTestSetPrefixSoap(soapEnvelope, 0, indented);
       }
   
       // run test using checkParserStateNs()
  -    private void checkTestSetPrefixSoap(String soapEnvelope, int extraNs) throws Exception {
  +    private void checkTestSetPrefixSoap(String soapEnvelope, int extraNs, boolean indented)
  +        throws Exception
  +    {
   
           //compare that XML representation of soapEnvelope is as desired
  +        if(!indented) {
           assertXmlEquals(SOAP12, soapEnvelope);
  +        }
   
           xpp.setInput(new StringReader(soapEnvelope));
   
  
  
  


------------------------ Yahoo! Groups Sponsor ---------------------~-->
Get 128 Bit SSL Encryption!
http://us.click.yahoo.com/FpY02D/vN2EAA/xGHJAA/2U_rlB/TM
---------------------------------------------------------------------~->

To unsubscribe from this group, send an email to:
[email protected]

 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/