Skip to content

Commit

Permalink
Merge branch '2.1.x' into 2.2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll committed Feb 18, 2020
2 parents e59d3fb + 2147976 commit 1d60184
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* 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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -38,6 +38,7 @@
import org.springframework.context.annotation.Import;
import org.springframework.core.type.AnnotatedTypeMetadata;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.util.StringUtils;

/**
* {@link EnableAutoConfiguration Auto-configuration} for {@link DataSource}.
Expand Down Expand Up @@ -122,6 +123,10 @@ static class EmbeddedDatabaseCondition extends SpringBootCondition {
@Override
public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) {
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)) {
return ConditionOutcome.noMatch(message.foundExactly("supported pooled data source"));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* 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");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,6 +27,7 @@
import java.util.Properties;
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.logging.Logger;

import javax.sql.DataSource;
Expand Down Expand Up @@ -143,15 +144,19 @@ void testEmbeddedTypeDefaultsUsername() {
});
}

@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
* still created if "spring.datasource.type" is present.
*/
@Test
void explicitTypeNoSupportedDataSource() {
this.contextRunner
.withClassLoader(new FilteredClassLoader("org.apache.tomcat", "com.zaxxer.hikari",
"org.apache.commons.dbcp", "org.apache.commons.dbcp2"))
void dataSourceWhenNoConnectionPoolsAreAvailableWithUrlAndTypeCreatesDataSource() {
this.contextRunner.with(hideConnectionPools())
.withPropertyValues("spring.datasource.driverClassName:org.hsqldb.jdbcDriver",
"spring.datasource.url:jdbc:hsqldb:mem:testdb",
"spring.datasource.type:" + SimpleDriverDataSource.class.getName())
Expand Down Expand Up @@ -197,6 +202,11 @@ void testDataSourceIsInitializedEarly() {
.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,
Consumer<T> consumer) {
FilteredClassLoader classLoader = new FilteredClassLoader(StringUtils.toStringArray(hiddenPackages));
Expand Down

0 comments on commit 1d60184

Please sign in to comment.