01: package com.mockrunner.mock.web;
02:
03: import java.lang.reflect.Method;
04: import java.util.HashMap;
05: import java.util.Map;
06:
07: import javax.servlet.jsp.el.FunctionMapper;
08:
09: /**
10: * Mock implementation of <code>FunctionMapper</code>.
11: */
12: public class MockFunctionMapper implements FunctionMapper {
13: private Map functions = new HashMap();
14:
15: /**
16: * Adds a function for the specified prefix and name.
17: * @param prefix the prefix of the function
18: * @param localName the name of the function
19: * @param function the function as <code>Method</code>
20: */
21: public void addFunction(String prefix, String localName,
22: Method function) {
23: FunctionMappingEntry entry = new FunctionMappingEntry(prefix,
24: localName);
25: functions.put(entry, function);
26: }
27:
28: /**
29: * Clears all functions.
30: */
31: public void clearFunctions() {
32: functions.clear();
33: }
34:
35: public Method resolveFunction(String prefix, String localName) {
36: FunctionMappingEntry entry = new FunctionMappingEntry(prefix,
37: localName);
38: return (Method) functions.get(entry);
39: }
40:
41: private class FunctionMappingEntry {
42: private String prefix;
43: private String localName;
44:
45: public FunctionMappingEntry(String prefix, String localName) {
46: this .prefix = prefix;
47: this .localName = localName;
48: }
49:
50: public String getLocalName() {
51: return localName;
52: }
53:
54: public String getPrefix() {
55: return prefix;
56: }
57:
58: public boolean equals(Object obj) {
59: if (!(obj instanceof FunctionMappingEntry))
60: return false;
61: if (obj == this )
62: return true;
63: FunctionMappingEntry otherEntry = (FunctionMappingEntry) obj;
64: boolean prefixOk = false;
65: boolean nameOk = false;
66: if (null == prefix) {
67: prefixOk = (otherEntry.getPrefix() == null);
68: } else {
69: prefixOk = prefix.equals(otherEntry.getPrefix());
70: }
71: if (null == localName) {
72: nameOk = (otherEntry.getLocalName() == null);
73: } else {
74: nameOk = localName.equals(otherEntry.getLocalName());
75: }
76: return (prefixOk && nameOk);
77: }
78:
79: public int hashCode() {
80: int hashCode = 17;
81: if (null != prefix)
82: hashCode = (31 * hashCode) + prefix.hashCode();
83: if (null != localName)
84: hashCode = (31 * hashCode) + localName.hashCode();
85: return hashCode;
86: }
87: }
88: }
|