1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts2;
23
24 import java.util.ArrayList;
25 import java.util.List;
26
27 import junit.framework.Assert;
28
29 import com.opensymphony.xwork2.ActionContext;
30 import com.opensymphony.xwork2.ActionInvocation;
31 import com.opensymphony.xwork2.Result;
32 import com.opensymphony.xwork2.util.ValueStack;
33 import com.opensymphony.xwork2.util.logging.Logger;
34 import com.opensymphony.xwork2.util.logging.LoggerFactory;
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 Logger LOG = LoggerFactory.getLogger(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 }