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