/** * Introduced to investigate claims made in a discussion on * <a href="http://stackoverflow.com/questions/24725438/what-could-cause-a-class-implementing-applicationlistenercontextrefreshedevent">Stack Overflow</a>. */ @Test public void buildMergedConfigWithAtWebAppConfigurationWithAnnotationAndClassesOnSuperclass() { Class<?> webTestClass = WebClassesFoo.class; Class<?> standardTestClass = ClassesFoo.class; WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) buildMergedContextConfiguration(webTestClass); MergedContextConfiguration standardMergedConfig = buildMergedContextConfiguration(standardTestClass); assertEquals(webMergedConfig, webMergedConfig); assertEquals(standardMergedConfig, standardMergedConfig); assertNotEquals(standardMergedConfig, webMergedConfig); assertNotEquals(webMergedConfig, standardMergedConfig); assertMergedConfig(webMergedConfig, webTestClass, EMPTY_STRING_ARRAY, new Class<?>[] { FooConfig.class }, WebDelegatingSmartContextLoader.class); assertMergedConfig(standardMergedConfig, standardTestClass, EMPTY_STRING_ARRAY, new Class<?>[] { FooConfig.class }, DelegatingSmartContextLoader.class); }
@Override public ApplicationContext loadContext(final MergedContextConfiguration config) throws Exception { assertValidAnnotations(config.getTestClass()); SpringApplication application = getSpringApplication(); application.setMainApplicationClass(config.getTestClass()); application.setSources(getSources(config)); ConfigurableEnvironment environment = new StandardEnvironment(); if (!ObjectUtils.isEmpty(config.getActiveProfiles())) { setActiveProfiles(environment, config.getActiveProfiles()); } Map<String, Object> properties = getEnvironmentProperties(config); addProperties(environment, properties); application.setEnvironment(environment); List<ApplicationContextInitializer<?>> initializers = getInitializers(config, application); if (config instanceof WebMergedContextConfiguration) { new WebConfigurer().configure(config, application, initializers); } else { application.setWebEnvironment(false); } application.setInitializers(initializers); ConfigurableApplicationContext applicationContext = application.run(); return applicationContext; }
void configure(MergedContextConfiguration configuration, SpringApplication application, List<ApplicationContextInitializer<?>> initializers) { if (!TestAnnotations.isIntegrationTest(configuration)) { WebMergedContextConfiguration webConfiguration = (WebMergedContextConfiguration) configuration; addMockServletContext(initializers, webConfiguration); application.setApplicationContextClass(WEB_CONTEXT_CLASS); } }
private void addMockServletContext( List<ApplicationContextInitializer<?>> initializers, WebMergedContextConfiguration webConfiguration) { SpringBootMockServletContext servletContext = new SpringBootMockServletContext( webConfiguration.getResourceBasePath()); initializers.add(0, new ServletContextApplicationContextInitializer( servletContext, true)); }
@Override public ApplicationContext loadContext(MergedContextConfiguration config) throws Exception { SpringApplication application = getSpringApplication(); application.setMainApplicationClass(config.getTestClass()); application.setSources(getSources(config)); ConfigurableEnvironment environment = new StandardEnvironment(); if (!ObjectUtils.isEmpty(config.getActiveProfiles())) { setActiveProfiles(environment, config.getActiveProfiles()); } TestPropertySourceUtils.addPropertiesFilesToEnvironment(environment, application.getResourceLoader() == null ? new DefaultResourceLoader(getClass().getClassLoader()) : application.getResourceLoader(), config.getPropertySourceLocations()); TestPropertySourceUtils.addInlinedPropertiesToEnvironment(environment, getInlinedProperties(config)); application.setEnvironment(environment); List<ApplicationContextInitializer<?>> initializers = getInitializers(config, application); if (config instanceof WebMergedContextConfiguration) { application.setWebEnvironment(true); if (!isEmbeddedWebEnvironment(config)) { new WebConfigurer().configure(config, application, initializers); } } else { application.setWebEnvironment(false); } application.setInitializers(initializers); ConfigurableApplicationContext context = application.run(); return context; }
void configure(MergedContextConfiguration configuration, SpringApplication application, List<ApplicationContextInitializer<?>> initializers) { WebMergedContextConfiguration webConfiguration = (WebMergedContextConfiguration) configuration; addMockServletContext(initializers, webConfiguration); application.setApplicationContextClass(WEB_CONTEXT_CLASS); }
/** * Customização para iniciar o singular * * @param context * @param webMergedConfig */ protected void customizeContext(AnnotationConfigWebApplicationContext context, WebMergedContextConfiguration webMergedConfig) { try { new SingularInitializer( new CommonsInitializerMock(context)).onStartup(context.getServletContext()); } catch (ServletException e) { throw SingularException.rethrow(e); } }
/** * Customização para iniciar o singular * @param context * @param webMergedConfig */ protected void customizeContext(AnnotationConfigWebApplicationContext context, WebMergedContextConfiguration webMergedConfig) { try { new SingularInitializer( new CommonsInitializerMock(context)).onStartup(context.getServletContext()); } catch (ServletException e) { throw SingularException.rethrow(e); } }
@Override protected void customizeContext(AnnotationConfigWebApplicationContext context, WebMergedContextConfiguration webMergedConfig) { try { new SingularInitializer(new ServerInitializerMock(context)).onStartup(context.getServletContext()); } catch (ServletException e) { throw new RuntimeException(e.getMessage(), e); } }
private void addMockServletContext( List<ApplicationContextInitializer<?>> initializers, WebMergedContextConfiguration webConfiguration) { SpringBootMockServletContext servletContext = new SpringBootMockServletContext( webConfiguration.getResourceBasePath()); initializers.add(0, new ServletContextApplicationContextInitializer(servletContext)); }
/** * {@code BroadleafGenericGroovyXmlWebContextLoader} supports the XML merging that * Broadleaf's framework features, but this is handled during the .refresh() method * in the loadContext method so this method was only pulled from the original class * {@link GenericXmlWebContextLoader} in case of the Spring-Test framework requiring * this method to have a particular behavior. * * @see AbstractGenericWebContextLoader#validateMergedContextConfiguration */ protected void validateMergedContextConfiguration(WebMergedContextConfiguration webMergedConfig) { if (webMergedConfig.hasClasses()) { String msg = String.format( "Test class [%s] has been configured with @ContextConfiguration's 'classes' attribute %s, " + "but %s does not support annotated classes.", webMergedConfig.getTestClass().getName(), ObjectUtils.nullSafeToString(webMergedConfig.getClasses()), getClass().getSimpleName()); throw new IllegalStateException(msg); } }
@Override protected MergedContextConfiguration processMergedContextConfiguration( MergedContextConfiguration mergedConfig) { return new WebMergedContextConfiguration( super.processMergedContextConfiguration(mergedConfig), ""); }
@Override protected void customizeContext(GenericWebApplicationContext context, WebMergedContextConfiguration webMergedConfig) { super.customizeContext(context, webMergedConfig); loader.registerMocksAndSpies(context); }
/** * Load a Spring {@link WebApplicationContext} from the supplied * {@link MergedContextConfiguration}. * <p/> * <p>Implementation details: * <p/> * <ul> * <li>Calls {@link #validateMergedContextConfiguration(WebMergedContextConfiguration)} * to allow subclasses to validate the supplied configuration before proceeding.</li> * <li>Creates a {@link GenericWebApplicationContext} instance.</li> * <li>If the supplied {@code MergedContextConfiguration} references a * {@linkplain MergedContextConfiguration#getParent() parent configuration}, * the corresponding {@link MergedContextConfiguration#getParentApplicationContext() * ApplicationContext} will be retrieved and * {@linkplain GenericWebApplicationContext#setParent(ApplicationContext) set as the parent} * for the context created by this method.</li> * <li>Delegates to {@link #configureWebResources} to create the * {@link MockServletContext} and set it in the {@code WebApplicationContext}.</li> * <li>Calls {@link #prepareContext} to allow for customizing the context * before bean definitions are loaded.</li> * <li>Delegates to {@link #loadBeanDefinitions} to populate the context * from the locations or classes in the supplied {@code MergedContextConfiguration}.</li> * <li>Delegates to {@link AnnotationConfigUtils} for * {@linkplain AnnotationConfigUtils#registerAnnotationConfigProcessors registering} * annotation configuration processors.</li> * <li>Calls {@link #customizeContext} to allow for customizing the context * before it is refreshed.</li> * <li>{@link ConfigurableApplicationContext#refresh Refreshes} the * context and registers a JVM shutdown hook for it.</li> * </ul> * * @return a new web application context * @see org.springframework.test.context.SmartContextLoader#loadContext(MergedContextConfiguration) * @see GenericWebApplicationContext */ @Override public final AnnotationConfigWebApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception { SingularContextSetup.reset(); if (!(mergedConfig instanceof WebMergedContextConfiguration)) { throw new IllegalArgumentException(String.format( "Cannot load WebApplicationContext from non-web merged context configuration %s. " + "Consider annotating your test class with @WebAppConfiguration.", mergedConfig)); } WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) mergedConfig; if (logger.isDebugEnabled()) { logger.debug(String.format("Loading WebApplicationContext for merged context configuration %s.", webMergedConfig)); } validateMergedContextConfiguration(webMergedConfig); AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); ApplicationContext parent = mergedConfig.getParentApplicationContext(); if (parent != null) { context.setParent(parent); } configureWebResources(context, webMergedConfig); prepareContext(context, webMergedConfig); customizeContext(context, webMergedConfig); loadBeanDefinitions(context, webMergedConfig); mockRequest(); context.refresh(); context.registerShutdownHook(); return context; }
/** * Load bean definitions into the supplied {@link GenericWebApplicationContext context} * from the locations or classes in the supplied {@code WebMergedContextConfiguration}. * <p/> * <p>Concrete subclasses must provide an appropriate implementation. * * @param context the context into which the bean definitions should be loaded * @param webMergedConfig the merged context configuration to use to load the * web application context * @see #loadContext(MergedContextConfiguration) */ protected void loadBeanDefinitions(AnnotationConfigWebApplicationContext context, WebMergedContextConfiguration webMergedConfig) { Class<?>[] annotatedClasses = webMergedConfig.getClasses(); if (logger.isDebugEnabled()) { logger.debug("Registering annotated classes: " + ObjectUtils.nullSafeToString(annotatedClasses)); } context.register(annotatedClasses); }
/** * Load a {@link MergeXmlWebApplicationContext} from the supplied {@link MergedContextConfiguration} * * <p>Implementation details: * * <ul> * <li>Calls {@link #validateMergedContextConfiguration(WebMergedContextConfiguration)} * to allow subclasses to validate the supplied configuration before proceeding.</li> * <li>Creates a {@link MergeXmlWebApplicationContext} instance.</li> * <li>If the supplied {@link MergeXmlWebApplicationContext} references a * {@linkplain MergeXmlWebApplicationContext#getParent() parent configuration}, * the corresponding {@link MergeXmlWebApplicationContext#getParentApplicationContext() * ApplicationContext} will be retrieved and * {@linkplain MergeXmlWebApplicationContext#setParent(ApplicationContext) set as the parent} * for the context created by this method.</li> * <li>Converts the patch locations into a single string to be set via * {@link MergeXmlWebApplicationContext#setPatchLocation(String)}</li> * <li>Sets the patch locations via {@link MergeXmlWebApplicationContext#setStandardLocationTypes(String)} * to the {@link StandardConfigLocations.TESTCONTEXTTYPE} for integration tests.</li> * <li>Delegates to {@link #configureWebResources} to create the {@link MockServletContext} and * set it in the {@code MergeXmlWebApplicationContext}.</li> * <li>Calls {@link #prepareContext} to allow for customizing the context before bean * definitions are loaded.</li> * <li>{@link ConfigurableApplicationContext#refresh Refreshes} the context and registers * a JVM shutdown hook for it.</li> * </ul></p> * * Refactored from {@link org.springframework.test.context.web.AbstractGenericWebContextLoader#loadContext(MergedContextConfiguration)} * * @return a new merge xml web application context * @see org.springframework.test.context.SmartContextLoader#loadContext(MergedContextConfiguration) * @see MergeXmlWebApplicationContext */ @Override public ConfigurableApplicationContext loadContext(MergedContextConfiguration mergedConfig) throws Exception { if (!(mergedConfig instanceof WebMergedContextConfiguration)) { throw new IllegalArgumentException(String.format( "Cannot load WebApplicationContext from non-web merged context configuration %s. " + "Consider annotating your test class with @WebAppConfiguration.", mergedConfig)); } WebMergedContextConfiguration webMergedConfig = (WebMergedContextConfiguration) mergedConfig; validateMergedContextConfiguration(webMergedConfig); MergeXmlWebApplicationContext context = new MergeXmlWebApplicationContext(); context.setPatchLocation(""); ApplicationContext parent = mergedConfig.getParentApplicationContext(); if (parent != null) { context.setParent(parent); context.setPatchLocation(StringUtils.removeEnd(((MergeXmlWebApplicationContext) parent).getPatchLocation(), "classpath:/bl-applicationContext-test.xml")); System.out.println(context.getPatchLocation()); } //Calls unique to Broadleaf Implementation of the Smart Context Loader // the ";classpath:/bl-applicationContext-test.xml" is required by all integration tests so we add it here. context.setPatchLocation(context.getPatchLocation() + StringUtils.join(mergedConfig.getLocations(), ";") +";classpath:/bl-applicationContext-test.xml"); context.setStandardLocationTypes(StandardConfigLocations.TESTCONTEXTTYPE); configureWebResources(context, webMergedConfig); prepareContext(context, webMergedConfig); context.refresh(); context.registerShutdownHook(); return context; }
/** * Validate the supplied {@link WebMergedContextConfiguration} with respect to * what this context loader supports. * <p>The default implementation is a <em>no-op</em> but can be overridden by * subclasses as appropriate. * * @param mergedConfig the merged configuration to validate * @throws IllegalStateException if the supplied configuration is not valid * for this context loader * @since 4.0.4 */ protected void validateMergedContextConfiguration(WebMergedContextConfiguration mergedConfig) { /* no-op */ }