You are viewing a plain text version of this content. The canonical link for it is here.
Posted to soap-dev@xml.apache.org by "STEARNS,JIM (HP-FtCollins,ex1)" <ji...@hp.com> on 2001/03/30 19:48:31 UTC

[PATCH] Deserialization of Floating Point Special Values

Apache 2.1 does not correctly deserialize floating point special values
for infinity and Not-a-Number.  This patch attempts to remedy that problem.

When deserializing a floating point string, the current deserialization
code reasonably and simply calls the class constructor with a string 
argument, as in "new Float(fpValAsString)". 

The problem is that Java falls down on deserializing floating point
special values.  Even for values it itself creates, Java generates
a NumberFormatException when attempting to 'deserialize' 
a floating point special value.  For example:

    String fpSpecValAsStr = new Float(Float.POSITIVE_INFINITY).toString();
    new Float(fpSpecValAsStr);  // throws NFE

To demonstrate the problem, JUnit tests for all four floating point
deserialization classes are attached, including changes to build.xml to
add targets for building and running the JUnit tests.

The proposed source changes can be found in the attached
PATCH-FPSpecValDeserSrc.zip.  The diff for the four changed
deserialization files (one line each) is also attached.

The following steps are suggested:

1.  Extract the PATCH-FPSpecValDeserTest.zip.  This adds the
    test classes and adds targets in build.xml to compile and
    run the JUnit tests.

2.  From the directory containing build.xml, issue "ant junit",
    which will build and run the JUnit tests against the current source 
    base.  24 of the 56 tests should fail.

3.  Extract the PATCH-FPSpecValDeserSrc.zip and either "ant clean"
    or touch the dates of the files just extracted from the zip.

4.  Ant junit again.  All 56 tests should pass.

Note that this fix covers deserialization only.  There is a minor issue
with serialization: Java toString() of Float.POSITIVE_INFINITY generates
"Infinity", whereas the XML Schema Datatypes specification expects "INF"
(See http://www.w3.org/TR/xmlschema-2/ at Section 3.2.4.1).  The same is
also the case for NEGATIVE_INFINITY.

As this is my first Apache SOAP submission, comments on format as well
as content as would be most welcome.

Thanks,

Jim Stearns
Hewlett-Packard

Index: build.xml
===================================================================
RCS file: /home/cvspublic/xml-soap/java/build.xml,v
retrieving revision 1.25
diff -r1.25 build.xml
23a24
>    JUnit Test Framework from http://www.junit.org
50a52,57
>   <!-- Moved out of a target so they can be referenced by peer of
>       target (such as path test-classpath).
>   -->
>   <property name="build.dir" value="./build"/>
>   <property name="build.dest" value="./build/classes"/>
> 
68,70d74
< 
<     <property name="build.dir" value="./build"/>
<     <property name="build.dest" value="./build/classes"/>
94a99
>     <available property="junit.present"
classname="junit.framework.TestCase" />
201a207,248
>   </target>
> 
> 
>   <!-- ===================================================================
-->
>   <!-- Compiles the JUnit testcases -->
>   <!-- ===================================================================
-->
> 
>   <path id="test-classpath">
>     <!-- build.dir for test classes and build.dest for classes to test -->
>     <pathelement location="${build.dir}" />
>     <pathelement location="${build.dest}" />
>   </path>
> 
>   <target name="buildTest" if="junit.present"
>         depends="init, prepare, 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="no">
>       <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>
Index: src/org/apache/soap/encoding/soapenc/DoubleDeserializer.java
===================================================================
RCS file:
/home/cvspublic/xml-soap/java/src/org/apache/soap/encoding/soapenc/DoubleDes
erializer.java,v
retrieving revision 1.1
diff -r1.1 DoubleDeserializer.java
70a71
>  * Covers deserialization of floating special values INF, -INF, NaN.
81,82c82
<     
<     return new Bean(double.class, new Double(value));
---
>     return new Bean(double.class, FPDeserUtil.newDouble(value));
Index: src/org/apache/soap/encoding/soapenc/DoubleObjectDeserializer.java
===================================================================
RCS file:
/home/cvspublic/xml-soap/java/src/org/apache/soap/encoding/soapenc/DoubleObj
ectDeserializer.java,v
retrieving revision 1.1
diff -r1.1 DoubleObjectDeserializer.java
70a71
>  * Covers deserialization of floating special values INF, -INF, NaN.
81,82c82
<     
<     return new Bean(Double.class, new Double(value));
---
>     return new Bean(Double.class, FPDeserUtil.newDouble(value));
Index: src/org/apache/soap/encoding/soapenc/FloatDeserializer.java
===================================================================
RCS file:
/home/cvspublic/xml-soap/java/src/org/apache/soap/encoding/soapenc/FloatDese
rializer.java,v
retrieving revision 1.1
diff -r1.1 FloatDeserializer.java
70a71
>  * Covers deserialization of floating special values INF, -INF, NaN.
81,82c82
<     
<     return new Bean(float.class, new Float(value));
---
>     return new Bean(float.class, FPDeserUtil.newFloat(value));
Index: src/org/apache/soap/encoding/soapenc/FloatObjectDeserializer.java
===================================================================
RCS file:
/home/cvspublic/xml-soap/java/src/org/apache/soap/encoding/soapenc/FloatObje
ctDeserializer.java,v
retrieving revision 1.1
diff -r1.1 FloatObjectDeserializer.java
70a71
>  * Covers deserialization of floating special values INF, -INF, NaN.
81,82c82
<     
<     return new Bean(Float.class, new Float(value));
---
>     return new Bean(Float.class, FPDeserUtil.newFloat(value));