001    package com.mockrunner.mock.web;
002    
003    import java.lang.reflect.Method;
004    import java.util.HashMap;
005    import java.util.Map;
006    
007    import javax.servlet.jsp.el.FunctionMapper;
008    
009    /**
010     * Mock implementation of <code>FunctionMapper</code>.
011     */
012    public class MockFunctionMapper implements FunctionMapper
013    {
014        private Map functions = new HashMap();
015        
016        /**
017         * Adds a function for the specified prefix and name.
018         * @param prefix the prefix of the function
019         * @param localName the name of the function
020         * @param function the function as <code>Method</code>
021         */
022        public void addFunction(String prefix, String localName, Method function)
023        {
024            FunctionMappingEntry entry = new FunctionMappingEntry(prefix, localName);
025            functions.put(entry, function);
026        }
027        
028        /**
029         * Clears all functions.
030         */
031        public void clearFunctions()
032        {
033            functions.clear();
034        }
035        
036        public Method resolveFunction(String prefix, String localName)
037        {
038            FunctionMappingEntry entry = new FunctionMappingEntry(prefix, localName);
039            return (Method)functions.get(entry);
040        }
041    
042        private class FunctionMappingEntry
043        {
044            private String prefix;
045            private String localName;
046            
047            public FunctionMappingEntry(String prefix, String localName)
048            {
049                this.prefix = prefix;
050                this.localName = localName;
051            }
052            
053            public String getLocalName()
054            {
055                return localName;
056            }
057    
058            public String getPrefix()
059            {
060                return prefix;
061            }
062            
063            public boolean equals(Object obj)
064            {
065                if(!(obj instanceof FunctionMappingEntry)) return false;
066                if(obj == this) return true;
067                FunctionMappingEntry otherEntry = (FunctionMappingEntry)obj;
068                boolean prefixOk = false;
069                boolean nameOk = false;
070                if(null == prefix)
071                {
072                    prefixOk = (otherEntry.getPrefix() == null);
073                }
074                else
075                {
076                    prefixOk = prefix.equals(otherEntry.getPrefix());
077                }
078                if(null == localName)
079                {
080                    nameOk = (otherEntry.getLocalName() == null);
081                }
082                else
083                {
084                    nameOk = localName.equals(otherEntry.getLocalName());
085                }
086                return (prefixOk && nameOk);
087            }
088    
089            public int hashCode()
090            {
091                int hashCode = 17;
092                if(null != prefix) hashCode = (31 * hashCode) + prefix.hashCode();
093                if(null != localName) hashCode = (31 * hashCode) + localName.hashCode();
094                return hashCode;
095            }
096        }
097    }