001 package com.mockrunner.mock.web; 002 003 import java.util.HashMap; 004 import java.util.Iterator; 005 import java.util.Map; 006 007 import org.apache.struts.action.ActionForward; 008 import org.apache.struts.action.ActionMapping; 009 010 /** 011 * Mock implementation of <code>ActionMapping</code>. 012 */ 013 public class MockActionMapping extends ActionMapping 014 { 015 private Map forwards; 016 017 public MockActionMapping() 018 { 019 super(); 020 forwards = new HashMap(); 021 } 022 023 /** 024 * Clears all specified forwards. 025 */ 026 public void clearForwards() 027 { 028 forwards.clear(); 029 } 030 031 /** 032 * Always return a valid <code>ActionForward</code> 033 * since we do not care if it exists in the 034 * struts-config. If an <code>ActionForward</code> 035 * was defined using {@link #addForward}, 036 * this <code>ActionForward</code> will be returned. 037 * Otherwise a new <code>ActionForward</code> 038 * (with equal name and path) will be returned. 039 * @param name the name 040 * @return the corresponding <code>ActionForward</code> 041 */ 042 public ActionForward findForward(String name) 043 { 044 Iterator iterator = forwards.keySet().iterator(); 045 while(iterator.hasNext()) 046 { 047 String key = (String)iterator.next(); 048 if(key.equals(name)) 049 { 050 return (ActionForward)forwards.get(key); 051 } 052 } 053 return new MockActionForward(name, name, false); 054 } 055 056 /** 057 * Adds an <code>ActionForward</code> 058 * with the specified name and path. 059 * @param forwardName the name of the forward 060 * @param forwardPath the path of the forward 061 */ 062 public void addForward(String forwardName, String forwardPath) 063 { 064 ActionForward forward = new MockActionForward(forwardName, forwardPath, false); 065 forwards.put(forwardName, forward); 066 } 067 068 /** 069 * Sets multiple <code>ActionForward</code> objects 070 * with equal name and path. 071 * @param forwardNames the forward names 072 */ 073 public void setupForwards(String[] forwardNames) 074 { 075 if(null == forwardNames) return; 076 for(int ii = 0; ii < forwardNames.length; ii++) 077 { 078 String name = forwardNames[ii]; 079 ActionForward forward = new MockActionForward(name, name, false); 080 forwards.put(name, forward); 081 } 082 } 083 084 /** 085 * Returns all forward names (set using {@link #addForward} 086 * or {@link #setupForwards}). 087 * @return the forward names 088 */ 089 public String[] findForwards() 090 { 091 return (String[])forwards.keySet().toArray(new String[forwards.size()]); 092 } 093 094 /** 095 * Always return a valid <code>ActionForward</code>. 096 * The input parameter of this mapping will be used 097 * as the name and path for the <code>ActionForward</code>. 098 */ 099 public ActionForward getInputForward() 100 { 101 return new MockActionForward(getInput(), getInput(), false); 102 } 103 }