logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rpo...@apache.org
Subject svn commit: r1617465 - in /logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache: log4j/util/SerialUtil.java logging/log4j/message/ObjectMessageTest.java
Date Tue, 12 Aug 2014 13:05:51 GMT
Author: rpopma
Date: Tue Aug 12 13:05:51 2014
New Revision: 1617465

URL: http://svn.apache.org/r1617465
Log:
added serialization unit tests

Added:
    logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache/log4j/util/SerialUtil.java
  (with props)
Modified:
    logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache/logging/log4j/message/ObjectMessageTest.java

Added: logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache/log4j/util/SerialUtil.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache/log4j/util/SerialUtil.java?rev=1617465&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache/log4j/util/SerialUtil.java
(added)
+++ logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache/log4j/util/SerialUtil.java
Tue Aug 12 13:05:51 2014
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache license, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the license for the specific language governing permissions and
+ * limitations under the license.
+ */
+
+package org.apache.log4j.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.Serializable;
+
+/**
+ * Utility class to facilitate serializing and deserializing objects.
+ */
+public class SerialUtil {
+    private SerialUtil() {
+    }
+
+    /**
+     * Serializes the specified object and returns the result as a byte array.
+     * @param obj the object to serialize
+     * @return the serialized object
+     */
+    public static byte[] serialize(final Serializable obj) {
+        try {
+            final ByteArrayOutputStream bas = new ByteArrayOutputStream(8192);
+            final ObjectOutputStream oos = new ObjectOutputStream(bas);
+            oos.writeObject(obj);
+            oos.flush();
+            return bas.toByteArray();
+        } catch (final Exception ex) {
+            throw new IllegalStateException("Could not serialize", ex);
+        }
+    }
+    
+    /**
+     * Deserialize an object from the specified byte array and returns the result.
+     * @param data byte array representing the serialized object
+     * @return the deserialized object
+     */
+    @SuppressWarnings("unchecked")
+    public static <T> T deserialize(final byte[] data) {
+        try {
+            final ByteArrayInputStream bas = new ByteArrayInputStream(data);
+            final ObjectInputStream ois = new ObjectInputStream(bas);
+            return (T) ois.readObject();
+        } catch (final Exception ex) {
+            throw new IllegalStateException("Could not deserialize", ex);
+        }
+    }
+}
\ No newline at end of file

Propchange: logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache/log4j/util/SerialUtil.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache/logging/log4j/message/ObjectMessageTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache/logging/log4j/message/ObjectMessageTest.java?rev=1617465&r1=1617464&r2=1617465&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache/logging/log4j/message/ObjectMessageTest.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-api/src/test/java/org/apache/logging/log4j/message/ObjectMessageTest.java
Tue Aug 12 13:05:51 2014
@@ -16,6 +16,11 @@
  */
 package org.apache.logging.log4j.message;
 
+import java.io.Serializable;
+import java.math.BigDecimal;
+
+import org.apache.log4j.util.SerialUtil;
+import org.junit.Ignore;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
@@ -62,4 +67,39 @@ public class ObjectMessageTest {
         final String actual = msg.getFormattedMessage();
         assertEquals("Should use initial param value", "abc", actual);
     }
+    
+    @Test
+    public void testSerializeWithSerializableParam() {
+        BigDecimal big = BigDecimal.valueOf(123.456);
+        ObjectMessage msg = new ObjectMessage(big);
+        ObjectMessage other = SerialUtil.deserialize(SerialUtil.serialize(msg));
+        assertEquals(msg, other);
+    }
+    
+    @Test
+    public void testDeserializeNonSerializableParamNotEqualIfToStringDiffers() {
+        ObjectMessageTest nonSerializable = new ObjectMessageTest();
+        assertFalse(nonSerializable instanceof Serializable);
+        ObjectMessage msg = new ObjectMessage(nonSerializable);
+        ObjectMessage other = SerialUtil.deserialize(SerialUtil.serialize(msg));
+        assertNotEquals("Expected different: toString is different", msg, other);
+    }
+    
+    @Ignore
+    @Test
+    public void testDeserializeNonSerializableParamEqualIfToStringSame() {
+        class NonSerializable {
+            public boolean equals(Object other) {
+                return other instanceof NonSerializable; // a very lenient equals()
+            }
+        }
+        NonSerializable nonSerializable = new NonSerializable();
+        assertFalse(nonSerializable instanceof Serializable);
+        ObjectMessage msg = new ObjectMessage(nonSerializable);
+        ObjectMessage other = SerialUtil.deserialize(SerialUtil.serialize(msg));
+
+// TODO this fails: msg.obj.equals(other.obj) is false...
+// TODO our equals() implementation does not match the serialization mechanism
+        assertEquals(msg, other);
+    }
 }



Mime
View raw message