KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > webwork > TestResult


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

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 JavaDoc;
16 import java.util.List JavaDoc;
17
18
19 /**
20  * TestResult
21  *
22  * @author Jason Carreira
23  * Created Apr 12, 2003 9:49:35 PM
24  */

25 public class TestResult implements Result {
26     //~ Static fields/initializers /////////////////////////////////////////////
27

28     private static final Log LOG = LogFactory.getLog(TestResult.class);
29
30     //~ Instance fields ////////////////////////////////////////////////////////
31

32     private List JavaDoc expectedValues = new ArrayList JavaDoc();
33     private List JavaDoc propertyNames = new ArrayList JavaDoc();
34
35     //~ Methods ////////////////////////////////////////////////////////////////
36

37     public void setExpectedValue(int index, String JavaDoc value) {
38         expectedValues.set(index, value);
39     }
40
41     public void setExpectedValue(String JavaDoc value) {
42         expectedValues.add(value);
43     }
44
45     public List JavaDoc getExpectedValues() {
46         return expectedValues;
47     }
48
49     public void setPropertyName(int index, String JavaDoc propertyName) {
50         propertyNames.set(index, propertyName);
51     }
52
53     public void setPropertyName(String JavaDoc propertyName) {
54         propertyNames.add(propertyName);
55     }
56
57     public List JavaDoc getPropertyNames() {
58         return propertyNames;
59     }
60
61     public void execute(ActionInvocation invocation) throws Exception JavaDoc {
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 JavaDoc propertyName = (String JavaDoc) propertyNames.get(i);
69                 String JavaDoc expectedValue = null;
70
71                 if (i < expectedValues.size()) {
72                     expectedValue = (String JavaDoc) expectedValues.get(i);
73                 }
74
75                 String JavaDoc value = (String JavaDoc) stack.findValue(propertyName, String JavaDoc.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