001 package com.mockrunner.struts;
002
003 import java.lang.reflect.Method;
004
005 import org.apache.struts.action.ActionMapping;
006 import org.apache.struts.config.ActionConfig;
007
008 import com.mockrunner.base.NestedApplicationException;
009 import com.mockrunner.mock.web.MockActionMapping;
010 import com.mockrunner.util.common.MethodUtil;
011
012 /**
013 * Helper class to generate CGLib proxies for <code>ActionMapping</code>. Not meant for application use.
014 */
015 public class ActionMappingProxyGenerator
016 {
017 private final static Method[] delegateMethods;
018 private final static Method[] duplicateMethods;
019 static
020 {
021 delegateMethods = new Method[3];
022 try
023 {
024 delegateMethods[0] = MockActionMapping.class.getDeclaredMethod("findForward", new Class[] {String.class});
025 delegateMethods[1] = MockActionMapping.class.getDeclaredMethod("findForwards", null);
026 delegateMethods[2] = MockActionMapping.class.getDeclaredMethod("getInputForward", null);
027 duplicateMethods = MethodUtil.getMatchingDeclaredMethods(ActionConfig.class, "(set.*)|(remove.*)|(add.*)");
028 }
029 catch(Exception exc)
030 {
031 throw new NestedApplicationException(exc);
032 }
033 }
034
035
036 private MockActionMapping delegateMapping;
037
038 public ActionMappingProxyGenerator(MockActionMapping delegateMapping)
039 {
040 this.delegateMapping = delegateMapping;
041 }
042
043 public ActionMapping createActionMappingProxy(Class mappingClass)
044 {
045 if(null == mappingClass) return null;
046 if(!ActionMapping.class.isAssignableFrom(mappingClass))
047 {
048 throw new ClassCastException(mappingClass.getClass().getName() + " must be an instance of " + ActionMapping.class.getName());
049 }
050 DynamicMockProxyGenerator generator = new DynamicMockProxyGenerator(mappingClass, delegateMapping, delegateMethods, duplicateMethods);
051 return (ActionMapping)generator.createProxy();
052 }
053 }