1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.struts2;
19
20 import java.util.ArrayList;
21 import java.util.List;
22
23 import junit.framework.Assert;
24
25 import org.apache.commons.logging.Log;
26 import org.apache.commons.logging.LogFactory;
27
28 import com.opensymphony.xwork2.ActionContext;
29 import com.opensymphony.xwork2.ActionInvocation;
30 import com.opensymphony.xwork2.Result;
31 import com.opensymphony.xwork2.util.ValueStack;
32
33
34 /***
35 * TestResult
36 *
37 */
38 public class TestResult implements Result {
39
40 private static final long serialVersionUID = -4429258122011663164L;
41
42
43 private static final Log LOG = LogFactory.getLog(TestResult.class);
44
45
46 private List expectedValues = new ArrayList();
47 private List propertyNames = new ArrayList();
48
49
50 public void setExpectedValue(int index, String value) {
51 expectedValues.set(index, value);
52 }
53
54 public void setExpectedValue(String value) {
55 expectedValues.add(value);
56 }
57
58 public List getExpectedValues() {
59 return expectedValues;
60 }
61
62 public void setPropertyName(int index, String propertyName) {
63 propertyNames.set(index, propertyName);
64 }
65
66 public void setPropertyName(String propertyName) {
67 propertyNames.add(propertyName);
68 }
69
70 public List getPropertyNames() {
71 return propertyNames;
72 }
73
74 public void execute(ActionInvocation invocation) throws Exception {
75 LOG.info("executing TestResult.");
76
77 if ((expectedValues != null) && (expectedValues.size() > 0) && (propertyNames != null) && (propertyNames.size() > 0))
78 {
79 ValueStack stack = ActionContext.getContext().getValueStack();
80
81 for (int i = 0; i < propertyNames.size(); i++) {
82 String propertyName = (String) propertyNames.get(i);
83 String expectedValue = null;
84
85 if (i < expectedValues.size()) {
86 expectedValue = (String) expectedValues.get(i);
87 }
88
89 String value = (String) stack.findValue(propertyName, String.class);
90 Assert.assertEquals(expectedValue, value);
91 }
92 } else {
93 LOG.error("One of expectedValues = " + expectedValues + " and propertyNames = " + propertyNames + " was null or empty.");
94 Assert.fail();
95 }
96 }
97 }