View Javadoc

1   /*
2    * $Id: TestResult.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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 }