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.XmlCursor;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;
import org.apache.xmlbeans.XmlString;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSBaseFaults12Draft01.BaseFaultType;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceLifetime12Draft01.CurrentTimeDocument;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.GetResourcePropertyDocument;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.QueryExpressionDocument;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.QueryExpressionType;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.ResourcePropertyValueChangeNotificationDocument;
import org.oasisOpen.docs.wsrf.x2004.x06.wsrfWSResourceProperties12Draft01.ResourcePropertyValueChangeNotificationType;
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;
import java.util.Calendar;

/**
* 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
   {
      ResourcePropertyValueChangeNotificationType          resourcePropertyValueChangeNotificationType =
         ResourcePropertyValueChangeNotificationType.Factory.newInstance(  );
      ResourcePropertyValueChangeNotificationType.OldValue oldValue =
         resourcePropertyValueChangeNotificationType.addNewOldValue(  );
      XmlBeanUtils.addChildElement( oldValue,
                                    XmlObject.Factory.parse( "<fo>fum</fo>" ) );

      // add a second one, so we can make sure they get added in the right order...
      XmlBeanUtils.addChildElement( oldValue,
                                    XmlObject.Factory.parse( "<fo>nub</fo>" ) );
      XmlObject[] childElems = XmlBeanUtils.getChildElements( oldValue,
                                                              new QName( "fo" ) );
      assertEquals( 2, childElems.length );
      XmlObject[] grandchildElems = XmlBeanUtils.getChildElements( childElems[0],
                                                                   new QName( "fo" ) );
      assertEquals( 0, grandchildElems.length ); // this tests the nefarious "redundant grandchild" cursor bug
      assertEquals( "fum",
                    XmlBeanUtils.getValue( childElems[0] ) );
      assertEquals( "nub",
                    XmlBeanUtils.getValue( childElems[1] ) );

      resourcePropertyValueChangeNotificationType =
         ResourcePropertyValueChangeNotificationType.Factory.newInstance(  );
      oldValue = resourcePropertyValueChangeNotificationType.addNewOldValue(  );
      CurrentTimeDocument currentTimeDocument = CurrentTimeDocument.Factory.newInstance(  );
      currentTimeDocument.setCurrentTime( Calendar.getInstance(  ) );
      XmlObject newXBean = XmlBeanUtils.copyXmlBean( currentTimeDocument.xgetCurrentTime(  ) );
      XmlBeanUtils.addChildElement( oldValue, newXBean );
      childElems =
         XmlBeanUtils.getChildElements( oldValue,
                                        new QName( "http://docs.oasis-open.org/wsrf/2004/06/wsrf-WS-ResourceLifetime-1.2-draft-01.xsd",
                                                   "CurrentTime" ) );
      assertEquals( 1, childElems.length );
      XmlObject currentTimeElem = childElems[0];
      childElems =
         XmlBeanUtils.getChildElements( currentTimeElem,
                                        new QName( "http://docs.oasis-open.org/wsrf/2004/06/wsrf-WS-ResourceLifetime-1.2-draft-01.xsd",
                                                   "CurrentTime" ) );
      assertEquals( 0, childElems.length );

      QName     elemName  = SushiPropertyQNames.IKA;
      String    elemValue = "whatever";
      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 );
      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 );
   }

   /**
    * Test for {@link XmlBeanUtils#copyXmlBean(XmlObject)}.
    */
   public void testCopyXmlBean(  )
   {
      // TODO: write this test
   }

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

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

   /**
    * DOCUMENT_ME
    */
   public void testSet(  )
   {
      XmlString xString1 = XmlString.Factory.newInstance(  );
      xString1.setStringValue( "one" );
      XmlString xString2 = XmlString.Factory.newInstance(  );
      xString2.setStringValue( "two" );
      XmlCursor xmlCursor = xString1.newCursor(  );
      xmlCursor.toPrevToken(  );
      XmlObject object = xmlCursor.getObject(  );
      xString1 = (XmlString) object.set( xString2 );
      System.out.println( xString1 );

      ResourcePropertyValueChangeNotificationDocument      resourcePropertyValueChangeNotificationDocument =
         ResourcePropertyValueChangeNotificationDocument.Factory.newInstance(  );
      ResourcePropertyValueChangeNotificationType          resourcePropertyValueChangeNotificationType =
         resourcePropertyValueChangeNotificationDocument.addNewResourcePropertyValueChangeNotification(  );
      ResourcePropertyValueChangeNotificationType.OldValue oldValue =
         resourcePropertyValueChangeNotificationType.addNewOldValue(  );
      XmlCursor                                            parentCursor = oldValue.newCursor(  );
      if ( parentCursor.toLastChild(  ) )
      {
         parentCursor.toEndToken(  );
         parentCursor.toNextToken(  );
      }

      parentCursor.toEndToken(  );
      parentCursor.insertElement( new QName( "http://docs.oasis-open.org/wsrf/2004/06/wsrf-WS-BaseFaults-1.2-draft-01.xsd",
                                             "BaseFault" ) );
      System.out.println( "BEFORE:\n" + oldValue.xmlText( new XmlOptions(  ).setSaveOuter(  ) ) );
      parentCursor.toPrevSibling(  );
      XmlObject childXBean = parentCursor.getObject(  );
      parentCursor.dispose(  );

      //BaseFaultDocument baseFaultDocument = BaseFaultDocument.Factory.newInstance( );
      //BaseFaultType baseFaultType = baseFaultDocument.addNewBaseFault();
      BaseFaultType baseFaultType = BaseFaultType.Factory.newInstance(  );
      baseFaultType.setTimestamp( Calendar.getInstance(  ) );
      childXBean.set( baseFaultType );
      System.out.println( "AFTER:\n" + oldValue.xmlText( new XmlOptions(  ).setSaveOuter(  ) ) );

      ResourcePropertyValueChangeNotificationDocument      resourcePropertyValueChangeNotificationDocument1 =
         ResourcePropertyValueChangeNotificationDocument.Factory.newInstance(  );
      ResourcePropertyValueChangeNotificationType          resourcePropertyValueChangeNotificationType1 =
         resourcePropertyValueChangeNotificationDocument.addNewResourcePropertyValueChangeNotification(  );
      ResourcePropertyValueChangeNotificationType.OldValue oldValue1 =
         resourcePropertyValueChangeNotificationType1.addNewOldValue(  );
      oldValue1.newCursor(  ).setTextValue( "one" );
      System.out.println( resourcePropertyValueChangeNotificationDocument );

      ResourcePropertyValueChangeNotificationType          resourcePropertyValueChangeNotificationType2 =
         ResourcePropertyValueChangeNotificationType.Factory.newInstance(  );
      ResourcePropertyValueChangeNotificationType.OldValue oldValue2 =
         resourcePropertyValueChangeNotificationType2.addNewOldValue(  );
      oldValue2.newCursor(  ).setTextValue( "two" );
      oldValue1 = (ResourcePropertyValueChangeNotificationType.OldValue) oldValue1.set( oldValue2 );
      System.out.println( resourcePropertyValueChangeNotificationDocument1 );

      GetResourcePropertyDocument getResourcePropertyDoc1 = GetResourcePropertyDocument.Factory.newInstance(  );
      getResourcePropertyDoc1.setGetResourceProperty( new QName( "one" ) );
      GetResourcePropertyDocument getResourcePropertyDoc2 = GetResourcePropertyDocument.Factory.newInstance(  );
      getResourcePropertyDoc2.setGetResourceProperty( new QName( "two" ) );
      getResourcePropertyDoc1 =
         (GetResourcePropertyDocument) getResourcePropertyDoc1.set( getResourcePropertyDoc2 );
      System.out.println( getResourcePropertyDoc1 );
   }

   /**
    * Test for {@link XmlBeanUtils#setValueAsQName(XmlObject, QName)}.
    */
   public void testSetValueAsQName(  )
   {
      QueryExpressionDocument queryExpressionDocument = QueryExpressionDocument.Factory.newInstance(  );
      QueryExpressionType     queryExpressionType = queryExpressionDocument.addNewQueryExpression(  );
      XmlBeanUtils.setValueAsQName( queryExpressionType, SushiPropertyQNames.AKAGI );
      XmlCursor xCursor = queryExpressionType.newCursor(  );
      String    prefix = xCursor.prefixForNamespace( SushiPropertyQNames.AKAGI.getNamespaceURI(  ) );
      String    value  = xCursor.getTextValue(  );
      assertEquals( prefix + ":" + SushiPropertyQNames.AKAGI.getLocalPart(  ), value );
   }
}
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.