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 */
017package org.apache.commons.proxy2.util;
018
019import static org.junit.Assert.assertTrue;
020
021import java.io.Serializable;
022import java.lang.reflect.Method;
023
024import org.apache.commons.lang3.SerializationUtils;
025import org.apache.commons.lang3.Validate;
026import org.apache.commons.lang3.builder.Builder;
027import org.apache.commons.proxy2.Invocation;
028import org.apache.commons.proxy2.ProxyUtils;
029
030public abstract class AbstractTestCase
031{
032    //**********************************************************************************************************************
033    // Other Methods
034    //**********************************************************************************************************************
035
036    protected void assertSerializable(Object o)
037    {
038        assertTrue(o instanceof Serializable);
039        SerializationUtils.clone((Serializable) o);
040    }
041
042    protected MockInvocationBuilder mockInvocation(Class<?> type, String name, Class<?>... argumentTypes)
043    {
044        try
045        {
046            return new MockInvocationBuilder(Validate.notNull(type).getMethod(name, argumentTypes));
047        }
048        catch (NoSuchMethodException e)
049        {
050            throw new IllegalArgumentException("Method not found.", e);
051        }
052    }
053
054    protected static final class MockInvocationBuilder implements Builder<Invocation>
055    {
056        private final Method method;
057        private Object[] arguments = ProxyUtils.EMPTY_ARGUMENTS;
058        private Object returnValue = null;
059
060        public MockInvocationBuilder(Method method)
061        {
062            this.method = method;
063        }
064
065        public MockInvocationBuilder withArguments(Object... arguments)
066        {
067            this.arguments = arguments;
068            return this;
069        }
070
071        public MockInvocationBuilder returning(Object value)
072        {
073            this.returnValue = value;
074            return this;
075        }
076
077        @Override
078        public Invocation build()
079        {
080            return new MockInvocation(method, returnValue, arguments);
081        }
082    }
083}