/** * Single point of {@link ActionDefinition} creation. * Also performs the replacement of action path macros! */ protected ActionDefinition createActionDef(String path, String httpMethod, String resultBasePath, final ActionNames actionNames) { path = replaceActionNameMacros(path, actionNames); if (httpMethod != null) { httpMethod = replaceActionNameMacros(httpMethod, actionNames); } if (resultBasePath != null) { resultBasePath = replaceActionNameMacros(resultBasePath, actionNames); } return new ActionDefinition(path, httpMethod, resultBasePath); }
actionDefinition = new ActionDefinition(actionPath, method, resultBasePath); actionDefinition = new ActionDefinition(actionPath, method);
protected ActionRequest createActionRequest(String actionPath) { HttpServletRequest servletRequest = mock(HttpServletRequest.class); HttpServletResponse servletResponse = mock(HttpServletResponse.class); HttpSession httpSession = mock(HttpSession.class); ServletContext servletContext = mock(ServletContext.class); when(servletRequest.getSession()).thenReturn(httpSession); when(httpSession.getServletContext()).thenReturn(servletContext); MadvocController madvocController = new MadvocController(); Object action = new Object(); ActionRuntime actionRuntime = new ActionRuntime( null, Action.class, ClassUtil.findMethod(Action.class, "view"), null, null, new ActionDefinition(actionPath, "GET"), ServletDispatcherActionResult.class, null, false, false, null, null); return new ActionRequest(madvocController, actionRuntime.getActionPath(), MadvocUtil.splitPathToChunks(actionRuntime.getActionPath()), actionRuntime, action, servletRequest, servletResponse); }
private MyActionRequest createMyActionRequest(ActionFilter[] actionFilters, ActionInterceptor[] actionInterceptors) { SimpleMadvocController madvocController = new SimpleMadvocController(); Action action = new Action(); ActionRuntime actionRuntime = new ActionRuntime( null, Action.class, ClassUtil.findMethod(Action.class, "view"), actionFilters, actionInterceptors, new ActionDefinition("path", "method"), ServletDispatcherActionResult.class, null, false, false, null, null); return new MyActionRequest( madvocController, "actionPath", actionRuntime, action, null, null); }
@Test void testActionPathMacros2() { WebApp webapp = new WebApp(); webapp.start(); ActionsManager actionsManager = webapp.madvocContainer().lookupComponent(ActionsManager.class); actionsManager.registerAction(FooAction.class, "two", new ActionDefinition("/xxx-{two}")); actionsManager.registerAction(FooAction.class, "one", new ActionDefinition("/{one}")); ActionRuntime actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/foo")); assertEquals("one", actionRuntime.getActionClassMethod().getName()); actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/foo/boo")); assertNull(actionRuntime); actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/xxx-foo")); assertEquals("two", actionRuntime.getActionClassMethod().getName()); // best match! }
@Test void testActionPathMacros4() { WebApp webapp = new WebApp(); webapp.start(); ActionsManager actionsManager = webapp.madvocContainer().lookupComponent(ActionsManager.class); actionsManager.registerAction(FooAction.class, "one", new ActionDefinition("/dummy")); // no macro actionsManager.registerAction(FooAction.class, "one", new ActionDefinition("/{one}")); actionsManager.registerAction(FooAction.class, "three", new ActionDefinition("/life/{three}")); actionsManager.registerAction(FooAction.class, "two", new ActionDefinition("/{two}/{three}")); ActionRuntime actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/foo")); assertEquals("one", actionRuntime.getActionClassMethod().getName()); actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/scott/ramonna")); assertEquals("two", actionRuntime.getActionClassMethod().getName()); actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/life/universe")); assertEquals("three", actionRuntime.getActionClassMethod().getName()); actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/scott/ramonna/envy")); assertNull(actionRuntime); actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/life/universe/else")); assertNull(actionRuntime); }
@Test void testActionPathMacros3() { WebApp webapp = new WebApp(); webapp.start(); ActionsManager actionsManager = webapp.madvocContainer().lookupComponent(ActionsManager.class); actionsManager.registerAction(FooAction.class, "one", new ActionDefinition("/yyy-{one}")); actionsManager.registerAction(FooAction.class, "two", new ActionDefinition("/xxx-{two}")); assertEquals(2, actionsManager.getActionsCount()); ActionRuntime actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/foo")); assertNull(actionRuntime); actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/yyy-111")); assertEquals("one", actionRuntime.getActionClassMethod().getName()); actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/xxx-222")); assertEquals("two", actionRuntime.getActionClassMethod().getName()); try { actionsManager.registerAction(FooAction.class, "two", new ActionDefinition("/xxx-{two}")); fail("error"); } catch (Exception ex) { // ignore } }
@Test void testActionPathMacrosWildcard() { WebApp webapp = new WebApp(); webapp.start(); ActionsManager actionsManager = webapp.madvocContainer().lookupComponent(ActionsManager.class); actionsManager.setPathMacroClass(WildcardPathMacros.class); actionsManager.registerAction(FooAction.class, "one", new ActionDefinition("/{one:a?a}")); ActionRuntime actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/aaa")); assertNotNull(actionRuntime); actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/aab")); assertNull(actionRuntime); } }
@Test void testActionPathMacrosRegexp() { WebApp webapp = new WebApp(); webapp.start(); ActionsManager actionsManager = webapp.madvocContainer().lookupComponent(ActionsManager.class); actionsManager.setPathMacroClass(RegExpPathMacros.class); actionsManager.registerAction(FooAction.class, "one", new ActionDefinition("/{one:[ab]+}")); ActionRuntime actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/a")); assertNotNull(actionRuntime); actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/ac")); assertNull(actionRuntime); }
@Test void testActionPathMacros1() { WebApp webapp = new WebApp(); webapp.start(); ActionsManager actionsManager = webapp.madvocContainer().lookupComponent(ActionsManager.class); actionsManager.registerAction(FooAction.class, "one", new ActionDefinition("/{one}")); ActionRuntime actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/foo")); assertNotNull(actionRuntime); actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/foo/boo")); assertNull(actionRuntime); actionRuntime = actionsManager.routes.lookup(null, MadvocUtil.splitPathToChunks("/foo/boo/zoo")); assertNull(actionRuntime); }