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 7c050266d28 Use AutoMockExtension on kernel modules (#24385)
7c050266d28 is described below

commit 7c050266d28e1296113b997448036faeac6225d1
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Feb 28 18:55:27 2023 +0800

    Use AutoMockExtension on kernel modules (#24385)
---
 kernel/data-pipeline/dialect/postgresql/pom.xml    |  7 ++
 .../postgresql/ingest/PostgreSQLWALDumperTest.java | 81 +++++++++++-----------
 .../AlterTransactionRuleStatementUpdaterTest.java  | 38 +++++-----
 3 files changed, 65 insertions(+), 61 deletions(-)

diff --git a/kernel/data-pipeline/dialect/postgresql/pom.xml 
b/kernel/data-pipeline/dialect/postgresql/pom.xml
index b268704c4de..ab49899cd61 100644
--- a/kernel/data-pipeline/dialect/postgresql/pom.xml
+++ b/kernel/data-pipeline/dialect/postgresql/pom.xml
@@ -34,6 +34,13 @@
             <version>${project.version}</version>
         </dependency>
         
+        <dependency>
+            <groupId>org.apache.shardingsphere</groupId>
+            <artifactId>shardingsphere-test-util</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+        </dependency>
+        
         <dependency>
             <groupId>org.postgresql</groupId>
             <artifactId>postgresql</artifactId>
diff --git 
a/kernel/data-pipeline/dialect/postgresql/src/test/java/org/apache/shardingsphere/data/pipeline/postgresql/ingest/PostgreSQLWALDumperTest.java
 
b/kernel/data-pipeline/dialect/postgresql/src/test/java/org/apache/shardingsphere/data/pipeline/postgresql/ingest/PostgreSQLWALDumperTest.java
index 08a049eb0d4..b0dffca041a 100644
--- 
a/kernel/data-pipeline/dialect/postgresql/src/test/java/org/apache/shardingsphere/data/pipeline/postgresql/ingest/PostgreSQLWALDumperTest.java
+++ 
b/kernel/data-pipeline/dialect/postgresql/src/test/java/org/apache/shardingsphere/data/pipeline/postgresql/ingest/PostgreSQLWALDumperTest.java
@@ -20,11 +20,9 @@ package 
org.apache.shardingsphere.data.pipeline.postgresql.ingest;
 import 
org.apache.shardingsphere.data.pipeline.api.config.TableNameSchemaNameMapping;
 import 
org.apache.shardingsphere.data.pipeline.api.config.ingest.DumperConfiguration;
 import 
org.apache.shardingsphere.data.pipeline.api.datasource.PipelineDataSourceManager;
-import 
org.apache.shardingsphere.data.pipeline.api.datasource.config.PipelineDataSourceConfiguration;
 import 
org.apache.shardingsphere.data.pipeline.api.datasource.config.impl.StandardPipelineDataSourceConfiguration;
 import org.apache.shardingsphere.data.pipeline.api.metadata.ActualTableName;
 import org.apache.shardingsphere.data.pipeline.api.metadata.LogicTableName;
-import 
org.apache.shardingsphere.data.pipeline.api.metadata.loader.PipelineTableMetaDataLoader;
 import 
org.apache.shardingsphere.data.pipeline.core.datasource.DefaultPipelineDataSourceManager;
 import 
org.apache.shardingsphere.data.pipeline.core.ingest.channel.EmptyAckCallback;
 import 
org.apache.shardingsphere.data.pipeline.core.ingest.channel.memory.MultiplexMemoryPipelineChannel;
@@ -33,14 +31,14 @@ import 
org.apache.shardingsphere.data.pipeline.core.metadata.loader.StandardPipe
 import 
org.apache.shardingsphere.data.pipeline.postgresql.ingest.wal.PostgreSQLLogicalReplication;
 import 
org.apache.shardingsphere.data.pipeline.postgresql.ingest.wal.WALPosition;
 import 
org.apache.shardingsphere.data.pipeline.postgresql.ingest.wal.decode.PostgreSQLLogSequenceNumber;
-import org.junit.After;
-import org.junit.Before;
-import org.junit.Test;
-import org.junit.runner.RunWith;
+import org.apache.shardingsphere.test.mock.AutoMockExtension;
+import org.apache.shardingsphere.test.mock.StaticMockSettings;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
 import org.mockito.Mock;
-import org.mockito.MockedStatic;
 import org.mockito.internal.configuration.plugins.Plugins;
-import org.mockito.junit.MockitoJUnitRunner;
 import org.postgresql.jdbc.PgConnection;
 import org.postgresql.replication.LogSequenceNumber;
 import org.postgresql.replication.PGReplicationStream;
@@ -56,10 +54,10 @@ import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.mockito.ArgumentMatchers.anyString;
 import static org.mockito.ArgumentMatchers.eq;
-import static org.mockito.Mockito.mockStatic;
 import static org.mockito.Mockito.when;
 
-@RunWith(MockitoJUnitRunner.class)
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings(PostgreSQLPositionInitializer.class)
 public final class PostgreSQLWALDumperTest {
     
     @Mock
@@ -81,42 +79,43 @@ public final class PostgreSQLWALDumperTest {
     
     private final PipelineDataSourceManager dataSourceManager = new 
DefaultPipelineDataSourceManager();
     
-    @Before
+    @BeforeEach
     public void setUp() {
         position = new WALPosition(new 
PostgreSQLLogSequenceNumber(LogSequenceNumber.valueOf(100L)));
         channel = new MultiplexMemoryPipelineChannel(1, 10000, new 
EmptyAckCallback());
-        dumperConfig = mockDumperConfiguration();
-        PipelineTableMetaDataLoader metaDataLoader = new 
StandardPipelineTableMetaDataLoader(dataSourceManager.getDataSource(dumperConfig.getDataSourceConfig()));
-        walDumper = new PostgreSQLWALDumper(dumperConfig, position, channel, 
metaDataLoader);
-        
-    }
-    
-    @After
-    public void tearDown() {
-        dataSourceManager.close();
-    }
-    
-    private DumperConfiguration mockDumperConfiguration() {
         String jdbcUrl = 
"jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=PostgreSQL";
         String username = "root";
         String password = "root";
-        try (Connection connection = DriverManager.getConnection(jdbcUrl, 
username, password)) {
-            try (Statement statement = connection.createStatement()) {
-                String sql = "CREATE TABLE t_order_0 (order_id INT NOT NULL, 
user_id INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (order_id))";
-                statement.execute(sql);
-            }
-        } catch (final SQLException e) {
-            throw new RuntimeException("Init table failed", e);
+        createTable(jdbcUrl, username, password);
+        dumperConfig = createDumperConfiguration(jdbcUrl, username, password);
+        walDumper = new PostgreSQLWALDumper(dumperConfig, position, channel, 
new 
StandardPipelineTableMetaDataLoader(dataSourceManager.getDataSource(dumperConfig.getDataSourceConfig())));
+    }
+    
+    private static void createTable(final String jdbcUrl, final String 
username, final String password) {
+        String sql = "CREATE TABLE t_order_0 (order_id INT NOT NULL, user_id 
INT NOT NULL, status VARCHAR(45) NULL, PRIMARY KEY (order_id))";
+        try (
+                Connection connection = DriverManager.getConnection(jdbcUrl, 
username, password);
+                Statement statement = connection.createStatement()) {
+            statement.execute(sql);
+        } catch (final SQLException ex) {
+            throw new RuntimeException("Init table failed.", ex);
         }
-        PipelineDataSourceConfiguration dataSourceConfig = new 
StandardPipelineDataSourceConfiguration(jdbcUrl, username, password);
+    }
+    
+    private DumperConfiguration createDumperConfiguration(final String 
jdbcUrl, final String username, final String password) {
         DumperConfiguration result = new DumperConfiguration();
         result.setJobId("0101123456");
-        result.setDataSourceConfig(dataSourceConfig);
+        result.setDataSourceConfig(new 
StandardPipelineDataSourceConfiguration(jdbcUrl, username, password));
         result.setTableNameMap(Collections.singletonMap(new 
ActualTableName("t_order_0"), new LogicTableName("t_order")));
         result.setTableNameSchemaNameMapping(new 
TableNameSchemaNameMapping(Collections.emptyMap()));
         return result;
     }
     
+    @AfterEach
+    public void tearDown() {
+        dataSourceManager.close();
+    }
+    
     @Test
     public void assertStart() throws SQLException, 
ReflectiveOperationException {
         StandardPipelineDataSourceConfiguration dataSourceConfig = 
(StandardPipelineDataSourceConfiguration) dumperConfig.getDataSourceConfig();
@@ -124,16 +123,14 @@ public final class PostgreSQLWALDumperTest {
             
Plugins.getMemberAccessor().set(PostgreSQLWALDumper.class.getDeclaredField("logicalReplication"),
 walDumper, logicalReplication);
             
when(logicalReplication.createConnection(dataSourceConfig)).thenReturn(pgConnection);
             
when(pgConnection.unwrap(PgConnection.class)).thenReturn(pgConnection);
-            try (MockedStatic<PostgreSQLPositionInitializer> 
positionInitializer = mockStatic(PostgreSQLPositionInitializer.class)) {
-                positionInitializer.when(() -> 
PostgreSQLPositionInitializer.getUniqueSlotName(eq(pgConnection), 
anyString())).thenReturn("0101123456");
-                when(logicalReplication.createReplicationStream(pgConnection, 
PostgreSQLPositionInitializer.getUniqueSlotName(pgConnection, ""), 
position.getLogSequenceNumber()))
-                        .thenReturn(pgReplicationStream);
-                ByteBuffer data = ByteBuffer.wrap("table public.t_order_0: 
DELETE: order_id[integer]:1".getBytes());
-                
when(pgReplicationStream.readPending()).thenReturn(null).thenReturn(data).thenThrow(new
 SQLException(""));
-                
when(pgReplicationStream.getLastReceiveLSN()).thenReturn(LogSequenceNumber.valueOf(101L));
-                // TODO NPE occurred here
-                walDumper.start();
-            }
+            
when(PostgreSQLPositionInitializer.getUniqueSlotName(eq(pgConnection), 
anyString())).thenReturn("0101123456");
+            when(logicalReplication.createReplicationStream(pgConnection, 
PostgreSQLPositionInitializer.getUniqueSlotName(pgConnection, ""), 
position.getLogSequenceNumber()))
+                    .thenReturn(pgReplicationStream);
+            ByteBuffer data = ByteBuffer.wrap("table public.t_order_0: DELETE: 
order_id[integer]:1".getBytes());
+            
when(pgReplicationStream.readPending()).thenReturn(null).thenReturn(data).thenThrow(new
 SQLException(""));
+            
when(pgReplicationStream.getLastReceiveLSN()).thenReturn(LogSequenceNumber.valueOf(101L));
+            // TODO NPE occurred here
+            walDumper.start();
         } catch (final IngestException ignored) {
         }
         assertThat(channel.fetchRecords(100, 0).size(), is(1));
diff --git 
a/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/update/AlterTransactionRuleStatementUpdaterTest.java
 
b/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/update/AlterTransactionRuleStatementUpdaterTest.java
index 172cd8fa34a..f3374245cf0 100644
--- 
a/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/update/AlterTransactionRuleStatementUpdaterTest.java
+++ 
b/kernel/transaction/distsql/handler/src/test/java/org/apache/shardingsphere/transaction/distsql/handler/update/AlterTransactionRuleStatementUpdaterTest.java
@@ -19,6 +19,8 @@ package 
org.apache.shardingsphere.transaction.distsql.handler.update;
 
 import org.apache.shardingsphere.infra.util.props.PropertiesConverter;
 import org.apache.shardingsphere.infra.util.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.test.mock.AutoMockExtension;
+import org.apache.shardingsphere.test.mock.StaticMockSettings;
 import org.apache.shardingsphere.test.util.PropertiesBuilder;
 import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
 import 
org.apache.shardingsphere.transaction.config.TransactionRuleConfiguration;
@@ -26,36 +28,34 @@ import 
org.apache.shardingsphere.transaction.distsql.handler.fixture.ShardingSph
 import 
org.apache.shardingsphere.transaction.distsql.parser.segment.TransactionProviderSegment;
 import 
org.apache.shardingsphere.transaction.distsql.parser.statement.updatable.AlterTransactionRuleStatement;
 import 
org.apache.shardingsphere.transaction.spi.ShardingSphereTransactionManager;
-import org.junit.Test;
-import org.mockito.MockedStatic;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
 
 import java.util.Collections;
 import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-import static org.mockito.Mockito.mockStatic;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.when;
 
+@ExtendWith(AutoMockExtension.class)
+@StaticMockSettings(ShardingSphereServiceLoader.class)
 public final class AlterTransactionRuleStatementUpdaterTest {
     
-    @SuppressWarnings("rawtypes")
     @Test
     public void assertExecuteWithXA() {
-        try (MockedStatic<ShardingSphereServiceLoader> 
shardingSphereServiceLoader = mockStatic(ShardingSphereServiceLoader.class)) {
-            shardingSphereServiceLoader.when(
-                    () -> 
ShardingSphereServiceLoader.getServiceInstances(ShardingSphereTransactionManager.class)).thenReturn(Collections.singleton(new
 ShardingSphereTransactionManagerFixture()));
-            AlterTransactionRuleStatementUpdater updater = new 
AlterTransactionRuleStatementUpdater();
-            TransactionRuleConfiguration actual = 
updater.buildAlteredRuleConfiguration(createTransactionRuleConfiguration(), new 
AlterTransactionRuleStatement("XA",
-                    new TransactionProviderSegment("Atomikos", 
PropertiesBuilder.build(new Property("host", "127.0.0.1"), new 
Property("databaseName", "jbossts")))));
-            assertThat(actual.getDefaultType(), is("XA"));
-            assertThat(actual.getProviderType(), is("Atomikos"));
-            assertFalse(actual.getProps().isEmpty());
-            String props = PropertiesConverter.convert(actual.getProps());
-            assertTrue(props.contains("host=127.0.0.1"));
-            assertTrue(props.contains("databaseName=jbossts"));
-        }
+        
when(ShardingSphereServiceLoader.getServiceInstances(ShardingSphereTransactionManager.class)).thenReturn(Collections.singleton(new
 ShardingSphereTransactionManagerFixture()));
+        AlterTransactionRuleStatementUpdater updater = new 
AlterTransactionRuleStatementUpdater();
+        TransactionRuleConfiguration actual = 
updater.buildAlteredRuleConfiguration(createTransactionRuleConfiguration(), new 
AlterTransactionRuleStatement("XA",
+                new TransactionProviderSegment("Atomikos", 
PropertiesBuilder.build(new Property("host", "127.0.0.1"), new 
Property("databaseName", "jbossts")))));
+        assertThat(actual.getDefaultType(), is("XA"));
+        assertThat(actual.getProviderType(), is("Atomikos"));
+        assertFalse(actual.getProps().isEmpty());
+        String props = PropertiesConverter.convert(actual.getProps());
+        assertTrue(props.contains("host=127.0.0.1"));
+        assertTrue(props.contains("databaseName=jbossts"));
     }
     
     @Test

Reply via email to