1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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 }