All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mockrunner.mock.web.MockActionMapping Maven / Gradle / Ivy

Go to download

Mockrunner is a lightweight framework for unit testing applications in the J2EE environment. It supports servlets, filters, tag classes and Struts actions. It includes a JDBC a JMS and a JCA test framework and can be used to test EJB based applications.

The newest version!
package com.mockrunner.mock.web;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * Mock implementation of ActionMapping.
 */
public class MockActionMapping extends ActionMapping
{
    private Map forwards;

    public MockActionMapping()
    {
        super();
        forwards = new HashMap();
    }
    
    /**
     * Clears all specified forwards.
     */
    public void clearForwards()
    {
        forwards.clear();
    }
    
    /**
     * Always return a valid ActionForward
     * since we do not care if it exists in the
     * struts-config. If an ActionForward
     * was defined using {@link #addForward},
     * this ActionForward will be returned.
     * Otherwise a new ActionForward
     * (with equal name and path) will be returned.
     * @param name the name
     * @return the corresponding ActionForward
     */
    public ActionForward findForward(String name)
    {
        Iterator iterator = forwards.keySet().iterator();
        while(iterator.hasNext())
        { 
            String key = (String)iterator.next();
            if(key.equals(name))
            {
                return (ActionForward)forwards.get(key);
            }
        }
        return new MockActionForward(name, name, false); 
    }
    
    /**
     * Adds an ActionForward
     * with the specified name and path.
     * @param forwardName the name of the forward
     * @param forwardPath the path of the forward
     */
    public void addForward(String forwardName, String forwardPath) 
    { 
        ActionForward forward = new MockActionForward(forwardName, forwardPath, false); 
        forwards.put(forwardName, forward); 
    } 
    
    /**
     * Sets multiple ActionForward objects
     * with equal name and path.
     * @param forwardNames the forward names
     */
    public void setupForwards(String[] forwardNames)
    {
        if(null == forwardNames) return; 
        for(int ii = 0; ii < forwardNames.length; ii++) 
        { 
            String name = forwardNames[ii];
            ActionForward forward = new MockActionForward(name, name, false); 
            forwards.put(name, forward);
        }
    }

    /**
     * Returns all forward names (set using {@link #addForward}
     * or {@link #setupForwards}).
     * @return the forward names
     */
    public String[] findForwards()
    {
        return (String[])forwards.keySet().toArray(new String[forwards.size()]);
    }
    
    /**
     * Always return a valid ActionForward.
     * The input parameter of this mapping will be used
     * as the name and path for the ActionForward.
     */
    public ActionForward getInputForward()
    {
        return new MockActionForward(getInput(), getInput(), false);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy