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 --- .../pl/edu/mimuw/cloudatlas/model/Attribute.java | 5 ++ .../edu/mimuw/cloudatlas/model/ValueBoolean.java | 4 ++ .../edu/mimuw/cloudatlas/model/ValueContact.java | 7 +++ .../pl/edu/mimuw/cloudatlas/model/ValueDouble.java | 4 ++ .../edu/mimuw/cloudatlas/model/ValueDuration.java | 4 ++ .../pl/edu/mimuw/cloudatlas/model/ValueInt.java | 4 ++ .../pl/edu/mimuw/cloudatlas/model/ValueList.java | 4 ++ .../pl/edu/mimuw/cloudatlas/model/ValueSet.java | 4 ++ .../pl/edu/mimuw/cloudatlas/model/ValueSimple.java | 4 ++ .../pl/edu/mimuw/cloudatlas/model/ValueString.java | 4 ++ .../pl/edu/mimuw/cloudatlas/model/ValueTime.java | 4 ++ .../java/pl/edu/mimuw/cloudatlas/model/ZMI.java | 20 ++++++ .../pl/edu/mimuw/cloudatlas/ValueDurationTest.java | 54 ---------------- .../mimuw/cloudatlas/model/ValueDurationTest.java | 54 ++++++++++++++++ .../pl/edu/mimuw/cloudatlas/model/ZMITest.java | 72 ++++++++++++++++++++++ 15 files changed, 194 insertions(+), 54 deletions(-) delete mode 100644 src/test/java/pl/edu/mimuw/cloudatlas/ValueDurationTest.java 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') diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/Attribute.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/Attribute.java index 46ffab3..aa0cb64 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/Attribute.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/Attribute.java @@ -48,6 +48,11 @@ public class Attribute { this.name = name; } + /* Kryo needs a no-args constructor */ + private Attribute() { + this.name = ""; + } + /** * Indicates whether an attribute represents a query. This is true if and only if the attribute's name starts with an ampersand. * diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueBoolean.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueBoolean.java index 57078c2..6ad3500 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueBoolean.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueBoolean.java @@ -40,6 +40,10 @@ public class ValueBoolean extends ValueSimple { super(value); } + /* Kryo needs a no-args constructor */ + private ValueBoolean() { + } + @Override public Type getType() { return TypePrimitive.BOOLEAN; diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueContact.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueContact.java index 670a025..c554777 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueContact.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueContact.java @@ -25,6 +25,7 @@ package pl.edu.mimuw.cloudatlas.model; import java.net.InetAddress; +import java.net.UnknownHostException; import pl.edu.mimuw.cloudatlas.model.Value; import pl.edu.mimuw.cloudatlas.model.ValueContact; @@ -50,6 +51,12 @@ public class ValueContact extends Value { this.address = address; } + /* Kryo needs a no-args constructor */ + private ValueContact() throws UnknownHostException { + name = new PathName(""); + address = InetAddress.getByAddress(new byte[] {0, 0, 0, 0}); + } + @Override public Value getDefaultValue() { return new ValueContact(null, null); diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueDouble.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueDouble.java index 920a8ef..7d331c9 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueDouble.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueDouble.java @@ -40,6 +40,10 @@ public class ValueDouble extends ValueSimple { super(value); } + /* Kryo needs a no-args constructor */ + private ValueDouble() { + } + @Override public Type getType() { return TypePrimitive.DOUBLE; diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueDuration.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueDuration.java index 444ce78..7022bbd 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueDuration.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueDuration.java @@ -55,6 +55,10 @@ public class ValueDuration extends ValueSimple { super(value); } + /* Kryo needs a no-args constructor */ + private ValueDuration() { + } + @Override public Type getType() { return TypePrimitive.DURATION; diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueInt.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueInt.java index 8811d57..c5aa1c6 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueInt.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueInt.java @@ -41,6 +41,10 @@ public class ValueInt extends ValueSimple { super(value); } + /* Kryo needs a no-args constructor */ + private ValueInt() { + } + @Override public Type getType() { return TypePrimitive.INTEGER; diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueList.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueList.java index 76d59ff..8414cc4 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueList.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueList.java @@ -60,6 +60,10 @@ public class ValueList extends ValueSimple> implements List { setValue(value); } + /* Kryo needs a no-args constructor */ + private ValueList() { + } + /** * Creates an empty list. * diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueSet.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueSet.java index 9843cd4..91524af 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueSet.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueSet.java @@ -59,6 +59,10 @@ public class ValueSet extends ValueSimple> implements Set { setValue(value); } + /* Kryo needs a no-args constructor */ + private ValueSet() { + } + /** * Creates an empty set. * diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueSimple.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueSimple.java index 803510f..a320e43 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueSimple.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueSimple.java @@ -43,6 +43,10 @@ abstract class ValueSimple extends Value { setValue(value); } + /* Kryo needs a no-args constructor */ + protected ValueSimple() { + } + /** * Returns a hash code value for this object. This is a hash code of underlying wrapped object. * diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueString.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueString.java index 314c602..20a083f 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueString.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueString.java @@ -47,6 +47,10 @@ public class ValueString extends ValueSimple { super(value); } + /* Kryo needs a no-args constructor */ + private ValueString() { + } + @Override public Type getType() { return TypePrimitive.STRING; diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueTime.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueTime.java index 8dd04fc..0b9a843 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueTime.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ValueTime.java @@ -49,6 +49,10 @@ public class ValueTime extends ValueSimple { super(value); } + /* Kryo needs a no-args constructor */ + private ValueTime() { + } + @Override public Type getType() { return TypePrimitive.TIME; diff --git a/src/main/java/pl/edu/mimuw/cloudatlas/model/ZMI.java b/src/main/java/pl/edu/mimuw/cloudatlas/model/ZMI.java index a6b78da..5a560ae 100644 --- a/src/main/java/pl/edu/mimuw/cloudatlas/model/ZMI.java +++ b/src/main/java/pl/edu/mimuw/cloudatlas/model/ZMI.java @@ -24,12 +24,18 @@ package pl.edu.mimuw.cloudatlas.model; +import java.io.InputStream; +import java.io.OutputStream; import java.io.PrintStream; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Map.Entry; +import com.esotericsoftware.kryo.Kryo; +import com.esotericsoftware.kryo.io.Input; +import com.esotericsoftware.kryo.io.Output; + /** * A zone management information object. This object is a single node in a zone hierarchy. It stores zone attributes as well as * references to its father and sons in the tree. @@ -163,4 +169,18 @@ public class ZMI implements Cloneable { public String toString() { return attributes.toString(); } + + public static ZMI deserialize(InputStream in) { + Kryo kryo = new Kryo(); + Input kryoInput = new Input(in); + ZMI zmi = kryo.readObject(kryoInput, ZMI.class); + return zmi; + } + + public void serialize(OutputStream out) { + Kryo kryo = new Kryo(); + Output kryoOut = new Output(out); + kryo.writeObject(kryoOut, this); + kryoOut.flush(); + } } diff --git a/src/test/java/pl/edu/mimuw/cloudatlas/ValueDurationTest.java b/src/test/java/pl/edu/mimuw/cloudatlas/ValueDurationTest.java deleted file mode 100644 index 9db36eb..0000000 --- a/src/test/java/pl/edu/mimuw/cloudatlas/ValueDurationTest.java +++ /dev/null @@ -1,54 +0,0 @@ -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/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