01: package com.mockrunner.struts;
02:
03: import java.lang.reflect.Method;
04:
05: import org.apache.struts.action.ActionMapping;
06: import org.apache.struts.config.ActionConfig;
07:
08: import com.mockrunner.base.NestedApplicationException;
09: import com.mockrunner.mock.web.MockActionMapping;
10: import com.mockrunner.util.common.MethodUtil;
11:
12: /**
13: * Helper class to generate CGLib proxies for <code>ActionMapping</code>. Not meant for application use.
14: */
15: public class ActionMappingProxyGenerator {
16: private final static Method[] delegateMethods;
17: private final static Method[] duplicateMethods;
18: static {
19: delegateMethods = new Method[3];
20: try {
21: delegateMethods[0] = MockActionMapping.class
22: .getDeclaredMethod("findForward",
23: new Class[] { String.class });
24: delegateMethods[1] = MockActionMapping.class
25: .getDeclaredMethod("findForwards", null);
26: delegateMethods[2] = MockActionMapping.class
27: .getDeclaredMethod("getInputForward", null);
28: duplicateMethods = MethodUtil.getMatchingDeclaredMethods(
29: ActionConfig.class, "(set.*)|(remove.*)|(add.*)");
30: } catch (Exception exc) {
31: throw new NestedApplicationException(exc);
32: }
33: }
34:
35: private MockActionMapping delegateMapping;
36:
37: public ActionMappingProxyGenerator(MockActionMapping delegateMapping) {
38: this .delegateMapping = delegateMapping;
39: }
40:
41: public ActionMapping createActionMappingProxy(Class mappingClass) {
42: if (null == mappingClass)
43: return null;
44: if (!ActionMapping.class.isAssignableFrom(mappingClass)) {
45: throw new ClassCastException(mappingClass.getClass()
46: .getName()
47: + " must be an instance of "
48: + ActionMapping.class.getName());
49: }
50: DynamicMockProxyGenerator generator = new DynamicMockProxyGenerator(
51: mappingClass, delegateMapping, delegateMethods,
52: duplicateMethods);
53: return (ActionMapping) generator.createProxy();
54: }
55: }
|