View Javadoc

1   /*
2    * $Id: TestResult.java 451544 2006-09-30 05:38:02Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2;
19  
20  import java.util.ArrayList;
21  import java.util.List;
22  
23  import junit.framework.Assert;
24  
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  
28  import com.opensymphony.xwork2.ActionContext;
29  import com.opensymphony.xwork2.ActionInvocation;
30  import com.opensymphony.xwork2.Result;
31  import com.opensymphony.xwork2.util.ValueStack;
32  
33  
34  /***
35   * TestResult
36   *
37   */
38  public class TestResult implements Result {
39  
40  	private static final long serialVersionUID = -4429258122011663164L;
41  
42  
43  	private static final Log LOG = LogFactory.getLog(TestResult.class);
44  
45  
46      private List expectedValues = new ArrayList();
47      private List propertyNames = new ArrayList();
48  
49  
50      public void setExpectedValue(int index, String value) {
51          expectedValues.set(index, value);
52      }
53  
54      public void setExpectedValue(String value) {
55          expectedValues.add(value);
56      }
57  
58      public List getExpectedValues() {
59          return expectedValues;
60      }
61  
62      public void setPropertyName(int index, String propertyName) {
63          propertyNames.set(index, propertyName);
64      }
65  
66      public void setPropertyName(String propertyName) {
67          propertyNames.add(propertyName);
68      }
69  
70      public List getPropertyNames() {
71          return propertyNames;
72      }
73  
74      public void execute(ActionInvocation invocation) throws Exception {
75          LOG.info("executing TestResult.");
76  
77          if ((expectedValues != null) && (expectedValues.size() > 0) && (propertyNames != null) && (propertyNames.size() > 0))
78          {
79              ValueStack stack = ActionContext.getContext().getValueStack();
80  
81              for (int i = 0; i < propertyNames.size(); i++) {
82                  String propertyName = (String) propertyNames.get(i);
83                  String expectedValue = null;
84  
85                  if (i < expectedValues.size()) {
86                      expectedValue = (String) expectedValues.get(i);
87                  }
88  
89                  String value = (String) stack.findValue(propertyName, String.class);
90                  Assert.assertEquals(expectedValue, value);
91              }
92          } else {
93              LOG.error("One of expectedValues = " + expectedValues + " and propertyNames = " + propertyNames + " was null or empty.");
94              Assert.fail();
95          }
96      }
97  }