Package org.apache.ws.util

Source Code of org.apache.ws.util.XmlBeanUtilsTestCase

/*=============================================================================*
*  Copyright 2004 The Apache Software Foundation
*
*  Licensed 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.ws.util;

import org.apache.ws.resource.properties.AbstractResourcePropertiesTestCase;
import org.apache.ws.resource.properties.SushiPropertyQNames;
import org.apache.ws.resource.properties.impl.XmlBeansResourcePropertySet;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlString;
import org.xmlsoap.schemas.ws.x2003.x03.addressing.EndpointReferenceDocument;
import org.xmlsoap.schemas.ws.x2003.x03.addressing.EndpointReferenceType;
import org.xmlsoap.schemas.ws.x2003.x03.addressing.ReferencePropertiesType;

import javax.xml.namespace.QName;

/**
* Test case for {@link XmlBeanUtils}.
*
* @author Ian Springer
*/
public class XmlBeanUtilsTestCase
        extends AbstractResourcePropertiesTestCase
{

    private XmlObject m_propsDoc;

    /**
     * DOCUMENT_ME
     *
     * @throws Exception DOCUMENT_ME
     */
    public void setUp()
            throws Exception
    {
        initResourcePropsDoc();
        m_propsDoc = ( (XmlBeansResourcePropertySet) m_resourcePropSet ).toXmlObject();
    }

    /**
     * Test for {@link XmlBeanUtils#addChildElement(org.apache.xmlbeans.XmlObject, org.apache.xmlbeans.XmlObject)}.
     * NOTE: This test relies on {@link XmlBeanUtils#getChildElements(org.apache.xmlbeans.XmlObject, javax.xml.namespace.QName)},
     *       so it assumes that method has been tested elsewhere.
     *
     * @throws Exception on error
     */
    public void testAddChildElement()
            throws Exception
    {
        final QName elemName = SushiPropertyQNames.IKA;
        final String elemValue = "whatever";
        final XmlObject xBean = XmlObject.Factory.parse(
                "<" + elemName.getLocalPart() + " xmlns=\"" + elemName.getNamespaceURI() + "\">" + elemValue + "</" +
                elemName.getLocalPart() +
                ">" );
        // first test adding to an element that already has children...
        XmlBeanUtils.addChildElement( m_propsDoc, xBean );
        XmlObject[] childElems = XmlBeanUtils.getChildElements( m_propsDoc, elemName );
        assertEquals( 2, childElems.length );
        assertTrue( childElems[1] instanceof XmlString );
        // now test adding to a childless element...
        EndpointReferenceDocument eprDoc = EndpointReferenceDocument.Factory.newInstance();
        EndpointReferenceType epr = eprDoc.addNewEndpointReference();
        ReferencePropertiesType refPropsType = epr.addNewReferenceProperties();
        XmlBeanUtils.addChildElement( refPropsType, xBean );
        childElems = XmlBeanUtils.getChildElements( refPropsType, elemName );
        assertEquals( 1, childElems.length );
        assertTrue( childElems[0] instanceof XmlString );
    }

    /**
     * Test for {@link XmlBeanUtils#addChildElement(org.apache.xmlbeans.XmlObject, QName)}.
     * NOTE: This test relies on {@link XmlBeanUtils#getChildElements(org.apache.xmlbeans.XmlObject, javax.xml.namespace.QName)},
     *       so it assumes that method has been tested elsewhere.
     *
     * @throws Exception on error
     */
    public void testAddChildElementByName()
            throws Exception
    {
        final QName elemName = SushiPropertyQNames.IKA;
        // first test adding to an element that already has children...
        XmlBeanUtils.addChildElement( m_propsDoc, elemName );
        XmlObject[] childElems = XmlBeanUtils.getChildElements( m_propsDoc, elemName );
        assertEquals( 2, childElems.length );
        // now test adding to a childless element...
        EndpointReferenceDocument eprDoc = EndpointReferenceDocument.Factory.newInstance();
        EndpointReferenceType epr = eprDoc.addNewEndpointReference();
        ReferencePropertiesType refPropsType = epr.addNewReferenceProperties();
        XmlBeanUtils.addChildElement( refPropsType, elemName );
        childElems = XmlBeanUtils.getChildElements( refPropsType, elemName );
        assertEquals( 1, childElems.length );
    }

    /**
     * DOCUMENT_ME
     */
    public void testGetChildElements()
    {
        XmlObject[] childElems = XmlBeanUtils.getChildElements( m_propsDoc, SushiPropertyQNames.IKA );
        assertEquals( 1, childElems.length );
        assertTrue( childElems[0] instanceof XmlString );
    }

    /**
     * DOCUMENT_ME
     */
    public void testGetName()
    {
        assertEquals( SushiPropertyQNames.OPEN_SUSHI_PROPERTIES,
                XmlBeanUtils.getName( m_propsDoc ) );
        // TODO: use reflection to reenable the below assertions
        //assertEquals( SushiPropertyQNames.SUSHI_PROPERTIES,
        //              XmlBeanUtils.getName( m_propsDoc.getSushiProperties() ) );
        //assertEquals( SushiPropertyQNames.EBI,
        //              XmlBeanUtils.getName( m_propsDoc.getSushiProperties(  ).xgetEbi(  ) ) );
    }

    /**
     * DOCUMENT_ME
     */
    public void testRemoveChildElements()
    {
        XmlObject[] ebiChildElems = XmlBeanUtils.getChildElements( m_propsDoc, SushiPropertyQNames.EBI );
        assertEquals( 1, ebiChildElems.length );
        int totalChildElemsBefore = XmlBeanUtils.getChildElements( m_propsDoc, SushiPropertyQNames.EBI ).length;
        XmlBeanUtils.removeChildElements( m_propsDoc, SushiPropertyQNames.EBI );
        ebiChildElems = XmlBeanUtils.getChildElements( m_propsDoc, SushiPropertyQNames.EBI );
        assertEquals( 0, ebiChildElems.length );
        int totalChildElemsAfter = XmlBeanUtils.getChildElements( m_propsDoc, SushiPropertyQNames.EBI ).length;
        assertEquals( 1, totalChildElemsBefore - totalChildElemsAfter );
    }
}
TOP

Related Classes of org.apache.ws.util.XmlBeanUtilsTestCase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.