@Before public void init() { dbUrl = "h2:mem:testdb-" + new Random().nextInt(); EnvironmentTestUtils.addEnvironment(context, "spring.datasource.initialize:false", "spring.datasource.url:jdbc:" + dbUrl); context.setClassLoader(new HidePackagesClassLoader("com.vladmihalcea.flexypool", "net.ttddyy.dsproxy")); context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, TraceAutoConfiguration.class, SleuthLogAutoConfiguration.class, SleuthListenerAutoConfiguration.class, SavingSpanReporterConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); dataSource = context.getBean(DataSource.class); spanReporter = context.getBean(CollectingSpanReporter.class); }
@Before public void init() { dbUrl = "h2:mem:testdb-" + new Random().nextInt(); EnvironmentTestUtils.addEnvironment(context, "spring.datasource.initialize:false", "spring.datasource.url:jdbc:" + dbUrl); context.setClassLoader(new HidePackagesClassLoader("com.vladmihalcea.flexypool", "com.p6spy")); context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, SavingSpanReporterConfiguration.class, TraceAutoConfiguration.class, SleuthLogAutoConfiguration.class, SleuthListenerAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); dataSource = context.getBean(DataSource.class); spanReporter = context.getBean(CollectingSpanReporter.class); }
@Test public void testDecoratedHikariSpecificPropertiesIsSet() throws Exception { EnvironmentTestUtils.addEnvironment(context, "spring.datasource.type:" + HikariDataSource.class.getName(), "spring.datasource.hikari.catalog:test_catalog"); context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); DataSource dataSource = context.getBean(DataSource.class); assertThat(dataSource).isNotNull(); assertThat(dataSource).isInstanceOf(DecoratedDataSource.class); DataSource realDataSource = ((DecoratedDataSource) dataSource).getRealDataSource(); assertThat(realDataSource).isInstanceOf(HikariDataSource.class); assertThat(((HikariDataSource) realDataSource).getCatalog()).isEqualTo("test_catalog"); }
@Test public void testDecoratingCanBeDisabledForSpecificBeans() throws Exception { EnvironmentTestUtils.addEnvironment(context, "decorator.datasource.exclude-beans:secondDataSource"); context.register(TestMultiDataSourceConfiguration.class, DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); DataSource dataSource = context.getBean("dataSource", DataSource.class); assertThat(dataSource).isInstanceOf(DecoratedDataSource.class); DataSource secondDataSource = context.getBean("secondDataSource", DataSource.class); assertThat(secondDataSource).isInstanceOf(BasicDataSource.class); }
@Test public void testDecoratingHikariDataSourceWithDefaultStrategies() throws Exception { EnvironmentTestUtils.addEnvironment(context, "spring.datasource.type:" + HikariDataSource.class.getName()); context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); DataSource dataSource = context.getBean(DataSource.class); assertDataSourceOfType(dataSource, HikariDataSource.class); FlexyPoolDataSource<HikariDataSource> flexyPoolDataSource = assertDataSourceOfType(dataSource, HikariDataSource.class); IncrementPoolOnTimeoutConnectionAcquiringStrategy strategy1 = findStrategy(flexyPoolDataSource, IncrementPoolOnTimeoutConnectionAcquiringStrategy.class); assertThat(strategy1).isNotNull(); assertThat(strategy1).hasFieldOrPropertyWithValue("maxOverflowPoolSize", 15); assertThat(strategy1).hasFieldOrPropertyWithValue("timeoutMillis", 500); RetryConnectionAcquiringStrategy strategy2 = findStrategy(flexyPoolDataSource, RetryConnectionAcquiringStrategy.class); assertThat(strategy2).isNotNull(); assertThat(strategy2).hasFieldOrPropertyWithValue("retryAttempts", 2); }
@Test public void testDecoratingHikariDataSourceWithCustomPropertyStrategies() throws Exception { EnvironmentTestUtils.addEnvironment(context, "spring.datasource.type:" + HikariDataSource.class.getName(), "decorator.datasource.flexy-pool.acquiring-strategy.increment-pool.max-overflow-pool-size:15", "decorator.datasource.flexy-pool.acquiring-strategy.increment-pool.timeout-millis:500", "decorator.datasource.flexy-pool.acquiring-strategy.retry.attempts:5"); context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, HikariConfiguration.class); context.refresh(); DataSource dataSource = context.getBean(DataSource.class); FlexyPoolDataSource<HikariDataSource> flexyPoolDataSource = assertDataSourceOfType(dataSource, HikariDataSource.class); IncrementPoolOnTimeoutConnectionAcquiringStrategy strategy1 = findStrategy(flexyPoolDataSource, IncrementPoolOnTimeoutConnectionAcquiringStrategy.class); assertThat(strategy1).isNotNull(); assertThat(strategy1).hasFieldOrPropertyWithValue("maxOverflowPoolSize", 35); assertThat(strategy1).hasFieldOrPropertyWithValue("timeoutMillis", 10000); RetryConnectionAcquiringStrategy strategy2 = findStrategy(flexyPoolDataSource, RetryConnectionAcquiringStrategy.class); assertThat(strategy2).isNotNull(); assertThat(strategy2).hasFieldOrPropertyWithValue("retryAttempts", 5); }
@Test(expected = BeanCreationException.class) public void testMissingSSLProperty() throws Exception { context.register(SpringBootstrap.class); // @formatter:off EnvironmentTestUtils.addEnvironment(this.context, "qonduit.server.ip:127.0.0.1", "qonduit.server.tcp-port:54321", "qonduit.server.udp-port:54325", "qonduit.http.ip:127.0.0.1", "qonduit.http.port:54322", "qonduit.websocket.ip:127.0.0.1", "qonduit.websocket.port:54323", "qonduit.accumulo.zookeepers:localhost:2181", "qonduit.accumulo.instance-name:test", "qonduit.accumulo.username:root", "qonduit.accumulo.password:secret", "qonduit.http.host:localhost"); // @formatter:on context.refresh(); }
@Test public void testSSLProperty() throws Exception { context.register(SpringBootstrap.class); // @formatter:off EnvironmentTestUtils.addEnvironment(this.context, "qonduit.server.ip:127.0.0.1", "qonduit.http.ip:127.0.0.1", "qonduit.http.port:54322", "qonduit.websocket.ip:127.0.0.1", "qonduit.websocket.port:54323", "qonduit.accumulo.zookeepers:localhost:2181", "qonduit.accumulo.instance-name:test", "qonduit.accumulo.username:root", "qonduit.accumulo.password:secret", "qonduit.http.host:localhost", "qonduit.security.ssl.certificate-file:/tmp/foo", "qonduit.security.ssl.key-file:/tmp/bar"); // @formatter:on context.refresh(); }
@Test public void createFromConfigClass() throws Exception { EnvironmentTestUtils.addEnvironment(this.context, "spring.thymeleaf.mode:XHTML", "spring.thymeleaf.suffix:"); this.context.register(ThymeleafAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); TemplateEngine engine = this.context.getBean(TemplateEngine.class); Context attrs = new Context(Locale.UK, Collections.singletonMap("foo", "bar")); String result = engine.process("template.txt", attrs); assertThat(result).isEqualTo("<html>bar</html>"); }
@Test public void testAuthorizationServerOverride() { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "security.oauth2.resourceId:resource-id"); this.context.register(AuthorizationAndResourceServerConfiguration.class, CustomAuthorizationServer.class, MinimalSecureWebApplication.class); this.context.refresh(); BaseClientDetails config = new BaseClientDetails(); config.setClientId("client"); config.setClientSecret("secret"); config.setResourceIds(Arrays.asList("resource-id")); config.setAuthorizedGrantTypes(Arrays.asList("password")); config.setAuthorities(AuthorityUtils.commaSeparatedStringToAuthorityList("USER")); config.setScope(Arrays.asList("read")); assertThat(countBeans(AUTHORIZATION_SERVER_CONFIG)).isEqualTo(0); assertThat(countBeans(RESOURCE_SERVER_CONFIG)).isEqualTo(1); verifyAuthentication(config); }
@Test @SuppressWarnings("unchecked") public void testNestedNaming() throws Exception { this.context.register(FooConfig.class); EnvironmentTestUtils.addEnvironment(this.context, "foo.bar.name:foo"); this.context.refresh(); ConfigurationPropertiesReportEndpoint report = this.context .getBean(ConfigurationPropertiesReportEndpoint.class); Map<String, Object> properties = report.invoke(); Map<String, Object> nestedProperties = (Map<String, Object>) properties .get("foo"); assertThat(nestedProperties).isNotNull(); Map<String, Object> map = (Map<String, Object>) nestedProperties .get("properties"); assertThat(map).isNotNull(); assertThat(map).hasSize(2); assertThat(((Map<String, Object>) map.get("bar")).get("name")).isEqualTo("foo"); }
@Test @SuppressWarnings("unchecked") public void testKeySanitizationWithCustomPatternUsingCompositeKeys() throws Exception { // gh-4415 this.context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "endpoints.configprops.keys-to-sanitize: .*\\.secrets\\..*, .*\\.hidden\\..*"); this.context.register(Config.class); this.context.refresh(); ConfigurationPropertiesReportEndpoint report = getEndpointBean(); Map<String, Object> properties = report.invoke(); Map<String, Object> nestedProperties = (Map<String, Object>) ((Map<String, Object>) properties .get("testProperties")).get("properties"); assertThat(nestedProperties).isNotNull(); Map<String, Object> secrets = (Map<String, Object>) nestedProperties .get("secrets"); Map<String, Object> hidden = (Map<String, Object>) nestedProperties.get("hidden"); assertThat(secrets.get("mine")).isEqualTo("******"); assertThat(secrets.get("yours")).isEqualTo("******"); assertThat(hidden.get("mine")).isEqualTo("******"); }
@Test public void customizationOfSupportedMediaTypesCanBeDisabled() { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); this.context.register(BaseConfig.class); EnvironmentTestUtils.addEnvironment(this.context, "spring.hateoas.use-hal-as-default-json-media-type:false"); this.context.refresh(); RequestMappingHandlerAdapter handlerAdapter = this.context .getBean(RequestMappingHandlerAdapter.class); for (HttpMessageConverter<?> converter : handlerAdapter.getMessageConverters()) { if (converter instanceof TypeConstrainedMappingJackson2HttpMessageConverter) { assertThat(converter.getSupportedMediaTypes()) .containsExactly(MediaTypes.HAL_JSON); } } }
@Test public void testSecurityFilterDoesNotCauseEarlyInitialization() throws Exception { AnnotationConfigEmbeddedWebApplicationContext context = new AnnotationConfigEmbeddedWebApplicationContext(); try { EnvironmentTestUtils.addEnvironment(context, "server.port:0", "security.user.password:password"); context.register(Config.class); context.refresh(); int port = context.getEmbeddedServletContainer().getPort(); new TestRestTemplate("user", "password") .getForEntity("http://localhost:" + port, Object.class); // If early initialization occurred a ConverterNotFoundException is thrown } finally { context.close(); } }
@SuppressWarnings("unchecked") @Test public void testKeySanitizationWithCustomPatternAndKeyByEnvironment() throws Exception { this.context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "endpoints.env.keys-to-sanitize: .*pass.*, key"); this.context.register(Config.class); this.context.refresh(); System.setProperty("dbPassword", "123456"); System.setProperty("apiKey", "123456"); EnvironmentEndpoint report = getEndpointBean(); Map<String, Object> env = report.invoke(); Map<String, Object> systemProperties = (Map<String, Object>) env .get("systemProperties"); assertThat(systemProperties.get("dbPassword")).isEqualTo("******"); assertThat(systemProperties.get("apiKey")).isEqualTo("******"); }
public void testFieldNamingStrategy(String strategy, Class<? extends FieldNamingStrategy> expectedType) { this.context = new AnnotationConfigApplicationContext(); if (strategy != null) { EnvironmentTestUtils.addEnvironment(this.context, "spring.data.mongodb.field-naming-strategy:" + strategy); } this.context.register(PropertyPlaceholderAutoConfiguration.class, MongoAutoConfiguration.class, MongoDataAutoConfiguration.class); this.context.refresh(); MongoMappingContext mappingContext = this.context .getBean(MongoMappingContext.class); FieldNamingStrategy fieldNamingStrategy = (FieldNamingStrategy) ReflectionTestUtils .getField(mappingContext, "fieldNamingStrategy"); assertThat(fieldNamingStrategy.getClass()).isEqualTo(expectedType); }
@Test public void testDataSourceInitializedWithSchemaCredentials() { this.context.register(DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.initialize:true", "spring.datasource.sqlScriptEncoding:UTF-8", "spring.datasource.schema:" + ClassUtils .addResourcePathToPackagePath(getClass(), "encoding-schema.sql"), "spring.datasource.data:" + ClassUtils .addResourcePathToPackagePath(getClass(), "encoding-data.sql"), "spring.datasource.schema-username:admin", "spring.datasource.schema-password:admin"); try { this.context.refresh(); fail("User does not exist"); } catch (Exception ex) { assertThat(ex).isInstanceOf(UnsatisfiedDependencyException.class); } }
@Test public void dataSourceHealthIndicatorWithCustomValidationQuery() { this.context.register(PropertyPlaceholderAutoConfiguration.class, ManagementServerProperties.class, DataSourceProperties.class, DataSourceConfig.class, DataSourcePoolMetadataProvidersConfiguration.class, HealthIndicatorAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.test.validation-query:SELECT from FOOBAR", "management.health.diskspace.enabled:false"); this.context.refresh(); Map<String, HealthIndicator> beans = this.context .getBeansOfType(HealthIndicator.class); assertThat(beans).hasSize(1); HealthIndicator healthIndicator = beans.values().iterator().next(); assertThat(healthIndicator.getClass()).isEqualTo(DataSourceHealthIndicator.class); DataSourceHealthIndicator dataSourceHealthIndicator = (DataSourceHealthIndicator) healthIndicator; assertThat(dataSourceHealthIndicator.getQuery()).isEqualTo("SELECT from FOOBAR"); }
@Test public void testInfoEndpointOrdering() throws Exception { this.context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "info.name:foo"); this.context.register(CustomInfoContributorsConfig.class, ProjectInfoAutoConfiguration.class, InfoContributorAutoConfiguration.class, EndpointAutoConfiguration.class); this.context.refresh(); InfoEndpoint endpoint = this.context.getBean(InfoEndpoint.class); Map<String, Object> info = endpoint.invoke(); assertThat(info).isNotNull(); assertThat(info.get("name")).isEqualTo("foo"); assertThat(info.get("version")).isEqualTo("1.0"); Object git = info.get("git"); assertThat(git).isInstanceOf(Map.class); }
@Test public void enableMapperFeature() throws Exception { this.context.register(JacksonAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(this.context, "spring.jackson.mapper.require_setters_for_getters:true"); this.context.refresh(); ObjectMapper mapper = this.context.getBean(ObjectMapper.class); assertThat(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS.enabledByDefault()) .isFalse(); assertThat(mapper.getSerializationConfig() .hasMapperFeatures(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS.getMask())) .isTrue(); assertThat(mapper.getDeserializationConfig() .hasMapperFeatures(MapperFeature.REQUIRE_SETTERS_FOR_GETTERS.getMask())) .isTrue(); }
@Test @SuppressWarnings("unchecked") public void testInetAddress() throws Exception { this.context.register(AddressedConfig.class); EnvironmentTestUtils.addEnvironment(this.context, "foo.address:192.168.1.10"); this.context.refresh(); ConfigurationPropertiesReportEndpoint report = this.context .getBean(ConfigurationPropertiesReportEndpoint.class); Map<String, Object> properties = report.invoke(); Map<String, Object> nestedProperties = (Map<String, Object>) properties .get("foo"); assertThat(nestedProperties).isNotNull(); System.err.println(nestedProperties); assertThat(nestedProperties.get("prefix")).isEqualTo("foo"); Map<String, Object> map = (Map<String, Object>) nestedProperties .get("properties"); assertThat(map).isNotNull(); assertThat(map).hasSize(3); assertThat(map.get("address")).isEqualTo("192.168.1.10"); }
@Test public void testCustomNamingStrategyViaJpaProperties() throws Exception { HibernateVersion.setRunning(HibernateVersion.V4); EnvironmentTestUtils.addEnvironment(this.context, "spring.jpa.properties.hibernate.ejb.naming_strategy:" + "org.hibernate.cfg.EJB3NamingStrategy"); setupTestConfiguration(); this.context.refresh(); LocalContainerEntityManagerFactoryBean bean = this.context .getBean(LocalContainerEntityManagerFactoryBean.class); String actual = (String) bean.getJpaPropertyMap() .get("hibernate.ejb.naming_strategy"); // You can't override this one from spring.jpa.properties because it has an // opinionated default assertThat(actual).isNotEqualTo("org.hibernate.cfg.EJB3NamingStrategy"); }
@Test public void testEnvironmentalOverrides() { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "security.oauth2.client.clientId:myclientid", "security.oauth2.client.clientSecret:mysecret", "security.oauth2.client.autoApproveScopes:read,write", "security.oauth2.client.accessTokenValiditySeconds:40", "security.oauth2.client.refreshTokenValiditySeconds:80"); this.context.register(AuthorizationAndResourceServerConfiguration.class, MinimalSecureWebApplication.class); this.context.refresh(); ClientDetails config = this.context.getBean(ClientDetails.class); assertThat(config.getClientId()).isEqualTo("myclientid"); assertThat(config.getClientSecret()).isEqualTo("mysecret"); assertThat(config.isAutoApprove("read")).isTrue(); assertThat(config.isAutoApprove("write")).isTrue(); assertThat(config.isAutoApprove("foo")).isFalse(); assertThat(config.getAccessTokenValiditySeconds()).isEqualTo(40); assertThat(config.getRefreshTokenValiditySeconds()).isEqualTo(80); verifyAuthentication(config); }
@Test public void defaultPropertyValues() throws Exception { this.context = new AnnotationConfigEmbeddedWebApplicationContext(); EnvironmentTestUtils.addEnvironment(this.context, "spring.mobile.devicedelegatingviewresolver.enabled:true"); this.context.register(Config.class, WebMvcAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, DeviceDelegatingViewResolverConfiguration.class); this.context.refresh(); LiteDeviceDelegatingViewResolver liteDeviceDelegatingViewResolver = this.context .getBean("deviceDelegatingViewResolver", LiteDeviceDelegatingViewResolver.class); DirectFieldAccessor accessor = new DirectFieldAccessor( liteDeviceDelegatingViewResolver); assertThat(accessor.getPropertyValue("enableFallback")).isEqualTo(Boolean.FALSE); assertThat(accessor.getPropertyValue("normalPrefix")).isEqualTo(""); assertThat(accessor.getPropertyValue("mobilePrefix")).isEqualTo("mobile/"); assertThat(accessor.getPropertyValue("tabletPrefix")).isEqualTo("tablet/"); assertThat(accessor.getPropertyValue("normalSuffix")).isEqualTo(""); assertThat(accessor.getPropertyValue("mobileSuffix")).isEqualTo(""); assertThat(accessor.getPropertyValue("tabletSuffix")).isEqualTo(""); }
@Override public void initialize(ConfigurableApplicationContext configurableApplicationContext) { EnvironmentTestUtils.addEnvironment("testcontainers", configurableApplicationContext.getEnvironment(), "cassandra.host=" + cassandra.getContainerIpAddress(), "cassandra.port=" + cassandra.getMappedPort(9042) ); }
@Test public void labelsLocationCanBeCustomized() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(context, "inception.labelsLocation:/remote"); context.register(Conf.class); context.refresh(); LabelImageProcessorProperties properties = context.getBean(LabelImageProcessorProperties.class); assertThat(properties.getLabelsLocation(), equalTo(context.getResource("/remote"))); }
@Test public void alternativesLengthCanBeCustomized() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(context, "inception.labelsLocation:/remote"); EnvironmentTestUtils.addEnvironment(context, "inception.alternativesLength:5"); context.register(Conf.class); context.refresh(); LabelImageProcessorProperties properties = context.getBean(LabelImageProcessorProperties.class); assertThat(properties.getAlternativesLength(), equalTo(5)); }
@Test public void modelLocationCanBeCustomized() { EnvironmentTestUtils.addEnvironment(context, "tensorflow.modelLocation:/remote"); context.register(Conf.class); context.refresh(); TensorflowProcessorProperties properties = context.getBean(TensorflowProcessorProperties.class); assertThat(properties.getModelLocation(), equalTo(context.getResource("/remote"))); }
@Test public void outputNameCanBeCustomized() { EnvironmentTestUtils.addEnvironment(context, "tensorflow.outputName:output1"); context.register(Conf.class); context.refresh(); TensorflowProcessorProperties properties = context.getBean(TensorflowProcessorProperties.class); assertThat(properties.getOutputName(), equalTo("output1")); }
@Test public void outputIndexCanBeCustomized() { EnvironmentTestUtils.addEnvironment(context, "tensorflow.outputIndex:666"); context.register(Conf.class); context.refresh(); TensorflowProcessorProperties properties = context.getBean(TensorflowProcessorProperties.class); assertThat(properties.getOutputIndex(), equalTo(666)); }
@Test public void saveOutputInHeaderCanBeCustomized() { EnvironmentTestUtils.addEnvironment(context, "tensorflow.saveOutputInHeader:false"); context.register(Conf.class); context.refresh(); TensorflowProcessorProperties properties = context.getBean(TensorflowProcessorProperties.class); assertFalse(properties.isSaveOutputInHeader()); }
@Test public void vocabularyLocationCanBeCustomized() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(context, "inception.vocabularyLocation:/remote"); context.register(Conf.class); context.refresh(); TwitterSentimentProcessorProperties properties = context.getBean(TwitterSentimentProcessorProperties.class); assertThat(properties.getVocabularyLocation(), equalTo(context.getResource("/remote"))); }
private void load(Class<?> config, String... environment) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); applicationContext.register(config); applicationContext.register(QpidJMSAutoConfiguration.class, JmsAutoConfiguration.class); EnvironmentTestUtils.addEnvironment(applicationContext, environment); applicationContext.refresh(); this.context = applicationContext; }
@Before public void init() { EnvironmentTestUtils.addEnvironment(context, "datasource.initialize:false", "datasource.url:jdbc:h2:mem:testdb-" + new Random().nextInt()); context.setClassLoader(new HidePackagesClassLoader("com.vladmihalcea.flexypool", "net.ttddyy.dsproxy")); }
@Before public void init() { EnvironmentTestUtils.addEnvironment(context, "datasource.initialize:false", "datasource.url:jdbc:h2:mem:testdb-" + new Random().nextInt()); context.setClassLoader(new HidePackagesClassLoader("com.vladmihalcea.flexypool", "com.p6spy")); }
@Test public void testRegisterLogAndSlowQueryLogUsingSlf4j() throws Exception { EnvironmentTestUtils.addEnvironment(context, "decorator.datasource.datasource-proxy.logging:slf4j"); context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); DataSource dataSource = context.getBean(DataSource.class); ProxyDataSource proxyDataSource = (ProxyDataSource) ((DecoratedDataSource) dataSource).getDecoratedDataSource(); ChainListener chainListener = proxyDataSource.getProxyConfig().getQueryListener(); assertThat(chainListener.getListeners()).extracting("class").contains(SLF4JSlowQueryListener.class); assertThat(chainListener.getListeners()).extracting("class").contains(SLF4JQueryLoggingListener.class); }
@Test public void testRegisterLogAndSlowQueryLogUsingJUL() throws Exception { EnvironmentTestUtils.addEnvironment(context, "decorator.datasource.datasourceProxy.logging:jul"); context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); DataSource dataSource = context.getBean(DataSource.class); ProxyDataSource proxyDataSource = (ProxyDataSource) ((DecoratedDataSource) dataSource).getDecoratedDataSource(); ChainListener chainListener = proxyDataSource.getProxyConfig().getQueryListener(); assertThat(chainListener.getListeners()).extracting("class").contains(JULSlowQueryListener.class); assertThat(chainListener.getListeners()).extracting("class").contains(JULQueryLoggingListener.class); }
@Test public void testRegisterLogAndSlowQueryLogUsingApacheCommons() throws Exception { EnvironmentTestUtils.addEnvironment(context, "decorator.datasource.datasourceProxy.logging:commons"); context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); DataSource dataSource = context.getBean(DataSource.class); ProxyDataSource proxyDataSource = (ProxyDataSource) ((DecoratedDataSource) dataSource).getDecoratedDataSource(); ChainListener chainListener = proxyDataSource.getProxyConfig().getQueryListener(); assertThat(chainListener.getListeners()).extracting("class").contains(CommonsSlowQueryListener.class); assertThat(chainListener.getListeners()).extracting("class").contains(CommonsQueryLoggingListener.class); }