1 5 package com.opensymphony.webwork; 6 7 import com.opensymphony.xwork.ActionContext; 8 import com.opensymphony.xwork.ActionInvocation; 9 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 25 public class TestResult implements Result { 26 28 private static final Log LOG = LogFactory.getLog(TestResult.class); 29 30 32 private List expectedValues = new ArrayList (); 33 private List propertyNames = new ArrayList (); 34 35 37 public void setExpectedValue(int index, String value) { 38 expectedValues.set(index, value); 39 } 40 41 public void setExpectedValue(String value) { 42 expectedValues.add(value); 43 } 44 45 public List getExpectedValues() { 46 return expectedValues; 47 } 48 49 public void setPropertyName(int index, String propertyName) { 50 propertyNames.set(index, propertyName); 51 } 52 53 public void setPropertyName(String propertyName) { 54 propertyNames.add(propertyName); 55 } 56 57 public List getPropertyNames() { 58 return propertyNames; 59 } 60 61 public void execute(ActionInvocation invocation) throws Exception { 62 LOG.info("executing TestResult."); 63 64 if ((expectedValues != null) && (expectedValues.size() > 0) && (propertyNames != null) && (propertyNames.size() > 0)) { 65 OgnlValueStack stack = ActionContext.getContext().getValueStack(); 66 67 for (int i = 0; i < propertyNames.size(); i++) { 68 String propertyName = (String ) propertyNames.get(i); 69 String expectedValue = null; 70 71 if (i < expectedValues.size()) { 72 expectedValue = (String ) expectedValues.get(i); 73 } 74 75 String value = (String ) stack.findValue(propertyName, String .class); 76 Assert.assertEquals(expectedValue, value); 77 } 78 } else { 79 LOG.error("One of expectedValues = " + expectedValues + " and propertyNames = " + propertyNames + " was null or empty."); 80 Assert.fail(); 81 } 82 } 83 } 84 | Popular Tags |