@Test public void requestsWithSubPaths() throws Exception { HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping"); doTestRequestsWithSubPaths(hm); }
public void registerSubContext(String webAppKey) { // get the sub contexts - servlet context ServletContext ctx = servletContext.getContext(webAppKey); if (ctx == null) { ctx = servletContext; } ContextLoader loader = new ContextLoader(); ConfigurableWebApplicationContext appCtx = (ConfigurableWebApplicationContext) loader.initWebApplicationContext(ctx); appCtx.setParent(applicationContext); appCtx.refresh(); ctx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, appCtx); ConfigurableBeanFactory appFactory = appCtx.getBeanFactory(); logger.debug("About to grab Webcontext bean for {}", webAppKey); Context webContext = (Context) appCtx.getBean("web.context"); webContext.setCoreBeanFactory(parentFactory); webContext.setClientRegistry(clientRegistry); webContext.setServiceInvoker(globalInvoker); webContext.setScopeResolver(globalResolver); webContext.setMappingStrategy(globalStrategy); WebScope scope = (WebScope) appFactory.getBean("web.scope"); scope.setServer(server); scope.setParent(global); scope.register(); scope.start(); // register the context so we dont try to reinitialize it registeredContexts.add(ctx); }
public <T> T instanceFor(Class<T> type) { try { return parentContext.getBean(type); } catch (NoSuchBeanDefinitionException e) { Map<String, T> beans = parentContext.getBeansOfType(type); for (Entry<String, T> def : beans.entrySet()) { BeanDefinition definition = parentContext.getBeanFactory().getBeanDefinition(def.getKey()); if (isPrimary(definition) || hasGreaterRoleThanInfrastructure(definition)) { return def.getValue(); } } throw e; } }
@Test public void doubleMappings() throws ServletException { BeanNameUrlHandlerMapping hm = (BeanNameUrlHandlerMapping) wac.getBean("handlerMapping"); try { hm.registerHandler("/mypath/welcome.html", new Object()); fail("Should have thrown IllegalStateException"); } catch (IllegalStateException ex) { // expected } }
@Test public void contextNesting() { TestBean father = (TestBean) this.applicationContext.getBean("father"); assertTrue("Bean from root context", father != null); assertTrue("Custom BeanPostProcessor applied", father.getFriends().contains("myFriend")); TestBean rod = (TestBean) this.applicationContext.getBean("rod"); assertTrue("Bean from child context", "Rod".equals(rod.getName())); assertTrue("Bean has external reference", rod.getSpouse() == father); assertTrue("Custom BeanPostProcessor not applied", !rod.getFriends().contains("myFriend")); rod = (TestBean) this.root.getBean("rod"); assertTrue("Bean from root context", "Roderick".equals(rod.getName())); assertTrue("Custom BeanPostProcessor applied", rod.getFriends().contains("myFriend")); }
@Test public void defaultMapping() throws Exception { Object bean = wac.getBean("starController"); MockHttpServletRequest req = new MockHttpServletRequest("GET", "/goggog.html"); HandlerExecutionChain hec = getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); }
@Before public void setUp() throws Exception { MockServletContext sc = new MockServletContext(""); wac = new XmlWebApplicationContext(); wac.setServletContext(sc); wac.setConfigLocations(new String[] {CONF}); wac.refresh(); hm = (HandlerMapping) wac.getBean("urlMapping"); }
@Test public void asteriskMatches() throws Exception { HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping"); Object bean = wac.getBean("godCtrl"); MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/test.html"); HandlerExecutionChain hec = hm.getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); req = new MockHttpServletRequest("GET", "/mypath/testarossa"); hec = hm.getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); req = new MockHttpServletRequest("GET", "/mypath/tes"); hec = hm.getHandler(req); assertTrue("Handler is correct bean", hec == null); }
@Test public void overlappingMappings() throws Exception { BeanNameUrlHandlerMapping hm = (BeanNameUrlHandlerMapping) wac.getBean("handlerMapping"); Object anotherHandler = new Object(); hm.registerHandler("/mypath/testaross*", anotherHandler); Object bean = wac.getBean("godCtrl"); MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/test.html"); HandlerExecutionChain hec = hm.getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); req = new MockHttpServletRequest("GET", "/mypath/testarossa"); hec = hm.getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == anotherHandler); req = new MockHttpServletRequest("GET", "/mypath/tes"); hec = hm.getHandler(req); assertTrue("Handler is correct bean", hec == null); }
@Test public void mappingExposedInRequest() throws Exception { Object bean = wac.getBean("mainController"); MockHttpServletRequest req = new MockHttpServletRequest("GET", "/show.html"); HandlerExecutionChain hec = getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); assertEquals("Mapping not exposed", "show.html", req.getAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE)); }
@Test public void requestsWithoutHandlers() throws Exception { HandlerMapping hm = (HandlerMapping) wac.getBean("handlerMapping"); MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/nonsense.html"); req.setContextPath("/myapp"); Object h = hm.getHandler(req); assertTrue("Handler is null", h == null); req = new MockHttpServletRequest("GET", "/foo/bar/baz.html"); h = hm.getHandler(req); assertTrue("Handler is null", h == null); }
@Test public void requestsWithHandlers() throws Exception { Object bean = wac.getBean("mainController"); MockHttpServletRequest req = new MockHttpServletRequest("GET", "/welcome.html"); HandlerExecutionChain hec = getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); req = new MockHttpServletRequest("GET", "/show.html"); hec = getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); req = new MockHttpServletRequest("GET", "/bookseats.html"); hec = getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); }
@Test public void requestsWithFullPaths() throws Exception { BeanNameUrlHandlerMapping hm = new BeanNameUrlHandlerMapping(); hm.setAlwaysUseFullPath(true); hm.setApplicationContext(wac); Object bean = wac.getBean("godCtrl"); MockHttpServletRequest req = new MockHttpServletRequest("GET", "/mypath/welcome.html"); HandlerExecutionChain hec = hm.getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); req = new MockHttpServletRequest("GET", "/myapp/mypath/welcome.html"); req.setContextPath("/myapp"); hec = hm.getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); req = new MockHttpServletRequest("GET", "/mypath/welcome.html"); req.setContextPath(""); req.setServletPath("/mypath"); hec = hm.getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); req = new MockHttpServletRequest("GET", "/Myapp/mypath/welcome.html"); req.setContextPath("/myapp"); req.setServletPath("/mypath"); hec = hm.getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); req = new MockHttpServletRequest("GET", "/"); hec = hm.getHandler(req); assertTrue("Handler is correct bean", hec != null && hec.getHandler() == bean); }
private void doTestRequestsWithSubPaths(HandlerMapping hm) throws Exception { Object bean = wac.getBean("godCtrl");
@Override public synchronized <T> T getBean(String arg0, Class<T> arg1) throws BeansException { return this.appContext.getBean(arg0, arg1); }
@Override public synchronized <T> T getBean(Class<T> requiredType) throws BeansException { return this.appContext.getBean(requiredType); }
@Override public synchronized Object getBean(String arg0) throws BeansException { return this.appContext.getBean(arg0); }
@Override public Object getBean(String arg0, Object... arg1) throws BeansException { return this.appContext.getBean(arg0, arg1); }
private static void postProcessSpringContext(ConfigurableWebApplicationContext springContext) { if (springContext.getBean("TheSnooper") == null) { springContext.addApplicationListener(new SpringEventSnooper<ApplicationEvent>()); } }