You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-dev@axis.apache.org by ru...@apache.org on 2001/05/04 02:20:45 UTC

cvs commit: xml-axis/java/test/encoding Data.java DataSer.java PackageTests.java TestSer.java

rubys       01/05/03 17:20:45

  Modified:    java     build.xml
  Added:       java/test/encoding Data.java DataSer.java PackageTests.java
                        TestSer.java
  Log:
  Convert Glen's sample into a unit test
  
  Revision  Changes    Path
  1.16      +41 -5     xml-axis/java/build.xml
  
  Index: build.xml
  ===================================================================
  RCS file: /home/cvs/xml-axis/java/build.xml,v
  retrieving revision 1.15
  retrieving revision 1.16
  diff -u -r1.15 -r1.16
  --- build.xml	2001/04/27 13:31:46	1.15
  +++ build.xml	2001/05/04 00:20:43	1.16
  @@ -38,7 +38,6 @@
   
   <project default="compile" basedir=".">
   
  -
     <!-- Give user a chance to override without editing this file
          (and without typing -D each time it compiles it) -->
     <property file="build.properties"/>
  @@ -75,6 +74,7 @@
     <path id="classpath">
       <pathelement location="${servlet.jar}"/>
       <pathelement location="${xerces.jar}"/>
  +    <pathelement location="${junit.jar}"/>
       <pathelement path="${java.class.path}"/>
     </path>
   
  @@ -90,6 +90,10 @@
         classname="javax.servlet.Servlet"
         classpathref="classpath"/>
   
  +    <available property="junit.present" 
  +      classname="junit.framework.TestCase"
  +      classpathref="classpath"/>
  +
       <echo message="--- Build environment for ${Name} ---" />
       <echo message="--- Flags (Note: If the {property name} is displayed, "/>
       <echo message="           then the component is not present)" />
  @@ -99,6 +103,7 @@
       <echo message=""/>
       <echo message="--- Optional Libraries ---" />
       <echo message="servlet.present=${servlet.present}" />
  +    <echo message="junit.present=${junit.present}" />
       <echo message=""/>
       <echo message="--- Property values" />
       <echo message="debug=${debug}" />
  @@ -140,14 +145,45 @@
     </target>
   
     <!-- =================================================================== -->
  -  <!-- Compiles the test tool(s)                                           -->
  +  <!-- Compiles the JUnit testcases -->
     <!-- =================================================================== -->
  -  <target name="testtools" depends="compile">
  -    <javac srcdir="." destdir="${build.dir}" debug="${debug}">
  +
  +  <path id="test-classpath">
  +    <!-- build.dir for test classes and build.dest for classes to test -->
  +    <pathelement location="${build.dir}" />
  +    <pathelement location="${build.dest}" />
  +    <pathelement path="${java.class.path}" />
  +  </path>
  +
  +  <target name="buildTest" if="junit.present" depends="compile">
  +    <echo message="junit package found ..."/>
  +
  +    <!-- Tests are packaged as test.*, so source dir is main dir -->
  +    <javac srcdir="${basedir}"
  +      destdir="${build.dir}">
         <include name="test/**/*.java" />
  +      <classpath refid="test-classpath" />
       </javac>
     </target>
   
  +
  +  <!-- =================================================================== -->
  +  <!-- Runs the JUnit testcases -->
  +  <!-- =================================================================== -->
  +  <target name="junit" if="junit.present" depends="buildTest">
  +    <junit printsummary="yes" haltonfailure="yes" fork="yes">
  +      <classpath refid="test-classpath" />
  +      <formatter type="plain" />
  +      <batchtest>
  +        <fileset dir="${build.dir}">
  +          <!-- Convention: each package that's being tested
  +                has its own test class collecting all the tests -->
  +          <include name="**/PackageTests.class" />
  +        </fileset>
  +      </batchtest>
  +    </junit>
  +  </target>
  +
     <!-- =================================================================== -->
     <!-- Creates the API documentation                                       -->
     <!-- =================================================================== -->
  @@ -169,7 +205,7 @@
     <!-- =================================================================== -->
     <!-- Creates the binary distribution                                     -->
     <!-- =================================================================== -->
  -  <target name="dist" depends="compile, javadocs, samples" >
  +  <target name="dist" depends="compile, javadocs, samples, junit" >
       <mkdir dir="${dist.dir}"/>
       <mkdir dir="${dist.dir}/docs"/>
       <mkdir dir="${dist.dir}/docs/apiDocs"/>
  
  
  
  1.1                  xml-axis/java/test/encoding/Data.java
  
  Index: Data.java
  ===================================================================
  package test.encoding;
  
  public class Data
  {
      public String stringMember;
      public Float floatMember;
  }
  
  
  
  1.1                  xml-axis/java/test/encoding/DataSer.java
  
  Index: DataSer.java
  ===================================================================
  package test.encoding;
  
  import org.apache.axis.encoding.*;
  
  import java.util.*;
  import java.io.*;
  import org.xml.sax.*;
  import org.apache.axis.utils.QName;
  
  public class DataSer extends DeserializerBase implements Serializer
  {
      public static final String STRINGMEMBER = "stringMember";
      public static final String FLOATMEMBER = "floatMember";
      
      public static class DataSerFactory implements DeserializerFactory {
          public DeserializerBase getDeserializer() {
              return new DataSer();
          }
      }
      public static DeserializerFactory getFactory()
      {
          return new DataSerFactory();
      }
      
      private Hashtable typesByMemberName = new Hashtable();  
      
      public DataSer()
      {
          typesByMemberName.put(STRINGMEMBER, SOAPTypeMappingRegistry.XSD_STRING);
          typesByMemberName.put(FLOATMEMBER, SOAPTypeMappingRegistry.XSD_FLOAT);
          value = new Data();
      }
      
      /** DESERIALIZER STUFF - event handlers
       */
      
      public void onStartChild(String namespace, String localName,
                               String qName, Attributes attributes)
          throws SAXException
      {
          QName typeQName = (QName)typesByMemberName.get(localName);
          if (typeQName == null)
              throw new SAXException("Invalid element in Data struct - " + localName);
          
          // These can come in either order.
          DeserializerBase dSer = context.getDeserializer(typeQName);
          
          if (dSer == null)
              throw new SAXException("No deserializer for a " + typeQName + "???");
          
          context.pushElementHandler(dSer);
      }
      
      public void onEndChild(String localName, DeserializerBase deserializer)
          throws SAXException
      {
          if (STRINGMEMBER.equals(localName)) {
              ((Data)value).stringMember = (String)deserializer.getValue();
          } else if (FLOATMEMBER.equals(localName)) {
              ((Data)value).floatMember = (Float)deserializer.getValue();
          } else {
              throw new SAXException("No such child - " + localName);
          }
      }
          
      /** SERIALIZER STUFF
       */
      
      public void serialize(QName name, Attributes attributes,
                            Object value, SerializationContext context)
          throws IOException
      {
          if (!(value instanceof Data))
              throw new IOException("Can't serialize a " + value.getClass().getName() + " with a DataSerializer.");
          Data data = (Data)value;
          
          context.startElement(name, attributes);
          context.serialize(new QName("", STRINGMEMBER), null, data.stringMember);
          context.serialize(new QName("", FLOATMEMBER), null, data.floatMember);
          context.endElement();
      }
  }
  
  
  
  1.1                  xml-axis/java/test/encoding/PackageTests.java
  
  Index: PackageTests.java
  ===================================================================
  package test.encoding;
  
  import junit.framework.TestCase;
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  /**
   * soapenc's PackageTests tests multiple floating point
   * deserialization classes (float and double, primitive and object).
   */
  public class PackageTests extends TestCase
  {
      public PackageTests(String name)
      {
          super(name);
      }
  
      public static Test suite() throws Exception
      {
          TestSuite suite = new TestSuite();
  
          suite.addTestSuite(TestSer.class);
  
          return suite;
      }
  }
  
  
  
  1.1                  xml-axis/java/test/encoding/TestSer.java
  
  Index: TestSer.java
  ===================================================================
  package test.encoding;
  
  import org.apache.axis.message.*;
  import org.apache.axis.encoding.*;
  import org.apache.axis.utils.QName;
  import org.xml.sax.InputSource;
  import org.apache.xerces.parsers.SAXParser;
  import java.io.*;
  import java.util.*;
  
  import junit.framework.TestCase;
  
  /** Little serialization test with a struct.
   */
  public class TestSer extends TestCase {
  
      public static final String myNS = "urn:myNS";
      
      public TestSer(String name) {
          super(name);
      }
  
      public void testData() throws Exception {
          SOAPEnvelope msg = new SOAPEnvelope();
          RPCParam arg1 = new RPCParam("urn:myNamespace", "testParam", "this is a string");
          
          Data data = new Data();
          data.stringMember = "String member";
          data.floatMember = new Float("4.54");
          
          RPCParam arg2 = new RPCParam("", "struct", data);
          RPCElement body = new RPCElement("urn:myNamespace", "method1", new Object[]{ arg1, arg2 });
          msg.addBodyElement(body);
          
          Writer stringWriter = new StringWriter();
          SerializationContext context = new SerializationContext(stringWriter);
          
          TypeMappingRegistry reg = context.getTypeMappingRegistry();
          QName dataQName = new QName("typeNS", "Data");
          
          reg.addSerializer(Data.class, dataQName, new DataSer());
  
          msg.output(context);
          
          String msgString = stringWriter.toString();
          
          StringReader reader = new StringReader(msgString);
          
          SAXAdapter adapter = new SAXAdapter(new SAXParser(), new InputSource(reader));
          reg = adapter.getContext().getTypeMappingRegistry();
          reg.addDeserializerFactory(dataQName, DataSer.getFactory());
          
          SOAPEnvelope env = adapter.getEnvelope();
          RPCElement rpcElem = (RPCElement)env.getFirstBody();
          RPCParam struct = rpcElem.getParam("struct");
          assertNotNull("No <struct> param", struct);
          
          Data val = (Data)struct.getValue();
          assertNotNull("No value for struct param", val);
          
          assertEquals(data.stringMember, val.stringMember);
          assertEquals(data.floatMember.floatValue(), 
                       val.floatMember.floatValue(), 0.00001F);
      }
  }