private void loadBeanDefinitions(String fileName) { this.appContext.setConfigLocation("classpath:org/springframework/web/servlet/config/" + fileName); this.appContext.refresh(); }
@Before public void setUp() { XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.setConfigLocations(CONTEXT); wac.refresh(); this.context = wac; }
@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; }
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()); }
wac.setNamespace("test-servlet"); wac.setConfigLocations(new String[] {"/org/springframework/web/context/WEB-INF/test-servlet.xml"}); wac.refresh(); return wac;
createdContext = ctx2; ctx2.refresh(); return ctx2;
@Inject private XmlWebApplicationContext context; @Inject private List<ClassPathXmlApplicationContext> childs; @PostConstruct public void refreshContext() { for(ClassPathXmlApplicationContext appContext : childs) { appContext.setParent(context); } context.refresh(); }
@Override public synchronized void refresh() throws BeansException, IllegalStateException { if (!active) { super.refresh(); active = true; } } }
@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; }
wac.setServletContext(sc); wac.setConfigLocations("/org/springframework/web/servlet/handler/map2.xml"); wac.refresh(); Object bean = wac.getBean("mainController"); Object otherBean = wac.getBean("otherController");
public void refresh() throws BeansException, IllegalStateException { if (classLoader != null) { classLoader.reload(); } super.refresh(); }
@Override public void reload() { XmlWebApplicationContext context = (XmlWebApplicationContext) WebApplicationContextUtils.getWebApplicationContext(servletContext); context.refresh(); SessionListener.getSessionCache(servletContext).invalidateAll(); } });
@Override protected ConfigurableApplicationContext createApplicationContext( String[] locations) { FileSystemResourceLoader rl = new FileSystemResourceLoader(); ServletContext servletContext = new MockServletContext(rl); XmlWebApplicationContext context = new XmlWebApplicationContext(); context.setServletContext(servletContext); context.setConfigLocations(locations); context.refresh(); return context; }
@Override protected ConfigurableApplicationContext createApplicationContext( String[] locations) { FileSystemResourceLoader rl = new FileSystemResourceLoader(); ServletContext servletContext = new MockServletContext(rl); XmlWebApplicationContext context = new XmlWebApplicationContext(); context.setServletContext(servletContext); context.setConfigLocations(locations); context.refresh(); return context; } }
public DispatcherServletChannelInitializer() throws ServletException { MockServletContext servletContext = new MockServletContext(); MockServletConfig servletConfig = new MockServletConfig(servletContext); XmlWebApplicationContext wac = new XmlWebApplicationContext(); wac.setServletContext(servletContext); wac.setServletConfig(servletConfig); wac.setConfigLocations(new String[] { "classpath:/servlet-context.xml", "classpath:dubbo-consumer.xml" }); wac.refresh(); this.dispatcherServlet = new DispatcherServlet(wac); this.dispatcherServlet.init(servletConfig); }