adbxmlstreamreadertest.java

来自「开源的axis2框架的源码。用于开发WEBSERVER」· Java 代码 · 共 862 行 · 第 1/3 页

JAVA
862
字号
            assertXMLEqual(newDocument(expectedXML), newDocument(actualXML));
        } catch (ParserConfigurationException e) {
            fail("Error has occurred " + e);
        } catch (SAXException e) {
            fail("Error has occurred " + e);
        } catch (IOException e) {
            fail("Error has occurred " + e);
        } catch (XMLStreamException e) {
            fail("Error has occurred " + e);
        }


    }

    /**
     * Test multiple unqulified attributes
     *
     * @throws XMLStreamException
     */
    public void testAttributes() throws XMLStreamException {

        String expectedXML =
                "<emp:Employee xmlns:emp=\"http://ec.org/software\" Attr2=\"Value 2\" " +
                        "Attr3=\"Value 3\" Attr1=\"Value 1\" Attr5=\"Value 5\" Attr4=\"Value 4\"></emp:Employee>";

        OMFactory factory = OMAbstractFactory.getOMFactory();
        QName elementQName = new QName("http://ec.org/software", "Employee", "emp");
        OMAttribute[] attribute = new OMAttribute[5];

        for (int i = 0; i < 5; i++) {
            attribute[i] = factory.createOMAttribute("Attr" + (i + 1), null, "Value " + (i + 1));
        }

        List omAttribList = new ArrayList();
        for (int i = 0; i < attribute.length; i++) {
            omAttribList.add(Constants.OM_ATTRIBUTE_KEY);
            omAttribList.add(attribute[i]);
        }


        String stringXML = getStringXML(new ADBXMLStreamReaderImpl(elementQName,
                                                                   null,
                                                                   omAttribList.toArray()));
        try {
            Document actualDom = newDocument(stringXML);
            Document expectedDocument = newDocument(expectedXML);
            assertXMLEqual(actualDom, expectedDocument);
        } catch (ParserConfigurationException e) {
            fail("Exception in parsing documents " + e);
        } catch (SAXException e) {
            fail("Exception in parsing documents " + e);
        } catch (IOException e) {
            fail("Exception in parsing documents " + e);
        }


    }

    /** A text only element */
    public void testElementText() {

        String expectedXML = "<ns1:testElementText xmlns:ns1=\"http://testElementText.org\">" +
                "This is some Text for the element</ns1:testElementText>";
        try {
            ArrayList properties = new ArrayList();
            properties.add(ADBXMLStreamReader.ELEMENT_TEXT);
            properties.add("This is some Text for the element");

            XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
                    new QName("http://testElementText.org", "testElementText", "ns1"),
                    properties.toArray(), null);

            String actualXML = getStringXML(pullParser);

            assertXMLEqual(newDocument(expectedXML), newDocument(actualXML));
        } catch (ParserConfigurationException e) {
            fail("Error has occurred " + e);
        } catch (SAXException e) {
            fail("Error has occurred " + e);
        } catch (IOException e) {
            fail("Error has occurred " + e);
        } catch (Exception e) {
            e.printStackTrace();
            fail("Error has occurred " + e);
        }
    }

/// todo Fails due to a bug in WSTX writer
//    /**
//     * Test multiple qualified attributes
//     * @throws XMLStreamException
//     */
//    public void testAttributesWithNamespaces() throws XMLStreamException {
//
//        String expectedXML = "<emp:Employee xmlns:emp=\"http://ec.org/software\" " +
//                "xmlns:attrNS=\"mailto:whoever@whatever.com\" attrNS:Attr2=\"Value 2\" " +
//                "attrNS:Attr3=\"Value 3\" attrNS:Attr1=\"Value 1\"\n" +
//                "              attrNS:Attr5=\"Value 5\" attrNS:Attr4=\"Value 4\"></emp:Employee>";
//
//        OMFactory factory = OMAbstractFactory.getOMFactory();
//        QName elementQName = new QName("http://ec.org/software", "Employee", "emp");
//        OMNamespace attrNS = factory.createOMNamespace("mailto:whoever@whatever.com", "attrNS");
//
//        // add some attributes with namespaces
//        OMAttribute[] attribute = new OMAttribute[5];
//        for (int i = 0; i < 5; i++) {
//            attribute[i] = factory.createOMAttribute("Attr" + (i + 1), attrNS, "Value " + (i + 1));
//        }
//
//        List omAttribList = new ArrayList();
//        for (int i = 0; i < attribute.length; i++) {
//            omAttribList.add(Constants.OM_ATTRIBUTE_KEY);
//            omAttribList.add(attribute[i]);
//        }
//        String stringXML = getStringXML(new ADBXMLStreamReaderImpl(elementQName,
//                null,
//                omAttribList.toArray()));
//        try {
//            Document actualDom = newDocument(stringXML);
//            Document expectedDocument = newDocument(expectedXML);
//            assertXMLEqual(actualDom, expectedDocument);
//        } catch (ParserConfigurationException e) {
//            fail("Exception in parsing documents " + e);
//        } catch (SAXException e) {
//            fail("Exception in parsing documents " + e);
//        } catch (IOException e) {
//            fail("Exception in parsing documents " + e);
//        }
//    }

    /** test for qualified attributes */
    public void testUnQualifiedAttributes() {

        String expectedXML =
                "<ns1:testElementText xmlns:ns1=\"http://testElementText.org\" MyUnQualifiedAttribute=\"MyAttributeValue\">" +
                        "<ns2:QualifiedElement xmlns:ns2=\"http://testQElementText.org\">" +
                        "This is some Text for the element</ns2:QualifiedElement></ns1:testElementText>";
        try {
            ArrayList properties = new ArrayList();
            properties.add(new QName("http://testQElementText.org", "QualifiedElement", "ns2"));
            properties.add("This is some Text for the element");

            String[] attributes = new String[2];
            attributes[0] = "MyUnQualifiedAttribute";
            attributes[1] = "MyAttributeValue";


            XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
                    new QName("http://testElementText.org", "testElementText", "ns1"),
                    properties.toArray(),
                    attributes);

            String actualXML = getStringXML(pullParser);

            assertXMLEqual(newDocument(expectedXML), newDocument(actualXML));
        } catch (ParserConfigurationException e) {
            fail("Error has occurred " + e);
        } catch (SAXException e) {
            fail("Error has occurred " + e);
        } catch (IOException e) {
            fail("Error has occurred " + e);
        } catch (Exception e) {
            fail("Error has occurred " + e);
        }
    }

    /** test for base64 */
    public void testBase64EncodedText() {

        String textTobeSent = "33344MthwrrewrIOTEN)(&**^E(W)EW";

        String expectedXML = "<ns1:testElementText xmlns:ns1=\"http://testElementText.org\">" +
                "<ns2:QualifiedElement xmlns:ns2=\"http://testQElementText.org\">" +
                Base64.encode(textTobeSent.getBytes()) +
                "</ns2:QualifiedElement></ns1:testElementText>";
        try {
            ArrayList properties = new ArrayList();
            properties.add(new QName("http://testQElementText.org", "QualifiedElement", "ns2"));
            properties.add(new DataHandler(new ByteArrayDataSource(textTobeSent.getBytes())));

            XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
                    new QName("http://testElementText.org", "testElementText", "ns1"),
                    properties.toArray(),
                    null);

            String actualXML = getStringXML(pullParser);

            assertXMLEqual(newDocument(expectedXML), newDocument(actualXML));
        } catch (ParserConfigurationException e) {
            fail("Error has occurred " + e);
        } catch (SAXException e) {
            fail("Error has occurred " + e);
        } catch (IOException e) {
            fail("Error has occurred " + e);
        } catch (Exception e) {
            fail("Error has occurred " + e);
        }
    }

    /** test the qualified elements A qulified element has been associated with a namespace */
    public void testQualifiedElement() {

        String expectedXML = "<ns1:testElementText xmlns:ns1=\"http://testElementText.org\">" +
                "<ns2:QualifiedElement xmlns:ns2=\"http://testQElementText.org\">" +
                "This is some Text for the element</ns2:QualifiedElement></ns1:testElementText>";
        try {
            ArrayList properties = new ArrayList();
            properties.add(new QName("http://testQElementText.org", "QualifiedElement", "ns2"));
            properties.add("This is some Text for the element");

            XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
                    new QName("http://testElementText.org", "testElementText", "ns1"),
                    properties.toArray(),
                    null);

            String actualXML = getStringXML(pullParser);
            assertXMLEqual(newDocument(expectedXML), newDocument(actualXML));
        } catch (ParserConfigurationException e) {
            fail("Error has occurred " + e);
        } catch (SAXException e) {
            fail("Error has occurred " + e);
        } catch (IOException e) {
            fail("Error has occurred " + e);
        } catch (Exception e) {
            fail("Error has occurred " + e);
        }
    }

    /**
     * Util method to convert the pullstream to a string
     *
     * @param reader
     * @return
     */
    private String getStringXML(XMLStreamReader reader) throws XMLStreamException {
        //the returned pullparser starts at an Element rather than the start
        //document event. This is somewhat disturbing but since an ADBBean
        //denotes an XMLFragment, it is justifiable to keep the current event
        //at the Start-element rather than the start document
        //What it boils down to is that we need to wrap the reader in a
        //stream wrapper to get a fake start-document event

        StreamingOMSerializer ser = new StreamingOMSerializer();
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        XMLStreamWriter writer = StAXUtils.createXMLStreamWriter(byteArrayOutputStream);
        ser.serialize(
                new StreamWrapper(reader),
                writer);
        writer.flush();
        return byteArrayOutputStream.toString();
    }

//     /**
//     * Util method to convert the pullstream to a string
//     * @param reader
//     * @return
//     */
//    private String getStringXML(XMLStreamReader reader) {
//        //the returned pullparser starts at an Element rather than the start
//        //document event. This is somewhat disturbing but since an ADBBean
//        //denotes an XMLFragment, it is justifiable to keep the current event
//        //at the Start-element rather than the start document
//        //What it boils down to is that we need to wrap the reader in a
//        //stream wrapper to get a fake start-document event
//        StAXOMBuilder stAXOMBuilder = new StAXOMBuilder(
//                new StreamWrapper(reader));
//        //stAXOMBuilder.setDoDebug(true);
//        OMElement omelement = stAXOMBuilder.getDocumentElement();
//        return omelement.toString();
//    }

    /**
     * Creates a DOM document from the string
     *
     * @param xml
     * @return
     * @throws ParserConfigurationException
     * @throws SAXException
     * @throws IOException
     */
    public Document newDocument(String xml)
            throws ParserConfigurationException, SAXException, IOException {
        return db.parse(new ByteArrayInputStream(xml.getBytes()));
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?