01: /*
02: * Copyright (c) 2002-2003 by OpenSymphony
03: * All rights reserved.
04: */
05: package com.opensymphony.webwork;
06:
07: import com.opensymphony.xwork.ActionContext;
08: import com.opensymphony.xwork.ActionInvocation;
09: import com.opensymphony.xwork.Result;
10: import com.opensymphony.xwork.util.OgnlValueStack;
11: import junit.framework.Assert;
12: import org.apache.commons.logging.Log;
13: import org.apache.commons.logging.LogFactory;
14:
15: import java.util.ArrayList;
16: import java.util.List;
17:
18: /**
19: * TestResult
20: *
21: * @author Jason Carreira
22: * Created Apr 12, 2003 9:49:35 PM
23: */
24: public class TestResult implements Result {
25:
26: private static final Log LOG = LogFactory.getLog(TestResult.class);
27:
28: private List expectedValues = new ArrayList();
29: private List propertyNames = new ArrayList();
30:
31: public void setExpectedValue(int index, String value) {
32: expectedValues.set(index, value);
33: }
34:
35: public void setExpectedValue(String value) {
36: expectedValues.add(value);
37: }
38:
39: public List getExpectedValues() {
40: return expectedValues;
41: }
42:
43: public void setPropertyName(int index, String propertyName) {
44: propertyNames.set(index, propertyName);
45: }
46:
47: public void setPropertyName(String propertyName) {
48: propertyNames.add(propertyName);
49: }
50:
51: public List getPropertyNames() {
52: return propertyNames;
53: }
54:
55: public void execute(ActionInvocation invocation) throws Exception {
56: LOG.info("executing TestResult.");
57:
58: if ((expectedValues != null) && (expectedValues.size() > 0)
59: && (propertyNames != null)
60: && (propertyNames.size() > 0)) {
61: OgnlValueStack stack = ActionContext.getContext()
62: .getValueStack();
63:
64: for (int i = 0; i < propertyNames.size(); i++) {
65: String propertyName = (String) propertyNames.get(i);
66: String expectedValue = null;
67:
68: if (i < expectedValues.size()) {
69: expectedValue = (String) expectedValues.get(i);
70: }
71:
72: String value = (String) stack.findValue(propertyName,
73: String.class);
74: Assert.assertEquals(expectedValue, value);
75: }
76: } else {
77: LOG.error("One of expectedValues = " + expectedValues
78: + " and propertyNames = " + propertyNames
79: + " was null or empty.");
80: Assert.fail();
81: }
82: }
83: }
|