001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.proxy.interceptor;
019    
020    import junit.framework.TestCase;
021    import org.apache.commons.proxy.ProxyFactory;
022    
023    import java.io.ByteArrayOutputStream;
024    
025    public class TestSerializingInterceptor extends TestCase
026    {
027        public void testWithSerializableParametersAndReturn()
028        {
029            final ObjectEchoImpl target = new ObjectEchoImpl();
030            ObjectEcho echo =
031                    (ObjectEcho) new ProxyFactory().createInterceptorProxy(target,
032                            new SerializingInterceptor(),
033                            new Class[]{ObjectEcho.class});
034            final Object originalParameter = "Hello, World!";
035            final Object returnValue = echo.echoBack(originalParameter);
036            assertNotSame(originalParameter, target.parameter);
037            assertNotSame(originalParameter, returnValue);
038            assertNotSame(returnValue, target.parameter);
039        }
040    
041        public void testWithInvalidParameterType()
042        {
043            try
044            {
045                final ObjectEchoImpl target = new ObjectEchoImpl();
046                ObjectEcho echo =
047                        (ObjectEcho) new ProxyFactory().createInterceptorProxy(target,
048                                new SerializingInterceptor(),
049                                new Class[]{ObjectEcho.class});
050                final Object originalParameter = new ByteArrayOutputStream();
051                echo.echoBack(originalParameter);
052                fail("Should not be able to call method with non-serializable parameter type.");
053            }
054            catch (RuntimeException e)
055            {
056    
057            }
058    
059        }
060    
061        public static interface ObjectEcho
062        {
063            public Object echoBack(Object object);
064        }
065    
066        public static class ObjectEchoImpl implements ObjectEcho
067        {
068            private Object parameter;
069    
070            public Object echoBack(Object object)
071            {
072                this.parameter = object;
073                return object;
074            }
075        }
076    }