This is an automated email from the ASF dual-hosted git repository.

zhangliang 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 e4b4572b06f Add more test cases on ShardingSphereMetaData (#37086)
e4b4572b06f is described below

commit e4b4572b06f02198f8aaa2929d140c58fd81f274
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Nov 13 11:36:09 2025 +0800

    Add more test cases on ShardingSphereMetaData (#37086)
---
 AGENTS.md                                          |  2 +-
 .../infra/metadata/ShardingSphereMetaDataTest.java | 24 +++++++++++++++++++++-
 2 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/AGENTS.md b/AGENTS.md
index 3ceb9005f11..53fe48d496c 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -156,7 +156,7 @@ Mention which topology you target, the registry used, and 
any compatibility cons
 - If an SPI returns an object that is not the subject under test, use 
`mockStatic` (or equivalent) to provide canned behavior.
 - When only a few properties of a complex object are used, mock it rather than 
assembling the full graph.
 - Do not mock simple objects that can be instantiated directly with `new`.
-- Prefer plain mocks; only opt into Mockito’s `RETURNS_DEEP_STUBS` when 
cascading (chained) interactions truly require it.
+- Do not enable Mockito’s `RETURNS_DEEP_STUBS` unless unavoidable chained 
interactions make explicit stubs impractical; if you must enable it, mention 
the justification in the test description.
 
 ## AI Self-Check Checklist (Pre-Submission Must-Do)
 1. Instruction precedence: `CODE_OF_CONDUCT.md` → user request → this guide → 
other docs. Are any conflicts unresolved?
diff --git 
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaDataTest.java
 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaDataTest.java
index 8dc654ce14d..17736ff6869 100644
--- 
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaDataTest.java
+++ 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/ShardingSphereMetaDataTest.java
@@ -29,6 +29,7 @@ import 
org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUn
 import org.apache.shardingsphere.infra.metadata.database.rule.RuleMetaData;
 import org.apache.shardingsphere.infra.rule.ShardingSphereRule;
 import org.apache.shardingsphere.infra.rule.attribute.RuleAttributes;
+import 
org.apache.shardingsphere.infra.rule.attribute.datasource.StaticDataSourceRuleAttribute;
 import org.apache.shardingsphere.infra.rule.scope.GlobalRule;
 import 
org.apache.shardingsphere.infra.rule.scope.GlobalRule.GlobalRuleChangedType;
 import org.apache.shardingsphere.test.infra.fixture.jdbc.MockedDataSource;
@@ -82,8 +83,9 @@ class ShardingSphereMetaDataTest {
         ResourceMetaData resourceMetaData = mock(ResourceMetaData.class, 
RETURNS_DEEP_STUBS);
         GlobalRule globalRule = mock(GlobalRule.class);
         MockedDataSource dataSource = new MockedDataSource();
+        StaticDataSourceRuleAttribute staticDataSourceRuleAttribute = 
mock(StaticDataSourceRuleAttribute.class);
         ShardingSphereRule databaseRule1 = mock(ShardingSphereRule.class);
-        when(databaseRule1.getAttributes()).thenReturn(new RuleAttributes());
+        when(databaseRule1.getAttributes()).thenReturn(new 
RuleAttributes(staticDataSourceRuleAttribute));
         ShardingSphereRule databaseRule2 = mock(ShardingSphereRule.class, 
withSettings().extraInterfaces(AutoCloseable.class));
         when(databaseRule2.getAttributes()).thenReturn(new RuleAttributes());
         ShardingSphereMetaData metaData = new ShardingSphereMetaData(new 
LinkedList<>(Collections.singleton(mockDatabase(resourceMetaData, dataSource, 
databaseRule1, databaseRule2))),
@@ -93,6 +95,7 @@ class ShardingSphereMetaDataTest {
         Awaitility.await().pollDelay(10L, 
TimeUnit.MILLISECONDS).until(dataSource::isClosed);
         assertTrue(dataSource.isClosed());
         verify(globalRule).refresh(metaData.getAllDatabases(), 
GlobalRuleChangedType.DATABASE_CHANGED);
+        verify(staticDataSourceRuleAttribute).cleanStorageNodeDataSources();
     }
     
     @SuppressWarnings("resource")
@@ -124,6 +127,25 @@ class ShardingSphereMetaDataTest {
         assertThat(metaData.getDatabase("foo_db"), is(database));
     }
     
+    @SuppressWarnings("resource")
+    @Test
+    void assertPutDatabase() {
+        ShardingSphereMetaData metaData = new ShardingSphereMetaData();
+        ShardingSphereDatabase database = mockDatabase(mock(), new 
MockedDataSource(), mock(ShardingSphereRule.class));
+        metaData.putDatabase(database);
+        assertThat(metaData.getDatabase("foo_db"), is(database));
+    }
+    
+    @Test
+    void assertCloseClosesAllRules() throws Exception {
+        GlobalRule closableGlobalRule = mock(GlobalRule.class, 
withSettings().extraInterfaces(AutoCloseable.class));
+        ShardingSphereDatabase database = 
mockDatabase(mock(ResourceMetaData.class), new MockedDataSource(), 
mock(ShardingSphereRule.class));
+        ShardingSphereMetaData metaData = new ShardingSphereMetaData(new 
LinkedList<>(Collections.singleton(database)),
+                mock(ResourceMetaData.class), new 
RuleMetaData(Collections.singleton(closableGlobalRule)), new 
ConfigurationProperties(new Properties()));
+        metaData.close();
+        verify((AutoCloseable) closableGlobalRule).close();
+    }
+    
     private ShardingSphereDatabase mockDatabase(final ResourceMetaData 
resourceMetaData, final DataSource dataSource, final ShardingSphereRule... 
rules) {
         ShardingSphereDatabase result = mock(ShardingSphereDatabase.class);
         when(result.getName()).thenReturn("foo_db");

Reply via email to