Skip to content

Commit 5637c28

Browse files
committed
Merge pull request #130 from reda-alaoui
* gh-130: ProxyDataSource#close uses instanceof AutoCloseable instead of isWrapperFor(AutoCloseable.class) Closes #129
2 parents 80c964c + f597a0a commit 5637c28

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

src/main/java/net/ttddyy/dsproxy/support/ProxyDataSource.java

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* A proxy of {@link javax.sql.DataSource} with {@link net.ttddyy.dsproxy.listener.QueryExecutionListener}.
2525
*
2626
* @author Tadaya Tsuyukubo
27+
* @author Réda Housni Alaoui
2728
*/
2829
public class ProxyDataSource extends ProxyLogicSupport implements DataSource, Closeable {
2930

@@ -153,14 +154,22 @@ public Logger getParentLogger() throws SQLFeatureNotSupportedException {
153154

154155
@Override
155156
public void close() throws IOException {
157+
try {
158+
doClose();
159+
} catch (Exception e) {
160+
throw new IOException(e);
161+
}
162+
}
163+
164+
private void doClose() throws Exception {
156165
if (dataSource instanceof Closeable) {
157166
((Closeable) dataSource).close();
158167
} else if (isAutoCloseablePresent && dataSource instanceof AutoCloseable) {
159-
try {
160-
((AutoCloseable) dataSource).close(); // (jdk7+)
161-
} catch (Exception ex) {
162-
throw new IOException(ex);
163-
}
168+
((AutoCloseable) dataSource).close(); // (jdk7+)
169+
} else if (dataSource.isWrapperFor(Closeable.class)) {
170+
dataSource.unwrap(Closeable.class).close();
171+
} else if (isAutoCloseablePresent && dataSource.isWrapperFor(AutoCloseable.class)) {
172+
dataSource.unwrap(AutoCloseable.class).close(); // (jdk7+)
164173
}
165174
}
166175

src/test/java/net/ttddyy/dsproxy/ProxyDataSourceTest.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package net.ttddyy.dsproxy;
22

3+
import java.io.Closeable;
4+
import java.util.concurrent.atomic.AtomicBoolean;
35
import net.ttddyy.dsproxy.listener.CallCheckMethodExecutionListener;
46
import net.ttddyy.dsproxy.listener.MethodExecutionContext;
57
import net.ttddyy.dsproxy.proxy.ProxyConfig;
@@ -26,13 +28,15 @@
2628
import static org.junit.Assert.assertTrue;
2729
import static org.mockito.Mockito.mock;
2830
import static org.mockito.Mockito.verify;
31+
import static org.mockito.Mockito.when;
2932
import static org.mockito.Mockito.withSettings;
3033

3134

3235
/**
3336
* TODO: clean up & rewrite
3437
*
3538
* @author Tadaya Tsuyukubo
39+
* @author Réda Housni Alaoui
3640
*/
3741
public class ProxyDataSourceTest {
3842

@@ -258,6 +262,58 @@ public void autoCloseable() throws Exception {
258262
verify((AutoCloseable) ds).close();
259263
}
260264

265+
@Test
266+
public void closeProxyOfAutoCloseableViaClose() throws Exception {
267+
DataSource ds = mock(DataSource.class);
268+
when(ds.isWrapperFor(AutoCloseable.class)).thenReturn(true);
269+
270+
AtomicBoolean closed = new AtomicBoolean();
271+
AutoCloseable autoCloseable = () -> closed.set(true);
272+
when(ds.unwrap(AutoCloseable.class)).thenReturn(autoCloseable);
273+
274+
new ProxyDataSource(ds).close();
275+
assertThat(closed).isTrue();
276+
}
277+
278+
@Test
279+
public void closeProxyOfCloseableViaClose() throws Exception {
280+
DataSource ds = mock(DataSource.class);
281+
when(ds.isWrapperFor(Closeable.class)).thenReturn(true);
282+
283+
AtomicBoolean closed = new AtomicBoolean();
284+
Closeable closeable = () -> closed.set(true);
285+
when(ds.unwrap(Closeable.class)).thenReturn(closeable);
286+
287+
new ProxyDataSource(ds).close();
288+
assertThat(closed).isTrue();
289+
}
290+
291+
@Test
292+
public void closeProxyOfAutoCloseableViaUnwrap() throws Exception {
293+
DataSource ds = mock(DataSource.class);
294+
when(ds.isWrapperFor(AutoCloseable.class)).thenReturn(true);
295+
296+
AtomicBoolean closed = new AtomicBoolean();
297+
AutoCloseable autoCloseable = () -> closed.set(true);
298+
when(ds.unwrap(AutoCloseable.class)).thenReturn(autoCloseable);
299+
300+
new ProxyDataSource(ds).unwrap(AutoCloseable.class).close();
301+
assertThat(closed).isTrue();
302+
}
303+
304+
@Test
305+
public void closeProxyOfCloseableViaUnwrap() throws Exception {
306+
DataSource ds = mock(DataSource.class);
307+
when(ds.isWrapperFor(Closeable.class)).thenReturn(true);
308+
309+
AtomicBoolean closed = new AtomicBoolean();
310+
Closeable closeable = () -> closed.set(true);
311+
when(ds.unwrap(Closeable.class)).thenReturn(closeable);
312+
313+
new ProxyDataSource(ds).unwrap(Closeable.class).close();
314+
assertThat(closed).isTrue();
315+
}
316+
261317
@Test
262318
public void getDataSource() {
263319
DataSource original = mock(DataSource.class);

0 commit comments

Comments
 (0)