You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by me...@apache.org on 2021/02/08 08:10:11 UTC

[shardingsphere] branch master updated: Add more test cases for ShardingSphereYamlRepresenterTest (#9389)

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

menghaoran 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 2f26d08  Add more test cases for ShardingSphereYamlRepresenterTest (#9389)
2f26d08 is described below

commit 2f26d0836a3131f71eaea472d4e1515bd91685a8
Author: Liang Zhang <te...@163.com>
AuthorDate: Mon Feb 8 16:09:47 2021 +0800

    Add more test cases for ShardingSphereYamlRepresenterTest (#9389)
    
    * For code format
    
    * Add more test for ShardingSphereYamlRepresenterTest
    
    * Rename YamlTupleProcessorFixture
    
    * Rename YamlConstructFixture
    
    * Rename ShardingSphereYamlRepresenterFixture
    
    * Rename CustomizedClassFixture
    
    * Refactor ShardingSphereYamlConstructor
---
 .../metadata/engine/MetadataRefreshEngine.java     |  1 -
 .../constructor/ShardingSphereYamlConstructor.java |  2 +-
 .../ShardingSphereYamlConstructorTest.java         |  9 ++--
 ...ustomClass.java => CustomizedClassFixture.java} |  2 +-
 ...a => ShardingSphereYamlRepresenterFixture.java} |  6 ++-
 ...amlConstruct.java => YamlConstructFixture.java} |  6 +--
 ...nstruct.java => YamlTupleProcessorFixture.java} | 22 +++++-----
 .../ShardingSphereYamlRepresenterTest.java         | 48 ++++------------------
 ....engine.constructor.ShardingSphereYamlConstruct |  2 +-
 ...ter.processor.ShardingSphereYamlTupleProcessor} |  2 +-
 10 files changed, 34 insertions(+), 66 deletions(-)

diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/engine/MetadataRefreshEngine.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/engine/MetadataRefreshEngine.java
index 398f60e..4793203 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/engine/MetadataRefreshEngine.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/metadata/engine/MetadataRefreshEngine.java
@@ -57,7 +57,6 @@ public final class MetadataRefreshEngine {
      * @param routeDataSourceNames route data source names
      * @throws SQLException SQL exception
      */
-    @SuppressWarnings("rawtypes")
     public void refresh(final SQLStatement sqlStatement, final Collection<String> routeDataSourceNames) throws SQLException {
         Optional<MetadataRefresher> metadataRefresher = MetadataRefresherFactory.newInstance(sqlStatement);
         if (metadataRefresher.isPresent()) {
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/engine/constructor/ShardingSphereYamlConstructor.java b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/engine/constructor/ShardingSphereYamlConstructor.java
index 9a1d8be..0a7fdcf 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/engine/constructor/ShardingSphereYamlConstructor.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/main/java/org/apache/shardingsphere/infra/yaml/engine/constructor/ShardingSphereYamlConstructor.java
@@ -36,7 +36,7 @@ public class ShardingSphereYamlConstructor extends Constructor {
         ShardingSphereServiceLoader.register(ShardingSphereYamlConstruct.class);
     }
     
-    private final Map<Class, Construct> typeConstructs = new HashMap<>();
+    private final Map<Class<?>, Construct> typeConstructs = new HashMap<>();
     
     public ShardingSphereYamlConstructor(final Class<?> rootClass) {
         super(rootClass);
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/constructor/ShardingSphereYamlConstructorTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/constructor/ShardingSphereYamlConstructorTest.java
index 3997a23..886b0bd 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/constructor/ShardingSphereYamlConstructorTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/constructor/ShardingSphereYamlConstructorTest.java
@@ -17,7 +17,7 @@
 
 package org.apache.shardingsphere.infra.yaml.engine.constructor;
 
-import org.apache.shardingsphere.infra.yaml.engine.fixture.DefaultYamlRepresenterFixture;
+import org.apache.shardingsphere.infra.yaml.engine.fixture.ShardingSphereYamlRepresenterFixture;
 import org.junit.Test;
 import org.yaml.snakeyaml.Yaml;
 
@@ -39,8 +39,9 @@ public final class ShardingSphereYamlConstructorTest {
                 + "  key1: value1\n"
                 + "  key2: value2\n"
                 + "value: value\n"
-                + "customClass:";
-        DefaultYamlRepresenterFixture actual = new Yaml(new ShardingSphereYamlConstructor(DefaultYamlRepresenterFixture.class)).loadAs(yamlString, DefaultYamlRepresenterFixture.class);
+                + "customizedClass:";
+        ShardingSphereYamlRepresenterFixture actual = new Yaml(
+                new ShardingSphereYamlConstructor(ShardingSphereYamlRepresenterFixture.class)).loadAs(yamlString, ShardingSphereYamlRepresenterFixture.class);
         assertThat(actual.getValue(), is("value"));
         assertThat(actual.getCollection().size(), is(2));
         Iterator<String> iterator = actual.getCollection().iterator();
@@ -49,6 +50,6 @@ public final class ShardingSphereYamlConstructorTest {
         assertThat(actual.getMap().size(), is(2));
         assertThat(actual.getMap().get("key1"), is("value1"));
         assertThat(actual.getMap().get("key2"), is("value2"));
-        assertNotNull(actual.getCustomClass());
+        assertNotNull(actual.getCustomizedClass());
     }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/FixtureCustomClass.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/CustomizedClassFixture.java
similarity index 95%
rename from shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/FixtureCustomClass.java
rename to shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/CustomizedClassFixture.java
index aab8e91..52f25f9 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/FixtureCustomClass.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/CustomizedClassFixture.java
@@ -17,5 +17,5 @@
 
 package org.apache.shardingsphere.infra.yaml.engine.fixture;
 
-public final class FixtureCustomClass {
+public final class CustomizedClassFixture {
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/DefaultYamlRepresenterFixture.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/ShardingSphereYamlRepresenterFixture.java
similarity index 88%
rename from shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/DefaultYamlRepresenterFixture.java
rename to shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/ShardingSphereYamlRepresenterFixture.java
index 3f5506d..1a4a5f3 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/DefaultYamlRepresenterFixture.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/ShardingSphereYamlRepresenterFixture.java
@@ -25,7 +25,7 @@ import java.util.Map;
 
 @Getter
 @Setter
-public final class DefaultYamlRepresenterFixture {
+public final class ShardingSphereYamlRepresenterFixture {
     
     private String value;
     
@@ -33,5 +33,7 @@ public final class DefaultYamlRepresenterFixture {
     
     private Map<String, String> map;
     
-    private FixtureCustomClass customClass;
+    private CustomizedClassFixture customizedClass;
+    
+    private String customizedTag;
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/FixtureShardingSphereYamlConstruct.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/YamlConstructFixture.java
similarity index 87%
copy from shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/FixtureShardingSphereYamlConstruct.java
copy to shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/YamlConstructFixture.java
index 798b3ae..d545cc8 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/FixtureShardingSphereYamlConstruct.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/YamlConstructFixture.java
@@ -20,11 +20,11 @@ package org.apache.shardingsphere.infra.yaml.engine.fixture;
 import org.apache.shardingsphere.infra.yaml.engine.constructor.ShardingSphereYamlConstruct;
 import org.yaml.snakeyaml.nodes.Node;
 
-public final class FixtureShardingSphereYamlConstruct implements ShardingSphereYamlConstruct {
+public final class YamlConstructFixture implements ShardingSphereYamlConstruct {
     
     @Override
     public Object construct(final Node node) {
-        return new FixtureCustomClass();
+        return new CustomizedClassFixture();
     }
     
     @Override
@@ -33,6 +33,6 @@ public final class FixtureShardingSphereYamlConstruct implements ShardingSphereY
     
     @Override
     public Class<?> getType() {
-        return FixtureCustomClass.class;
+        return CustomizedClassFixture.class;
     }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/FixtureShardingSphereYamlConstruct.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/YamlTupleProcessorFixture.java
similarity index 55%
rename from shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/FixtureShardingSphereYamlConstruct.java
rename to shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/YamlTupleProcessorFixture.java
index 798b3ae..6af7f02 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/FixtureShardingSphereYamlConstruct.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/YamlTupleProcessorFixture.java
@@ -17,22 +17,22 @@
 
 package org.apache.shardingsphere.infra.yaml.engine.fixture;
 
-import org.apache.shardingsphere.infra.yaml.engine.constructor.ShardingSphereYamlConstruct;
-import org.yaml.snakeyaml.nodes.Node;
+import org.apache.shardingsphere.infra.yaml.engine.representer.processor.ShardingSphereYamlTupleProcessor;
+import org.yaml.snakeyaml.nodes.NodeTuple;
+import org.yaml.snakeyaml.nodes.ScalarNode;
+import org.yaml.snakeyaml.nodes.Tag;
 
-public final class FixtureShardingSphereYamlConstruct implements ShardingSphereYamlConstruct {
+public final class YamlTupleProcessorFixture implements ShardingSphereYamlTupleProcessor {
     
     @Override
-    public Object construct(final Node node) {
-        return new FixtureCustomClass();
+    public String getTupleName() {
+        return "customizedTag";
     }
     
+    @SuppressWarnings("ReturnOfNull")
     @Override
-    public void construct2ndStep(final Node node, final Object newInstance) {
-    }
-    
-    @Override
-    public Class<?> getType() {
-        return FixtureCustomClass.class;
+    public NodeTuple process(final NodeTuple nodeTuple) {
+        String value = ((ScalarNode) nodeTuple.getValueNode()).getValue();
+        return "null".equals(value) ? null : new NodeTuple(nodeTuple.getKeyNode(), new ScalarNode(Tag.STR, String.join("_", "converted", value), null, null, null));
     }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/representer/ShardingSphereYamlRepresenterTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/representer/ShardingSphereYamlRepresenterTest.java
index bb8e2a6..51d6888 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/representer/ShardingSphereYamlRepresenterTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/representer/ShardingSphereYamlRepresenterTest.java
@@ -17,74 +17,40 @@
 
 package org.apache.shardingsphere.infra.yaml.engine.representer;
 
-import org.apache.shardingsphere.infra.yaml.engine.fixture.DefaultYamlRepresenterFixture;
+import org.apache.shardingsphere.infra.yaml.engine.fixture.ShardingSphereYamlRepresenterFixture;
 import org.junit.Test;
 import org.yaml.snakeyaml.Yaml;
-import org.yaml.snakeyaml.introspector.MethodProperty;
-import org.yaml.snakeyaml.nodes.Node;
-import org.yaml.snakeyaml.nodes.NodeTuple;
-import org.yaml.snakeyaml.nodes.ScalarNode;
-import org.yaml.snakeyaml.nodes.SequenceNode;
 
-import java.beans.IntrospectionException;
-import java.beans.PropertyDescriptor;
-import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Collections;
 import java.util.LinkedHashMap;
-import java.util.List;
 import java.util.Map;
-import java.util.stream.Collectors;
 
 import static org.hamcrest.CoreMatchers.containsString;
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertThat;
-import static org.junit.Assert.assertTrue;
 
 public final class ShardingSphereYamlRepresenterTest {
     
     @Test
-    public void assertToYamlWithNull() {
-        DefaultYamlRepresenterFixture actual = new DefaultYamlRepresenterFixture();
+    public void assertToYamlWithoutContent() {
+        ShardingSphereYamlRepresenterFixture actual = new ShardingSphereYamlRepresenterFixture();
         assertThat(new Yaml(new ShardingSphereYamlRepresenter()).dumpAsMap(actual), is("{}\n"));
     }
     
     @Test
-    public void assertToYamlWithEmpty() {
-        DefaultYamlRepresenterFixture actual = new DefaultYamlRepresenterFixture();
-        actual.setValue("");
-        actual.setCollection(Collections.emptyList());
-        actual.setMap(Collections.emptyMap());
-        assertThat(new Yaml(new ShardingSphereYamlRepresenter()).dumpAsMap(actual), is("value: ''\n"));
-    }
-    
-    @Test
-    public void assertToYamlWithValue() {
-        DefaultYamlRepresenterFixture actual = new DefaultYamlRepresenterFixture();
+    public void assertToYamlWithAllContents() {
+        ShardingSphereYamlRepresenterFixture actual = new ShardingSphereYamlRepresenterFixture();
         actual.setValue("value");
         actual.setCollection(Arrays.asList("value1", "value2"));
         Map<String, String> map = new LinkedHashMap<>(2, 1);
         map.put("key1", "value1");
         map.put("key2", "value2");
         actual.setMap(map);
+        actual.setCustomizedTag("customized_tag");
         String expected = new Yaml(new ShardingSphereYamlRepresenter()).dumpAsMap(actual);
         assertThat(expected, containsString("collection:\n- value1\n- value2\n"));
         assertThat(expected, containsString("map:\n  key1: value1\n  key2: value2\n"));
         assertThat(expected, containsString("value: value\n"));
-    }
-    
-    @Test
-    public void assertRepresentJavaBeanProperty() throws IntrospectionException {
-        ShardingSphereYamlRepresenter shardingSphereYamlRepresenter = new ShardingSphereYamlRepresenter();
-        DefaultYamlRepresenterFixture javaBean = new DefaultYamlRepresenterFixture();
-        PropertyDescriptor propertyDescriptor = new PropertyDescriptor("collection", DefaultYamlRepresenterFixture.class);
-        MethodProperty methodProperty = new MethodProperty(propertyDescriptor);
-        List<String> propertyValue = new ArrayList<>(2);
-        propertyValue.add("value1");
-        propertyValue.add("value2");
-        NodeTuple actual = shardingSphereYamlRepresenter.representJavaBeanProperty(javaBean, methodProperty, propertyValue, null);
-        List<Node> nodes = ((SequenceNode) actual.getValueNode()).getValue();
-        assertThat(nodes.size(), is(2));
-        assertTrue(nodes.stream().map(node -> ((ScalarNode) node).getValue()).collect(Collectors.toList()).containsAll(propertyValue));
+        assertThat(expected, containsString("customizedTag: converted_customized_tag\n"));
     }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.engine.constructor.ShardingSphereYamlConstruct b/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.engine.constructor.ShardingSphereYamlConstruct
index 83a589b..3dfc1db 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.engine.constructor.ShardingSphereYamlConstruct
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.engine.constructor.ShardingSphereYamlConstruct
@@ -15,4 +15,4 @@
 # limitations under the License.
 #
 
-org.apache.shardingsphere.infra.yaml.engine.fixture.FixtureShardingSphereYamlConstruct
+org.apache.shardingsphere.infra.yaml.engine.fixture.YamlConstructFixture
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.engine.constructor.ShardingSphereYamlConstruct b/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.engine.representer.processor.ShardingSphereYamlTupleProcessor
similarity index 90%
copy from shardingsphere-infra/shardingsphere-infra-common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.engine.constructor.ShardingSphereYamlConstruct
copy to shardingsphere-infra/shardingsphere-infra-common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.engine.representer.processor.ShardingSphereYamlTupleProcessor
index 83a589b..5d8ddfd 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.engine.constructor.ShardingSphereYamlConstruct
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.engine.representer.processor.ShardingSphereYamlTupleProcessor
@@ -15,4 +15,4 @@
 # limitations under the License.
 #
 
-org.apache.shardingsphere.infra.yaml.engine.fixture.FixtureShardingSphereYamlConstruct
+org.apache.shardingsphere.infra.yaml.engine.fixture.YamlTupleProcessorFixture