01: package com.mockrunner.mock.web;
02:
03: import java.util.HashMap;
04: import java.util.Iterator;
05: import java.util.Map;
06:
07: import org.apache.struts.action.ActionForward;
08: import org.apache.struts.action.ActionMapping;
09:
10: /**
11: * Mock implementation of <code>ActionMapping</code>.
12: */
13: public class MockActionMapping extends ActionMapping {
14: private Map forwards;
15:
16: public MockActionMapping() {
17: super ();
18: forwards = new HashMap();
19: }
20:
21: /**
22: * Clears all specified forwards.
23: */
24: public void clearForwards() {
25: forwards.clear();
26: }
27:
28: /**
29: * Always return a valid <code>ActionForward</code>
30: * since we do not care if it exists in the
31: * struts-config. If an <code>ActionForward</code>
32: * was defined using {@link #addForward},
33: * this <code>ActionForward</code> will be returned.
34: * Otherwise a new <code>ActionForward</code>
35: * (with equal name and path) will be returned.
36: * @param name the name
37: * @return the corresponding <code>ActionForward</code>
38: */
39: public ActionForward findForward(String name) {
40: Iterator iterator = forwards.keySet().iterator();
41: while (iterator.hasNext()) {
42: String key = (String) iterator.next();
43: if (key.equals(name)) {
44: return (ActionForward) forwards.get(key);
45: }
46: }
47: return new MockActionForward(name, name, false);
48: }
49:
50: /**
51: * Adds an <code>ActionForward</code>
52: * with the specified name and path.
53: * @param forwardName the name of the forward
54: * @param forwardPath the path of the forward
55: */
56: public void addForward(String forwardName, String forwardPath) {
57: ActionForward forward = new MockActionForward(forwardName,
58: forwardPath, false);
59: forwards.put(forwardName, forward);
60: }
61:
62: /**
63: * Sets multiple <code>ActionForward</code> objects
64: * with equal name and path.
65: * @param forwardNames the forward names
66: */
67: public void setupForwards(String[] forwardNames) {
68: if (null == forwardNames)
69: return;
70: for (int ii = 0; ii < forwardNames.length; ii++) {
71: String name = forwardNames[ii];
72: ActionForward forward = new MockActionForward(name, name,
73: false);
74: forwards.put(name, forward);
75: }
76: }
77:
78: /**
79: * Returns all forward names (set using {@link #addForward}
80: * or {@link #setupForwards}).
81: * @return the forward names
82: */
83: public String[] findForwards() {
84: return (String[]) forwards.keySet().toArray(
85: new String[forwards.size()]);
86: }
87:
88: /**
89: * Always return a valid <code>ActionForward</code>.
90: * The input parameter of this mapping will be used
91: * as the name and path for the <code>ActionForward</code>.
92: */
93: public ActionForward getInputForward() {
94: return new MockActionForward(getInput(), getInput(), false);
95: }
96: }
|