adbxmlstreamreadertest.java

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

JAVA
862
字号

    public static class DummyADBBean implements ADBBean {
        ArrayList propertyList = new ArrayList();

        public DummyADBBean() {
            propertyList.add("Name");
            propertyList.add("FooTwo");
            propertyList.add("Age");
            propertyList.add("25");
            propertyList.add("Sex");
            propertyList.add("Male");
        }

        public DummyADBBean addAnotherBean() {
            propertyList.add(new QName("Depemdent"));
            DummyADBBean dummyBean = new DummyADBBean();
            propertyList.add(dummyBean);
            return dummyBean;
        }

        public XMLStreamReader getPullParser(QName adbBeanQName) {
            return new ADBXMLStreamReaderImpl(adbBeanQName, propertyList.toArray(), null);
        }
    }

    public void testWithOMElements() throws XMLStreamException {

        String expectedXML =
                "<OMElementTest><axis2:FirstOMElement xmlns:axis2=\"http://ws.apache.org/namespaces/axis2\">" +
                        "<axis2:SecondOMElement></axis2:SecondOMElement></axis2:FirstOMElement><Foo>Some Text</Foo>" +
                        "<Dependent><Name>FooTwo</Name><Age>25</Age><Sex>Male</Sex></Dependent>" +
                        "<axis2:SecondOMElement xmlns:axis2=\"http://ws.apache.org/namespaces/axis2\">" +
                        "</axis2:SecondOMElement></OMElementTest>";

        OMFactory factory = OMAbstractFactory.getOMFactory();
        OMNamespace axis2Namespace = factory.createOMNamespace(
                org.apache.axis2.Constants.AXIS2_NAMESPACE_URI,
                org.apache.axis2.Constants.AXIS2_NAMESPACE_PREFIX);
        OMElement firstElement = factory.createOMElement("FirstOMElement", axis2Namespace);
        OMElement secondElement =
                factory.createOMElement("SecondOMElement", axis2Namespace, firstElement);

        ArrayList propertyList = new ArrayList();

        // add an OMElement
        propertyList.add(firstElement.getQName());
        propertyList.add(firstElement);

        // add some more stuff
        propertyList.add("Foo");
        propertyList.add("Some Text");
        propertyList.add(new QName("Dependent"));
        DummyADBBean dummyBean = new DummyADBBean();
        propertyList.add(dummyBean);

//         lets add one more element
        propertyList.add(secondElement.getQName());
        propertyList.add(secondElement);


        XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(new QName("OMElementTest"),
                                                                propertyList.toArray(), null);
        String stringXML = getStringXML(pullParser);
        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 a completely null element */
    public void testNullableAttribute() {
        try {

            /*
            This is what I expect :

            */
            String exptectedXML =
                    "<Person xmlns=\"\"><Name xmlns=\"\">FooOne</Name><DependentOne xmlns=\"\" " +
                            "xsi:nil=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>" +
                            "</Person>";

            ArrayList propertyList = new ArrayList();
            propertyList.add("Name");
            propertyList.add("FooOne");
            propertyList.add(new QName("DependentOne"));
            propertyList.add(null);

            QName projectQName = new QName("Person");
            XMLStreamReader pullParser =
                    new ADBXMLStreamReaderImpl(projectQName, propertyList.toArray(), null);

            Document actualDom = newDocument(getStringXML(pullParser));
            Document expectedDocument = newDocument(exptectedXML);
            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);
        } catch (XMLStreamException e) {
            fail("Exception in parsing documents " + e);
        }

    }

    /** Test a simple array */
    public void testSimpleStringArrayScenario() {
        try {
            String expectedXML =
                    "<ns1:TestComplexStringArrayScenario xmlns:ns1=\"http://testComplexStringArrayScenario.org\">" +
                            "<StringInfo>Some Text 0</StringInfo>" +
                            "<StringInfo>Some Text 1</StringInfo>" +
                            "<StringInfo>Some Text 2</StringInfo>" +
                            "<StringInfo>Some Text 3</StringInfo>" +
                            "</ns1:TestComplexStringArrayScenario>";

            ArrayList propertyList = new ArrayList();

            String[] stringArray = new String[4];
            for (int i = 0; i < 4; i++) {
                stringArray[i] = "Some Text " + i;
            }
            propertyList.add("StringInfo");
            propertyList.add(stringArray);

            XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
                    new QName("http://testComplexStringArrayScenario.org",
                              "TestComplexStringArrayScenario", "ns1"),
                    propertyList.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 (XMLStreamException e) {
            fail("Error has occurred " + e);
        }


    }

    /** Test a simple array with null's inbetween */
    public void testSimpleStringArrayScenarioWithNulls() {
        try {
            String expectedXML =
                    "<ns1:TestComplexStringArrayScenario xmlns:ns1=\"http://testComplexStringArrayScenario.org\">" +
                            "<StringInfo>Some Text 0</StringInfo>" +
                            "<StringInfo xsi:nil=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"/>" +
                            "<StringInfo>Some Text 2</StringInfo>" +
                            "<StringInfo>Some Text 3</StringInfo>" +
                            "</ns1:TestComplexStringArrayScenario>";

            ArrayList propertyList = new ArrayList();

            String[] stringArray = new String[4];
            for (int i = 0; i < 4; i++) {
                stringArray[i] = "Some Text " + i;
            }
            stringArray[1] = null;

            propertyList.add("StringInfo");
            propertyList.add(stringArray);

            XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
                    new QName("http://testComplexStringArrayScenario.org",
                              "TestComplexStringArrayScenario", "ns1"),
                    propertyList.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 (XMLStreamException e) {
            fail("Error has occurred " + e);
        }


    }


    /** test the mixed content */
    public void testComplexStringArrayScenarioWithMixedContent() {
        try {
            String expectedXML =
                    "<ns1:TestComplexStringArrayScenario xmlns:ns1=\"http://testComplexStringArrayScenario.org\">" +
                            "<Foo>Some Text</Foo>" +
                            "<Dependent>" +
                            "<Name>FooTwo</Name>" +
                            "<Age>25</Age>" +
                            "<Sex>Male</Sex>" +
                            "</Dependent>" +
                            "<StringInfo>Some Text 0</StringInfo>" +
                            "<StringInfo>Some Text 1</StringInfo>" +
                            "<StringInfo>Some Text 2</StringInfo>" +
                            "<StringInfo>Some Text 3</StringInfo>" +
                            "<Bar>Some More Text</Bar>" +
                            "</ns1:TestComplexStringArrayScenario>";

            ArrayList propertyList = new ArrayList();
            propertyList.add("Foo");
            propertyList.add("Some Text");
            propertyList.add(new QName("Dependent"));
            DummyADBBean dummyBean = new DummyADBBean();
            propertyList.add(dummyBean);

            String[] stringArray = new String[4];
            for (int i = 0; i < 4; i++) {
                stringArray[i] = "Some Text " + i;
            }
            propertyList.add("StringInfo");
            propertyList.add(stringArray);

            propertyList.add("Bar");
            propertyList.add("Some More Text");

            XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
                    new QName("http://testComplexStringArrayScenario.org",
                              "TestComplexStringArrayScenario", "ns1"),
                    propertyList.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 a simple array with one element nil */
    public void testComplexStringArrayScenarioWithNull() {
        try {
            String expectedXML = "<ns1:TestComplexStringArrayScenario " +
                    "xmlns:ns1=\"http://testComplexStringArrayScenario.org\" " +
                    ">" +
                    "<StringInfo>Some Text 0</StringInfo>" +
                    "<StringInfo xsi:nil=\"true\" " +
                    "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"></StringInfo>" +
                    "<StringInfo>Some Text 2</StringInfo>" +
                    "<StringInfo>Some Text 3</StringInfo>" +
                    "</ns1:TestComplexStringArrayScenario>";

            ArrayList propertyList = new ArrayList();

            String[] stringArray = new String[4];
            for (int i = 0; i < 4; i++) {
                if (i != 1) stringArray[i] = "Some Text " + i;
            }
            stringArray[1] = null;

            propertyList.add("StringInfo");
            propertyList.add(stringArray);

            XMLStreamReader pullParser = new ADBXMLStreamReaderImpl(
                    new QName("http://testComplexStringArrayScenario.org",
                              "TestComplexStringArrayScenario", "ns1"),
                    propertyList.toArray(), null);
            String actualXML = getStringXML(pullParser);


⌨️ 快捷键说明

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