| 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 |
|
|
|
|
|
| 98% |
Uncovered Elements: 1 (50) |
Complexity: 9 |
Complexity Density: 0.21 |
|
| 20 |
|
public class TestXWorkTransactionInterceptor extends MockObjectTestCase |
| 21 |
|
{ |
| 22 |
|
private Mock transactionManager; |
| 23 |
|
private XWorkTransactionInterceptor interceptor; |
| 24 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 25 |
3
|
protected void setUp() throws Exception... |
| 26 |
|
{ |
| 27 |
3
|
super.setUp(); |
| 28 |
|
|
| 29 |
3
|
transactionManager = mock(PlatformTransactionManager.class); |
| 30 |
3
|
interceptor = new XWorkTransactionInterceptor() { |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 31 |
3
|
public PlatformTransactionManager getTransactionManager()... |
| 32 |
|
{ |
| 33 |
3
|
return (PlatformTransactionManager) transactionManager.proxy(); |
| 34 |
|
} |
| 35 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 36 |
3
|
protected boolean shouldIntercept(ActionInvocation invocation)... |
| 37 |
|
{ |
| 38 |
3
|
return true; |
| 39 |
|
} |
| 40 |
|
}; |
| 41 |
|
} |
| 42 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
1
PASS
|
|
| 43 |
1
|
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 |
|
|
|
|
|
| 88.9% |
Uncovered Elements: 1 (9) |
Complexity: 2 |
Complexity Density: 0.22 |
1
PASS
|
|
| 56 |
1
|
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 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 1 |
Complexity Density: 0.09 |
1
PASS
|
|
| 74 |
1
|
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 |
|
|
| 95 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.33 |
|
| 96 |
3
|
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 |
|
|
| 105 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 1 |
Complexity Density: 0.14 |
|
| 106 |
3
|
private Configuration buildXWorkConfiguration(String actionName, Class actionClass, String result, Class resultClass)... |
| 107 |
|
{ |
| 108 |
|
|
| 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(); |
| 116 |
3
|
return configuration; |
| 117 |
|
} |
| 118 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 1 |
|
| 119 |
|
public static class FailingAction implements Action |
| 120 |
|
{ |
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
| 121 |
1
|
public String execute() throws Exception... |
| 122 |
|
{ |
| 123 |
1
|
throw new RuntimeException("Expected exception thrown by FailingAction"); |
| 124 |
|
} |
| 125 |
|
} |
| 126 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (3) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 127 |
|
public static class RollBackAction implements Action |
| 128 |
|
{ |
| 129 |
|
public static TransactionStatus transactionStatus; |
| 130 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
| 131 |
1
|
public String execute() throws Exception... |
| 132 |
|
{ |
| 133 |
1
|
transactionStatus.setRollbackOnly(); |
| 134 |
1
|
return SUCCESS; |
| 135 |
|
} |
| 136 |
|
} |
| 137 |
|
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: - |
|
| 138 |
|
public static class DoNothingResult implements Result |
| 139 |
|
{ |
|
|
|
| - |
Uncovered Elements: 0 (0) |
Complexity: 1 |
Complexity Density: - |
|
| 140 |
2
|
public void execute(ActionInvocation invocation) throws Exception... |
| 141 |
|
{ |
| 142 |
|
} |
| 143 |
|
} |
| 144 |
|
} |