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 44771f4c207 Add test cases on ResourceMetaData (#33245)
44771f4c207 is described below

commit 44771f4c207f3568746432bb2cd17ef94d1cf0e1
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Oct 14 23:22:51 2024 +0800

    Add test cases on ResourceMetaData (#33245)
    
    * Add test cases on ResourceMetaData
    
    * Add test cases on StorageNode
---
 .../database/resource/ResourceMetaData.java        |  7 ++-
 .../database/resource/ResourceMetaDataTest.java    | 46 +++++++++++++++++++
 .../database/resource/node/StorageNodeTest.java    | 52 ++++++++++++++++++++++
 3 files changed, 101 insertions(+), 4 deletions(-)

diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java
index f7b00717f8c..6a88e9b6907 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaData.java
@@ -50,10 +50,9 @@ public final class ResourceMetaData {
                 .collect(Collectors.toMap(each -> each, StorageNode::new, 
(oldValue, currentValue) -> oldValue, LinkedHashMap::new));
         Map<String, DataSourcePoolProperties> dataSourcePoolPropsMap = 
dataSources.entrySet().stream().collect(
                 Collectors.toMap(Entry::getKey, entry -> 
DataSourcePoolPropertiesCreator.create(entry.getValue()), (oldValue, 
currentValue) -> oldValue, LinkedHashMap::new));
-        storageUnits = new LinkedHashMap<>();
-        for (Entry<String, StorageNode> entry : storageUnitNodeMap.entrySet()) 
{
-            storageUnits.put(entry.getKey(), new StorageUnit(entry.getValue(), 
dataSourcePoolPropsMap.get(entry.getKey()), 
dataSources.get(entry.getValue().getName())));
-        }
+        storageUnits = storageUnitNodeMap.entrySet().stream()
+                .collect(Collectors.toMap(Entry::getKey, entry -> new 
StorageUnit(entry.getValue(), dataSourcePoolPropsMap.get(entry.getKey()), 
dataSources.get(entry.getValue().getName())),
+                        (a, b) -> b, LinkedHashMap::new));
     }
     
     /**
diff --git 
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaDataTest.java
 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaDataTest.java
new file mode 100644
index 00000000000..5ff3dc2775b
--- /dev/null
+++ 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/ResourceMetaDataTest.java
@@ -0,0 +1,46 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.metadata.database.resource;
+
+import org.apache.shardingsphere.test.fixture.jdbc.MockedDataSource;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class ResourceMetaDataTest {
+    
+    @Test
+    void assertGetAllInstanceDataSourceNames() {
+        assertThat(new ResourceMetaData(Collections.singletonMap("foo", new 
MockedDataSource())).getAllInstanceDataSourceNames(), 
is(Collections.singletonList("foo")));
+    }
+    
+    @Test
+    void assertGetNotExistedDataSources() {
+        assertTrue(new ResourceMetaData(Collections.singletonMap("foo", new 
MockedDataSource())).getNotExistedDataSources(Collections.singleton("foo")).isEmpty());
+    }
+    
+    @Test
+    void assertGetDataSourceMap() {
+        assertThat(new ResourceMetaData(Collections.singletonMap("foo", new 
MockedDataSource())).getDataSourceMap().size(), is(1));
+        assertTrue(new ResourceMetaData(Collections.singletonMap("foo", new 
MockedDataSource())).getDataSourceMap().containsKey("foo"));
+    }
+}
diff --git 
a/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeTest.java
 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeTest.java
new file mode 100644
index 00000000000..ef2f7a1cf48
--- /dev/null
+++ 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/metadata/database/resource/node/StorageNodeTest.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.infra.metadata.database.resource.node;
+
+import org.junit.jupiter.api.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+class StorageNodeTest {
+    
+    @Test
+    void assertEquals() {
+        assertThat(new StorageNode("localhost", 3306, "root"), is(new 
StorageNode("LOCALHOST", 3306, "ROOT")));
+    }
+    
+    @Test
+    void assertNotEqualsWithDifferentClassType() {
+        assertThat(new StorageNode("localhost", 3306, "root"), not(new 
Object()));
+    }
+    
+    @Test
+    void assertNotEqualsWithDifferentName() {
+        assertThat(new StorageNode("localhost", 3306, "root"), not(new 
StorageNode("12.0.0.1", 3306, "ROOT")));
+    }
+    
+    @Test
+    void assertHashcode() {
+        assertThat(new StorageNode("localhost", 3306, "root").hashCode(), 
is(new StorageNode("LOCALHOST", 3306, "ROOT").hashCode()));
+    }
+    
+    @Test
+    void assertToString() {
+        assertThat(new StorageNode("localhost", 3306, "root").toString(), 
is("localhost_3306_root"));
+    }
+}

Reply via email to