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

[shardingsphere] branch master updated: Use YAML file instead string concat in YAML test cases (#9400)

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

zhangyonglun 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 c2f5e61  Use YAML file instead string concat in YAML test cases (#9400)
c2f5e61 is described below

commit c2f5e612930a4b89bf75d49b78c860bd5704596a
Author: Liang Zhang <te...@163.com>
AuthorDate: Tue Feb 9 17:02:54 2021 +0800

    Use YAML file instead string concat in YAML test cases (#9400)
    
    * Refactor ShardingSphereAlgorithmConfigurationYamlSwapperTest
    
    * Refactor YamlRuleConfigurationSwapperFixture
    
    * Rename ShardingSphereYamlObjectFixture
    
    * Add customized-obj.yaml
    
    * Rename accepted-class.yaml
---
 .../infra/yaml/engine/YamlEngineTest.java          | 43 ++++++++++++----------
 .../ShardingSphereYamlConstructorTest.java         | 30 +++++++--------
 ...e.java => ShardingSphereYamlObjectFixture.java} |  2 +-
 .../fixture/YamlPropsRuleConfigurationFixture.java | 39 --------------------
 .../ShardingSphereYamlRepresenterTest.java         |  6 +--
 ...phereAlgorithmConfigurationYamlSwapperTest.java |  4 +-
 .../YamlRuleConfigurationSwapperFixture.java       | 20 +++++-----
 ...re-rule-with-props.yaml => accepted-class.yaml} |  3 +-
 ...re-rule-with-props.yaml => customized-obj.yaml} | 14 +++++--
 9 files changed, 64 insertions(+), 97 deletions(-)

diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/YamlEngineTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/YamlEngineTest.java
index afb5888..d1c1530 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/YamlEngineTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/YamlEngineTest.java
@@ -17,7 +17,7 @@
 
 package org.apache.shardingsphere.infra.yaml.engine;
 
-import org.apache.shardingsphere.infra.yaml.engine.fixture.YamlPropsRuleConfigurationFixture;
+import org.apache.shardingsphere.infra.yaml.config.YamlRootRuleConfigurations;
 import org.apache.shardingsphere.infra.yaml.swapper.fixture.YamlRuleConfigurationFixture;
 import org.junit.Test;
 import org.yaml.snakeyaml.constructor.ConstructorException;
@@ -36,7 +36,9 @@ import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
 import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
 
 public final class YamlEngineTest {
     
@@ -96,18 +98,19 @@ public final class YamlEngineTest {
         YamlEngine.unmarshal("url: !!java.net.URLClassLoader [[!!java.net.URL [\"http://localhost\"]]]", Collections.emptyList());
     }
     
+    @SuppressWarnings("unchecked")
     @Test
-    public void assertUnmarshalMapWithAcceptClasses() {
-        Collection<Class<?>> acceptClasses = new LinkedList<>();
-        acceptClasses.add(URLClassLoader.class);
-        acceptClasses.add(URL.class);
-        Map<String, URLClassLoader> actual = (Map) YamlEngine.unmarshal("url: !!java.net.URLClassLoader [[!!java.net.URL [\"http://localhost\"]]]", acceptClasses);
+    public void assertUnmarshalMapWithAcceptedClasses() {
+        Collection<Class<?>> acceptedClasses = new LinkedList<>();
+        acceptedClasses.add(URLClassLoader.class);
+        acceptedClasses.add(URL.class);
+        Map<String, URLClassLoader> actual = (Map) YamlEngine.unmarshal("url: !!java.net.URLClassLoader [[!!java.net.URL [\"http://localhost\"]]]", acceptedClasses);
         assertThat(actual.get("url").getClass().getName(), is(URLClassLoader.class.getName()));
     }
     
     @Test
-    public void assertUnmarshalWithAcceptClass() throws IOException {
-        URL url = getClass().getClassLoader().getResource("yaml/fixture-rule-with-props.yaml");
+    public void assertUnmarshalWithAcceptedClass() throws IOException {
+        URL url = getClass().getClassLoader().getResource("yaml/accepted-class.yaml");
         assertNotNull(url);
         StringBuilder yamlContent = new StringBuilder();
         try (
@@ -118,18 +121,20 @@ public final class YamlEngineTest {
                 yamlContent.append(line).append("\n");
             }
         }
-        Collection<Class<?>> acceptClasses = new LinkedList<>();
-        acceptClasses.add(URLClassLoader.class);
-        acceptClasses.add(URL.class);
-        acceptClasses.add(YamlPropsRuleConfigurationFixture.class);
-        YamlPropsRuleConfigurationFixture actual = YamlEngine.unmarshal(yamlContent.toString(), 
-                YamlPropsRuleConfigurationFixture.class, acceptClasses);
-        assertThat(actual.getName(), is("test"));
+        Collection<Class<?>> acceptedClasses = new LinkedList<>();
+        acceptedClasses.add(URLClassLoader.class);
+        acceptedClasses.add(URL.class);
+        acceptedClasses.add(YamlRootRuleConfigurations.class);
+        YamlRootRuleConfigurations actual = YamlEngine.unmarshal(yamlContent.toString(), YamlRootRuleConfigurations.class, acceptedClasses);
+        assertThat(actual.getProps().size(), is(2));
+        assertThat(actual.getProps().getProperty("normal"), is("normal"));
+        assertTrue(actual.getProps().containsKey("url"));
+        assertNull(actual.getProps().getProperty("url"));
     }
     
     @Test(expected = ConstructorException.class)
-    public void assertUnmarshalWithoutAcceptClass() throws IOException {
-        URL url = getClass().getClassLoader().getResource("yaml/fixture-rule-with-props.yaml");
+    public void assertUnmarshalWithoutAcceptedClass() throws IOException {
+        URL url = getClass().getClassLoader().getResource("yaml/accepted-class.yaml");
         assertNotNull(url);
         StringBuilder yamlContent = new StringBuilder();
         try (
@@ -140,8 +145,6 @@ public final class YamlEngineTest {
                 yamlContent.append(line).append("\n");
             }
         }
-        YamlPropsRuleConfigurationFixture actual = YamlEngine.unmarshalWithFilter(yamlContent.toString(), 
-                YamlPropsRuleConfigurationFixture.class);
-        assertThat(actual.getName(), is("test"));
+        YamlEngine.unmarshalWithFilter(yamlContent.toString(), YamlRootRuleConfigurations.class);
     }
 }
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 886b0bd..a023ac3 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,11 +17,13 @@
 
 package org.apache.shardingsphere.infra.yaml.engine.constructor;
 
-import org.apache.shardingsphere.infra.yaml.engine.fixture.ShardingSphereYamlRepresenterFixture;
+import org.apache.shardingsphere.infra.yaml.engine.fixture.ShardingSphereYamlObjectFixture;
 import org.junit.Test;
 import org.yaml.snakeyaml.Yaml;
 
-import java.util.Iterator;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.junit.Assert.assertNotNull;
@@ -30,23 +32,17 @@ import static org.junit.Assert.assertThat;
 public final class ShardingSphereYamlConstructorTest {
     
     @Test
-    public void assertToObject() {
-        String yamlString = ""
-                + "collection:\n"
-                + "- value1\n"
-                + "- value2\n"
-                + "map:\n"
-                + "  key1: value1\n"
-                + "  key2: value2\n"
-                + "value: value\n"
-                + "customizedClass:";
-        ShardingSphereYamlRepresenterFixture actual = new Yaml(
-                new ShardingSphereYamlConstructor(ShardingSphereYamlRepresenterFixture.class)).loadAs(yamlString, ShardingSphereYamlRepresenterFixture.class);
+    public void assertToObject() throws IOException {
+        try (InputStream inputStream = ShardingSphereYamlConstructorTest.class.getClassLoader().getResourceAsStream("yaml/customized-obj.yaml")) {
+            ShardingSphereYamlObjectFixture actual = new Yaml(new ShardingSphereYamlConstructor(ShardingSphereYamlObjectFixture.class)).loadAs(inputStream, ShardingSphereYamlObjectFixture.class);
+            assertYamlObject(actual);
+        }
+    }
+    
+    private void assertYamlObject(final ShardingSphereYamlObjectFixture actual) {
         assertThat(actual.getValue(), is("value"));
         assertThat(actual.getCollection().size(), is(2));
-        Iterator<String> iterator = actual.getCollection().iterator();
-        assertThat(iterator.next(), is("value1"));
-        assertThat(iterator.next(), is("value2"));
+        assertThat(actual.getCollection(), is(Arrays.asList("value1", "value2")));
         assertThat(actual.getMap().size(), is(2));
         assertThat(actual.getMap().get("key1"), is("value1"));
         assertThat(actual.getMap().get("key2"), is("value2"));
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/ShardingSphereYamlRepresenterFixture.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/ShardingSphereYamlObjectFixture.java
similarity index 95%
rename from shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/ShardingSphereYamlRepresenterFixture.java
rename to shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/ShardingSphereYamlObjectFixture.java
index 1a4a5f3..8b31536 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/ShardingSphereYamlRepresenterFixture.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/ShardingSphereYamlObjectFixture.java
@@ -25,7 +25,7 @@ import java.util.Map;
 
 @Getter
 @Setter
-public final class ShardingSphereYamlRepresenterFixture {
+public final class ShardingSphereYamlObjectFixture {
     
     private String value;
     
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/YamlPropsRuleConfigurationFixture.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/YamlPropsRuleConfigurationFixture.java
deleted file mode 100644
index e9e6a32..0000000
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/fixture/YamlPropsRuleConfigurationFixture.java
+++ /dev/null
@@ -1,39 +0,0 @@
-/*
- * 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.yaml.engine.fixture;
-
-import lombok.Getter;
-import lombok.Setter;
-import org.apache.shardingsphere.infra.yaml.config.YamlRuleConfiguration;
-import org.apache.shardingsphere.infra.yaml.swapper.fixture.RuleConfigurationFixture;
-
-import java.util.Properties;
-
-@Getter
-@Setter
-public final class YamlPropsRuleConfigurationFixture implements YamlRuleConfiguration {
-    
-    private String name;
-    
-    private Properties props;
-    
-    @Override
-    public Class<RuleConfigurationFixture> getRuleConfigurationType() {
-        return RuleConfigurationFixture.class;
-    }
-}
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 51d6888..0e346c4 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,7 +17,7 @@
 
 package org.apache.shardingsphere.infra.yaml.engine.representer;
 
-import org.apache.shardingsphere.infra.yaml.engine.fixture.ShardingSphereYamlRepresenterFixture;
+import org.apache.shardingsphere.infra.yaml.engine.fixture.ShardingSphereYamlObjectFixture;
 import org.junit.Test;
 import org.yaml.snakeyaml.Yaml;
 
@@ -33,13 +33,13 @@ public final class ShardingSphereYamlRepresenterTest {
     
     @Test
     public void assertToYamlWithoutContent() {
-        ShardingSphereYamlRepresenterFixture actual = new ShardingSphereYamlRepresenterFixture();
+        ShardingSphereYamlObjectFixture actual = new ShardingSphereYamlObjectFixture();
         assertThat(new Yaml(new ShardingSphereYamlRepresenter()).dumpAsMap(actual), is("{}\n"));
     }
     
     @Test
     public void assertToYamlWithAllContents() {
-        ShardingSphereYamlRepresenterFixture actual = new ShardingSphereYamlRepresenterFixture();
+        ShardingSphereYamlObjectFixture actual = new ShardingSphereYamlObjectFixture();
         actual.setValue("value");
         actual.setCollection(Arrays.asList("value1", "value2"));
         Map<String, String> map = new LinkedHashMap<>(2, 1);
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/swapper/algorithm/ShardingSphereAlgorithmConfigurationYamlSwapperTest.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/swapper/algorithm/ShardingSphereAlgorithmConfigurationYamlSwapperTest.java
index 6aa2755..fd237a4 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/swapper/algorithm/ShardingSphereAlgorithmConfigurationYamlSwapperTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/swapper/algorithm/ShardingSphereAlgorithmConfigurationYamlSwapperTest.java
@@ -38,9 +38,7 @@ public final class ShardingSphereAlgorithmConfigurationYamlSwapperTest {
     
     @Test
     public void assertSwapToObject() {
-        YamlShardingSphereAlgorithmConfiguration yamlConfig = new YamlShardingSphereAlgorithmConfiguration();
-        yamlConfig.setType("TEST");
-        yamlConfig.setProps(createProps());
+        YamlShardingSphereAlgorithmConfiguration yamlConfig = new YamlShardingSphereAlgorithmConfiguration("TEST", createProps());
         ShardingSphereAlgorithmConfiguration actual = new ShardingSphereAlgorithmConfigurationYamlSwapper().swapToObject(yamlConfig);
         assertThat(actual.getType(), is("TEST"));
         assertThat(actual.getProps().getProperty("key"), is("value"));
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/swapper/fixture/YamlRuleConfigurationSwapperFixture.java b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/swapper/fixture/YamlRuleConfigurationSwapperFixture.java
index 736dfaf..eeb0837 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/swapper/fixture/YamlRuleConfigurationSwapperFixture.java
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/swapper/fixture/YamlRuleConfigurationSwapperFixture.java
@@ -22,16 +22,6 @@ import org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
 public final class YamlRuleConfigurationSwapperFixture implements YamlRuleConfigurationSwapper<YamlRuleConfigurationFixture, RuleConfigurationFixture> {
     
     @Override
-    public String getRuleTagName() {
-        return "FIXTURE";
-    }
-    
-    @Override
-    public int getOrder() {
-        return 0;
-    }
-    
-    @Override
     public Class<RuleConfigurationFixture> getTypeClass() {
         return RuleConfigurationFixture.class;
     }
@@ -49,4 +39,14 @@ public final class YamlRuleConfigurationSwapperFixture implements YamlRuleConfig
         result.setName(yamlConfig.getName());
         return result;
     }
+    
+    @Override
+    public String getRuleTagName() {
+        return "FIXTURE";
+    }
+    
+    @Override
+    public int getOrder() {
+        return 0;
+    }
 }
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/yaml/fixture-rule-with-props.yaml b/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/yaml/accepted-class.yaml
similarity index 97%
copy from shardingsphere-infra/shardingsphere-infra-common/src/test/resources/yaml/fixture-rule-with-props.yaml
copy to shardingsphere-infra/shardingsphere-infra-common/src/test/resources/yaml/accepted-class.yaml
index 31687e6..7dc1af6 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/yaml/fixture-rule-with-props.yaml
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/yaml/accepted-class.yaml
@@ -15,6 +15,7 @@
 # limitations under the License.
 #
 
-name: test
 props:
+  normal: normal
   url: !!java.net.URLClassLoader [[!!java.net.URL ["http://localhost"]]]
+
diff --git a/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/yaml/fixture-rule-with-props.yaml b/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/yaml/customized-obj.yaml
similarity index 88%
rename from shardingsphere-infra/shardingsphere-infra-common/src/test/resources/yaml/fixture-rule-with-props.yaml
rename to shardingsphere-infra/shardingsphere-infra-common/src/test/resources/yaml/customized-obj.yaml
index 31687e6..09fb2d0 100644
--- a/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/yaml/fixture-rule-with-props.yaml
+++ b/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/yaml/customized-obj.yaml
@@ -15,6 +15,14 @@
 # limitations under the License.
 #
 
-name: test
-props:
-  url: !!java.net.URLClassLoader [[!!java.net.URL ["http://localhost"]]]
+collection:
+- value1
+- value2
+
+map:
+  key1: value1
+  key2: value2
+
+value: value
+
+customizedClass: