View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.proxy2.util;
18  
19  import static org.junit.Assert.assertTrue;
20  
21  import java.io.Serializable;
22  import java.lang.reflect.Method;
23  
24  import org.apache.commons.lang3.SerializationUtils;
25  import org.apache.commons.lang3.Validate;
26  import org.apache.commons.lang3.builder.Builder;
27  import org.apache.commons.proxy2.Invocation;
28  import org.apache.commons.proxy2.ProxyUtils;
29  
30  public abstract class AbstractTestCase
31  {
32      //**********************************************************************************************************************
33      // Other Methods
34      //**********************************************************************************************************************
35  
36      protected void assertSerializable(Object o)
37      {
38          assertTrue(o instanceof Serializable);
39          SerializationUtils.clone((Serializable) o);
40      }
41  
42      protected MockInvocationBuilder mockInvocation(Class<?> type, String name, Class<?>... argumentTypes)
43      {
44          try
45          {
46              return new MockInvocationBuilder(Validate.notNull(type).getMethod(name, argumentTypes));
47          }
48          catch (NoSuchMethodException e)
49          {
50              throw new IllegalArgumentException("Method not found.", e);
51          }
52      }
53  
54      protected static final class MockInvocationBuilder implements Builder<Invocation>
55      {
56          private final Method method;
57          private Object[] arguments = ProxyUtils.EMPTY_ARGUMENTS;
58          private Object returnValue = null;
59  
60          public MockInvocationBuilder(Method method)
61          {
62              this.method = method;
63          }
64  
65          public MockInvocationBuilder withArguments(Object... arguments)
66          {
67              this.arguments = arguments;
68              return this;
69          }
70  
71          public MockInvocationBuilder returning(Object value)
72          {
73              this.returnValue = value;
74              return this;
75          }
76  
77          @Override
78          public Invocation build()
79          {
80              return new MockInvocation(method, returnValue, arguments);
81          }
82      }
83  }