1 |
|
package com.atlassian.xwork.interceptors; |
2 |
|
|
3 |
|
import com.opensymphony.xwork.*; |
4 |
|
import junit.framework.AssertionFailedError; |
5 |
|
import org.jmock.Mock; |
6 |
|
import org.jmock.MockObjectTestCase; |
7 |
|
|
8 |
|
import java.lang.reflect.InvocationHandler; |
9 |
|
import java.lang.reflect.Method; |
10 |
|
import java.lang.reflect.Proxy; |
11 |
|
|
|
|
| 76.9% |
Uncovered Elements: 12 (52) |
Complexity: 13 |
Complexity Density: 0.39 |
|
12 |
|
public class TestSimpleValidationInterceptor extends MockObjectTestCase |
13 |
|
{ |
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
1
PASS
|
|
14 |
1
|
public void testReturnsInputIfNoErrors() throws Exception... |
15 |
|
{ |
16 |
1
|
Mock mockActionInvocation = mock(ActionInvocation.class); |
17 |
|
|
18 |
1
|
Action action = getStubValidatingAction("result", true); |
19 |
1
|
mockActionInvocation.expects(once()).method("getAction").will(returnValue(action)); |
20 |
|
|
21 |
1
|
SimpleValidationInterceptor interceptor = new SimpleValidationInterceptor(); |
22 |
1
|
assertEquals("input", interceptor.intercept((ActionInvocation) mockActionInvocation.proxy())); |
23 |
|
} |
24 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1
PASS
|
|
25 |
1
|
public void testInvokesIfNoErrors() throws Exception... |
26 |
|
{ |
27 |
1
|
Mock mockActionInvocation = mock(ActionInvocation.class); |
28 |
|
|
29 |
1
|
Action action = getStubValidatingAction("result", false); |
30 |
1
|
mockActionInvocation.expects(once()).method("getAction").will(returnValue(action)); |
31 |
1
|
mockActionInvocation.expects(once()).method("invoke").will(returnValue(action.execute())); |
32 |
|
|
33 |
1
|
SimpleValidationInterceptor interceptor = new SimpleValidationInterceptor(); |
34 |
1
|
assertEquals("result", interceptor.intercept((ActionInvocation) mockActionInvocation.proxy())); |
35 |
|
} |
36 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
1
PASS
|
|
37 |
1
|
public void testNonValidatingAction() throws Exception... |
38 |
|
{ |
39 |
1
|
Mock mockActionInvocation = mock(ActionInvocation.class); |
40 |
|
|
41 |
1
|
Action action = getStubNonValidatingAction(ActionSupport.SUCCESS); |
42 |
1
|
mockActionInvocation.expects(once()).method("getAction").will(returnValue(action)); |
43 |
1
|
mockActionInvocation.expects(once()).method("invoke").will(returnValue(action.execute())); |
44 |
|
|
45 |
1
|
SimpleValidationInterceptor interceptor = new SimpleValidationInterceptor(); |
46 |
1
|
assertEquals(ActionSupport.SUCCESS, interceptor.intercept((ActionInvocation) mockActionInvocation.proxy())); |
47 |
|
} |
48 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
49 |
1
|
private Action getStubNonValidatingAction(final String result)... |
50 |
|
{ |
51 |
1
|
return (Action) Proxy.newProxyInstance(getClass().getClassLoader(), |
52 |
|
new Class[] { Action.class }, |
53 |
|
new InvocationHandler() |
54 |
|
{ |
|
|
| 33.3% |
Uncovered Elements: 6 (9) |
Complexity: 3 |
Complexity Density: 0.6 |
|
55 |
1
|
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable... |
56 |
|
{ |
57 |
1
|
if (method.getName().equals("execute")) |
58 |
1
|
return result; |
59 |
0
|
if (method.getName().equals("toString")) |
60 |
0
|
return "ProxyAction: [ result: " + result + "]"; |
61 |
0
|
throw new AssertionFailedError("Unexpected method call: " + method.getName()); |
62 |
|
} |
63 |
|
}); |
64 |
|
} |
65 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
66 |
2
|
private Action getStubValidatingAction(final String result, final boolean hasErrors)... |
67 |
|
{ |
68 |
2
|
return (Action) Proxy.newProxyInstance(getClass().getClassLoader(), |
69 |
|
new Class[] { Action.class, Validateable.class, ValidationAware.class }, |
70 |
|
new InvocationHandler() |
71 |
|
{ |
|
|
| 64.7% |
Uncovered Elements: 6 (17) |
Complexity: 5 |
Complexity Density: 0.56 |
|
72 |
5
|
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable... |
73 |
|
{ |
74 |
5
|
if (method.getName().equals("execute")) |
75 |
1
|
return result; |
76 |
4
|
if (method.getName().equals("hasErrors")) |
77 |
2
|
return Boolean.valueOf(hasErrors); |
78 |
2
|
if (method.getName().equals("validate")) |
79 |
2
|
return null; |
80 |
0
|
if (method.getName().equals("toString")) |
81 |
0
|
return "ProxyAction: [ result: " + result + ", hasErrors: " + hasErrors + "]"; |
82 |
0
|
throw new AssertionFailedError("Unexpected method call: " + method.getName()); |
83 |
|
} |
84 |
|
}); |
85 |
|
} |
86 |
|
|
87 |
|
} |
88 |
|
|