From 3defb9361eca7fcb3aacce605e4901f3d9fad60c Mon Sep 17 00:00:00 2001 From: Stephen Colebourne Date: Wed, 21 Nov 2012 23:35:59 +0000 Subject: [PATCH] Add serialization tests for ZOT and ZOTR --- src/tck/java/javax/time/AbstractTCKTest.java | 96 ++++++++++++++++++ .../time/zone/TCKZoneOffsetTransition.java | 26 ++--- .../zone/TCKZoneOffsetTransitionRule.java | 26 ++--- src/test/resources/ZoneOffsetTransition.bin | Bin 0 -> 63 bytes .../resources/ZoneOffsetTransitionRule.bin | Bin 0 -> 52 bytes 5 files changed, 114 insertions(+), 34 deletions(-) create mode 100644 src/tck/java/javax/time/AbstractTCKTest.java create mode 100644 src/test/resources/ZoneOffsetTransition.bin create mode 100644 src/test/resources/ZoneOffsetTransitionRule.bin diff --git a/src/tck/java/javax/time/AbstractTCKTest.java b/src/tck/java/javax/time/AbstractTCKTest.java new file mode 100644 index 00000000..ce443408 --- /dev/null +++ b/src/tck/java/javax/time/AbstractTCKTest.java @@ -0,0 +1,96 @@ +/* + * Copyright (c) 2011-2012, Stephen Colebourne & Michael Nascimento Santos + * + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * + * * 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. + * + * * Neither the name of JSR-310 nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS 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 COPYRIGHT OWNER OR + * 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. + */ +package javax.time; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertSame; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; + +/** + * Base test class. + */ +public abstract class AbstractTCKTest { + + private static final String SERIALISATION_DATA_FOLDER = "src/test/resources/"; + + protected static boolean isIsoLeap(long year) { + if (year % 4 != 0) { + return false; + } + if (year % 100 == 0 && year % 400 != 0) { + return false; + } + return true; + } + + protected static void assertSerializable(Object o) throws IOException, ClassNotFoundException { + Object deserialisedObject = writeThenRead(o); + assertEquals(deserialisedObject, o); + } + + protected static void assertSerializableAndSame(Object o) throws IOException, ClassNotFoundException { + Object deserialisedObject = writeThenRead(o); + assertSame(deserialisedObject, o); + } + + protected static Object writeThenRead(Object o) throws IOException, ClassNotFoundException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + try (ObjectOutputStream oos = new ObjectOutputStream(baos) ) { + oos.writeObject(o); + } + try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) { + return ois.readObject(); + } + } + + protected static void assertEqualsSerialisedForm(Object objectSerialised) throws IOException, ClassNotFoundException { + assertEqualsSerialisedForm(objectSerialised, objectSerialised.getClass()); + } + + protected static void assertEqualsSerialisedForm(Object objectSerialised, Class cls) throws IOException, ClassNotFoundException { + String className = cls.getSimpleName(); +// try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(SERIALISATION_DATA_FOLDER + className + ".bin"))) { +// out.writeObject(objectSerialised); +// } + try (ObjectInputStream in = new ObjectInputStream(new FileInputStream(SERIALISATION_DATA_FOLDER + className + ".bin"))) { + Object objectFromFile = in.readObject(); + assertEquals(objectFromFile, objectSerialised); + } + } + +} diff --git a/src/tck/java/javax/time/zone/TCKZoneOffsetTransition.java b/src/tck/java/javax/time/zone/TCKZoneOffsetTransition.java index 4f1f839a..7f832687 100644 --- a/src/tck/java/javax/time/zone/TCKZoneOffsetTransition.java +++ b/src/tck/java/javax/time/zone/TCKZoneOffsetTransition.java @@ -34,11 +34,9 @@ import static javax.time.calendrical.ChronoUnit.HOURS; import static org.testng.Assert.assertEquals; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; +import java.io.IOException; +import javax.time.AbstractTCKTest; import javax.time.Duration; import javax.time.LocalDateTime; import javax.time.Year; @@ -50,7 +48,7 @@ * Test ZoneOffsetTransition. */ @Test -public class TCKZoneOffsetTransition { +public class TCKZoneOffsetTransition extends AbstractTCKTest { private static final ZoneOffset OFFSET_0100 = ZoneOffset.ofHours(1); private static final ZoneOffset OFFSET_0200 = ZoneOffset.ofHours(2); @@ -121,6 +119,7 @@ public void test_getters_overlap() throws Exception { assertSerializable(test); } + //----------------------------------------------------------------------- @Test(groups={"tck"}) public void test_serialization_unusual1() throws Exception { LocalDateTime ldt = LocalDateTime.of(Year.MAX_YEAR, 12, 31, 1, 31, 53); @@ -135,18 +134,11 @@ public void test_serialization_unusual2() throws Exception { assertSerializable(test); } - private void assertSerializable(ZoneOffsetTransition test) throws Exception { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream out = new ObjectOutputStream(baos); - out.writeObject(test); - baos.close(); - byte[] bytes = baos.toByteArray(); - - ByteArrayInputStream bais = new ByteArrayInputStream(bytes); - ObjectInputStream in = new ObjectInputStream(bais); - ZoneOffsetTransition result = (ZoneOffsetTransition) in.readObject(); - - assertEquals(result, test); + @Test(groups={"tck"}) + public void test_serialization_format() throws ClassNotFoundException, IOException { + LocalDateTime ldt = LocalDateTime.of(Year.MIN_YEAR, 1, 1, 12, 1, 3); + ZoneOffsetTransition test = ZoneOffsetTransition.of(ldt, ZoneOffset.of("+02:04:56"), ZoneOffset.of("+10:02:34")); + assertEqualsSerialisedForm(test); } //----------------------------------------------------------------------- diff --git a/src/tck/java/javax/time/zone/TCKZoneOffsetTransitionRule.java b/src/tck/java/javax/time/zone/TCKZoneOffsetTransitionRule.java index 8e14db64..ac541575 100644 --- a/src/tck/java/javax/time/zone/TCKZoneOffsetTransitionRule.java +++ b/src/tck/java/javax/time/zone/TCKZoneOffsetTransitionRule.java @@ -33,11 +33,9 @@ import static org.testng.Assert.assertEquals; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; +import java.io.IOException; +import javax.time.AbstractTCKTest; import javax.time.DayOfWeek; import javax.time.LocalDateTime; import javax.time.LocalTime; @@ -51,7 +49,7 @@ * Test ZoneOffsetTransitionRule. */ @Test -public class TCKZoneOffsetTransitionRule { +public class TCKZoneOffsetTransitionRule extends AbstractTCKTest { private static final LocalTime TIME_0100 = LocalTime.of(1, 0); private static final ZoneOffset OFFSET_0200 = ZoneOffset.ofHours(2); @@ -210,18 +208,12 @@ public void test_serialization_unusualTime() throws Exception { assertSerializable(test); } - private void assertSerializable(ZoneOffsetTransitionRule test) throws Exception { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream out = new ObjectOutputStream(baos); - out.writeObject(test); - baos.close(); - byte[] bytes = baos.toByteArray(); - - ByteArrayInputStream bais = new ByteArrayInputStream(bytes); - ObjectInputStream in = new ObjectInputStream(bais); - ZoneOffsetTransitionRule result = (ZoneOffsetTransitionRule) in.readObject(); - - assertEquals(result, test); + @Test(groups={"tck"}) + public void test_serialization_format() throws ClassNotFoundException, IOException { + ZoneOffsetTransitionRule test = new ZoneOffsetTransitionRule( + Month.MARCH, 20, DayOfWeek.TUESDAY, LocalTime.of(13, 34, 56), false, TimeDefinition.STANDARD, + OFFSET_0200, OFFSET_0200, OFFSET_0300); + assertEqualsSerialisedForm(test); } //----------------------------------------------------------------------- diff --git a/src/test/resources/ZoneOffsetTransition.bin b/src/test/resources/ZoneOffsetTransition.bin new file mode 100644 index 0000000000000000000000000000000000000000..525ab1b32989dcc62911a46b65d1c1f4c1dfef9d GIT binary patch literal 63 zcmZ4UmVvdnh(S0ju`IDduOu@!Rj(>PFI6u%wWwvozt=TV!TWd^7%B?NMVS8o?|&>H S=B~26o`FHu14#5*RR91AZx?R> literal 0 HcmV?d00001 diff --git a/src/test/resources/ZoneOffsetTransitionRule.bin b/src/test/resources/ZoneOffsetTransitionRule.bin new file mode 100644 index 0000000000000000000000000000000000000000..ca3860b7bd51ce23a70ac015c95b2d8cf205550f GIT binary patch literal 52 zcmZ4UmVvdnh(S0ju`IDduOu@!Rj(>PFI6u%wWwvozt=TV!TWd^7%B?NIhk$6Uo}RL|011#200000 literal 0 HcmV?d00001