Merge branch '2.1.x'

Closes gh-16940
pull/16948/head
Andy Wilkinson 6 years ago
commit 9ba5c78878

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2018 the original author or authors. * Copyright 2012-2019 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.
@ -76,11 +76,14 @@ public final class DataSourceUnwrapper {
private static <S> S safeUnwrap(Wrapper wrapper, Class<S> target) { private static <S> S safeUnwrap(Wrapper wrapper, Class<S> target) {
try { try {
return wrapper.unwrap(target); if (wrapper.isWrapperFor(target)) {
return wrapper.unwrap(target);
}
} }
catch (Exception ex) { catch (Exception ex) {
return null; // Continue
} }
return null;
} }
private static class DelegatingDataSourceUnwrapper { private static class DelegatingDataSourceUnwrapper {

@ -16,6 +16,8 @@
package org.springframework.boot.jdbc; package org.springframework.boot.jdbc;
import java.sql.SQLException;
import javax.sql.DataSource; import javax.sql.DataSource;
import com.zaxxer.hikari.HikariDataSource; import com.zaxxer.hikari.HikariDataSource;
@ -27,6 +29,9 @@ import org.springframework.jdbc.datasource.DelegatingDataSource;
import org.springframework.jdbc.datasource.SingleConnectionDataSource; import org.springframework.jdbc.datasource.SingleConnectionDataSource;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
/** /**
* Tests for {@link DataSourceUnwrapper}. * Tests for {@link DataSourceUnwrapper}.
@ -91,6 +96,17 @@ public class DataSourceUnwrapperTests {
.isSameAs(dataSource); .isSameAs(dataSource);
} }
@Test
public void unwrappingIsNotAttemptedWhenDataSourceIsNotWrapperForTarget()
throws SQLException {
DataSource dataSource = mock(DataSource.class);
DataSource actual = DataSourceUnwrapper.unwrap(dataSource,
HikariDataSource.class);
assertThat(actual).isNull();
verify(dataSource).isWrapperFor(HikariDataSource.class);
verifyNoMoreInteractions(dataSource);
}
private DataSource wrapInProxy(DataSource dataSource) { private DataSource wrapInProxy(DataSource dataSource) {
return (DataSource) new ProxyFactory(dataSource).getProxy(); return (DataSource) new ProxyFactory(dataSource).getProxy();
} }

Loading…
Cancel
Save