private void setup(Class<?> configurationClass) { AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext(); applicationContext.register(configurationClass); applicationContext.refresh(); this.applicationContext = applicationContext; ContextLoaderTestUtils.setCurrentWebApplicationContext(this.applicationContext); }
@Before public void setup() { this.serverContainer = mock(ServerContainer.class); this.servletContext = new MockServletContext(); this.servletContext.setAttribute("javax.websocket.server.ServerContainer", this.serverContainer); this.webAppContext = new AnnotationConfigWebApplicationContext(); this.webAppContext.register(Config.class); this.webAppContext.setServletContext(this.servletContext); this.webAppContext.refresh(); this.exporter = new ServerEndpointExporter(); }
@Test @SuppressWarnings("resource") public void configLocationWithSingleClass() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.setConfigLocation(Config.class.getName()); ctx.refresh(); TestBean bean = ctx.getBean(TestBean.class); assertNotNull(bean); }
private ApplicationContext initContext(Class<?>... configClasses) { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setServletContext(new MockServletContext()); context.register(configClasses); context.refresh(); return context; }
@Test @SuppressWarnings("resource") public void withBeanNameGenerator() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.setBeanNameGenerator(new AnnotationBeanNameGenerator() { @Override public String generateBeanName(BeanDefinition definition, BeanDefinitionRegistry registry) { return "custom-" + super.generateBeanName(definition, registry); } }); ctx.setConfigLocation(Config.class.getName()); ctx.refresh(); assertThat(ctx.containsBean("custom-myConfig"), is(true)); }
@Before public void setup() throws Exception { logger.debug("Setting up '" + this.testName.getMethodName() + "', client=" + this.webSocketClient.getClass().getSimpleName() + ", server=" + this.server.getClass().getSimpleName()); this.wac = new AnnotationConfigWebApplicationContext(); this.wac.register(getAnnotatedConfigClasses()); this.wac.register(upgradeStrategyConfigTypes.get(this.server.getClass())); if (this.webSocketClient instanceof Lifecycle) { ((Lifecycle) this.webSocketClient).start(); } this.server.setup(); this.server.deployConfig(this.wac); this.server.start(); this.wac.setServletContext(this.server.getServletContext()); this.wac.refresh(); }
@Test @SuppressWarnings("resource") public void registerSingleClass() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(Config.class); ctx.refresh(); TestBean bean = ctx.getBean(TestBean.class); assertNotNull(bean); }
private void initWebApplicationContext(Class<?> configClass) { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setServletContext(new MockServletContext()); context.register(configClass); context.refresh(); this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, context); }
@Test public void annotationConfigWebApplicationContext() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.setEnvironment(prodWebEnv); ctx.setConfigLocation(EnvironmentAwareBean.class.getName()); ctx.refresh(); assertHasEnvironment(ctx, prodWebEnv); assertEnvironmentBeanRegistered(ctx); assertEnvironmentAwareInvoked(ctx, prodWebEnv); }
@Test @SuppressWarnings("resource") public void configLocationWithBasePackage() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.setConfigLocation("org.springframework.web.context.support"); ctx.refresh(); TestBean bean = ctx.getBean(TestBean.class); assertNotNull(bean); }
@Test public void register() throws ServletException { initializer.onStartup(servletContext); assertEquals(1, servlets.size()); assertNotNull(servlets.get(SERVLET_NAME)); DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME); WebApplicationContext wac = servlet.getWebApplicationContext(); ((AnnotationConfigWebApplicationContext) wac).refresh(); assertTrue(wac.containsBean("bean")); assertTrue(wac.getBean("bean") instanceof MyBean); assertEquals(1, servletRegistrations.size()); assertNotNull(servletRegistrations.get(SERVLET_NAME)); MockServletRegistration servletRegistration = servletRegistrations.get(SERVLET_NAME); assertEquals(Collections.singleton(SERVLET_MAPPING), servletRegistration.getMappings()); assertEquals(1, servletRegistration.getLoadOnStartup()); assertEquals(ROLE_NAME, servletRegistration.getRunAsRole()); assertTrue(servletRegistration.isAsyncSupported()); assertEquals(4, filterRegistrations.size()); assertNotNull(filterRegistrations.get("hiddenHttpMethodFilter")); assertNotNull(filterRegistrations.get("delegatingFilterProxy")); assertNotNull(filterRegistrations.get("delegatingFilterProxy#0")); assertNotNull(filterRegistrations.get("delegatingFilterProxy#1")); for (MockFilterRegistration filterRegistration : filterRegistrations.values()) { assertTrue(filterRegistration.isAsyncSupported()); EnumSet<DispatcherType> enumSet = EnumSet.of(DispatcherType.REQUEST, DispatcherType.FORWARD, DispatcherType.INCLUDE, DispatcherType.ASYNC); assertEquals(enumSet, filterRegistration.getMappings().get(SERVLET_NAME)); } }
@Test public void rootContextOnly() throws ServletException { initializer = new MyAnnotationConfigDispatcherServletInitializer() { @Override protected Class<?>[] getRootConfigClasses() { return new Class<?>[] {MyConfiguration.class}; } @Override protected Class<?>[] getServletConfigClasses() { return null; } }; initializer.onStartup(servletContext); DispatcherServlet servlet = (DispatcherServlet) servlets.get(SERVLET_NAME); servlet.init(new MockServletConfig(this.servletContext)); WebApplicationContext wac = servlet.getWebApplicationContext(); ((AnnotationConfigWebApplicationContext) wac).refresh(); assertTrue(wac.containsBean("bean")); assertTrue(wac.getBean("bean") instanceof MyBean); }
@Test public void getCorsConfigurationActual() throws Exception { AnnotationConfigWebApplicationContext cxt = new AnnotationConfigWebApplicationContext(); cxt.register(TestConfig.class); cxt.refresh(); MockHttpServletRequest request = new MockHttpServletRequest("POST", "/path"); request.addHeader("Origin", "http://localhost:9000"); CorsConfiguration corsConfig = getIntrospector(cxt).getCorsConfiguration(request); assertNotNull(corsConfig); assertEquals(Collections.singletonList("http://localhost:9000"), corsConfig.getAllowedOrigins()); assertEquals(Collections.singletonList("POST"), corsConfig.getAllowedMethods()); }
@Test public void getCorsConfigurationPreFlight() throws Exception { AnnotationConfigWebApplicationContext cxt = new AnnotationConfigWebApplicationContext(); cxt.register(TestConfig.class); cxt.refresh(); // PRE-FLIGHT MockHttpServletRequest request = new MockHttpServletRequest("OPTIONS", "/path"); request.addHeader("Origin", "http://localhost:9000"); request.addHeader(HttpHeaders.ACCESS_CONTROL_REQUEST_METHOD, "POST"); CorsConfiguration corsConfig = getIntrospector(cxt).getCorsConfiguration(request); assertNotNull(corsConfig); assertEquals(Collections.singletonList("http://localhost:9000"), corsConfig.getAllowedOrigins()); assertEquals(Collections.singletonList("POST"), corsConfig.getAllowedMethods()); }
@Before @SuppressWarnings("resource") public void setup() throws Exception { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setServletContext(new MockServletContext()); context.register(WebConfig.class); context.refresh(); this.request = new MockHttpServletRequest("GET", "/"); this.request.setContextPath("/myapp"); this.response = new MockHttpServletResponse(); this.filterChain = new MockFilterChain(this.servlet, new ResourceUrlEncodingFilter(), (request, response, chain) -> { Object urlProvider = context.getBean(ResourceUrlProvider.class); request.setAttribute(ResourceUrlProviderExposingInterceptor.RESOURCE_URL_PROVIDER_ATTR, urlProvider); chain.doFilter(request, response); }); }
@Before public void setUp() throws Exception { logger.debug("Setting up before '" + this.testName.getMethodName() + "'"); this.wac = new AnnotationConfigWebApplicationContext(); this.wac.register(TestConfig.class); this.wac.refresh(); this.server = new TomcatWebSocketTestServer(); this.server.setup(); this.server.deployConfig(this.wac); this.server.start(); WebSocketClient webSocketClient = new StandardWebSocketClient(); this.stompClient = new WebSocketStompClient(webSocketClient); this.stompClient.setMessageConverter(new StringMessageConverter()); }
@Before public void setup() throws Exception { logger.debug("Setting up '" + this.testName.getMethodName() + "'"); this.testFilter = new TestFilter(); this.wac = new AnnotationConfigWebApplicationContext(); this.wac.register(TestConfig.class, upgradeStrategyConfigClass()); this.server = createWebSocketTestServer(); this.server.setup(); this.server.deployConfig(this.wac, this.testFilter); this.server.start(); this.wac.setServletContext(this.server.getServletContext()); this.wac.refresh(); this.baseUrl = "http://localhost:" + this.server.getPort(); }
@Test // SPR-12592 @SuppressWarnings("resource") public void initializeOnce() { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setServletContext(new MockServletContext()); context.register(HandlerMappingConfiguration.class); context.refresh(); assertThat(context.getBean(ResourceUrlProvider.class).getHandlerMap(), Matchers.hasKey(pattern("/resources/**"))); }
@Test // SPR-12592 @SuppressWarnings("resource") public void initializeOnce() throws Exception { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.setServletContext(new MockServletContext()); context.register(HandlerMappingConfiguration.class); context.refresh(); ResourceUrlProvider urlProviderBean = context.getBean(ResourceUrlProvider.class); assertThat(urlProviderBean.getHandlerMap(), Matchers.hasKey("/resources/**")); assertFalse(urlProviderBean.isAutodetect()); }
private MockHttpServletResponse runTest(Class<?> configClass) throws ServletException, IOException { String basePath = "org/springframework/web/servlet/config/annotation"; MockServletContext servletContext = new MockServletContext(basePath); MockServletConfig servletConfig = new MockServletConfig(servletContext); MockHttpServletRequest request = new MockHttpServletRequest("GET", "/"); MockHttpServletResponse response = new MockHttpServletResponse(); AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); context.register(configClass); context.setServletContext(servletContext); context.refresh(); DispatcherServlet servlet = new DispatcherServlet(context); servlet.init(servletConfig); servlet.service(request, response); return response; }