[prev in list] [next in list] [prev in thread] [next in thread]
List: soap-dev
Subject: cvs commit: xml-soap/java/test/encoding/soapenc DoubleDeserializerTest.java DoubleObjectDeserializer
From: rubys () apache ! org
Date: 2001-03-30 20:30:44
[Download RAW message or body]
rubys 01/03/30 12:30:44
Modified: java build.xml
java/src/org/apache/soap/encoding/soapenc
DoubleDeserializer.java
DoubleObjectDeserializer.java
FloatDeserializer.java FloatObjectDeserializer.java
Added: java/src/org/apache/soap/encoding/soapenc FPDeserUtil.java
java/test/encoding/soapenc DoubleDeserializerTest.java
DoubleObjectDeserializerTest.java
FPDeserializerTest.java FloatDeserializerTest.java
FloatObjectDeserializerTest.java PackageTests.java
Log:
Deserialization of Floating Point Special Values
Submitted by: Jim Stearns <jim_stearns@hp.com>
Revision Changes Path
1.26 +51 -4 xml-soap/java/build.xml
Index: build.xml
===================================================================
RCS file: /home/cvs/xml-soap/java/build.xml,v
retrieving revision 1.25
retrieving revision 1.26
diff -u -r1.25 -r1.26
--- build.xml 2001/03/19 17:16:13 1.25
+++ build.xml 2001/03/30 20:30:40 1.26
@@ -21,6 +21,7 @@
http://oss.software.ibm.com/developerworks/opensource/pop3/index.html
http://oss.software.ibm.com/developerworks/opensource/smtp/index.html
http://java.sun.com/products/jsse/
+ JUnit Test Framework from http://www.junit.org
Build Instructions:
To build, run
@@ -48,6 +49,12 @@
<project default="compile" basedir=".">
+ <!-- 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"/>
+
<target name="init">
<property name="name" value="soap"/>
<property name="Name" value="Soap"/>
@@ -65,9 +72,6 @@
<property name="packages" value="org.*"/>
<property name="build.file" value="build.xml"/>
-
- <property name="build.dir" value="./build"/>
- <property name="build.dest" value="./build/classes"/>
<property name="build.lib" value="./build/lib"/>
<property name="build.samples" value="./build/samples"/>
<property name="build.javadocs" value="./build/javadocs"/>
@@ -92,6 +96,7 @@
classname="com.ibm.network.mail.smtp.protocol.CoreProtocolBean" />
<available property="ejb.present" classname="javax.ejb.SessionBean" />
<available property="ssl.present" classname="javax.net.ssl.SSLSocket" />
+ <available property="junit.present" classname="junit.framework.TestCase" />
<uptodate property="javac.notRequired"
@@ -201,6 +206,48 @@
</copy>
</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="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>
+
<!-- =================================================================== -->
<!-- Compiles the MS COM Pluggable Provider CPP code requires MS VS 6.0 -->
<!-- =================================================================== -->
@@ -238,7 +285,7 @@
<!-- =================================================================== -->
<!-- Creates the binary distribution -->
<!-- =================================================================== -->
- <target name="dist" depends="compile, javadocsIfRequired, samples">
+ <target name="dist" depends="compile, junit, javadocsIfRequired, samples">
<mkdir dir="${dist.dir}"/>
<mkdir dir="${dist.dir}/docs"/>
<mkdir dir="${dist.dir}/docs/apiDocs"/>
1.2 +2 -2 xml-soap/java/src/org/apache/soap/encoding/soapenc/DoubleDeserializer.java
Index: DoubleDeserializer.java
===================================================================
RCS file: /home/cvs/xml-soap/java/src/org/apache/soap/encoding/soapenc/DoubleDeserializer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DoubleDeserializer.java 2001/01/09 05:35:26 1.1
+++ DoubleDeserializer.java 2001/03/30 20:30:41 1.2
@@ -68,6 +68,7 @@
* This deserializer can be used to deserialize something into a double.
* Its needed when defining mappings for base types via the deployment
* descriptor (or equivalent on the client-side).
+ * Covers deserialization of floating special values INF, -INF, NaN.
*
* @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
*/
@@ -78,7 +79,6 @@
throws IllegalArgumentException {
Element root = (Element)src;
String value = DOMUtils.getChildCharacterData(root);
-
- return new Bean(double.class, new Double(value));
+ return new Bean(double.class, FPDeserUtil.newDouble(value));
}
}
1.2 +2 -2 xml-soap/java/src/org/apache/soap/encoding/soapenc/DoubleObjectDeserializer.java
Index: DoubleObjectDeserializer.java
===================================================================
RCS file: /home/cvs/xml-soap/java/src/org/apache/soap/encoding/soapenc/DoubleObjectDeserializer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- DoubleObjectDeserializer.java 2001/01/09 05:35:26 1.1
+++ DoubleObjectDeserializer.java 2001/03/30 20:30:41 1.2
@@ -68,6 +68,7 @@
* This deserializer can be used to deserialize something into a Double.
* Its needed when defining mappings for base types via the deployment
* descriptor (or equivalent on the client-side).
+ * Covers deserialization of floating special values INF, -INF, NaN.
*
* @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
*/
@@ -78,7 +79,6 @@
throws IllegalArgumentException {
Element root = (Element)src;
String value = DOMUtils.getChildCharacterData(root);
-
- return new Bean(Double.class, new Double(value));
+ return new Bean(Double.class, FPDeserUtil.newDouble(value));
}
}
1.2 +2 -2 xml-soap/java/src/org/apache/soap/encoding/soapenc/FloatDeserializer.java
Index: FloatDeserializer.java
===================================================================
RCS file: /home/cvs/xml-soap/java/src/org/apache/soap/encoding/soapenc/FloatDeserializer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FloatDeserializer.java 2001/01/09 05:35:26 1.1
+++ FloatDeserializer.java 2001/03/30 20:30:42 1.2
@@ -68,6 +68,7 @@
* This deserializer can be used to deserialize something into a float.
* Its needed when defining mappings for base types via the deployment
* descriptor (or equivalent on the client-side).
+ * Covers deserialization of floating special values INF, -INF, NaN.
*
* @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
*/
@@ -78,7 +79,6 @@
throws IllegalArgumentException {
Element root = (Element)src;
String value = DOMUtils.getChildCharacterData(root);
-
- return new Bean(float.class, new Float(value));
+ return new Bean(float.class, FPDeserUtil.newFloat(value));
}
}
1.2 +2 -2 xml-soap/java/src/org/apache/soap/encoding/soapenc/FloatObjectDeserializer.java
Index: FloatObjectDeserializer.java
===================================================================
RCS file: /home/cvs/xml-soap/java/src/org/apache/soap/encoding/soapenc/FloatObjectDeserializer.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- FloatObjectDeserializer.java 2001/01/09 05:35:26 1.1
+++ FloatObjectDeserializer.java 2001/03/30 20:30:42 1.2
@@ -68,6 +68,7 @@
* This deserializer can be used to deserialize something into a Float.
* Its needed when defining mappings for base types via the deployment
* descriptor (or equivalent on the client-side).
+ * Covers deserialization of floating special values INF, -INF, NaN.
*
* @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
*/
@@ -78,7 +79,6 @@
throws IllegalArgumentException {
Element root = (Element)src;
String value = DOMUtils.getChildCharacterData(root);
-
- return new Bean(Float.class, new Float(value));
+ return new Bean(Float.class, FPDeserUtil.newFloat(value));
}
}
1.1 xml-soap/java/src/org/apache/soap/encoding/soapenc/FPDeserUtil.java
Index: FPDeserUtil.java
===================================================================
/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2000 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 "SOAP" 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 apache@apache.org.
*
* 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 and was
* originally based on software copyright (c) 2000, International
* Business Machines, Inc., http://www.apache.org. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*/
package org.apache.soap.encoding.soapenc;
/**
* FPDeserUtil: Utilities to help with deserialization of
* floating point special values.
*
* Supplements Java.lang deserialization of float, Float, double, and
* Double by handling the special values INF, -INF, and NaN, something
* that Java doesn't do (i.e. new Float("INF") will cause a
* NumberFormatException).
*
* These special values specify infinity, negative infinity,
* and Not-a-Number in the XML Schema - Datatypes specification
* for the primitive datatypes "float" (Section 3.2.4.1) and
* "double" (Section 3.2.5.1).
*
* Also accepts "Infinity" for "INF" and "-Infinity" for "-INF"
* (case-insensitive) because that's what Java generates for
* toString() of a Float or Double.
*
* @author Jim Stearns (Jim_Stearns@hp.com)
* @see XML Schema Part 2: Datatypes (W3C PR 16 March 2001)
* (http://www.w3.org/TR/xmlschema-2/)
*/
public class FPDeserUtil
{
// Static methods only, so disable constructor
private FPDeserUtil()
{
}
// Deserialize INF, -INF, and NaN, too.
public static Float newFloat(String value)
{
Float res = null;
try {
res = new Float(value);
} catch (NumberFormatException e) {
if (value.equals("INF")
|| value.toLowerCase().equals("infinity")) {
return new Float(Float.POSITIVE_INFINITY);
} else if (value.equals("-INF")
|| value.toLowerCase().equals("-infinity")) {
return new Float(Float.NEGATIVE_INFINITY);
} else if (value.equals("NaN")) {
return new Float(Float.NaN);
}
throw e;
}
return res;
}
public static Double newDouble(String value)
{
Double res = null;
try {
res = new Double(value);
} catch (NumberFormatException e) {
if (value.equals("INF")
|| value.toLowerCase().equals("infinity")) {
return new Double(Double.POSITIVE_INFINITY);
} else if (value.equals("-INF")
|| value.toLowerCase().equals("-infinity")) {
return new Double(Double.NEGATIVE_INFINITY);
} else if (value.equals("NaN")) {
return new Double(Double.NaN);
}
throw e;
}
return res;
}
}
1.1 xml-soap/java/test/encoding/soapenc/DoubleDeserializerTest.java
Index: DoubleDeserializerTest.java
===================================================================
package test.encoding.soapenc;
/**
* Actual tests are in superclass. This class allows tests
* of double deserialization.
*
* @author Jim Stearns (Jim_Stearns@hp.com)
*/
public class DoubleDeserializerTest extends FPDeserializerTest
{
public DoubleDeserializerTest(String name)
throws ClassNotFoundException, IllegalAccessException,
InstantiationException
{
super(name, "org.apache.soap.encoding.soapenc.DoubleDeserializer");
}
}
1.1 xml-soap/java/test/encoding/soapenc/DoubleObjectDeserializerTest.java
Index: DoubleObjectDeserializerTest.java
===================================================================
package test.encoding.soapenc;
/**
* Actual tests are in superclass. This class allows tests
* of Double deserialization.
*
* @author Jim Stearns (Jim_Stearns@hp.com)
*/
public class DoubleObjectDeserializerTest extends FPDeserializerTest
{
public DoubleObjectDeserializerTest(String name)
throws ClassNotFoundException, IllegalAccessException,
InstantiationException
{
super(name, "org.apache.soap.encoding.soapenc.DoubleObjectDeserializer");
}
}
1.1 xml-soap/java/test/encoding/soapenc/FPDeserializerTest.java
Index: FPDeserializerTest.java
===================================================================
package test.encoding.soapenc;
import javax.xml.parsers.*; // JAXP interfaces
import org.w3c.dom.*;
import org.apache.soap.util.Bean;
import org.apache.soap.util.xml.Deserializer;
import java.io.ByteArrayInputStream;
import junit.framework.TestCase;
/**
* This floating point deserializer test class serves
* as a superclass for 4 test classes - one each for
* float, Float, double, and Double. This allows these
* tests to be re-used for each type of floating point
* deserialization, with the only difference being
* the particular type of floating point object to test.
*
* @author Jim Stearns (Jim_Stearns@hp.com)
*/
public class FPDeserializerTest extends TestCase
{
// For the assert comparison of two floating point values.
private static final double okDblDelta = 0.000001d;
private static final float okFltDelta = 0.000001f;
private Deserializer fpObjectToTest;
public FPDeserializerTest(String name)
{
super(name);
}
/* This constructor allows this same class to test all of the
* floating types: float, Float, double, and Double.
*/
public FPDeserializerTest(String name, String fpClassToTest)
throws ClassNotFoundException, IllegalAccessException,
InstantiationException
{
super(name);
Class testClass = Class.forName(fpClassToTest);
fpObjectToTest = (Deserializer) testClass.newInstance();
}
private Node domFromXMLString(String str) throws Exception
{
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
DocumentBuilder builder;
Document document;
builder = factory.newDocumentBuilder();
ByteArrayInputStream bais = new ByteArrayInputStream(str.getBytes());
document = builder.parse(bais);
return document.getDocumentElement();
}
private Bean runUnmarshall(String fpVal) throws Exception
{
Node myNode = domFromXMLString(
"<fpValue>" + fpVal + "</fpValue>");
return fpObjectToTest.unmarshall("dontCare", /* inScopeEncStyle */
null, /* QName: dontcare */
myNode,
null, /* XMLJavaMappingRegistry: dontcare */
null); /* SOAPContext: dontcare */
}
/* Convention: regardless of type of Bean (float, Double, ...),
* the good value is always passed in as a Double.
*/
private void assertBeanValueEquals(Bean bean, Double goodVal)
throws Exception
{
// Workaround for JUnit defect: will submit to Kent Beck
if (bean.value.toString().equals(goodVal.toString()))
return;
if (bean.type == Float.class) {
Float result = (Float) bean.value;
float fpGoodVal = goodVal.floatValue();
assertEquals(fpGoodVal, result.floatValue(), okFltDelta);
} else if (bean.type == float.class) {
float result = ((Float)bean.value).floatValue();
float fpGoodVal = goodVal.floatValue();
assertEquals(fpGoodVal, result, okFltDelta);
} else if (bean.type == Double.class) {
Double result = (Double) bean.value;
double fpGoodVal = goodVal.doubleValue();
assertEquals(fpGoodVal, result.doubleValue(), okDblDelta);
} else if (bean.type == double.class) {
double result = ((Double)bean.value).doubleValue();
double fpGoodVal = goodVal.doubleValue();
assertEquals(fpGoodVal, result, okDblDelta);
} else {
throw new Exception("Unexpected Bean type");
}
}
public void testGoodValue1() throws Exception
{
Bean bean = runUnmarshall("1.1");
assertBeanValueEquals(bean, new Double("1.1"));
}
public void testGoodValue2() throws Exception
{
Bean bean = runUnmarshall("0");
assertBeanValueEquals(bean, new Double("0"));
}
public void testGoodValue3() throws Exception
{
Bean bean = runUnmarshall("-1E4");
assertBeanValueEquals(bean, new Double("-1E4"));
}
public void testGoodValue4() throws Exception
{
Bean bean = runUnmarshall("0");
assertBeanValueEquals(bean, new Double("0"));
}
public void testGoodValue5() throws Exception
{
Bean bean = runUnmarshall("-0");
assertBeanValueEquals(bean, new Double("0"));
}
public void testGoodValue6() throws Exception
{
Bean bean = runUnmarshall("12.78e-12");
assertBeanValueEquals(bean, new Double("12.78e-12"));
}
public void testBadValue() throws Exception
{
try {
runUnmarshall("NoSuchSpecialFPValue");
fail("Didn't get expected NumberFormatException");
} catch (NumberFormatException nfe) {
return;
}
}
public void testBadCloseValue() throws Exception
{
try {
runUnmarshall("InfinityAndBeyond");
fail("Didn't get expected NumberFormatException");
} catch (NumberFormatException nfe) {
return;
}
}
public void testGoodInf() throws Exception
{
Bean bean = runUnmarshall("INF");
assertBeanValueEquals(bean, new Double(Double.POSITIVE_INFINITY));
}
/*
* A forgiving listener: allow what Java Float.toString()
* generates for Float.POSITIVE_INFINITY.
*/
public void testGoodEnoughInfinity() throws Exception
{
Bean bean = runUnmarshall("Infinity");
assertBeanValueEquals(bean, new Double(Double.POSITIVE_INFINITY));
}
public void testGoodEnoughInfinityCaseInsensitive() throws Exception
{
Bean bean = runUnmarshall("InFiNiTy");
assertBeanValueEquals(bean, new Double(Double.POSITIVE_INFINITY));
}
public void testGoodNegInf() throws Exception
{
Bean bean = runUnmarshall("-INF");
assertBeanValueEquals(bean, new Double(Double.NEGATIVE_INFINITY));
}
/*
* A forgiving listener: allow what Java Float.toString()
* generates for Float.NEGATIVE_INFINITY.
*/
public void testGoodEnoughNegativeInfinity() throws Exception
{
Bean bean = runUnmarshall("-Infinity");
assertBeanValueEquals(bean, new Double(Double.NEGATIVE_INFINITY));
}
public void testGoodNaN() throws Exception
{
Bean bean = runUnmarshall("NaN");
assertBeanValueEquals(bean, new Double(Double.NaN));
}
}
1.1 xml-soap/java/test/encoding/soapenc/FloatDeserializerTest.java
Index: FloatDeserializerTest.java
===================================================================
package test.encoding.soapenc;
/**
* Actual tests are in superclass. This class allows tests
* of float deserialization.
*
* @author Jim Stearns (Jim_Stearns@hp.com)
*/
public class FloatDeserializerTest extends FPDeserializerTest
{
public FloatDeserializerTest(String name)
throws ClassNotFoundException, IllegalAccessException,
InstantiationException
{
super(name, "org.apache.soap.encoding.soapenc.FloatDeserializer");
}
}
1.1 xml-soap/java/test/encoding/soapenc/FloatObjectDeserializerTest.java
Index: FloatObjectDeserializerTest.java
===================================================================
package test.encoding.soapenc;
/**
* Actual tests are in superclass. This class allows tests
* of Double deserialization.
*
* @author Jim Stearns (Jim_Stearns@hp.com)
*/
public class FloatObjectDeserializerTest extends FPDeserializerTest
{
public FloatObjectDeserializerTest(String name)
throws ClassNotFoundException, IllegalAccessException,
InstantiationException
{
super(name, "org.apache.soap.encoding.soapenc.FloatObjectDeserializer");
}
}
1.1 xml-soap/java/test/encoding/soapenc/PackageTests.java
Index: PackageTests.java
===================================================================
package test.encoding.soapenc;
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(DoubleDeserializerTest.class);
suite.addTestSuite(DoubleObjectDeserializerTest.class);
suite.addTestSuite(FloatDeserializerTest.class);
suite.addTestSuite(FloatObjectDeserializerTest.class);
return suite;
}
}
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic