Search in sources :

Example 26 with FilteredClassLoader

use of org.springframework.boot.test.context.FilteredClassLoader in project spring-boot by spring-projects.

the class ConditionalOnJavaTests method getJavaVersion.

private String getJavaVersion(Class<?>... hiddenClasses) throws Exception {
    FilteredClassLoader classLoader = new FilteredClassLoader(hiddenClasses);
    Class<?> javaVersionClass = Class.forName(JavaVersion.class.getName(), false, classLoader);
    Method getJavaVersionMethod = ReflectionUtils.findMethod(javaVersionClass, "getJavaVersion");
    Object javaVersion = ReflectionUtils.invokeMethod(getJavaVersionMethod, null);
    classLoader.close();
    return javaVersion.toString();
}
Also used : Method(java.lang.reflect.Method) JavaVersion(org.springframework.boot.system.JavaVersion) FilteredClassLoader(org.springframework.boot.test.context.FilteredClassLoader)

Example 27 with FilteredClassLoader

use of org.springframework.boot.test.context.FilteredClassLoader in project spring-boot by spring-projects.

the class DataSourcePropertiesTests method determineUrlWithNoEmbeddedSupport.

@Test
void determineUrlWithNoEmbeddedSupport() throws Exception {
    DataSourceProperties properties = new DataSourceProperties();
    properties.setBeanClassLoader(new FilteredClassLoader("org.h2", "org.apache.derby", "org.hsqldb"));
    properties.afterPropertiesSet();
    assertThatExceptionOfType(DataSourceProperties.DataSourceBeanCreationException.class).isThrownBy(properties::determineUrl).withMessageContaining("Failed to determine suitable jdbc url");
}
Also used : FilteredClassLoader(org.springframework.boot.test.context.FilteredClassLoader) Test(org.junit.jupiter.api.Test)

Example 28 with FilteredClassLoader

use of org.springframework.boot.test.context.FilteredClassLoader in project spring-boot by spring-projects.

the class DataSourceAutoConfigurationTests method assertDataSource.

private <T extends DataSource> void assertDataSource(Class<T> expectedType, List<String> hiddenPackages, Consumer<T> consumer) {
    FilteredClassLoader classLoader = new FilteredClassLoader(StringUtils.toStringArray(hiddenPackages));
    this.contextRunner.withClassLoader(classLoader).run((context) -> {
        DataSource bean = context.getBean(DataSource.class);
        assertThat(bean).isInstanceOf(expectedType);
        consumer.accept(expectedType.cast(bean));
    });
}
Also used : FilteredClassLoader(org.springframework.boot.test.context.FilteredClassLoader) DataSource(javax.sql.DataSource) BasicDataSource(org.apache.commons.dbcp2.BasicDataSource) HikariDataSource(com.zaxxer.hikari.HikariDataSource) SimpleDriverDataSource(org.springframework.jdbc.datasource.SimpleDriverDataSource)

Example 29 with FilteredClassLoader

use of org.springframework.boot.test.context.FilteredClassLoader in project spring-boot by spring-projects.

the class ValidatorAdapterTests method wrapperWhenValidationProviderNotPresentShouldNotThrowException.

@Test
void wrapperWhenValidationProviderNotPresentShouldNotThrowException() {
    ClassPathResource hibernateValidator = new ClassPathResource("META-INF/services/jakarta.validation.spi.ValidationProvider");
    this.contextRunner.withClassLoader(new FilteredClassLoader(FilteredClassLoader.ClassPathResourceFilter.of(hibernateValidator), FilteredClassLoader.PackageFilter.of("org.hibernate.validator"))).run((context) -> ValidatorAdapter.get(context, null));
}
Also used : ClassPathResource(org.springframework.core.io.ClassPathResource) FilteredClassLoader(org.springframework.boot.test.context.FilteredClassLoader) Test(org.junit.jupiter.api.Test)

Example 30 with FilteredClassLoader

use of org.springframework.boot.test.context.FilteredClassLoader in project spring-boot by spring-projects.

the class ReactiveWebServerFactoryAutoConfigurationTests method nettyServerCustomizerRegisteredAsBeanAndViaFactoryIsOnlyCalledOnce.

@Test
void nettyServerCustomizerRegisteredAsBeanAndViaFactoryIsOnlyCalledOnce() {
    new ReactiveWebApplicationContextRunner(AnnotationConfigReactiveWebServerApplicationContext::new).withConfiguration(AutoConfigurations.of(ReactiveWebServerFactoryAutoConfiguration.class)).withClassLoader(new FilteredClassLoader(Tomcat.class, Server.class, Undertow.class)).withUserConfiguration(DoubleRegistrationNettyServerCustomizerConfiguration.class, HttpHandlerConfiguration.class).withPropertyValues("server.port: 0").run((context) -> {
        NettyReactiveWebServerFactory factory = context.getBean(NettyReactiveWebServerFactory.class);
        NettyServerCustomizer customizer = context.getBean("serverCustomizer", NettyServerCustomizer.class);
        assertThat(factory.getServerCustomizers()).contains(customizer);
        then(customizer).should().apply(any(HttpServer.class));
    });
}
Also used : NettyReactiveWebServerFactory(org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory) NettyServerCustomizer(org.springframework.boot.web.embedded.netty.NettyServerCustomizer) HttpServer(reactor.netty.http.server.HttpServer) ReactiveWebApplicationContextRunner(org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner) FilteredClassLoader(org.springframework.boot.test.context.FilteredClassLoader) Test(org.junit.jupiter.api.Test)

Aggregations

FilteredClassLoader (org.springframework.boot.test.context.FilteredClassLoader)31 Test (org.junit.jupiter.api.Test)27 WebApplicationContextRunner (org.springframework.boot.test.context.runner.WebApplicationContextRunner)8 AnnotationConfigServletWebServerApplicationContext (org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext)7 ReactiveWebApplicationContextRunner (org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner)6 HttpServer (reactor.netty.http.server.HttpServer)6 Tomcat (org.apache.catalina.startup.Tomcat)4 Server (org.eclipse.jetty.server.Server)4 UndertowServletWebServerFactory (org.springframework.boot.web.embedded.undertow.UndertowServletWebServerFactory)4 AnnotationConfigReactiveWebApplicationContext (org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext)3 Builder (io.undertow.Undertow.Builder)2 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)2 JettyReactiveWebServerFactory (org.springframework.boot.web.embedded.jetty.JettyReactiveWebServerFactory)2 JettyServerCustomizer (org.springframework.boot.web.embedded.jetty.JettyServerCustomizer)2 JettyServletWebServerFactory (org.springframework.boot.web.embedded.jetty.JettyServletWebServerFactory)2 NettyReactiveWebServerFactory (org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory)2 UndertowBuilderCustomizer (org.springframework.boot.web.embedded.undertow.UndertowBuilderCustomizer)2 UndertowReactiveWebServerFactory (org.springframework.boot.web.embedded.undertow.UndertowReactiveWebServerFactory)2 Gson (com.google.gson.Gson)1 Config (com.hazelcast.config.Config)1