From 29aba171ed5286884bb4976e7a9d6d4653a1ca22 Mon Sep 17 00:00:00 2001 From: "Mark R. Diggory" Date: Wed, 19 Nov 2003 13:26:42 +0000 Subject: [PATCH] Additional tooling for simple JUnit testing of serialization. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/math/trunk@141038 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/math/TestUtils.java | 38 ++++++++++++++++++- .../math/stat/DescriptiveStatisticsTest.java | 30 ++++++++++++++- 2 files changed, 65 insertions(+), 3 deletions(-) diff --git a/src/test/org/apache/commons/math/TestUtils.java b/src/test/org/apache/commons/math/TestUtils.java index 34644ba02..d4a23175a 100644 --- a/src/test/org/apache/commons/math/TestUtils.java +++ b/src/test/org/apache/commons/math/TestUtils.java @@ -54,12 +54,19 @@ package org.apache.commons.math; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + import org.apache.commons.math.complex.Complex; import junit.framework.Assert; /** - * @version $Revision: 1.7 $ $Date: 2003/11/15 18:52:31 $ + * @version $Revision: 1.8 $ $Date: 2003/11/19 13:26:42 $ */ public class TestUtils { /** @@ -85,4 +92,33 @@ public class TestUtils { assertEquals(expected.getReal(), actual.getReal(), delta); assertEquals(expected.getImaginary(), actual.getImaginary(), delta); } + + public static Object serializeAndRecover(Object o){ + + Object result = null; + + File tmp = null; + + try { + + // serialize the Object + tmp = File.createTempFile("test",".ser"); + FileOutputStream fo = new FileOutputStream(tmp); + ObjectOutputStream so = new ObjectOutputStream(fo); + so.writeObject(o); + so.flush(); + + // deserialize the Book + FileInputStream fi = new FileInputStream(tmp); + ObjectInputStream si = new ObjectInputStream(fi); + result = si.readObject(); + + }catch (Exception e) { + e.printStackTrace(); + }finally{ + if(tmp != null) tmp.delete(); + } + + return result; + } } diff --git a/src/test/org/apache/commons/math/stat/DescriptiveStatisticsTest.java b/src/test/org/apache/commons/math/stat/DescriptiveStatisticsTest.java index c0cdafdd3..f279d51f3 100644 --- a/src/test/org/apache/commons/math/stat/DescriptiveStatisticsTest.java +++ b/src/test/org/apache/commons/math/stat/DescriptiveStatisticsTest.java @@ -57,13 +57,14 @@ import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; +import org.apache.commons.math.TestUtils; import org.apache.commons.math.random.RandomData; import org.apache.commons.math.random.RandomDataImpl; /** * Test cases for the {@link Univariate} class. * - * @version $Revision: 1.1 $ $Date: 2003/11/15 16:01:41 $ + * @version $Revision: 1.2 $ $Date: 2003/11/19 13:26:42 $ */ public final class DescriptiveStatisticsTest extends TestCase { @@ -340,6 +341,31 @@ public final class DescriptiveStatisticsTest extends TestCase { assertTrue("empty value set should return NaN", Double.isNaN(u.getPercentile(50))); } - + + /** test stats */ + public void testSerialization() { + DescriptiveStatistics u = DescriptiveStatistics.newInstance(); + assertEquals("total count",0,u.getN(),tolerance); + u.addValue(one); + u.addValue(two); + + DescriptiveStatistics u2 = (DescriptiveStatistics)TestUtils.serializeAndRecover(u); + + u2.addValue(two); + u2.addValue(three); + + assertEquals("N",n,u2.getN(),tolerance); + assertEquals("sum",sum,u2.getSum(),tolerance); + assertEquals("sumsq",sumSq,u2.getSumsq(),tolerance); + assertEquals("var",var,u2.getVariance(),tolerance); + assertEquals("std",std,u2.getStandardDeviation(),tolerance); + assertEquals("mean",mean,u2.getMean(),tolerance); + assertEquals("min",min,u2.getMin(),tolerance); + assertEquals("max",max,u2.getMax(),tolerance); + + u2.clear(); + assertEquals("total count",0,u2.getN(),tolerance); + } + }