Skip to content

Commit 1bb58c5

Browse files
committed
#12 add logging of jdbc url for connecting to embedded database
1 parent 912a315 commit 1bb58c5

File tree

7 files changed

+84
-13
lines changed

7 files changed

+84
-13
lines changed

embedded-database-spring-test/src/main/java/io/zonky/test/db/flyway/DefaultFlywayDataSourceContext.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public void releaseTarget(Object target) throws Exception {
121121
}
122122

123123
@Override
124-
public synchronized ListenableFuture<Void> reload(Flyway flyway) {
124+
public synchronized ListenableFuture<DataSource> reload(Flyway flyway) {
125125
Executor executor = bootstrapExecutor != null ? bootstrapExecutor : Runnable::run;
126126

127127
List<Consumer<Builder>> customizers = ImmutableList.<Consumer<Builder>>builder()
@@ -151,7 +151,7 @@ public synchronized ListenableFuture<Void> reload(Flyway flyway) {
151151
// main data source future must never fail, otherwise all following tests will fail
152152
dataSourceFuture = reloadFuture.exceptionally(throwable -> null);
153153

154-
return new CompletableToListenableFutureAdapter<>(reloadFuture.thenApply(dataSource -> null));
154+
return new CompletableToListenableFutureAdapter<>(reloadFuture);
155155
}
156156

157157
/**

embedded-database-spring-test/src/main/java/io/zonky/test/db/flyway/FlywayDataSourceContext.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@
2020
import org.springframework.aop.TargetSource;
2121
import org.springframework.util.concurrent.ListenableFuture;
2222

23+
import javax.sql.DataSource;
24+
2325
/**
2426
* Interface extending {@link TargetSource} that is used by {@link io.zonky.test.db.postgres.FlywayEmbeddedPostgresDataSourceFactoryBean}
2527
* for deferred initialization of the embedded database until the application context is fully loaded and the flyway bean is available.
2628
*/
2729
public interface FlywayDataSourceContext extends TargetSource {
2830

29-
ListenableFuture<Void> reload(Flyway flyway);
31+
ListenableFuture<DataSource> reload(Flyway flyway);
3032

3133
}

embedded-database-spring-test/src/main/java/io/zonky/test/db/flyway/OptimizedFlywayTestExecutionListener.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import com.google.common.collect.Iterables;
2020
import com.google.common.collect.ObjectArrays;
21+
import io.zonky.test.db.logging.EmbeddedDatabaseReporter;
2122
import org.apache.commons.lang3.ArrayUtils;
2223
import org.flywaydb.core.Flyway;
2324
import org.flywaydb.core.api.FlywayException;
@@ -37,6 +38,7 @@
3738
import org.springframework.util.ClassUtils;
3839
import org.springframework.util.CollectionUtils;
3940

41+
import javax.sql.DataSource;
4042
import java.lang.annotation.Annotation;
4143
import java.lang.reflect.AnnotatedElement;
4244
import java.lang.reflect.InvocationTargetException;
@@ -93,7 +95,7 @@ public void beforeTestClass(TestContext testContext) throws Exception {
9395
logger.warn("Optimized database loading is not supported when using multiple flyway test annotations");
9496
}
9597
for (FlywayTest annotation : annotations) {
96-
optimizedDbReset(testContext, annotation);
98+
optimizedDbReset(testContext, testClass, annotation);
9799
}
98100
}
99101

@@ -106,7 +108,7 @@ public void beforeTestMethod(TestContext testContext) throws Exception {
106108
logger.warn("Optimized database loading is not supported when using multiple flyway test annotations");
107109
}
108110
for (FlywayTest annotation : annotations) {
109-
optimizedDbReset(testContext, annotation);
111+
optimizedDbReset(testContext, testMethod, annotation);
110112
}
111113
}
112114

@@ -126,7 +128,7 @@ protected FlywayTest[] findFlywayTestAnnotations(AnnotatedElement element) {
126128
return new FlywayTest[0];
127129
}
128130

129-
protected synchronized void optimizedDbReset(TestContext testContext, FlywayTest annotation) throws Exception {
131+
protected synchronized void optimizedDbReset(TestContext testContext, AnnotatedElement element, FlywayTest annotation) throws Exception {
130132
String dbResetMethodName = repeatableAnnotationPresent ? "dbResetWithAnnotation" : "dbResetWithAnotation";
131133

132134
if (annotation != null && annotation.invokeCleanDB() && annotation.invokeMigrateDB()
@@ -141,7 +143,8 @@ protected synchronized void optimizedDbReset(TestContext testContext, FlywayTest
141143
if (dataSourceContext != null) {
142144

143145
dataSourceContext.getTarget(); // wait for completion of running flyway migration
144-
prepareDataSourceContext(dataSourceContext, flywayBean, annotation);
146+
DataSource dataSource = reloadDataSource(dataSourceContext, flywayBean, annotation);
147+
EmbeddedDatabaseReporter.reportDataSource(dataSource, element);
145148

146149
FlywayTest adjustedAnnotation = copyAnnotation(annotation, false, false, true);
147150
invokeMethod(this, dbResetMethodName, testContext, adjustedAnnotation);
@@ -164,9 +167,9 @@ protected synchronized void optimizedDbReset(TestContext testContext, FlywayTest
164167
}
165168
}
166169

167-
protected static void prepareDataSourceContext(FlywayDataSourceContext dataSourceContext, Flyway flywayBean, FlywayTest annotation) throws Exception {
170+
protected static DataSource reloadDataSource(FlywayDataSourceContext dataSourceContext, Flyway flywayBean, FlywayTest annotation) throws Exception {
168171
if (isAppendable(flywayBean, annotation)) {
169-
dataSourceContext.reload(flywayBean).get();
172+
return dataSourceContext.reload(flywayBean).get();
170173
} else {
171174
String[] oldLocations = getFlywayLocations(flywayBean);
172175
try {
@@ -175,7 +178,7 @@ protected static void prepareDataSourceContext(FlywayDataSourceContext dataSourc
175178
} else {
176179
flywayBean.setLocations(ObjectArrays.concat(oldLocations, annotation.locationsForMigrate(), String.class));
177180
}
178-
dataSourceContext.reload(flywayBean).get();
181+
return dataSourceContext.reload(flywayBean).get();
179182
} finally {
180183
flywayBean.setLocations(oldLocations);
181184
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.zonky.test.db.logging;
18+
19+
import org.postgresql.ds.common.BaseDataSource;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
22+
23+
import javax.sql.DataSource;
24+
import java.lang.reflect.AnnotatedElement;
25+
import java.lang.reflect.Method;
26+
27+
public class EmbeddedDatabaseReporter {
28+
29+
private static final String JDBC_FORMAT = "jdbc:postgresql://localhost:%s/%s?user=%s";
30+
31+
private static final Logger logger = LoggerFactory.getLogger(EmbeddedDatabaseReporter.class);
32+
33+
public static void reportDataSource(DataSource dataSource) {
34+
logger.info("JDBC URL to connect to the embedded database: {}", getJdbcUrl(dataSource));
35+
}
36+
37+
public static void reportDataSource(DataSource dataSource, AnnotatedElement element) {
38+
logger.info("JDBC URL to connect to the embedded database: {}, scope: {}", getJdbcUrl(dataSource), getElementName(element));
39+
}
40+
41+
private static String getJdbcUrl(DataSource dataSource) {
42+
try {
43+
BaseDataSource ds = dataSource.unwrap(BaseDataSource.class);
44+
return String.format(JDBC_FORMAT, ds.getPortNumber(), ds.getDatabaseName(), ds.getUser());
45+
} catch (Exception e) {
46+
logger.warn("Unexpected error occurred while resolving url to the embedded database", e);
47+
return "unknown";
48+
}
49+
}
50+
51+
private static String getElementName(AnnotatedElement element) {
52+
if (element instanceof Class) {
53+
return ((Class) element).getSimpleName();
54+
} else if (element instanceof Method) {
55+
Method method = (Method) element;
56+
return getElementName(method.getDeclaringClass()) + "#" + method.getName();
57+
} else {
58+
return element.toString();
59+
}
60+
}
61+
}

embedded-database-spring-test/src/main/java/io/zonky/test/db/postgres/EmptyEmbeddedPostgresDataSourceFactoryBean.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import com.opentable.db.postgres.embedded.PreparedDbProvider;
2626
import io.zonky.test.db.flyway.BlockingDataSourceWrapper;
2727
import io.zonky.test.db.postgres.embedded.DefaultPostgresBinaryResolver;
28+
import io.zonky.test.db.logging.EmbeddedDatabaseReporter;
2829
import org.postgresql.ds.PGSimpleDataSource;
2930
import org.springframework.beans.factory.FactoryBean;
3031
import org.springframework.beans.factory.InitializingBean;
@@ -91,6 +92,7 @@ public void afterPropertiesSet() throws Exception {
9192

9293
PreparedDbProvider provider = PreparedDbProvider.forPreparer(EmptyDatabasePreparer.INSTANCE, customizers);
9394
PGSimpleDataSource dataSource = provider.createDataSource().unwrap(PGSimpleDataSource.class);
95+
EmbeddedDatabaseReporter.reportDataSource(dataSource);
9496
Semaphore semaphore = CONNECTION_SEMAPHORES.get(dataSource.getPortNumber());
9597
this.dataSource = new BlockingDataSourceWrapper(dataSource, semaphore);
9698
}

embedded-database-spring-test/src/main/java/io/zonky/test/db/postgres/FlywayEmbeddedPostgresDataSourceFactoryBean.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package io.zonky.test.db.postgres;
1818

1919
import io.zonky.test.db.flyway.FlywayDataSourceContext;
20+
import io.zonky.test.db.logging.EmbeddedDatabaseReporter;
2021
import org.apache.commons.lang3.StringUtils;
2122
import org.flywaydb.core.Flyway;
2223
import org.slf4j.Logger;
@@ -78,8 +79,10 @@ public void afterPropertiesSet() throws Exception {
7879
@Override
7980
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
8081
if (bean instanceof Flyway && StringUtils.equals(beanName, flywayName)) {
81-
ListenableFuture<Void> reloadFuture = dataSourceContext.reload((Flyway) bean);
82-
reloadFuture.addCallback(r -> {}, e -> logger.error("Unexpected error during the initialization of embedded database", e));
82+
ListenableFuture<DataSource> reloadFuture = dataSourceContext.reload((Flyway) bean);
83+
reloadFuture.addCallback(
84+
result -> EmbeddedDatabaseReporter.reportDataSource(result),
85+
error -> logger.error("Unexpected error during the initialization of embedded database", error));
8386
}
8487
return bean;
8588
}

embedded-database-spring-test/src/test/resources/logback-test.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
<logger name="org.flywaydb" level="INFO"/>
1313

14-
<root level="WARN">
14+
<root level="INFO">
1515
<appender-ref ref="STDOUT" />
1616
</root>
1717

0 commit comments

Comments
 (0)