tomj        02/02/25 07:24:15

  Modified:    java/test/encoding PackageTests.java
  Added:       java/test/encoding TestAttributes.java AttributeBean.java
  Log:
  Add a test case to test attribute serialization.
  
  Revision  Changes    Path
  1.13      +1 -0      xml-axis/java/test/encoding/PackageTests.java
  
  Index: PackageTests.java
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/test/encoding/PackageTests.java,v
  retrieving revision 1.12
  retrieving revision 1.13
  diff -u -r1.12 -r1.13
  --- PackageTests.java 1 Dec 2001 03:39:49 -0000       1.12
  +++ PackageTests.java 25 Feb 2002 15:24:14 -0000      1.13
  @@ -31,6 +31,7 @@
           suite.addTestSuite(TestArrayListConversions.class);
           suite.addTestSuite(TestXsiType.class);
           suite.addTestSuite(TestOutputter.class);
  +        suite.addTestSuite(TestAttributes.class);
   
           return suite;
       }
  
  
  
  1.1                  xml-axis/java/test/encoding/TestAttributes.java
  
  Index: TestAttributes.java
  ===================================================================
  package test.encoding;
  
  import junit.framework.TestCase;
  import org.apache.axis.MessageContext;
  import org.apache.axis.encoding.DeserializationContext;
  import org.apache.axis.encoding.DeserializationContextImpl;
  import org.apache.axis.encoding.SerializationContext;
  import org.apache.axis.encoding.SerializationContextImpl;
  import org.apache.axis.encoding.TypeMappingRegistry;
  import org.apache.axis.encoding.TypeMapping;
  import org.apache.axis.encoding.ser.BeanSerializerFactory;
  import org.apache.axis.encoding.ser.BeanDeserializerFactory;
  import org.apache.axis.Constants;
  import org.apache.axis.utils.XMLUtils;
  import org.apache.axis.client.Call;
  import org.apache.axis.message.RPCElement;
  import org.apache.axis.message.RPCParam;
  import org.apache.axis.message.SOAPEnvelope;
  import org.apache.axis.server.AxisServer;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.xml.sax.InputSource;
  
  import javax.xml.rpc.namespace.QName;
  import java.io.StringReader;
  import java.io.StringWriter;
  import java.io.Writer;
  
  import AttributeBean;
  
  /**
   *  Test the serialization of a bean with attributes
   * 
   * @author Tom Jordahl ([EMAIL PROTECTED])
   */
  public class TestAttributes extends TestCase {
      static Log log =
              LogFactory.getLog(TestAttributes.class.getName());
  
      public static final String myNS = "urn:myNS";
      
      public static void main(String [] args) throws Exception
      {
          TestAttributes tester = new TestAttributes("TestAttributes");
          tester.testBean();
      }
      
      public TestAttributes(String name) {
          super(name);
      }
      
      static final String expectedXML =
              "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
              "<SOAP-ENV:Envelope 
xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"; 
xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"; 
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\";>\n" +
              " <SOAP-ENV:Body>\n" +
              "  <method1 xmlns=\"urn:myNamespace\">\n" +
              "   <struct name=\"James Bond\" male=\"true\">\n" +
              "    <ID>1.15</ID>\n" +
              "    <age>35</age>\n" +
              "   </struct>\n" +
              "  </method1>\n" +
              " </SOAP-ENV:Body>\n" +
              "</SOAP-ENV:Envelope>";
      
      public void testBean () throws Exception {
          MessageContext msgContext = new MessageContext(new AxisServer());
          SOAPEnvelope msg = new SOAPEnvelope();
          
          // set no encoding (use=literal)
          msgContext.setEncodingStyle(null);
          // Don't serialize xsi:type attributes
          msgContext.setProperty(Call.SEND_TYPE_ATTR, "false" );
  
          
          // Create bean with data
          AttributeBean bean = new AttributeBean();
          bean.setAge(35);
          bean.setID(1.15F);
          bean.setMale(true);
          bean.setName("James Bond");
          
          RPCParam arg = new RPCParam("", "struct", bean);
          RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object[]{ 
arg });
          msg.addBodyElement(body);
          body.setEncodingStyle(null);
          
          Writer stringWriter = new StringWriter();
          SerializationContext context = new SerializationContextImpl(stringWriter, 
msgContext);
          context.setDoMultiRefs(false);  // no multirefs
          context.setPretty(false);
          
          // Create a TypeMapping and register the Bean serializer/deserializer
          TypeMappingRegistry reg = context.getTypeMappingRegistry();
          TypeMapping tm = (TypeMapping) reg.createTypeMapping();
          // The "" namespace is literal (no encoding).
          tm.setSupportedNamespaces(new String[] {""});
          reg.register("", tm);
          
          QName beanQName = new QName("typeNS", "TheBean");
          tm.register(AttributeBean.class, 
                      beanQName, 
                      new BeanSerializerFactory(AttributeBean.class, beanQName), 
                      new BeanDeserializerFactory(AttributeBean.class, beanQName));
  
          // Serialize the bean in to XML
          msg.output(context);
          // Get the XML as a string
          String msgString = stringWriter.toString();
          // verify results
          assertEquals("Serialized bean and expected results don't match",
                        expectedXML, msgString);
          
          log.debug("---");
          log.debug(msgString);
          log.debug("---");
  /*        
          TODO: This part of the test is wrong
          
          // Now feed the XML in to the deserializer
          StringReader reader = new StringReader(msgString);
          DeserializationContext dser = new DeserializationContextImpl(
              new InputSource(reader), msgContext, org.apache.axis.Message.REQUEST);
  
          // deserialize it
          dser.parse();
  
          // get the results
          SOAPEnvelope env = dser.getEnvelope();
          RPCElement rpcElem = (RPCElement)env.getFirstBody();
          RPCParam struct = rpcElem.getParam("struct");
          assertNotNull("No <struct> param", struct);
          
          Object obj = struct.getValue();
          System.out.println(obj.toString());
          AttributeBean val = (AttributeBean)struct.getValue();
          assertNotNull("No value for struct param", val);
          
          assertEquals("Bean and Val Age members are not equal", bean.getAge(), 
val.getAge());
          assertEquals("Bean and Val ID members are not equal",bean.getID(), 
bean.getID(), 1.15F);
          assertEquals("Bean and Val boolean attributes are not equal",bean.getMale(), 
bean.getMale());
          assertEquals("Bean and Val name attributes are not equal",bean.getName(), 
bean.getName());
  */
      }
  
  }
  
  
  
  1.1                  xml-axis/java/test/encoding/AttributeBean.java
  
  Index: AttributeBean.java
  ===================================================================
  /*
  * The Apache Software License, Version 1.1
  *
  *
  * Copyright (c) 2001 The Apache Software Foundation.  All rights
  * reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
  *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
  *    the documentation and/or other materials provided with the
  *    distribution.
  *
  * 3. The end-user documentation included with the redistribution,
  *    if any, must include the following acknowledgment:
  *       "This product includes software developed by the
  *        Apache Software Foundation (http://www.apache.org/)."
  *    Alternately, this acknowledgment may appear in the software itself,
  *    if and wherever such third-party acknowledgments normally appear.
  *
  * 4. The names "Axis" and "Apache Software Foundation" must
  *    not be used to endorse or promote products derived from this
  *    software without prior written permission. For written
  *    permission, please contact [EMAIL PROTECTED]
  *
  * 5. Products derived from this software may not be called "Apache",
  *    nor may "Apache" appear in their name, without prior written
  *    permission of the Apache Software Foundation.
  *
  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  * SUCH DAMAGE.
  * ====================================================================
  *
  * This software consists of voluntary contributions made by many
  * individuals on behalf of the Apache Software Foundation.  For more
  * information on the Apache Software Foundation, please see
  * <http://www.apache.org/>.
  */
  
  /**
   * Simple Java Bean with fields that should be serialized as attributes
   */ 
  public class AttributeBean implements java.io.Serializable {
      private int age;
      private float iD;
      private java.lang.String name;  // attribute
      private boolean male;           // attribute
      
      public AttributeBean() {
      }
      
      public AttributeBean(int age, float iD, java.lang.String name, boolean male) {
          this.age = age;
          this.iD = iD;
          this.name = name;
          this.male = male;
      }
      
      public int getAge() {
          return age;
      }
      
      public void setAge(int age) {
          this.age = age;
      }
      
      public float getID() {
          return iD;
      }
      
      public void setID(float iD) {
          this.iD = iD;
      }
      
      public java.lang.String getName() {
          return name;
      }
      
      public void setName(java.lang.String name) {
          this.name = name;
      }
      
      public boolean getMale() {
          return male;
      }
      
      public void setMale(boolean male) {
          this.male = male;
      }
      
      // List of fields that are XML attributes
      public static java.lang.String[] _attrs = new String[] {
          "name", 
          "male", 
      };
      
      /**
       * Return list of bean field names that are attributes
       */
      public static java.lang.String[] getAttributeElements() {
          return _attrs;
      }
      
  }
  
  
  


Reply via email to