001: /*
002: * $Id: TestResult.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2;
022:
023: import java.util.ArrayList;
024: import java.util.List;
025:
026: import junit.framework.Assert;
027:
028: import org.apache.commons.logging.Log;
029: import org.apache.commons.logging.LogFactory;
030:
031: import com.opensymphony.xwork2.ActionContext;
032: import com.opensymphony.xwork2.ActionInvocation;
033: import com.opensymphony.xwork2.Result;
034: import com.opensymphony.xwork2.util.ValueStack;
035:
036: /**
037: * TestResult
038: *
039: */
040: public class TestResult implements Result {
041:
042: private static final long serialVersionUID = -4429258122011663164L;
043:
044: private static final Log LOG = LogFactory.getLog(TestResult.class);
045:
046: private List expectedValues = new ArrayList();
047: private List propertyNames = new ArrayList();
048:
049: public void setExpectedValue(int index, String value) {
050: expectedValues.set(index, value);
051: }
052:
053: public void setExpectedValue(String value) {
054: expectedValues.add(value);
055: }
056:
057: public List getExpectedValues() {
058: return expectedValues;
059: }
060:
061: public void setPropertyName(int index, String propertyName) {
062: propertyNames.set(index, propertyName);
063: }
064:
065: public void setPropertyName(String propertyName) {
066: propertyNames.add(propertyName);
067: }
068:
069: public List getPropertyNames() {
070: return propertyNames;
071: }
072:
073: public void execute(ActionInvocation invocation) throws Exception {
074: LOG.info("executing TestResult.");
075:
076: if ((expectedValues != null) && (expectedValues.size() > 0)
077: && (propertyNames != null)
078: && (propertyNames.size() > 0)) {
079: ValueStack stack = ActionContext.getContext()
080: .getValueStack();
081:
082: for (int i = 0; i < propertyNames.size(); i++) {
083: String propertyName = (String) propertyNames.get(i);
084: String expectedValue = null;
085:
086: if (i < expectedValues.size()) {
087: expectedValue = (String) expectedValues.get(i);
088: }
089:
090: String value = (String) stack.findValue(propertyName,
091: String.class);
092: Assert.assertEquals(expectedValue, value);
093: }
094: } else {
095: LOG.error("One of expectedValues = " + expectedValues
096: + " and propertyNames = " + propertyNames
097: + " was null or empty.");
098: Assert.fail();
099: }
100: }
101: }
|