@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 providesCollectorComponent_whenBootstrapServersSet() { addEnvironment(context, "zipkin.sparkstreaming.stream.kafka.bootstrap-servers:" + KAFKA_BOOTSTRAP ); context.register( PropertyPlaceholderAutoConfiguration.class, ZipkinKafkaStreamFactoryAutoConfiguration.class ); context.refresh(); ZipkinKafkaStreamFactoryProperties props = context.getBean(ZipkinKafkaStreamFactoryProperties.class); assertThat(props.getBootstrapServers()) .containsExactly(KAFKA_BOOTSTRAP); }
@Test public void providesCollectorComponent_whenKafkaZookeeperSet() { addEnvironment(context, "zipkin.sparkstreaming.stream.kafka.zookeeper.connect:" + KAFKA_ZOOKEEPER ); context.register( PropertyPlaceholderAutoConfiguration.class, ZipkinKafkaStreamFactoryAutoConfiguration.class ); context.refresh(); ZipkinKafkaStreamFactoryProperties props = context.getBean(ZipkinKafkaStreamFactoryProperties.class); assertThat(props.getZookeeper().getConnect()) .isEqualTo(KAFKA_ZOOKEEPER); }
@Test public void provideCollectorComponent_setsZipkinSqsCollectorProperties() { context = new AnnotationConfigApplicationContext(); addEnvironment(context, "zipkin.collector.sqs.queue-url:" + sqsRule.queueUrl()); addEnvironment(context, "zipkin.collector.sqs.wait-time-seconds:1"); addEnvironment(context, "zipkin.collector.sqs.parallelism:3"); addEnvironment(context, "zipkin.collector.sqs.aws-access-key-id: x"); addEnvironment(context, "zipkin.collector.sqs.aws-secret-access-key: x"); context.register(PropertyPlaceholderAutoConfiguration.class, Region.class, ZipkinSQSCollectorAutoConfiguration.class, ZipkinSQSCredentialsAutoConfiguration.class, InMemoryConfiguration.class); context.refresh(); ZipkinSQSCollectorProperties properties = context.getBean(ZipkinSQSCollectorProperties.class); assertThat(properties.getQueueUrl()).isEqualTo(sqsRule.queueUrl()); assertThat(properties.getWaitTimeSeconds()).isEqualTo(1); assertThat(properties.getParallelism()).isEqualTo(3); }
@Test public void provideSecurityTokenService_whenAwsStsRoleArnIsSet() { context = new AnnotationConfigApplicationContext(); addEnvironment(context, "zipkin.collector.sqs.queue-url:" + sqsRule.queueUrl()); addEnvironment(context, "zipkin.collector.sqs.wait-time-seconds:1"); addEnvironment(context, "zipkin.collector.sqs.aws-access-key-id: x"); addEnvironment(context, "zipkin.collector.sqs.aws-secret-access-key: x"); addEnvironment(context, "zipkin.collector.sqs.aws-sts-role-arn: test"); context.register(PropertyPlaceholderAutoConfiguration.class, Region.class, ZipkinSQSCollectorAutoConfiguration.class, ZipkinSQSCredentialsAutoConfiguration.class, InMemoryConfiguration.class); context.refresh(); assertThat(context.getBean(SQSCollector.class)).isNotNull(); assertThat(context.getBean(AWSSecurityTokenService.class)).isNotNull(); assertThat(context.getBean(AWSCredentialsProvider.class)).isInstanceOf(STSAssumeRoleSessionCredentialsProvider.class); }
@Test public void testWithConfigurationVariablesAndPropertiesOtherKey() { TestPropertyValues.of( "mybatis.configuration.variables.key1:value1", "mybatis.configuration-properties.key2:value2") .applyTo(this.context); this.context.register(EmbeddedDataSourceConfiguration.class, MybatisAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class); this.context.refresh(); Properties variables = this.context.getBean(SqlSessionFactory.class).getConfiguration().getVariables(); assertThat(variables).hasSize(2); assertThat(variables.getProperty("key1")).isEqualTo("value1"); assertThat(variables.getProperty("key2")).isEqualTo("value2"); }
@Test public void defaultsToV2Endpoint() throws Exception { context = new AnnotationConfigApplicationContext(); addEnvironment(context, "spring.zipkin.base-url:" + server.url("/")); context.register( PropertyPlaceholderAutoConfiguration.class, TraceMetricsAutoConfiguration.class, ZipkinAutoConfiguration.class); context.refresh(); SpanReporter spanReporter = context.getBean(SpanReporter.class); spanReporter.report(span); Awaitility.await().untilAsserted(() -> then(server.getRequestCount()).isGreaterThan(0)); RecordedRequest request = server.takeRequest(); then(request.getPath()).isEqualTo("/api/v2/spans"); then(request.getBody().readUtf8()).contains("localEndpoint"); }
@Test public void encoderDirectsEndpoint() throws Exception { context = new AnnotationConfigApplicationContext(); addEnvironment( context, "spring.zipkin.base-url:" + server.url("/"), "spring.zipkin.encoder:JSON_V1"); context.register( PropertyPlaceholderAutoConfiguration.class, TraceMetricsAutoConfiguration.class, ZipkinAutoConfiguration.class); context.refresh(); SpanReporter spanReporter = context.getBean(SpanReporter.class); spanReporter.report(span); Awaitility.await().untilAsserted(() -> then(server.getRequestCount()).isGreaterThan(0)); RecordedRequest request = server.takeRequest(); then(request.getPath()).isEqualTo("/api/v1/spans"); then(request.getBody().readUtf8()).contains("binaryAnnotations"); }
@Test public void overrideRabbitMQQueue() throws Exception { context = new AnnotationConfigApplicationContext(); addEnvironment(context, "spring.zipkin.rabbitmq.queue:zipkin2"); context.register( PropertyPlaceholderAutoConfiguration.class, TraceMetricsAutoConfiguration.class, RabbitAutoConfiguration.class, ZipkinAutoConfiguration.class); context.refresh(); SpanReporter spanReporter = context.getBean(SpanReporter.class); assertThat(spanReporter).extracting("reporter.sender.queue") .contains("zipkin2"); context.close(); }
@Test public void overrideKafkaTopic() throws Exception { context = new AnnotationConfigApplicationContext(); addEnvironment(context, "spring.zipkin.kafka.topic:zipkin2"); context.register( PropertyPlaceholderAutoConfiguration.class, TraceMetricsAutoConfiguration.class, KafkaAutoConfiguration.class, ZipkinAutoConfiguration.class); context.refresh(); SpanReporter spanReporter = context.getBean(SpanReporter.class); assertThat(spanReporter).extracting("reporter.sender.topic") .contains("zipkin2"); context.close(); }
@Test public void canOverrideBySender() throws Exception { context = new AnnotationConfigApplicationContext(); addEnvironment(context, "spring.zipkin.sender.type:web"); context.register( PropertyPlaceholderAutoConfiguration.class, TraceMetricsAutoConfiguration.class, RabbitAutoConfiguration.class, KafkaAutoConfiguration.class, ZipkinAutoConfiguration.class); context.refresh(); SpanReporter spanReporter = context.getBean(SpanReporter.class); assertThat(spanReporter).extracting("reporter.sender").allSatisfy( s -> assertThat(s.getClass().getSimpleName()).isEqualTo("RestTemplateSender") ); context.close(); }
@Test public void rabbitWinsWhenKafkaPresent() throws Exception { context = new AnnotationConfigApplicationContext(); context.register( PropertyPlaceholderAutoConfiguration.class, TraceMetricsAutoConfiguration.class, RabbitAutoConfiguration.class, KafkaAutoConfiguration.class, ZipkinAutoConfiguration.class); context.refresh(); SpanReporter spanReporter = context.getBean(SpanReporter.class); assertThat(spanReporter).extracting("reporter.sender") .allSatisfy(s -> assertThat(s).isInstanceOf(RabbitMQSender.class)); context.close(); }
@Test public void defaultsTo64BitTraceId() { context = new AnnotationConfigApplicationContext(); context.register(PropertyPlaceholderAutoConfiguration.class, SleuthLogAutoConfiguration.class, TraceAutoConfiguration.class); context.refresh(); Tracer tracer = context.getBean(Tracer.class); Span span = null; try { span = tracer.createSpan("foo", NeverSampler.INSTANCE); assertThat(span.getTraceIdHigh()).isEqualTo(0L); assertThat(span.getTraceId()).isNotEqualTo(0L); } finally { if (span != null) { tracer.close(span); } } }
@Test public void optInto128BitTraceId() { EnvironmentTestUtils.addEnvironment(context, "spring.sleuth.traceId128:true"); context.register(PropertyPlaceholderAutoConfiguration.class, SleuthLogAutoConfiguration.class, TraceAutoConfiguration.class); context.refresh(); Tracer tracer = context.getBean(Tracer.class); Span span = null; try { span = tracer.createSpan("foo", NeverSampler.INSTANCE); assertThat(span.getTraceIdHigh()).isNotEqualTo(0L); assertThat(span.getTraceId()).isNotEqualTo(0L); } finally { if (span != null) { tracer.close(span); } } }
@Test public void testOrderConfiguration() { ConfigurableApplicationContext applicationContext = SpringApplication.run(new Class[]{BatchEventAutoConfiguration.JobExecutionListenerConfiguration.class, EventJobExecutionConfiguration.class, PropertyPlaceholderAutoConfiguration.class, TestSupportBinderAutoConfiguration.class}, new String[]{"--spring.cloud.task.closecontext_enable=false", "--spring.main.web-environment=false", "--spring.cloud.task.batch.events.chunk-order=5", "--spring.cloud.task.batch.events.item-process-order=5", "--spring.cloud.task.batch.events.item-read-order=5", "--spring.cloud.task.batch.events.item-write-order=5", "--spring.cloud.task.batch.events.job-execution-order=5", "--spring.cloud.task.batch.events.skip-order=5", "--spring.cloud.task.batch.events.step-execution-order=5" }); for (String beanName : LISTENER_BEAN_NAMES) { Ordered ordered = (Ordered)applicationContext.getBean(beanName); assertEquals("Expected order value of 5 for " + beanName,ordered.getOrder(),5); } }
@Test public void testMultipleDataSources() { this.applicationContext = SpringApplication.run(new Class[] {JobConfigurationMultipleDataSources.class, PropertyPlaceholderAutoConfiguration.class, EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class, TaskBatchAutoConfiguration.class}, ARGS); TaskExplorer taskExplorer = this.applicationContext.getBean(TaskExplorer.class); Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application", new PageRequest(0, 1)); Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(page.iterator().next().getExecutionId()); assertEquals(1, jobExecutionIds.size()); assertEquals(1, taskExplorer.getTaskExecution(jobExecutionIds.iterator().next()).getExecutionId()); }
@Test public void testAutobuiltDataSourceNoJob() { this.applicationContext = SpringApplication.run(new Class[] {NoJobConfiguration.class, PropertyPlaceholderAutoConfiguration.class, EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class, TaskBatchAutoConfiguration.class}, ARGS); TaskExplorer taskExplorer = this.applicationContext.getBean(TaskExplorer.class); Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application", PageRequest.of(0, 1)); Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(page.iterator().next().getExecutionId()); assertEquals(0, jobExecutionIds.size()); }
@Test public void testMapBased() { this.applicationContext = SpringApplication.run(new Class[] {JobConfiguration.class, PropertyPlaceholderAutoConfiguration.class, EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class, TaskBatchAutoConfiguration.class}, ARGS); TaskExplorer taskExplorer = this.applicationContext.getBean(TaskExplorer.class); Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application", PageRequest.of(0, 1)); Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(page.iterator().next().getExecutionId()); assertEquals(1, jobExecutionIds.size()); assertEquals(1, (long) taskExplorer.getTaskExecutionIdByJobExecutionId(jobExecutionIds.iterator().next())); }
@Test public void testMultipleJobs() { this.applicationContext = SpringApplication.run(new Class[] {EmbeddedDataSourceConfiguration.class, MultipleJobConfiguration.class, PropertyPlaceholderAutoConfiguration.class, BatchAutoConfiguration.class, TaskBatchAutoConfiguration.class}, ARGS); TaskExplorer taskExplorer = this.applicationContext.getBean(TaskExplorer.class); Page<TaskExecution> page = taskExplorer.findTaskExecutionsByName("application", PageRequest.of(0, 1)); Set<Long> jobExecutionIds = taskExplorer.getJobExecutionIdsByTaskExecutionId(page.iterator().next().getExecutionId()); assertEquals(2, jobExecutionIds.size()); Iterator<Long> jobExecutionIdsIterator = jobExecutionIds.iterator(); assertEquals(1, (long) taskExplorer.getTaskExecutionIdByJobExecutionId(jobExecutionIdsIterator.next())); assertEquals(1, (long) taskExplorer.getTaskExecutionIdByJobExecutionId(jobExecutionIdsIterator.next())); }
@Test public void successfulTaskTest() { applicationContext = new SpringApplicationBuilder().sources(new Class[]{TaskConfiguration.class, PropertyPlaceholderAutoConfiguration.class}).build().run(new String[]{ "--spring.cloud.task.closecontext.enable=false", "--spring.cloud.task.name=" + TASK_NAME, "--spring.main.web-environment=false"}); String output = this.outputCapture.toString(); assertTrue("Test results do not show create task message: " + output, output.contains(CREATE_TASK_MESSAGE)); assertTrue("Test results do not show success message: " + output, output.contains(UPDATE_TASK_MESSAGE)); assertTrue("Test results have incorrect exit code: " + output, output.contains(SUCCESS_EXIT_CODE_MESSAGE)); }
@Test public void invalidExecutionId() { boolean exceptionFired = false; try { applicationContext = new SpringApplicationBuilder().sources(TaskExceptionConfiguration.class, PropertyPlaceholderAutoConfiguration.class).build().run(new String[]{ "--spring.cloud.task.closecontext.enable=false", "--spring.cloud.task.name=" + TASK_NAME, "--spring.main.web-environment=false", "--spring.cloud.task.executionid=55"}); } catch (ApplicationContextException exception) { exceptionFired = true; } assertTrue("An ApplicationContextException should have been thrown", exceptionFired); String output = this.outputCapture.toString(); assertTrue("Test results do not show the correct exception message: " + output, output.contains(EXCEPTION_INVALID_TASK_EXECUTION_ID)); }
@Test public void testRepositoryNotInitialized() throws Exception { Properties properties = new Properties(); properties.put("spring.cloud.task.tablePrefix", "foobarless"); PropertiesPropertySource propertiesSource = new PropertiesPropertySource("test", properties); this.context = new AnnotationConfigApplicationContext(); this.context.getEnvironment().getPropertySources().addLast(propertiesSource); ((AnnotationConfigApplicationContext)context).register(SimpleTaskConfiguration.class); ((AnnotationConfigApplicationContext)context).register(PropertyPlaceholderAutoConfiguration.class); ((AnnotationConfigApplicationContext)context).register(EmbeddedDataSourceConfiguration.class); boolean wasExceptionThrown = false; try { this.context.refresh(); } catch (ApplicationContextException ex) { wasExceptionThrown = true; } assertTrue("Expected ApplicationContextException to be thrown", wasExceptionThrown); }
private DeployerProperties loadCloudProperties() { final ConfigurableApplicationContext context = new SpringApplicationBuilder( PropertyPlaceholderAutoConfiguration.class, DeployerConfiguration.class) .bannerMode(Mode.OFF).logStartupInfo(false).web(false) .properties("spring.config.name=cloud", "logging.level.ROOT=OFF", "spring.cloud.launcher.list=true", "launcher.version=" + getVersion()) .run(this.args); try { return context.getBean(DeployerProperties.class); } finally { context.close(); } }
@Test public void serviceUrlWithCompositePropertySource() { CompositePropertySource source = new CompositePropertySource("composite"); this.context.getEnvironment().getPropertySources().addFirst(source); source.addPropertySource(new MapPropertySource("config", Collections .<String, Object> singletonMap("eureka.client.serviceUrl.defaultZone", "http://example.com,http://example2.com"))); this.context.register(PropertyPlaceholderAutoConfiguration.class, TestConfiguration.class); this.context.refresh(); assertEquals("{defaultZone=http://example.com,http://example2.com}", this.context.getBean(EurekaClientConfigBean.class).getServiceUrl() .toString()); assertEquals("[http://example.com/, http://example2.com/]", getEurekaServiceUrlsForDefaultZone()); }
private void setup(String... env) { AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(); EnvironmentTestUtils.addEnvironment(parent, env); parent.register(UtilAutoConfiguration.class, EurekaDiscoveryClientConfiguration.class, PropertyPlaceholderAutoConfiguration.class, EnvironmentKnobbler.class, EurekaDiscoveryClientConfigServiceBootstrapConfiguration.class, DiscoveryClientConfigServiceBootstrapConfiguration.class, ConfigClientProperties.class); parent.refresh(); this.context = new AnnotationConfigApplicationContext(); this.context.setParent(parent); this.context.register(PropertyPlaceholderAutoConfiguration.class, EurekaDiscoveryClientConfigServiceAutoConfiguration.class, EurekaClientAutoConfiguration.class); this.context.refresh(); }
@Override public Environment findOne(String config, String profile, String label) { SpringApplicationBuilder builder = new SpringApplicationBuilder( PropertyPlaceholderAutoConfiguration.class); ConfigurableEnvironment environment = getEnvironment(profile); builder.environment(environment); builder.web(WebApplicationType.NONE).bannerMode(Mode.OFF); if (!logger.isDebugEnabled()) { // Make the mini-application startup less verbose builder.logStartupInfo(false); } String[] args = getArgs(config, profile, label); // Explicitly set the listeners (to exclude logging listener which would change // log levels in the caller) builder.application() .setListeners(Arrays.asList(new ConfigFileApplicationListener())); ConfigurableApplicationContext context = builder.run(args); environment.getPropertySources().remove("profiles"); try { return clean(new PassthruEnvironmentRepository(environment).findOne(config, profile, label)); } finally { context.close(); } }
private void setup(String... env) { AnnotationConfigApplicationContext parent = new AnnotationConfigApplicationContext(); TestPropertyValues.of(env).applyTo(parent); parent.register(UtilAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class, EnvironmentKnobbler.class, ConsulDiscoveryClientConfigServiceBootstrapConfiguration.class, DiscoveryClientConfigServiceBootstrapConfiguration.class, ConfigClientProperties.class); parent.refresh(); this.context = new AnnotationConfigApplicationContext(); this.context.setParent(parent); this.context.register(PropertyPlaceholderAutoConfiguration.class, ConsulConfigServerAutoConfiguration.class, ConsulAutoConfiguration.class, ConsulDiscoveryClientConfiguration.class); this.context.refresh(); }
private void setupContextForGraph(String graph) { String[] ARGS = new String[] {CLOSE_CONTEXT_ARG, TASK_NAME_ARG, "--graph=" + graph}; this.applicationContext = SpringApplication.run(new Object[] {ComposedRunnerVisitorConfiguration.class, PropertyPlaceholderAutoConfiguration.class, EmbeddedDataSourceConfiguration.class, BatchAutoConfiguration.class, TaskBatchAutoConfiguration.class}, ARGS); }
@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); }