Clover Coverage Report - Atlassian XWork(Aggregated)
Coverage timestamp: Wed Jul 27 2011 23:39:31 CDT
45   144   12   4.09
0   114   0.27   2.75
11     1.09  
4    
 
 
  TestXWorkTransactionInterceptor       Line # 20 42 9 98% 0.98
  TestXWorkTransactionInterceptor.FailingAction       Line # 119 1 1 100% 1.0
  TestXWorkTransactionInterceptor.RollBackAction       Line # 127 2 1 100% 1.0
  TestXWorkTransactionInterceptor.DoNothingResult       Line # 138 0 1 100% 1.0
 
  (3)
 
1    package com.atlassian.xwork.interceptors;
2   
3    import com.opensymphony.xwork.*;
4    import com.opensymphony.xwork.config.Configuration;
5    import com.opensymphony.xwork.config.ConfigurationManager;
6    import com.opensymphony.xwork.config.entities.ActionConfig;
7    import com.opensymphony.xwork.config.entities.PackageConfig;
8    import com.opensymphony.xwork.config.entities.ResultConfig;
9    import com.opensymphony.xwork.config.impl.DefaultConfiguration;
10    import org.jmock.Mock;
11    import org.jmock.MockObjectTestCase;
12    import org.springframework.transaction.PlatformTransactionManager;
13    import org.springframework.transaction.TransactionStatus;
14    import org.springframework.transaction.interceptor.DefaultTransactionAttribute;
15    import org.springframework.transaction.interceptor.TransactionAttribute;
16    import org.springframework.transaction.support.SimpleTransactionStatus;
17   
18    import java.util.Collections;
19   
 
20    public class TestXWorkTransactionInterceptor extends MockObjectTestCase
21    {
22    private Mock transactionManager;
23    private XWorkTransactionInterceptor interceptor;
24   
 
25  3 toggle protected void setUp() throws Exception
26    {
27  3 super.setUp();
28   
29  3 transactionManager = mock(PlatformTransactionManager.class);
30  3 interceptor = new XWorkTransactionInterceptor() {
 
31  3 toggle public PlatformTransactionManager getTransactionManager()
32    {
33  3 return (PlatformTransactionManager) transactionManager.proxy();
34    }
35   
 
36  3 toggle protected boolean shouldIntercept(ActionInvocation invocation)
37    {
38  3 return true;
39    }
40    };
41    }
42   
 
43  1 toggle public void testSuccessCommitsTwice() throws Exception
44    {
45  1 ConfigurationManager.setConfiguration(buildXWorkConfiguration("test", ActionSupport.class, "success", DoNothingResult.class));
46  1 ActionInvocation actionInvocation = buildActionInvocation("test");
47   
48  1 DefaultTransactionAttribute expectedAttributes = new DefaultTransactionAttribute(TransactionAttribute.PROPAGATION_REQUIRED);
49  1 TransactionStatus transaction = new SimpleTransactionStatus();
50  1 transactionManager.expects(exactly(2)).method("getTransaction").with(eq(expectedAttributes)).will(returnValue(transaction));
51  1 transactionManager.expects(exactly(2)).method("commit").with(same(transaction));
52   
53  1 interceptor.intercept(actionInvocation);
54    }
55   
 
56  1 toggle public void testRuntimeExceptionInActionRollsBack() throws Exception
57    {
58  1 ConfigurationManager.setConfiguration(buildXWorkConfiguration("test", FailingAction.class, "success", DoNothingResult.class));
59  1 ActionInvocation actionInvocation = buildActionInvocation("test");
60   
61  1 DefaultTransactionAttribute expectedAttributes = new DefaultTransactionAttribute(TransactionAttribute.PROPAGATION_REQUIRED);
62  1 TransactionStatus transaction = new SimpleTransactionStatus();
63  1 transactionManager.expects(exactly(1)).method("getTransaction").with(eq(expectedAttributes)).will(returnValue(transaction));
64  1 transactionManager.expects(exactly(1)).method("rollback").with(same(transaction));
65   
66  1 try
67    {
68  1 interceptor.intercept(actionInvocation);
69  0 fail("Expected RuntimeException not thrown");
70    }
71    catch (RuntimeException expected) {}
72    }
73   
 
74  1 toggle public void testRollBackInActionDoesNotRollBackAgain() throws Exception
75    {
76  1 ConfigurationManager.setConfiguration(buildXWorkConfiguration("test", RollBackAction.class, "success", DoNothingResult.class));
77  1 ActionInvocation actionInvocation = buildActionInvocation("test");
78   
79  1 DefaultTransactionAttribute expectedAttributes = new DefaultTransactionAttribute(TransactionAttribute.PROPAGATION_REQUIRED);
80  1 TransactionStatus controllerTransaction = new SimpleTransactionStatus();
81   
82  1 RollBackAction.transactionStatus = controllerTransaction;
83  1 transactionManager.expects(once()).method("getTransaction").with(eq(expectedAttributes)).will(returnValue(controllerTransaction));
84  1 transactionManager.expects(once()).method("rollback").with(same(controllerTransaction));
85   
86  1 TransactionStatus viewTransaction = new SimpleTransactionStatus();
87  1 transactionManager.expects(once()).method("getTransaction").with(eq(expectedAttributes)).will(returnValue(viewTransaction));
88  1 transactionManager.expects(exactly(1)).method("commit").with(same(viewTransaction));
89   
90  1 interceptor.intercept(actionInvocation);
91    }
92   
93    /**
94    * Creates an action invocation for an action with the given name. The action is passed no parameters.
95    */
 
96  3 toggle private ActionInvocation buildActionInvocation(String actionName) throws Exception
97    {
98  3 ActionProxyFactory factory = DefaultActionProxyFactory.getFactory();
99  3 ActionProxy actionProxy = factory.createActionProxy("", actionName, Collections.EMPTY_MAP);
100  3 return factory.createActionInvocation(actionProxy);
101    }
102   
103    /**
104    * Creates an XWork configuration with a single action and a single result for that action.
105    */
 
106  3 toggle private Configuration buildXWorkConfiguration(String actionName, Class actionClass, String result, Class resultClass)
107    {
108    // static configuration -- not nice :-(
109  3 ActionConfig actionConfig = new ActionConfig(null, actionClass, Collections.EMPTY_MAP,
110    Collections.singletonMap(result, new ResultConfig("result", resultClass)), Collections.EMPTY_LIST);
111  3 PackageConfig packageConfig = new PackageConfig();
112  3 packageConfig.addActionConfig(actionName, actionConfig);
113  3 DefaultConfiguration configuration = new DefaultConfiguration();
114  3 configuration.addPackageConfig("default", packageConfig);
115  3 configuration.rebuildRuntimeConfiguration(); // nothing works without this line
116  3 return configuration;
117    }
118   
 
119    public static class FailingAction implements Action
120    {
 
121  1 toggle public String execute() throws Exception
122    {
123  1 throw new RuntimeException("Expected exception thrown by FailingAction");
124    }
125    }
126   
 
127    public static class RollBackAction implements Action
128    {
129    public static TransactionStatus transactionStatus;
130   
 
131  1 toggle public String execute() throws Exception
132    {
133  1 transactionStatus.setRollbackOnly();
134  1 return SUCCESS;
135    }
136    }
137   
 
138    public static class DoNothingResult implements Result
139    {
 
140  2 toggle public void execute(ActionInvocation invocation) throws Exception
141    {
142    }
143    }
144    }