public SpringTestContext configLocations(String... configLocations) { XmlWebApplicationContext context = new XmlWebApplicationContext(); context.setConfigLocations(configLocations); this.context = context; return this; }
@Before public void setUp() { XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.setConfigLocations(CONTEXT); wac.refresh(); this.context = wac; }
@Before public void setUp() throws Exception { TestMockServletContext servletContext = new TestMockServletContext(); appContext = new XmlWebApplicationContext(); appContext.setServletContext(servletContext); LocaleContextHolder.setLocale(Locale.US); String attributeName = WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE; appContext.getServletContext().setAttribute(attributeName, appContext); handler = new TestController(); handlerMethod = new InvocableHandlerMethod(handler, TestController.class.getMethod("testBind", Date.class, Double.class, TestBean.class, BindingResult.class)); }
/** * If context config locations have been specified neither against the application * context nor the context loader listener, then fall back to default values. */ @Test public void abstractRefreshableWAC_fallsBackToConventionBasedNaming() { XmlWebApplicationContext ctx = new XmlWebApplicationContext(); //ctx.setConfigLocation("programmatic.xml"); // nothing set programmatically ContextLoaderListener cll = new ContextLoaderListener(ctx); MockServletContext sc = new MockServletContext(); // no init-param set //sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml"); try { cll.contextInitialized(new ServletContextEvent(sc)); fail("expected exception"); } catch (Throwable t) { // assert that an attempt was made to load the correct XML System.out.println(t.getMessage()); assertTrue(t.getMessage().endsWith( "Could not open ServletContext resource [/WEB-INF/applicationContext.xml]")); } }
@Test public void abstractRefreshableWAC_respectsProgrammaticConfigLocations() { XmlWebApplicationContext ctx = new XmlWebApplicationContext(); ctx.setConfigLocation("programmatic.xml"); ContextLoaderListener cll = new ContextLoaderListener(ctx); MockServletContext sc = new MockServletContext(); try { cll.contextInitialized(new ServletContextEvent(sc)); fail("expected exception"); } catch (Throwable t) { // assert that an attempt was made to load the correct XML assertTrue(t.getMessage(), t.getMessage().endsWith( "Could not open ServletContext resource [/programmatic.xml]")); } }
@Before public void setUp() throws Exception { MockServletContext sc = new MockServletContext(""); wac = new XmlWebApplicationContext(); wac.setServletContext(sc); wac.setConfigLocations(new String[] {CONF}); wac.refresh(); }
@Test public void xmlWebApplicationContext() { AbstractRefreshableWebApplicationContext ctx = new XmlWebApplicationContext(); ctx.setConfigLocation("classpath:" + XML_PATH); ctx.setEnvironment(prodWebEnv); ctx.refresh(); assertHasEnvironment(ctx, prodWebEnv); assertEnvironmentBeanRegistered(ctx); assertEnvironmentAwareInvoked(ctx, prodWebEnv); assertThat(ctx.containsBean(DEV_BEAN_NAME), is(false)); assertThat(ctx.containsBean(PROD_BEAN_NAME), is(true)); }
/** * If setConfigLocation has not been called explicitly against the application context, * then fall back to the ContextLoaderListener init-param if present. */ @Test public void abstractRefreshableWAC_fallsBackToInitParam() { XmlWebApplicationContext ctx = new XmlWebApplicationContext(); //ctx.setConfigLocation("programmatic.xml"); // nothing set programmatically ContextLoaderListener cll = new ContextLoaderListener(ctx); MockServletContext sc = new MockServletContext(); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml"); try { cll.contextInitialized(new ServletContextEvent(sc)); fail("expected exception"); } catch (Throwable t) { // assert that an attempt was made to load the correct XML assertTrue(t.getMessage().endsWith( "Could not open ServletContext resource [/from-init-param.xml]")); } }
@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"); }
/** * If a contextConfigLocation init-param has been specified for the ContextLoaderListener, * then it should take precedence. This is generally not a recommended practice, but * when it does happen, the init-param should be considered more specific than the * programmatic configuration, given that it still quite possibly externalized in * hybrid web.xml + WebApplicationInitializer cases. */ @Test public void abstractRefreshableWAC_respectsInitParam_overProgrammaticConfigLocations() { XmlWebApplicationContext ctx = new XmlWebApplicationContext(); ctx.setConfigLocation("programmatic.xml"); ContextLoaderListener cll = new ContextLoaderListener(ctx); MockServletContext sc = new MockServletContext(); sc.addInitParameter(ContextLoader.CONFIG_LOCATION_PARAM, "from-init-param.xml"); try { cll.contextInitialized(new ServletContextEvent(sc)); fail("expected exception"); } catch (Throwable t) { // assert that an attempt was made to load the correct XML assertTrue(t.getMessage(), t.getMessage().endsWith( "Could not open ServletContext resource [/from-init-param.xml]")); } }
@Test @SuppressWarnings("resource") public void withoutMessageSource() throws Exception { MockServletContext sc = new MockServletContext(""); XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.setParent(root); wac.setServletContext(sc); wac.setNamespace("testNamespace"); wac.setConfigLocations(new String[] {"/org/springframework/web/context/WEB-INF/test-servlet.xml"}); wac.refresh(); try { wac.getMessage("someMessage", null, Locale.getDefault()); fail("Should have thrown NoSuchMessageException"); } catch (NoSuchMessageException ex) { // expected; } String msg = wac.getMessage("someMessage", null, "default", Locale.getDefault()); assertTrue("Default message returned", "default".equals(msg)); }
@Test public void configureWhenUserDetailsServiceInParentContextThenLocatesSuccessfully() { assertThatCode(() -> this.spring.configLocations(this.xml("MissingUserDetailsService")).autowire()) .isInstanceOf(BeansException.class); try ( XmlWebApplicationContext parent = new XmlWebApplicationContext() ) { parent.setConfigLocations(this.xml("AutoConfig")); parent.refresh(); try ( XmlWebApplicationContext child = new XmlWebApplicationContext() ) { child.setParent(parent); child.setConfigLocation(this.xml("MissingUserDetailsService")); child.refresh(); } } }
@Test @SuppressWarnings("resource") public void handlerBeanNotFound() { MockServletContext sc = new MockServletContext(""); XmlWebApplicationContext root = new XmlWebApplicationContext(); root.setServletContext(sc); root.setConfigLocations("/org/springframework/web/servlet/handler/map1.xml"); root.refresh(); XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.setParent(root); wac.setServletContext(sc); wac.setNamespace("map2err"); wac.setConfigLocations("/org/springframework/web/servlet/handler/map2err.xml"); try { wac.refresh(); fail("Should have thrown NoSuchBeanDefinitionException"); } catch (FatalBeanException ex) { NoSuchBeanDefinitionException nestedEx = (NoSuchBeanDefinitionException) ex.getCause(); assertEquals("mainControlle", nestedEx.getBeanName()); } }
private void loadConfig(String... configLocations) { for (int i=0; i<configLocations.length; i++) { configLocations[i] = getClass().getName().replaceAll("\\.", "/") + "-" + configLocations[i]; } XmlWebApplicationContext context = new XmlWebApplicationContext(); context.setConfigLocations(configLocations); context.setServletContext(new MockServletContext()); context.refresh(); this.context = context; context.getAutowireCapableBeanFactory().autowireBean(this); Filter springSecurityFilterChain = context.getBean("springSecurityFilterChain", Filter.class); mockMvc = MockMvcBuilders .standaloneSetup(new FooController()) .addFilters(springSecurityFilterChain) .build(); }
public static XmlWebApplicationContext setUpContext() { webApplicationContext = new XmlWebApplicationContext(); MockEnvironment mockEnvironment = getMockEnvironment(); webApplicationContext.setEnvironment(mockEnvironment); webApplicationContext.setServletContext(new MockServletContext() { @Override public <Type extends EventListener> void addListener(Type t) { //no op } }); new YamlServletProfileInitializerContextInitializer() .initializeContext(webApplicationContext, "endpoint_test_config.yml,uaa.yml,login.yml,required_configuration.yml"); webApplicationContext.setConfigLocation("file:./src/main/webapp/WEB-INF/spring-servlet.xml"); webApplicationContext.refresh(); webApplicationContext.registerShutdownHook(); EventFactory honeycombEventFactory = honeycombEventFactory( System.getenv("HONEYCOMB_KEY"), System.getenv("HONEYCOMB_DATASET"), Optional.ofNullable(System.getProperty("testId")).orElse("-1") ); honeycombAuditEventTestListenerAuthenticationFailureLockedEvent( webApplicationContext, honeycombEventFactory); honeycombAuditEventTestListenerIdentityProviderAuthenticationFailureEvent( webApplicationContext, honeycombEventFactory); honeycombAuditEventTestListenerMfaAuthenticationFailureEvent( webApplicationContext, honeycombEventFactory); return webApplicationContext; }
@Override protected ConfigurableApplicationContext createContext() throws Exception { InitAndIB.constructed = false; root = new XmlWebApplicationContext(); root.getEnvironment().addActiveProfile("rootProfile1"); MockServletContext sc = new MockServletContext(""); XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.getEnvironment().addActiveProfile("wacProfile1"); wac.setParent(root);
public static void clearDatabase() { webApplicationContext = new XmlWebApplicationContext(); webApplicationContext.setEnvironment(getMockEnvironment()); webApplicationContext.setConfigLocations("classpath:spring/env.xml", "classpath:spring/data-source.xml"); webApplicationContext.refresh(); webApplicationContext.getBean(Flyway.class).clean(); webApplicationContext.destroy(); }
public void setUp(MockEnvironment environment) throws Exception { this.environment = environment; webApplicationContext = new XmlWebApplicationContext(); webApplicationContext.setEnvironment(environment); webApplicationContext.setConfigLocations(new String[]{"classpath:spring/env.xml", "classpath:spring/data-source.xml"}); webApplicationContext.refresh(); flyway = webApplicationContext.getBean(Flyway.class); jdbcTemplate = webApplicationContext.getBean(JdbcTemplate.class); dataSource = webApplicationContext.getBean(DataSource.class); limitSqlAdapter = webApplicationContext.getBean(LimitSqlAdapter.class); validationQuery = webApplicationContext.getBean("validationQuery", String.class); IdentityZoneHolder.setProvisioning(new JdbcIdentityZoneProvisioning(jdbcTemplate)); IdentityZoneHolder.get().getConfig().getUserConfig().setDefaultGroups(emptyList()); }
@Override public final ConfigurableApplicationContext loadContext(String... locations) throws Exception { XmlWebApplicationContext context = new XmlWebApplicationContext(); context.setConfigLocations(locations); MockServletContext sc = new MockServletContext(); sc.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, context); context.setServletContext(sc); context.refresh(); return context; }
@SuppressWarnings("resource") private void checkMappings(String beanName) throws Exception { MockServletContext sc = new MockServletContext(""); XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.setServletContext(sc); wac.setConfigLocations("/org/springframework/web/servlet/handler/map2.xml");