Commit 46ecf7a9 authored by Andy Wilkinson's avatar Andy Wilkinson

Only unwrap when DataSource is a wrapper for required type

Closes gh-16863
parent da12ad0c
/*
* 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");
* 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) {
try {
return wrapper.unwrap(target);
if (wrapper.isWrapperFor(target)) {
return wrapper.unwrap(target);
}
}
catch (Exception ex) {
return null;
// Continue
}
return null;
}
private static class DelegatingDataSourceUnwrapper {
......
/*
* 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");
* you may not use this file except in compliance with the License.
......@@ -16,6 +16,8 @@
package org.springframework.boot.jdbc;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.zaxxer.hikari.HikariDataSource;
......@@ -27,6 +29,9 @@ import org.springframework.jdbc.datasource.DelegatingDataSource;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
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}.
......@@ -91,6 +96,17 @@ public class DataSourceUnwrapperTests {
.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) {
return (DataSource) new ProxyFactory(dataSource).getProxy();
}
......
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