1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
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));
}
}
|