add support interfaces and unit test by way of example for supporting the serialization proxy pattern when the default Serializable implementations fall short

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/proxy/trunk@1581040 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/core/src/main/java/org/apache/commons/proxy2/serialization/ReadResolve.java b/core/src/main/java/org/apache/commons/proxy2/serialization/ReadResolve.java
new file mode 100755
index 0000000..eadcd3c
--- /dev/null
+++ b/core/src/main/java/org/apache/commons/proxy2/serialization/ReadResolve.java
@@ -0,0 +1,32 @@
+/*
+ * 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.commons.proxy2.serialization;
+
+import java.io.Serializable;
+
+/**
+ * Defines a contract around the {@code Object readResolve()} method used by Java deserialization.
+ * 
+ * @since 2.0
+ */
+public interface ReadResolve extends Serializable {
+    /**
+     * Get the deserialized version of this {@link Serializable}.
+     * @return Object
+     */
+    Object readResolve();
+}
diff --git a/core/src/main/java/org/apache/commons/proxy2/serialization/WriteReplace.java b/core/src/main/java/org/apache/commons/proxy2/serialization/WriteReplace.java
new file mode 100755
index 0000000..409071b
--- /dev/null
+++ b/core/src/main/java/org/apache/commons/proxy2/serialization/WriteReplace.java
@@ -0,0 +1,32 @@
+/*
+ * 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.commons.proxy2.serialization;
+
+import java.io.Serializable;
+
+/**
+ * Defines a contract around the {@code Object writeReplace()} method used by Java deserialization.
+ * 
+ * @since 2.0
+ */
+public interface WriteReplace extends Serializable {
+    /**
+     * Get the serialized version of this object.
+     * @return Object
+     */
+    Object writeReplace();
+}
diff --git a/core/src/main/java/org/apache/commons/proxy2/serialization/package-info.java b/core/src/main/java/org/apache/commons/proxy2/serialization/package-info.java
new file mode 100755
index 0000000..d67eaff
--- /dev/null
+++ b/core/src/main/java/org/apache/commons/proxy2/serialization/package-info.java
@@ -0,0 +1,37 @@
+/*
+ * 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.
+ */
+/**
+ * The various {@link org.apache.commons.proxy2.ProxyFactory} implementations create {@link Serializable} proxies;
+ * however it is not always possible or practical to serialize the complete structure of a given proxy object. The
+ * intent of this package is to facilitate the "serialization proxy pattern" by means of the {@code readResolve()} and
+ * {@code writeReplace} methods supported by Java's serialization mechanism. This would normally be problematic with
+ * Commons Proxy because its proxies are generalized to expose only methods declared by superclasses (where applicable)
+ * or proxied interfaces. Therefore we declare the following interfaces:
+ * <ul>
+ *   <li>{@link ReadResolve}</li>
+ *   <li>{@link WriteReplace}</li>
+ * </ul>
+ *
+ * Typically, you should define your proxy to include {@link WriteReplace} among its interfaces, and implement it to
+ * return some object that implements {@link ReadResolve} (or simply declares the {@code Object readResolve()} method
+ * in any scope, but using the interface brings compiler assistance).
+ *
+ * Hint: Your {@link ReadResolve#readResolve()} implementation will typically use serialized information to recreate an
+ * equivalent proxy object, which probably implies some form of {@code static} access.
+ */
+package org.apache.commons.proxy2.serialization;
+import java.io.Serializable;
diff --git a/test/pom.xml b/test/pom.xml
index ffa7638..bcd7288 100644
--- a/test/pom.xml
+++ b/test/pom.xml
@@ -55,5 +55,10 @@
             <artifactId>commons-proxy2-asm4</artifactId>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-lang3</artifactId>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 </project>
diff --git a/test/src/test/java/org/apache/commons/proxy2/serialization/SerializationProxyTest.java b/test/src/test/java/org/apache/commons/proxy2/serialization/SerializationProxyTest.java
new file mode 100755
index 0000000..1956e7c
--- /dev/null
+++ b/test/src/test/java/org/apache/commons/proxy2/serialization/SerializationProxyTest.java
@@ -0,0 +1,111 @@
+/*
+ * 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.commons.proxy2.serialization;
+
+import static org.junit.Assert.*;
+
+import java.io.Serializable;
+
+import org.apache.commons.lang3.SerializationException;
+import org.apache.commons.lang3.SerializationUtils;
+import org.apache.commons.proxy2.ProxyFactory;
+import org.apache.commons.proxy2.interceptor.InterceptorUtils;
+import org.apache.commons.proxy2.interceptor.SwitchInterceptor;
+import org.apache.commons.proxy2.interceptor.matcher.invocation.DeclaredByMatcher;
+import org.apache.commons.proxy2.stub.AbstractProxyFactoryAgnosticTest;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+public class SerializationProxyTest extends AbstractProxyFactoryAgnosticTest
+{
+    public static class NonSerializableStringWrapper
+    {
+        private final String value;
+
+        NonSerializableStringWrapper(String value)
+        {
+            this.value = value;
+        }
+
+        public String getValue()
+        {
+            return value;
+        }
+    }
+
+    public interface Provider
+    {
+        NonSerializableStringWrapper getObject();
+    }
+
+    private static final ThreadLocal<ProxyFactory> PROXY_FACTORY = new ThreadLocal<ProxyFactory>();
+
+    private static SwitchInterceptor implementProvider(String value)
+    {
+        return new SwitchInterceptor().when(new DeclaredByMatcher(Provider.class)).then(
+            InterceptorUtils.constant(new NonSerializableStringWrapper(value)));
+    }
+
+    private static Provider serializableProvider(final String value)
+    {
+        return PROXY_FACTORY.get().createInterceptorProxy(
+            null,
+            implementProvider(value).when(new DeclaredByMatcher(WriteReplace.class)).then(
+                InterceptorUtils.constant(new ReadResolve()
+                {
+                    private static final long serialVersionUID = 1L;
+
+                    @Override
+                    public Object readResolve()
+                    {
+                        return serializableProvider(value);
+                    }
+                })), Provider.class, WriteReplace.class);
+    }
+
+    @Before
+    public void captureProxyFactory()
+    {
+        PROXY_FACTORY.set(proxyFactory);
+    }
+
+    @After
+    public void clearProxyFactory()
+    {
+        PROXY_FACTORY.remove();
+    }
+
+    @Test(expected = SerializationException.class)
+    public void testNaive()
+    {
+        final Provider proxy =
+            proxyFactory.createInterceptorProxy(null, implementProvider("foo"), Provider.class, Serializable.class);
+        assertEquals("foo", proxy.getObject().getValue());
+        assertTrue(Serializable.class.isInstance(proxy));
+        SerializationUtils.roundtrip((Serializable) proxy);
+    }
+
+    @Test
+    public void testSerializationProxy() {
+        final Provider proxy = serializableProvider("foo");
+        assertEquals("foo", proxy.getObject().getValue());
+        assertTrue(Serializable.class.isInstance(proxy));
+        final Provider proxy2 = (Provider) SerializationUtils.roundtrip((Serializable) proxy);
+        assertEquals("foo", proxy2.getObject().getValue());
+    }
+}