/** * Lookups value as an alias and, if not found, as a default alias. */ protected String lookupAlias(final String alias) { String value = actionsManager.lookupPathAlias(alias); if (value == null) { ActionRuntime cfg = actionsManager.lookup(alias); if (cfg != null) { value = cfg.getActionPath(); } } return value; }
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); }
actionRuntime -> actionRuntime.getActionPath() + ' ' + actionRuntime.getActionMethod())) .forEach(ar -> { Chalk256.chalk().green(), ar.getActionPath(), Chalk256.chalk().blue(), signature, width - 7 - 1 actionRuntime -> actionRuntime.getActionPath() + ' ' + actionRuntime.getActionMethod())) .forEach(ar -> { final String actionPath = ar.getActionPath();
/** * Registers manually created {@link ActionRuntime action runtime configurations}. * Optionally, if action path with the same name already exist, * exception will be thrown. */ public ActionRuntime registerActionRuntime(final ActionRuntime actionRuntime) { final String actionPath = actionRuntime.getActionPath(); final String method = actionRuntime.getActionMethod(); log.debug(() -> "Madvoc action: " + ifNotNull(method, m -> m + " ") + actionRuntime.getActionPath() + " => " + actionRuntime.createActionString()); final RouteChunk routeChunk = routes.registerPath(method, actionPath); if (routeChunk.value() != null) { // existing chunk if (detectDuplicatePathsEnabled) { throw new MadvocException("Duplicate action path for [" + actionRuntime + "] occupied by: [" + routeChunk.value() + "]"); } } else { actionsCount++; } routeChunk.bind(actionRuntime); // finally runtimes.put(actionRuntime.createActionString(), actionRuntime); // async check if (actionRuntime.isAsync()) { asyncMode = true; } return actionRuntime; }
@Test void testDefaultMethods() { WebApp webapp = new WebApp(); webapp.start(); ActionMethodParser actionMethodParser = webapp.madvocContainer().lookupComponent(ActionMethodParser.class); ActionRuntime cfg = parse(actionMethodParser, "fixtures.tst.BooAction#foo"); assertEquals("/boo.foo", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst.BooAction#view"); assertEquals("/boo", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst.BooAction#execute"); assertEquals("/boo", cfg.getActionPath()); }
@Test void testClasses() { WebApp webapp = new WebApp(); webapp.start(); ActionMethodParser actionMethodParser = webapp.madvocContainer().lookupComponent(ActionMethodParser.class); ActionRuntime cfg = parse(actionMethodParser, "fixtures.tst.Boo1Action#foo"); assertNotNull(cfg); assertEquals(Boo1Action.class, cfg.getActionClass()); assertEquals("/boo1.foo", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst.Boo2Action#foo"); assertNotNull(cfg); assertEquals(Boo2Action.class, cfg.getActionClass()); assertEquals("/bbb.foo", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst.Boo2Action#foo1"); assertEquals("/bbb.xxx", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst.Boo2Action#foo2"); assertEquals("/bbb.foo2.xxx", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst.Boo2Action#foo3"); assertEquals("/bbb", cfg.getActionPath()); }
@Test void testPackage() { WebApp webapp = new WebApp(); webapp.start(); ActionMethodParser actionMethodParser = webapp.madvocContainer().lookupComponent(ActionMethodParser.class); RootPackages rootPackages = webapp.madvocContainer().lookupComponent(RootPackages.class); rootPackages.addRootPackageOf(this.getClass()); ActionRuntime cfg = parse(actionMethodParser, "fixtures.tst2.Boo4Action#foo"); assertNotNull(cfg); assertEquals(Boo4Action.class, cfg.getActionClass()); assertEquals("/ttt/www.foo", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst2.Boo4Action#foo1"); assertEquals("/ttt/www.xxx", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst2.Boo4Action#foo2"); assertEquals("/ttt/www.foo2.xxx", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst2.Boo4Action#foo3"); assertEquals("/ttt/www", cfg.getActionPath()); }
@Test void testNoPackage() { WebApp webapp = new WebApp(); webapp.start(); ActionMethodParser actionMethodParser = webapp.madvocContainer().lookupComponent(ActionMethodParser.class); RootPackages rootPackages = webapp.madvocContainer().lookupComponent(RootPackages.class); rootPackages.addRootPackageOf(this.getClass()); ActionRuntime cfg = parse(actionMethodParser, "fixtures.tst2.Boo5Action#foo"); assertNotNull(cfg); assertEquals(Boo5Action.class, cfg.getActionClass()); assertEquals("/www.foo", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst2.Boo5Action#foo1"); assertEquals("/www.xxx", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst2.Boo5Action#foo2"); assertEquals("/www.foo2.xxx", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst2.Boo5Action#foo3"); assertEquals("/www", cfg.getActionPath()); }
@Test void testClassesWithoutPackage() { WebApp webapp = new WebApp(); webapp.start(); ActionMethodParser actionMethodParser = webapp.madvocContainer().lookupComponent(ActionMethodParser.class); RootPackages rootPackages = webapp.madvocContainer().lookupComponent(RootPackages.class); rootPackages.addRootPackageOf(this.getClass()); ActionRuntime cfg = parse(actionMethodParser, "fixtures.tst.Boo3Action#foo"); assertNotNull(cfg); assertEquals(Boo3Action.class, cfg.getActionClass()); assertEquals("/bbb.foo", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst.Boo3Action#foo1"); assertEquals("/bbb.xxx", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst.Boo3Action#foo2"); assertEquals("/bbb.foo2.xxx", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst.Boo3Action#foo3"); assertEquals("/bbb", cfg.getActionPath()); }
@Test void testAlias3() { WebApp webapp = new WebApp(); webapp.start(); ActionsManager actionsManager = webapp.madvocContainer().lookupComponent(ActionsManager.class); actionsManager.registerAction(BooAction.class, "foo2", null); assertEquals("/boo.foo2.xxx", actionsManager.lookup(BooAction.class.getName() + "#foo2").getActionPath()); }
assertNotNull(cfg); assertEquals(BooAction.class, cfg.getActionClass()); assertEquals("/fixtures/tst/boo.foo", cfg.getActionPath()); assertEquals("/fixtures/tst/boo.xxx", cfg.getActionPath()); assertEquals("/fixtures/tst/boo.foo2.xxx", cfg.getActionPath()); assertEquals("/fixtures/tst/boo", cfg.getActionPath()); assertEquals("/xxx", cfg.getActionPath()); assertNull(cfg.getActionMethod()); assertEquals("/xxx", cfg.getActionPath()); assertEquals("DELETE", cfg.getActionMethod()); assertEquals("/xxx.html", cfg.getActionPath()); assertEquals("POST", cfg.getActionMethod()); assertEquals("/fixtures/tst/boo.qfoo62", cfg.getActionPath()); assertEquals("/foo7.html", cfg.getActionPath());
@Test void testClassesWithPackage() { WebApp webapp = new WebApp(); webapp.start(); ActionMethodParser actionMethodParser = webapp.madvocContainer().lookupComponent(ActionMethodParser.class); RootPackages rootPackages = webapp.madvocContainer().lookupComponent(RootPackages.class); rootPackages.addRootPackageOf(this.getClass()); ActionRuntime cfg = parse(actionMethodParser, "fixtures.tst.Boo1Action#foo"); assertNotNull(cfg); assertEquals(Boo1Action.class, cfg.getActionClass()); assertEquals("/fixtures/tst/boo1.foo", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst.Boo2Action#foo"); assertNotNull(cfg); assertEquals(Boo2Action.class, cfg.getActionClass()); assertEquals("/fixtures/tst/bbb.foo", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst.Boo2Action#foo1"); assertEquals("/fixtures/tst/bbb.xxx", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst.Boo2Action#foo2"); assertEquals("/fixtures/tst/bbb.foo2.xxx", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst.Boo2Action#foo3"); assertEquals("/fixtures/tst/bbb", cfg.getActionPath()); }
@Test void testMethod() { WebApp webapp = new WebApp(); webapp.start(); ActionMethodParser actionMethodParser = webapp.madvocContainer().lookupComponent(ActionMethodParser.class); ActionRuntime cfg = parse(actionMethodParser, "fixtures.tst.BooAction#foo"); assertNotNull(cfg); assertEquals(BooAction.class, cfg.getActionClass()); assertEquals("/boo.foo", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst.BooAction#foo1"); assertEquals("/boo.xxx", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst.BooAction#foo2"); assertEquals("/boo.foo2.xxx", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst.BooAction#foo3"); assertEquals("/boo", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst.BooAction#foo4"); assertEquals("/xxx", cfg.getActionPath()); assertNull(cfg.getActionMethod()); cfg = parse(actionMethodParser, "fixtures.tst.BooAction#foo41"); assertEquals("/xxx", cfg.getActionPath()); assertEquals("DELETE", cfg.getActionMethod()); cfg = parse(actionMethodParser, "fixtures.tst.BooAction#foo5"); assertEquals("/xxx.html", cfg.getActionPath()); assertEquals("POST", cfg.getActionMethod()); ActionsManager actionsManager = webapp.madvocContainer().lookupComponent(ActionsManager.class); assertEquals("/xxx.html", actionsManager.lookupPathAlias("dude")); }
@Test void testEndSlashClassName() { WebApp webapp = new WebApp(); webapp.start(); ActionMethodParser actionMethodParser = webapp.madvocContainer().lookupComponent(ActionMethodParser.class); RootPackages rootPackages = webapp.madvocContainer().lookupComponent(RootPackages.class); rootPackages.addRootPackageOf(this.getClass()); ActionRuntime cfg = parse(actionMethodParser, "fixtures.tst2.ReAction#hello"); assertNotNull(cfg); assertEquals(ReAction.class, cfg.getActionClass()); assertEquals("/re/hello", cfg.getActionPath()); cfg = parse(actionMethodParser, "fixtures.tst2.ReAction#macro"); assertNotNull(cfg); assertEquals(ReAction.class, cfg.getActionClass()); assertEquals("/re/user/{id}/macro", cfg.getActionPath()); }
@Test void testZqq() { WebApp webapp = new WebApp(); webapp.start(); ActionsManager actionsManager = webapp.madvocContainer().lookupComponent(ActionsManager.class); RootPackages rootPackages = webapp.madvocContainer().lookupComponent(RootPackages.class); rootPackages.addRootPackageOf(this.getClass()); actionsManager.registerAction(ReAction.class, "zqq1", null); actionsManager.registerAction(ReAction.class, "zqq2", null); ActionRuntime cfg = actionsManager.lookup("GET", MadvocUtil.splitPathToChunks("/config/dba.delete_multi.do")); assertNotNull(cfg); assertEquals("/{entityName}/dba.delete_multi.do", cfg.getActionPath()); }
@Test void testAlias2() { WebApp webapp = new WebApp(); webapp.start(); ActionsManager actionsManager = webapp.madvocContainer().lookupComponent(ActionsManager.class); actionsManager.registerAction(BooAction.class, "foo2", null); actionsManager.registerPathAlias("/boo.foo2.xxx", "/aliased"); ResultMapper resultMapper = webapp.madvocContainer().lookupComponent(ResultMapper.class); ActionMethodParser actionMethodParser = webapp.madvocContainer().lookupComponent(ActionMethodParser.class); ActionRuntime cfg = parse(actionMethodParser, "fixtures.tst.BooAction#foo2"); String path = cfg.getActionPath(); String resultPath = resultMapper.resolveResultPathString(path, null); assertEquals("/aliased", resultPath); }
@Test void testMacrosDups() { WebApp webapp = new WebApp(); webapp.start(); ActionsManager actionsManager = webapp.madvocContainer().lookupComponent(ActionsManager.class); webapp.madvocContainer().lookupComponent(RootPackages.class).addRootPackageOf(this.getClass()); actionsManager.setPathMacroClass(RegExpPathMacros.class); actionsManager.registerAction(ReAction.class, "duplo2", null); actionsManager.registerAction(ReAction.class, "duplo1", null); ActionRuntime cfg = actionsManager.lookup("GET", MadvocUtil.splitPathToChunks("/re/duplo/123")); assertNotNull(cfg); assertEquals(ReAction.class, cfg.getActionClass()); assertEquals("/re/duplo/{id:^[0-9]+}", cfg.getActionPath()); RouteChunk chunk = cfg.getRouteChunk(); assertEquals(1, chunk.pathMacros().macrosCount()); assertEquals("id", chunk.pathMacros().names()[0]); cfg = actionsManager.lookup("GET", MadvocUtil.splitPathToChunks("/re/duplo/aaa")); assertNotNull(cfg); assertEquals(ReAction.class, cfg.getActionClass()); assertEquals("/re/duplo/{sid}", cfg.getActionPath()); chunk = cfg.getRouteChunk(); assertEquals(1, chunk.pathMacros().macrosCount()); assertEquals("sid", chunk.pathMacros().names()[0]); assertEquals(2, actionsManager.getActionsCount()); }
assertNotNull(cfg); assertEquals(ReAction.class, cfg.getActionClass()); assertEquals("/re/wild{id}cat", cfg.getActionPath()); assertNotNull(cfg); assertEquals(ReAction.class, cfg.getActionClass()); assertEquals("/re/wild{id}dog", cfg.getActionPath()); assertEquals("POST", cfg.getActionMethod()); chunk = cfg.getRouteChunk();
assertEquals("/re/user/{id}/macro", cfg.getActionPath()); assertEquals("/re/user/image/{id}/{fmt}/macro2", cfg.getActionPath()); chunk = cfg.getRouteChunk(); assertNull(chunk.pathMacros()); assertEquals("/re/users/{id}/macro3", cfg.getActionPath()); assertEquals("POST", cfg.getActionMethod()); chunk = cfg.getRouteChunk();
actionRuntime -> actionRuntime.getActionPath() + ' ' + actionRuntime.getActionMethod())) .forEach(ar -> { Chalk256.chalk().green(), ar.getActionPath(), Chalk256.chalk().blue(), signature, width - 7 - 1 actionRuntime -> actionRuntime.getActionPath() + ' ' + actionRuntime.getActionMethod())) .forEach(ar -> { final String actionPath = ar.getActionPath();