|
1 |
| package com.agical.rmock.core.strategy.impl; |
|
2 |
| |
|
3 |
| import java.lang.reflect.InvocationTargetException; |
|
4 |
| import java.lang.reflect.Method; |
|
5 |
| |
|
6 |
| import com.agical.rmock.core.strategy.TestStep; |
|
7 |
| |
|
8 |
| |
|
9 |
| public class MethodStep implements TestStep { |
|
10 |
| protected static final Class[] NO_PARAMETERS = new Class[0]; |
|
11 |
| protected static final Object[] NO_ARGS = new Object[0]; |
|
12 |
| private final Object target; |
|
13 |
| private Method method; |
|
14 |
| |
|
15 |
573
| public MethodStep(Object target, String method) {
|
|
16 |
573
| this.target = target;
|
|
17 |
573
| Class clazz = target.getClass();
|
|
18 |
573
| try {
|
|
19 |
573
| this.method = clazz.getMethod(method, NO_PARAMETERS);
|
|
20 |
| } catch (NoSuchMethodException e) { |
|
21 |
2
| throw new com.agical.rmock.core.exception.NoSuchMethodException(target.getClass(), method, NO_ARGS);
|
|
22 |
| } |
|
23 |
| |
|
24 |
| } |
|
25 |
| |
|
26 |
568
| public void run() throws Throwable {
|
|
27 |
568
| try {
|
|
28 |
568
| if (method != null) {
|
|
29 |
568
| invokeMethod(target, method);
|
|
30 |
| } |
|
31 |
| } catch (InvocationTargetException e) { |
|
32 |
24
| throw e.getTargetException();
|
|
33 |
| } |
|
34 |
| } |
|
35 |
| |
|
36 |
557
| protected void invokeMethod(Object target, Method method) throws IllegalAccessException, InvocationTargetException {
|
|
37 |
557
| method.invoke(target, NO_ARGS);
|
|
38 |
| } |
|
39 |
| |
|
40 |
| } |