From 362310157373eef80f49ea6023b1666b3883f4b1 Mon Sep 17 00:00:00 2001 From: Marcin Chrzanowski Date: Sun, 3 Nov 2019 16:39:58 +0100 Subject: Implement ZMI serialization --- .../mimuw/cloudatlas/model/ValueDurationTest.java | 54 ++++++++++++++++ .../pl/edu/mimuw/cloudatlas/model/ZMITest.java | 72 ++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100644 src/test/java/pl/edu/mimuw/cloudatlas/model/ValueDurationTest.java create mode 100644 src/test/java/pl/edu/mimuw/cloudatlas/model/ZMITest.java (limited to 'src/test/java/pl/edu/mimuw/cloudatlas/model') diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/model/ValueDurationTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/model/ValueDurationTest.java new file mode 100644 index 0000000..9db36eb --- /dev/null +++ b/src/test/java/pl/edu/mimuw/cloudatlas/model/ValueDurationTest.java @@ -0,0 +1,54 @@ +package pl.edu.mimuw.cloudatlas.model; + +import org.junit.Test; +import static org.junit.Assert.*; + +public class ValueDurationTest { + @Test + public void testGetValue() { + ValueDuration v = new ValueDuration(42l); + assertEquals(new Long(42l), v.getValue()); + } + + @Test + public void testParseDurationMillisecond() { + ValueDuration v = new ValueDuration("+0 00:00:00.001"); + assertEquals(new Long(1l), v.getValue()); + } + + @Test + public void testParseDurationSecond() { + ValueDuration v = new ValueDuration("+0 00:00:01.000"); + assertEquals(new Long(1000l), v.getValue()); + } + + @Test + public void testParseDurationMinute() { + ValueDuration v = new ValueDuration("+0 00:01:00.000"); + assertEquals(new Long(60000l), v.getValue()); + } + + @Test + public void testParseDurationHour() { + ValueDuration v = new ValueDuration("+0 01:00:00.000"); + assertEquals(new Long(3600000l), v.getValue()); + } + + @Test + public void testParseDurationDay() { + ValueDuration v = new ValueDuration("+1 00:00:00.000"); + assertEquals(new Long(86400000l), v.getValue()); + } + + @Test + public void testParseDurationComplex() { + ValueDuration v = new ValueDuration("+0 01:59:40.000"); + assertEquals(new Long(7180000l), v.getValue()); + } + + @Test + public void testParseDurationNegative() { + ValueDuration v = new ValueDuration("-1 01:01:01.001"); + assertEquals(new Long(-90061001l), v.getValue()); + } +} diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/model/ZMITest.java b/src/test/java/pl/edu/mimuw/cloudatlas/model/ZMITest.java new file mode 100644 index 0000000..fa419f5 --- /dev/null +++ b/src/test/java/pl/edu/mimuw/cloudatlas/model/ZMITest.java @@ -0,0 +1,72 @@ +package pl.edu.mimuw.cloudatlas.model; + +import java.io.OutputStream; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.FileOutputStream; + +import org.junit.Test; +import static org.junit.Assert.*; + +public class ZMITest { + @Test + public void testSimpleSerialization1() { + ZMI zmi = new ZMI(); + AttributesMap attributes = zmi.getAttributes(); + attributes.add("foo", new ValueInt(42l)); + serializationTest(zmi); + } + + @Test + public void testSimpleSerialization2() { + ZMI zmi = new ZMI(); + AttributesMap attributes = zmi.getAttributes(); + attributes.add("foo", new ValueInt(42l)); + attributes.add("bar", new ValueDuration("+1 11:43:45.342")); + serializationTest(zmi); + } + + @Test + public void testHierarchySerialization() { + ZMI root = new ZMI(); + + AttributesMap rootAttributes = root.getAttributes(); + rootAttributes.add("foo", new ValueInt(42l)); + rootAttributes.add("bar", new ValueDuration("+1 11:43:45.342")); + + ZMI son1 = new ZMI(root); + root.addSon(son1); + + AttributesMap son1Attributes = son1.getAttributes(); + son1Attributes.add("foo", new ValueInt(43l)); + son1Attributes.add("bar", new ValueDuration("+1 12:43:47.342")); + + ZMI son2 = new ZMI(root); + root.addSon(son2); + + AttributesMap son2Attributes = son2.getAttributes(); + son2Attributes.add("foo", new ValueInt(47l)); + son2Attributes.add("bar", new ValueDuration("+1 15:45:43.342")); + + ZMI grandson1 = new ZMI(son1); + son1.addSon(grandson1); + + AttributesMap grandson1Attributes = grandson1.getAttributes(); + grandson1Attributes.add("foo", new ValueInt(52l)); + grandson1Attributes.add("bar", new ValueDuration("-2 15:45:43.342")); + + serializationTest(root); + serializationTest(son1); + serializationTest(grandson1); + } + + private void serializationTest(ZMI zmi) { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + zmi.serialize(out); + + ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray()); + ZMI zmi2 = ZMI.deserialize(in); + + assertEquals(zmi.toString(), zmi2.toString()); + } +} -- cgit v1.2.3