/** * {@inheritDoc} * <p>This implementation creates an {@link AnnotationConfigWebApplicationContext}, * providing it the annotated classes returned by {@link #getServletConfigClasses()}. */ @Override protected WebApplicationContext createServletApplicationContext() { AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); Class<?>[] configClasses = getServletConfigClasses(); if (!ObjectUtils.isEmpty(configClasses)) { context.register(configClasses); } return context; }
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); }
@Before public void setup() { this.servletContext = new MockServletContext(); this.webAppContext = new AnnotationConfigWebApplicationContext(); this.webAppContext.register(Config.class); this.contextLoader = new ContextLoader(this.webAppContext); this.contextLoader.initWebApplicationContext(this.servletContext); this.configurator = new SpringConfigurator(); }
@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()); }
/** * Ensure that ContextLoaderListener and AnnotationConfigApplicationContext interact nicely. */ @Test public void annotationConfigWAC() { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.scan("does.not.matter"); ContextLoaderListener cll = new ContextLoaderListener(ctx); cll.contextInitialized(new ServletContextEvent(new MockServletContext())); } }
@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 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; }