Skip to content
This repository has been archived by the owner on Mar 20, 2018. It is now read-only.

Commit

Permalink
Add serialization tests for ZOT and ZOTR
Browse files Browse the repository at this point in the history
  • Loading branch information
jodastephen committed Nov 21, 2012
1 parent 1a5baea commit 3defb93
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 34 deletions.
96 changes: 96 additions & 0 deletions 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);
}
}

}
26 changes: 9 additions & 17 deletions src/tck/java/javax/time/zone/TCKZoneOffsetTransition.java
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

//-----------------------------------------------------------------------
Expand Down
26 changes: 9 additions & 17 deletions src/tck/java/javax/time/zone/TCKZoneOffsetTransitionRule.java
Expand Up @@ -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;
Expand All @@ -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);
Expand Down Expand Up @@ -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);
}

//-----------------------------------------------------------------------
Expand Down
Binary file added src/test/resources/ZoneOffsetTransition.bin
Binary file not shown.
Binary file added src/test/resources/ZoneOffsetTransitionRule.bin
Binary file not shown.

0 comments on commit 3defb93

Please sign in to comment.