@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 testAddsDatasourceProxyListener() { context.setClassLoader(new HidePackagesClassLoader("com.vladmihalcea.flexypool", "com.p6spy")); context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, TraceAutoConfiguration.class, SleuthLogAutoConfiguration.class, SleuthListenerAutoConfiguration.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(TracingQueryExecutionListener.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 testCustomDataSourceDecoratorApplied() throws Exception { System.setProperty(PropertyLoader.PROPERTIES_FILE_PATH, "db/decorator/flexy-pool.properties"); context.register(TestDataSourceDecoratorConfiguration.class, DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); DataSource dataSource = context.getBean(DataSource.class); assertThat(dataSource).isNotNull(); DataSource customDataSource = ((DecoratedDataSource) dataSource).getDecoratedDataSource(); assertThat(customDataSource).isInstanceOf(CustomDataSourceProxy.class); DataSource realDataSource = ((DecoratedDataSource) dataSource).getRealDataSource(); assertThat(realDataSource).isInstanceOf(org.apache.tomcat.jdbc.pool.DataSource.class); assertThatDataSourceDecoratingChain(dataSource).containsExactly(CustomDataSourceProxy.class, P6DataSource.class, ProxyDataSource.class, FlexyPoolDataSource.class); }
@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 public void testJpaCoexistsHappily() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.url:jdbc:hsqldb:mem:testsecdb"); EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.initialize:false"); this.context.register(EntityConfiguration.class, PropertyPlaceholderAutoConfiguration.class, DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class, SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class); // This can fail if security @Conditionals force early instantiation of the // HibernateJpaAutoConfiguration (e.g. the EntityManagerFactory is not found) this.context.refresh(); assertThat(this.context.getBean(JpaTransactionManager.class)).isNotNull(); }
@Test public void testDefaultDdlAutoForMySql() throws Exception { // Set up environment so we get a MySQL database but don't require server to be // running... EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.driverClassName:com.mysql.jdbc.Driver", "spring.datasource.url:jdbc:mysql://localhost/nonexistent", "spring.datasource.initialize:false", "spring.jpa.database:MYSQL"); this.context.register(TestConfiguration.class, DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, HibernateJpaAutoConfiguration.class); this.context.refresh(); JpaProperties bean = this.context.getBean(JpaProperties.class); DataSource dataSource = this.context.getBean(DataSource.class); String actual = bean.getHibernateProperties(dataSource) .get("hibernate.hbm2ddl.auto"); // Default is generic and safe assertThat(actual).isNull(); }
@Test public void processEngineWithJpaEntityManager() throws Exception { AnnotationConfigApplicationContext context = this.context(DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class, JpaProcessEngineAutoConfiguration.JpaConfiguration.class); Assert.assertNotNull("entityManagerFactory should not be null", context.getBean(EntityManagerFactory.class)); Assert.assertNotNull("the processEngine should not be null!", context.getBean(ProcessEngine.class)); SpringProcessEngineConfiguration configuration = context.getBean(SpringProcessEngineConfiguration.class); Assert.assertNotNull("the " + SpringProcessEngineConfiguration.class.getName() + " should not be null", configuration); Assert.assertNotNull(configuration.getJpaEntityManagerFactory()); }
@Test public void testJpaCoexistsHappily() throws Exception { this.context = new AnnotationConfigWebApplicationContext(); this.context.setServletContext(new MockServletContext()); EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.url:jdbc:hsqldb:mem:testsecdb"); EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.initialize:false"); this.context.register(EntityConfiguration.class, PropertyPlaceholderAutoConfiguration.class, DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class, SecurityAutoConfiguration.class, ServerPropertiesAutoConfiguration.class); // This can fail if security @Conditionals force early instantiation of the // HibernateJpaAutoConfiguration (e.g. the EntityManagerFactory is not found) this.context.refresh(); assertNotNull(this.context.getBean(JpaTransactionManager.class)); }
@Test public void testDefaultDdlAutoForMySql() throws Exception { // Set up environment so we get a MySQL database but don't require server to be // running... EnvironmentTestUtils.addEnvironment(this.context, "spring.datasource.driverClassName:com.mysql.jdbc.Driver", "spring.datasource.url:jdbc:mysql://localhost/nonexistent", "spring.datasource.initialize:false", "spring.jpa.database:MYSQL"); this.context.register(TestConfiguration.class, DataSourceAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, HibernateJpaAutoConfiguration.class); this.context.refresh(); JpaProperties bean = this.context.getBean(JpaProperties.class); DataSource dataSource = this.context.getBean(DataSource.class); String actual = bean.getHibernateProperties(dataSource) .get("hibernate.hbm2ddl.auto"); // Default is generic and safe assertThat(actual, nullValue()); }
@Test public void testAddsP6SpyListener() { context.setClassLoader(new HidePackagesClassLoader("com.vladmihalcea.flexypool", "net.ttddyy.dsproxy")); context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, TraceAutoConfiguration.class, SleuthLogAutoConfiguration.class, SleuthListenerAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); JdbcEventListenerFactory jdbcEventListenerFactory = context.getBean(JdbcEventListenerFactory.class); CompoundJdbcEventListener jdbcEventListener = (CompoundJdbcEventListener) jdbcEventListenerFactory.createJdbcEventListener(); assertThat(jdbcEventListener.getEventListeners()).extracting("class").contains(TracingJdbcEventListener.class); }
@Test public void testDoesNotAddP6SpyListenerIfNoTracer() { context.setClassLoader(new HidePackagesClassLoader("com.vladmihalcea.flexypool", "net.ttddyy.dsproxy")); context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, SleuthListenerAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); JdbcEventListenerFactory jdbcEventListenerFactory = context.getBean(JdbcEventListenerFactory.class); CompoundJdbcEventListener jdbcEventListener = (CompoundJdbcEventListener) jdbcEventListenerFactory.createJdbcEventListener(); assertThat(jdbcEventListener.getEventListeners()).extracting("class").doesNotContain(TracingJdbcEventListener.class); }
@Test public void testDoesntAddDatasourceProxyListenerIfNoTracer() { context.setClassLoader(new HidePackagesClassLoader("com.vladmihalcea.flexypool", "com.p6spy")); context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, SleuthListenerAutoConfiguration.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").doesNotContain(TracingQueryExecutionListener.class); }
@Test public void testCustomListeners() throws Exception { context.register(CustomListenerConfiguration.class, DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); DataSource dataSource = context.getBean(DataSource.class); JdbcEventListenerFactory jdbcEventListenerFactory = context.getBean(JdbcEventListenerFactory.class); GetCountingListener getCountingListener = context.getBean(GetCountingListener.class); ClosingCountingListener closingCountingListener = context.getBean(ClosingCountingListener.class); P6DataSource p6DataSource = (P6DataSource) ((DecoratedDataSource) dataSource).getDecoratedDataSource(); assertThat(p6DataSource).extracting("jdbcEventListenerFactory").containsOnly(jdbcEventListenerFactory); CompoundJdbcEventListener jdbcEventListener = (CompoundJdbcEventListener) jdbcEventListenerFactory.createJdbcEventListener(); assertThat(jdbcEventListener.getEventListeners()).contains(getCountingListener, closingCountingListener); assertThat(getCountingListener.connectionCount).isEqualTo(0); Connection connection1 = p6DataSource.getConnection(); assertThat(getCountingListener.connectionCount).isEqualTo(1); assertThat(closingCountingListener.connectionCount).isEqualTo(0); Connection connection2 = p6DataSource.getConnection(); assertThat(getCountingListener.connectionCount).isEqualTo(2); connection1.close(); assertThat(closingCountingListener.connectionCount).isEqualTo(1); connection2.close(); assertThat(closingCountingListener.connectionCount).isEqualTo(2); }
@Test public void testRegisterLogAndSlowQueryLogByDefaultToSlf4j() throws Exception { 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 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); }
@Test public void testCustomParameterAndQueryTransformer() throws Exception { context.register(CustomParameterAndQueryTransformerConfiguration.class, DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); DataSource dataSource = context.getBean(DataSource.class); ProxyDataSource proxyDataSource = (ProxyDataSource) ((DecoratedDataSource) dataSource).getDecoratedDataSource(); ParameterTransformer parameterTransformer = context.getBean(ParameterTransformer.class); QueryTransformer queryTransformer = context.getBean(QueryTransformer.class); assertThat(proxyDataSource.getProxyConfig().getParameterTransformer()).isSameAs(parameterTransformer); assertThat(proxyDataSource.getProxyConfig().getQueryTransformer()).isSameAs(queryTransformer); }
@Test public void testCustomListeners() throws Exception { context.register(CustomListenerConfiguration.class, DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); DataSource dataSource = context.getBean(DataSource.class); ProxyDataSource proxyDataSource = (ProxyDataSource) ((DecoratedDataSource) dataSource).getDecoratedDataSource(); QueryExecutionListener queryExecutionListener = context.getBean(QueryExecutionListener.class); ChainListener chainListener = proxyDataSource.getProxyConfig().getQueryListener(); assertThat(chainListener.getListeners()).contains(queryExecutionListener); }
@Test public void testDecoratingInDefaultOrder() throws Exception { context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); DataSource dataSource = context.getBean(DataSource.class); assertThatDataSourceDecoratingChain(dataSource).containsExactly(P6DataSource.class, ProxyDataSource.class, FlexyPoolDataSource.class); }
@Test public void testNoDecoratingForExcludeBeans() throws Exception { EnvironmentTestUtils.addEnvironment(context, "decorator.datasource.exclude-beans:dataSource"); context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); DataSource dataSource = context.getBean(DataSource.class); assertThat(dataSource).isInstanceOf(org.apache.tomcat.jdbc.pool.DataSource.class); }
@Test public void testDecoratingWhenDefaultProxyProviderNotAvailable() throws Exception { context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.setClassLoader(new HidePackagesClassLoader("com.vladmihalcea.flexypool")); context.refresh(); DataSource dataSource = context.getBean(DataSource.class); assertThat(((DecoratedDataSource) dataSource).getRealDataSource()).isInstanceOf(org.apache.tomcat.jdbc.pool.DataSource.class); assertThatDataSourceDecoratingChain(dataSource).containsExactly(P6DataSource.class, ProxyDataSource.class); }
@Test public void testDefaultDataSourceIsDecorated() throws Exception { context.register(TestDataSourceConfiguration.class, DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); DataSource dataSource = context.getBean(DataSource.class); assertThat(dataSource).isInstanceOf(DecoratedDataSource.class); DataSource realDataSource = ((DecoratedDataSource) dataSource).getRealDataSource(); assertThat(realDataSource).isInstanceOf(BasicDataSource.class); }
@Test public void testDecoratingCanBeDisabled() throws Exception { EnvironmentTestUtils.addEnvironment(context, "decorator.datasource.enabled:false"); context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); DataSource dataSource = context.getBean(DataSource.class); assertThat(dataSource).isInstanceOf(org.apache.tomcat.jdbc.pool.DataSource.class); }
@Test public void testDecoratingChainBuiltCorrectly() throws Exception { System.setProperty(PropertyLoader.PROPERTIES_FILE_PATH, "db/decorator/flexy-pool.properties"); context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); DataSource dataSource = context.getBean(DataSource.class); DecoratedDataSource dataSource1 = context.getBean(DecoratedDataSource.class); assertThat(dataSource1).isNotNull(); DataSource p6DataSource = dataSource1.getDecoratedDataSource(); assertThat(p6DataSource).isNotNull(); assertThat(p6DataSource).isInstanceOf(P6DataSource.class); DataSource proxyDataSource = (DataSource) new DirectFieldAccessor(p6DataSource) .getPropertyValue("realDataSource"); assertThat(proxyDataSource).isNotNull(); assertThat(proxyDataSource).isInstanceOf(ProxyDataSource.class); DataSource flexyDataSource = (DataSource) new DirectFieldAccessor(proxyDataSource) .getPropertyValue("dataSource"); assertThat(flexyDataSource).isNotNull(); assertThat(flexyDataSource).isInstanceOf(FlexyPoolDataSource.class); DataSource realDataSource = (DataSource) new DirectFieldAccessor(flexyDataSource) .getPropertyValue("targetDataSource"); assertThat(realDataSource).isNotNull(); assertThat(realDataSource).isInstanceOf((Class<? extends DataSource>) org.apache.tomcat.jdbc.pool.DataSource.class); assertThatDataSourceDecoratingChain(dataSource).containsExactly(P6DataSource.class, ProxyDataSource.class, FlexyPoolDataSource.class); assertThat(dataSource.toString()).isEqualTo( "p6SpyDataSourceDecorator [com.p6spy.engine.spy.P6DataSource] -> " + "proxyDataSourceDecorator [net.ttddyy.dsproxy.support.ProxyDataSource] -> " + "flexyPoolDataSourceDecorator [com.vladmihalcea.flexypool.FlexyPoolDataSource] -> " + "dataSource [org.apache.tomcat.jdbc.pool.DataSource]"); }
@Test public void testReturnDataSourcePoolMetadataProviderForHikari() { 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); Assertions.assertThat(dataSource).isInstanceOf(DecoratedDataSource.class); DataSourcePoolMetadataProviders poolMetadataProvider = new DataSourcePoolMetadataProviders(context.getBeansOfType(DataSourcePoolMetadataProvider.class).values()); DataSourcePoolMetadata dataSourcePoolMetadata = poolMetadataProvider.getDataSourcePoolMetadata(dataSource); Assertions.assertThat(dataSourcePoolMetadata).isInstanceOf(HikariDataSourcePoolMetadata.class); }
@Test public void testReturnDataSourcePoolMetadataProviderForTomcat() { context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); DataSource dataSource = context.getBean(DataSource.class); Assertions.assertThat(dataSource).isInstanceOf(DecoratedDataSource.class); DataSourcePoolMetadataProviders poolMetadataProvider = new DataSourcePoolMetadataProviders(context.getBeansOfType(DataSourcePoolMetadataProvider.class).values()); DataSourcePoolMetadata dataSourcePoolMetadata = poolMetadataProvider.getDataSourcePoolMetadata(dataSource); Assertions.assertThat(dataSourcePoolMetadata).isInstanceOf(TomcatDataSourcePoolMetadata.class); }
@Test public void testReturnDataSourcePoolMetadataProviderForDbcp2() { EnvironmentTestUtils.addEnvironment(context, "spring.datasource.type:" + org.apache.commons.dbcp2.BasicDataSource.class.getName()); context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); DataSource dataSource = context.getBean(DataSource.class); Assertions.assertThat(dataSource).isInstanceOf(DecoratedDataSource.class); DataSourcePoolMetadataProviders poolMetadataProvider = new DataSourcePoolMetadataProviders(context.getBeansOfType(DataSourcePoolMetadataProvider.class).values()); DataSourcePoolMetadata dataSourcePoolMetadata = poolMetadataProvider.getDataSourcePoolMetadata(dataSource); Assertions.assertThat(dataSourcePoolMetadata).isInstanceOf(CommonsDbcp2DataSourcePoolMetadata.class); }
@Test public void testDecoratingTomcatDataSource() throws Exception { context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); DataSource dataSource = context.getBean(DataSource.class); assertDataSourceOfType(dataSource, org.apache.tomcat.jdbc.pool.DataSource.class); }
@Test public void testNoDecoratingTomcatDataSourceWithoutAdapterDependency() throws Exception { context.setClassLoader(new HidePackagesClassLoader("net.ttddyy.dsproxy", "com.p6spy", "com.vladmihalcea.flexypool.adaptor.TomcatCPPoolAdapter")); context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); DataSource dataSource = context.getBean(DataSource.class); assertThat(dataSource).isInstanceOf(org.apache.tomcat.jdbc.pool.DataSource.class); }
@Test public void testDecoratingDbcp2DataSource() throws Exception { EnvironmentTestUtils.addEnvironment(context, "spring.datasource.type:" + BasicDataSource.class.getName()); context.register(DataSourceAutoConfiguration.class, DataSourceDecoratorAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); context.refresh(); DataSource dataSource = context.getBean(DataSource.class); DataSource realDataSource = ((DecoratedDataSource) dataSource).getRealDataSource(); assertDataSourceOfType(dataSource, BasicDataSource.class); }
@Test public void testDecoratingHikariDataSourceWithCustomBeanStrategies() throws Exception { EnvironmentTestUtils.addEnvironment(context, "spring.datasource.type:" + HikariDataSource.class.getName()); 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); HikariConnectionAcquiringFactory strategy3 = findStrategy(flexyPoolDataSource, HikariConnectionAcquiringFactory.class); assertThat(strategy3).isNotNull(); Dbcp2ConnectionAcquiringFactory strategy4 = findStrategy(flexyPoolDataSource, Dbcp2ConnectionAcquiringFactory.class); assertThat(strategy4).isNull(); }
@Test public void autoDataSource() { load(DataSourceAutoConfiguration.class); PublicMetrics bean = this.context.getBean(DataSourcePublicMetrics.class); Collection<Metric<?>> metrics = bean.metrics(); assertMetrics(metrics, "datasource.primary.active", "datasource.primary.usage"); }
@Test public void singleTransactionManager() { load(DataSourceAutoConfiguration.class, DataSourceTransactionManagerAutoConfiguration.class); PlatformTransactionManager transactionManager = this.context .getBean(PlatformTransactionManager.class); TransactionTemplate transactionTemplate = this.context .getBean(TransactionTemplate.class); assertThat(transactionTemplate.getTransactionManager()) .isSameAs(transactionManager); }