/** * Returns existing component. Throws an exception if component is not registered. */ public <T> T requestComponent(final Class<T> component) { T existingComponent = lookupComponent(component); if (existingComponent == null) { throw new MadvocException("Madvoc component not found: " + component.getName()); } return existingComponent; }
/** * Returns existing component. Throws an exception if component is not registered. */ public <T> T requestComponent(final String componentName) { T existingComponent = (T) lookupComponent(componentName); if (existingComponent == null) { throw new MadvocException("Madvoc component not found: " + componentName); } return existingComponent; }
/** * Configure defaults. */ protected void configureDefaults() { final ActionConfigManager actionConfigManager = madvocContainer.lookupComponent(ActionConfigManager.class); actionConfigManager.registerAnnotation(Action.class); actionConfigManager.registerAnnotation(RestAction.class); }
@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 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 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()); }
@Test void testDefaultMethods() { WebApp webapp = new WebApp(); webapp.start(); ActionMethodParser actionMethodParser = webapp.madvocContainer().lookupComponent(ActionMethodParser.class); ActionRuntime cfg = parse(actionMethodParser, "fixtures.tst.SuperAction#add"); assertNotNull(cfg); }
@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 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 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 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 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 testAlias() { WebApp webapp = new WebApp(); webapp.start(); ActionsManager actionsManager = webapp.madvocContainer().lookupComponent(ActionsManager.class); actionsManager.registerAction(BooAction.class, "foo5", null); actionsManager.registerPathAlias("ok", "xxx.jsp"); actionsManager.registerPathAlias("sok", "zzz"); ResultMapper resultMapper = webapp.madvocContainer().lookupComponent(ResultMapper.class); String path = "/boo.foo.html"; ResultPath resultPath = resultMapper.resolveResultPath(path, "/<ok>?foo=1"); assertEquals("/xxx.jsp?foo=1", resultPath.pathValue()); resultPath = resultMapper.resolveResultPath(path, "<sok>"); assertEquals("/boo.foo.html.zzz", resultPath.pathValue()); resultPath = resultMapper.resolveResultPath(path, "#<sok>"); assertEquals("/boo.foo.zzz", resultPath.pathValue()); resultPath = resultMapper.resolveResultPath(path, "<dude>?foo=1"); assertEquals("/xxx.html?foo=1", resultPath.pathValue()); }
@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 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 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 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 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 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); }
@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()); }