adbxmlstreamreadertest.java

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

JAVA
862
字号
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.axis2.databinding.utils.reader;

import org.apache.axiom.attachments.ByteArrayDataSource;
import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMAttribute;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.impl.serialize.StreamingOMSerializer;
import org.apache.axiom.om.util.Base64;
import org.apache.axiom.om.util.StAXUtils;
import org.apache.axis2.databinding.ADBBean;
import org.apache.axis2.databinding.utils.Constants;
import org.apache.axis2.util.StreamWrapper;
import org.custommonkey.xmlunit.XMLTestCase;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.activation.DataHandler;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public class ADBXMLStreamReaderTest extends XMLTestCase {

    private DocumentBuilder db;

    protected void setUp() throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        db = dbf.newDocumentBuilder();
    }

    /** complex array scenario */
    public void testComplexObjectArrayScenario() {
        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>" +
                            "<AdditionalDependent>" +
                            "<Name>FooTwo</Name>" +
                            "<Age>25</Age>" +
                            "<Sex>Male</Sex>" +
                            "</AdditionalDependent>" +
                            "<AdditionalDependent>" +
                            "<Name>FooTwo</Name>" +
                            "<Age>25</Age>" +
                            "<Sex>Male</Sex>" +
                            "</AdditionalDependent>" +
                            "<AdditionalDependent>" +
                            "<Name>FooTwo</Name>" +
                            "<Age>25</Age>" +
                            "<Sex>Male</Sex>" +
                            "</AdditionalDependent>" +
                            "<AdditionalDependent>" +
                            "<Name>FooTwo</Name>" +
                            "<Age>25</Age>" +
                            "<Sex>Male</Sex>" +
                            "</AdditionalDependent>" +
                            "<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);

            ADBBean[] adbBeans = new ADBBean[4];
            for (int i = 0; i < 4; i++) {
                adbBeans[i] = new DummyADBBean();
            }
            for (int i = 0; i < adbBeans.length; i++) {
                propertyList.add(new QName("AdditionalDependent"));
                propertyList.add(adbBeans[i]);

            }

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

    /** complex array scenario with nulls in between */
    public void testComplexObjectArrayScenarioWithNulls() {
        try {
            String expectedXML =
                    "<ns1:TestComplexStringArrayScenario xmlns:ns1=\"http://testComplexStringArrayScenario.org\">" +
                            "<AdditionalDependent>" +
                            "<Name>FooTwo</Name>" +
                            "<Age>25</Age>" +
                            "<Sex>Male</Sex>" +
                            "</AdditionalDependent>" +
                            "<AdditionalDependent>" +
                            "<Name>FooTwo</Name>" +
                            "<Age>25</Age>" +
                            "<Sex>Male</Sex>" +
                            "</AdditionalDependent>" +
                            "<AdditionalDependent>" +
                            "<Name>FooTwo</Name>" +
                            "<Age>25</Age>" +
                            "<Sex>Male</Sex>" +
                            "</AdditionalDependent>" +
                            "<AdditionalDependent xsi:nil=\"true\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" +
                            "</AdditionalDependent>" +
                            "<Bar>Some More Text</Bar><" +
                            "/ns1:TestComplexStringArrayScenario>";

            ArrayList propertyList = new ArrayList();

            ADBBean[] adbBeans = new ADBBean[4];
            for (int i = 0; i < 4; i++) {
                adbBeans[i] = new DummyADBBean();
            }

            adbBeans[3] = null;

            for (int i = 0; i < adbBeans.length; i++) {
                propertyList.add(new QName("AdditionalDependent"));
                propertyList.add(adbBeans[i]);

            }

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

    /** Empty array */
    public void testComplexObjectArrayScenarioEmptyArray() {
        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>" +
                            "<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[] array = new String[] { };
            propertyList.add(new QName("AdditionalDependent"));
            propertyList.add(array);

            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 complex array list */
    public void testComplexArrayList() {
        try {

            String exptectedXML = "<Person><Name>FooOne</Name><Organization>Apache</Organization>" +
                    "<Dependent><Name>FooTwo</Name><Age>25</Age><Sex>Male</Sex><Depemdent>" +
                    "<Name>FooTwo</Name><Age>25</Age><Sex>Male</Sex><Depemdent><Name>FooTwo</Name>" +
                    "<Age>25</Age><Sex>Male</Sex></Depemdent></Depemdent></Dependent>" +
                    "<test:Dependent xmlns:test=\"http://whatever.com\"><Name>FooTwo</Name><Age>25</Age>" +
                    "<Sex>Male</Sex><Depemdent><Name>FooTwo</Name><Age>25</Age><Sex>Male</Sex>" +
                    "</Depemdent></test:Dependent></Person>";


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

            propertyList.add("Organization");
            propertyList.add("Apache");

            propertyList.add(new QName("Dependent"));
            DummyADBBean dummyBean = new DummyADBBean();
            DummyADBBean nextdummyBean = dummyBean.addAnotherBean();
            nextdummyBean.addAnotherBean();
            propertyList.add(dummyBean);

            propertyList.add(new QName("http://whatever.com", "Dependent", "test"));
            dummyBean = new DummyADBBean();
            dummyBean.addAnotherBean();
            propertyList.add(dummyBean);

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

    }

⌨️ 快捷键说明

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