This is an automated email from the ASF dual-hosted git repository.
sunnianjun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 55882b99d71 Revise #31060 (#31082)
55882b99d71 is described below
commit 55882b99d719a7c279d0052e7281d980c949d4a1
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Apr 30 20:53:10 2024 +0800
Revise #31060 (#31082)
---
.../infra/util/close/DataSourcesCloserTest.java | 23 +++++++++++++++-------
.../infra/util/close/QuietlyCloserTest.java | 15 +++++++++++---
2 files changed, 28 insertions(+), 10 deletions(-)
diff --git
a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/close/DataSourcesCloserTest.java
b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/close/DataSourcesCloserTest.java
index 4a6a82eb7ca..0c93ad5aa49 100644
---
a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/close/DataSourcesCloserTest.java
+++
b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/close/DataSourcesCloserTest.java
@@ -17,22 +17,31 @@
package org.apache.shardingsphere.infra.util.close;
+import
org.apache.shardingsphere.infra.exception.core.external.sql.type.wrapper.SQLWrapperException;
import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
import javax.sql.DataSource;
-import java.util.Collections;
+import java.sql.SQLException;
+import java.util.Arrays;
-import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.withSettings;
class DataSourcesCloserTest {
@Test
- void assertClose() {
- DataSource mockDataSource = Mockito.mock(DataSource.class,
Mockito.withSettings().extraInterfaces(AutoCloseable.class));
- DataSourcesCloser.close(Collections.singletonList(mockDataSource));
- assertDoesNotThrow(() -> verify((AutoCloseable) mockDataSource,
times(1)).close());
+ void assertClose() throws Exception {
+ DataSource dataSource0 = mock(DataSource.class,
withSettings().extraInterfaces(AutoCloseable.class));
+ DataSource dataSource1 = mock(DataSource.class,
withSettings().extraInterfaces(AutoCloseable.class));
+ doThrow(new SQLException("test")).when((AutoCloseable)
dataSource1).close();
+ DataSource dataSource2 = mock(DataSource.class,
withSettings().extraInterfaces(AutoCloseable.class));
+ assertThrows(SQLWrapperException.class, () ->
DataSourcesCloser.close(Arrays.asList(dataSource0, dataSource1, dataSource2)));
+ verify((AutoCloseable) dataSource0, times(1)).close();
+ verify((AutoCloseable) dataSource1, times(1)).close();
+ verify((AutoCloseable) dataSource2, times(1)).close();
}
}
diff --git
a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/close/QuietlyCloserTest.java
b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/close/QuietlyCloserTest.java
index 56df68107a9..41593689ab5 100644
---
a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/close/QuietlyCloserTest.java
+++
b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/close/QuietlyCloserTest.java
@@ -18,18 +18,27 @@
package org.apache.shardingsphere.infra.util.close;
import org.junit.jupiter.api.Test;
-import org.mockito.Mockito;
+
+import java.sql.SQLException;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
class QuietlyCloserTest {
@Test
- void testClose() {
- AutoCloseable mockAutoCloseable = Mockito.mock(AutoCloseable.class);
+ void assertClose() throws Exception {
+ AutoCloseable mockAutoCloseable = mock(AutoCloseable.class);
+ doThrow(new SQLException("test")).when(mockAutoCloseable).close();
QuietlyCloser.close(mockAutoCloseable);
assertDoesNotThrow(() -> verify(mockAutoCloseable, times(1)).close());
}
+
+ @Test
+ void assertCloseWithNullResource() {
+ assertDoesNotThrow(() -> QuietlyCloser.close(null));
+ }
}