Commit 1d601840 authored by Stephane Nicoll's avatar Stephane Nicoll

Merge branch '2.1.x' into 2.2.x

Closes gh-20217
parents e59d3fbb 2147976c
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -38,6 +38,7 @@ import org.springframework.context.annotation.Configuration; ...@@ -38,6 +38,7 @@ import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import org.springframework.core.type.AnnotatedTypeMetadata; import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.util.StringUtils;
/** /**
* {@link EnableAutoConfiguration Auto-configuration} for {@link DataSource}. * {@link EnableAutoConfiguration Auto-configuration} for {@link DataSource}.
...@@ -122,6 +123,10 @@ public class DataSourceAutoConfiguration { ...@@ -122,6 +123,10 @@ public class DataSourceAutoConfiguration {
@Override @Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
ConditionMessage.Builder message = ConditionMessage.forCondition("EmbeddedDataSource"); ConditionMessage.Builder message = ConditionMessage.forCondition("EmbeddedDataSource");
String url = context.getEnvironment().getProperty("spring.datasource.url");
if (StringUtils.hasText(url)) {
return ConditionOutcome.noMatch(message.found("explicit url").items(url));
}
if (anyMatches(context, metadata, this.pooledCondition)) { if (anyMatches(context, metadata, this.pooledCondition)) {
return ConditionOutcome.noMatch(message.foundExactly("supported pooled data source")); return ConditionOutcome.noMatch(message.foundExactly("supported pooled data source"));
} }
......
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -27,6 +27,7 @@ import java.util.List; ...@@ -27,6 +27,7 @@ import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.Random; import java.util.Random;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Function;
import java.util.logging.Logger; import java.util.logging.Logger;
import javax.sql.DataSource; import javax.sql.DataSource;
...@@ -143,15 +144,19 @@ class DataSourceAutoConfigurationTests { ...@@ -143,15 +144,19 @@ class DataSourceAutoConfigurationTests {
}); });
} }
@Test
void dataSourceWhenNoConnectionPoolsAreAvailableWithUrlDoesNotCreateDataSource() {
this.contextRunner.with(hideConnectionPools())
.run((context) -> assertThat(context).doesNotHaveBean(DataSource.class));
}
/** /**
* This test makes sure that if no supported data source is present, a datasource is * This test makes sure that if no supported data source is present, a datasource is
* still created if "spring.datasource.type" is present. * still created if "spring.datasource.type" is present.
*/ */
@Test @Test
void explicitTypeNoSupportedDataSource() { void dataSourceWhenNoConnectionPoolsAreAvailableWithUrlAndTypeCreatesDataSource() {
this.contextRunner this.contextRunner.with(hideConnectionPools())
.withClassLoader(new FilteredClassLoader("org.apache.tomcat", "com.zaxxer.hikari",
"org.apache.commons.dbcp", "org.apache.commons.dbcp2"))
.withPropertyValues("spring.datasource.driverClassName:org.hsqldb.jdbcDriver", .withPropertyValues("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
"spring.datasource.url:jdbc:hsqldb:mem:testdb", "spring.datasource.url:jdbc:hsqldb:mem:testdb",
"spring.datasource.type:" + SimpleDriverDataSource.class.getName()) "spring.datasource.type:" + SimpleDriverDataSource.class.getName())
...@@ -197,6 +202,11 @@ class DataSourceAutoConfigurationTests { ...@@ -197,6 +202,11 @@ class DataSourceAutoConfigurationTests {
.isTrue()); .isTrue());
} }
private static Function<ApplicationContextRunner, ApplicationContextRunner> hideConnectionPools() {
return (runner) -> runner.withClassLoader(new FilteredClassLoader("org.apache.tomcat", "com.zaxxer.hikari",
"org.apache.commons.dbcp", "org.apache.commons.dbcp2"));
}
private <T extends DataSource> void assertDataSource(Class<T> expectedType, List<String> hiddenPackages, private <T extends DataSource> void assertDataSource(Class<T> expectedType, List<String> hiddenPackages,
Consumer<T> consumer) { Consumer<T> consumer) {
FilteredClassLoader classLoader = new FilteredClassLoader(StringUtils.toStringArray(hiddenPackages)); FilteredClassLoader classLoader = new FilteredClassLoader(StringUtils.toStringArray(hiddenPackages));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment