Clover Coverage Report - Atlassian XWork(Aggregated)
Coverage timestamp: Wed Jul 27 2011 23:39:31 CDT
33   88   13   4.71
12   73   0.39   7
7     1.86  
1    
 
 
  TestSimpleValidationInterceptor       Line # 12 33 13 76.9% 0.7692308
 
  (3)
 
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   
 
12    public class TestSimpleValidationInterceptor extends MockObjectTestCase
13    {
 
14  1 toggle 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   
 
25  1 toggle 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   
 
37  1 toggle 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   
 
49  1 toggle private Action getStubNonValidatingAction(final String result)
50    {
51  1 return (Action) Proxy.newProxyInstance(getClass().getClassLoader(),
52    new Class[] { Action.class },
53    new InvocationHandler()
54    {
 
55  1 toggle 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   
 
66  2 toggle 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    {
 
72  5 toggle 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