ErrorTag should be Serializable 31/97031/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 26 Jul 2021 22:55:47 +0000 (00:55 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 27 Jul 2021 07:30:54 +0000 (09:30 +0200)
We want to transport ErrorTag as a type-safe string. Add the infra
to do that. Note that the serialization format places an upper bound
of 64K -- and that is very reasonable for an ErrorTag.

JIRA: YANGTOOLS-1309
Change-Id: Ib0049fbddc92e9b8ec9e1b37851e0de6f6faaad6
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/ETv1.java [new file with mode: 0644]
common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/ErrorTag.java
common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/ErrorTagTest.java [new file with mode: 0644]

diff --git a/common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/ETv1.java b/common/yang-common/src/main/java/org/opendaylight/yangtools/yang/common/ETv1.java
new file mode 100644 (file)
index 0000000..fa42a56
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.common;
+
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+
+/**
+ * Externalizable proxy for {@link ErrorTag}.
+ */
+final class ETv1 implements Externalizable {
+    private static final long serialVersionUID = 1L;
+
+    private String elementBody;
+
+    @SuppressWarnings("checkstyle:redundantModifier")
+    public ETv1() {
+        // visible for Externalizable
+    }
+
+    ETv1(final ErrorTag tag) {
+        elementBody = tag.elementBody();
+    }
+
+    @Override
+    public void writeExternal(final ObjectOutput out) throws IOException {
+        out.writeUTF(elementBody);
+    }
+
+    @Override
+    public void readExternal(final ObjectInput in) throws IOException {
+        elementBody = in.readUTF();
+    }
+
+    Object readResolve() {
+        return new ErrorTag(elementBody);
+    }
+}
index 4e0a0b913b2bfd58b0f1d728e91acfa0ea8bb139..6380b97c3a06cbd7b32975aad84dba59bb56300e 100644 (file)
@@ -9,6 +9,7 @@ package org.opendaylight.yangtools.yang.common;
 
 import static java.util.Objects.requireNonNull;
 
+import java.io.Serializable;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
 
@@ -23,7 +24,7 @@ import org.eclipse.jdt.annotation.Nullable;
  * error tag.
  */
 @NonNullByDefault
-public final class ErrorTag {
+public final class ErrorTag implements Serializable {
     /**
      * {@code access-denied} {@link ErrorTag}.
      */
@@ -147,6 +148,8 @@ public final class ErrorTag {
      */
     public static final ErrorTag UNKNOWN_NAMESPACE = new ErrorTag("unknown-namespace");
 
+    private static final long serialVersionUID = 1L;
+
     private final String elementBody;
 
     public ErrorTag(final String elementBody) {
@@ -176,4 +179,8 @@ public final class ErrorTag {
     public String toString() {
         return elementBody;
     }
-}
\ No newline at end of file
+
+    Object writeReplace() {
+        return new ETv1(this);
+    }
+}
diff --git a/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/ErrorTagTest.java b/common/yang-common/src/test/java/org/opendaylight/yangtools/yang/common/ErrorTagTest.java
new file mode 100644 (file)
index 0000000..96dddba
--- /dev/null
@@ -0,0 +1,35 @@
+/*
+ * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.common;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import org.junit.Test;
+
+public class ErrorTagTest {
+    @Test
+    public void testSerialization() throws Exception {
+        final ErrorTag expected = new ErrorTag("test");
+
+        final byte[] bytes;
+        try (ByteArrayOutputStream bos = new ByteArrayOutputStream()) {
+            try (ObjectOutputStream oos = new ObjectOutputStream(bos)) {
+                oos.writeObject(expected);
+            }
+            bytes = bos.toByteArray();
+        }
+
+        try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(bytes))) {
+            assertEquals(expected, ois.readObject());
+        }
+    }
+}