|
1 | 1 | package net.ttddyy.dsproxy; |
2 | 2 |
|
| 3 | +import java.io.Closeable; |
| 4 | +import java.util.concurrent.atomic.AtomicBoolean; |
3 | 5 | import net.ttddyy.dsproxy.listener.CallCheckMethodExecutionListener; |
4 | 6 | import net.ttddyy.dsproxy.listener.MethodExecutionContext; |
5 | 7 | import net.ttddyy.dsproxy.proxy.ProxyConfig; |
|
26 | 28 | import static org.junit.Assert.assertTrue; |
27 | 29 | import static org.mockito.Mockito.mock; |
28 | 30 | import static org.mockito.Mockito.verify; |
| 31 | +import static org.mockito.Mockito.when; |
29 | 32 | import static org.mockito.Mockito.withSettings; |
30 | 33 |
|
31 | 34 |
|
32 | 35 | /** |
33 | 36 | * TODO: clean up & rewrite |
34 | 37 | * |
35 | 38 | * @author Tadaya Tsuyukubo |
| 39 | + * @author Réda Housni Alaoui |
36 | 40 | */ |
37 | 41 | public class ProxyDataSourceTest { |
38 | 42 |
|
@@ -258,6 +262,58 @@ public void autoCloseable() throws Exception { |
258 | 262 | verify((AutoCloseable) ds).close(); |
259 | 263 | } |
260 | 264 |
|
| 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 | + |
261 | 317 | @Test |
262 | 318 | public void getDataSource() { |
263 | 319 | DataSource original = mock(DataSource.class); |
|
0 commit comments