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 abff6f1 #6595 add test cases for infra.yaml (#6632)
abff6f1 is described below
commit abff6f160719817b553b14f4b7a0c99e95b0ac3b
Author: yuzel <[email protected]>
AuthorDate: Thu Aug 6 11:43:57 2020 +0800
#6595 add test cases for infra.yaml (#6632)
* #6595 add test cases for infra.yaml
* empty line
* remove UnusedImports
Co-authored-by: liuyuze <[email protected]>
---
...mlShardingSphereAlgorithmConfigurationTest.java | 54 +++++++++++++
.../infra/yaml/engine/YamlEngineTest.java | 88 ++++++++++++++++++++++
.../ShardingSphereYamlRepresenterTest.java | 26 +++++++
.../YamlRuleConfigurationSwapperEngineTest.java | 58 ++++++++++++++
.../fixture/FixtureConfigurationYamlSwapper.java | 52 +++++++++++++
.../swapper/fixture/FixtureRuleConfiguration.java | 30 ++++++++
.../fixture/FixtureYamlRuleConfiguration.java | 35 +++++++++
...infra.yaml.swapper.YamlRuleConfigurationSwapper | 18 +++++
.../src/test/resources/yaml/fixture-rule.yaml | 18 +++++
9 files changed, 379 insertions(+)
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/config/algorithm/YamlShardingSphereAlgorithmConfigurationTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/config/algorithm/YamlShardingSphereAlgorithmConfigurationTest.java
new file mode 100644
index 0000000..7089098
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/config/algorithm/YamlShardingSphereAlgorithmConfigurationTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.config.algorithm;
+
+import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.infra.spi.fixture.TypedSPIFixture;
+import org.apache.shardingsphere.infra.spi.fixture.TypedSPIFixtureImpl;
+import org.junit.Test;
+
+import java.util.Collection;
+import java.util.Optional;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+public final class YamlShardingSphereAlgorithmConfigurationTest {
+
+ @Test
+ public void assertBuildByTypedSPI() {
+ ShardingSphereServiceLoader.register(TypedSPIFixture.class);
+ Collection<TypedSPIFixture> spiFixtures =
ShardingSphereServiceLoader.newServiceInstances(TypedSPIFixture.class);
+ Optional<TypedSPIFixture> spiOptional =
spiFixtures.stream().filter(echo -> echo instanceof
TypedSPIFixtureImpl).findFirst();
+ assertTrue(spiOptional.isPresent());
+ TypedSPIFixture typedSPIFixture = spiOptional.get();
+ typedSPIFixture.setProps(createProps());
+ YamlShardingSphereAlgorithmConfiguration actual =
YamlShardingSphereAlgorithmConfiguration.buildByTypedSPI(typedSPIFixture);
+ assertThat(actual.getType(), is("FIXTURE"));
+ assertThat(actual.getProps().getProperty("key"), is("value"));
+ }
+
+ private Properties createProps() {
+ Properties result = new Properties();
+ result.setProperty("key", "value");
+ return result;
+ }
+
+}
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
new file mode 100644
index 0000000..73b152d
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/engine/YamlEngineTest.java
@@ -0,0 +1,88 @@
+/*
+ * 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;
+
+import
org.apache.shardingsphere.infra.yaml.swapper.fixture.FixtureYamlRuleConfiguration;
+import org.junit.Test;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileReader;
+import java.io.IOException;
+import java.net.URL;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThat;
+
+public final class YamlEngineTest {
+
+ @Test
+ public void assertUnmarshalWithFile() throws IOException {
+ URL url =
getClass().getClassLoader().getResource("yaml/fixture-rule.yaml");
+ assertNotNull(url);
+ FixtureYamlRuleConfiguration actual = YamlEngine.unmarshal(new
File(url.getFile()), FixtureYamlRuleConfiguration.class);
+ assertThat(actual.getName(), is("test"));
+ }
+
+ @Test
+ public void assertUnmarshalWithYamlBytes() throws IOException {
+ URL url =
getClass().getClassLoader().getResource("yaml/fixture-rule.yaml");
+ assertNotNull(url);
+ StringBuilder yamlContent = new StringBuilder();
+ try (
+ FileReader fileReader = new FileReader(url.getFile());
+ BufferedReader reader = new BufferedReader(fileReader)) {
+ String line;
+ while (null != (line = reader.readLine())) {
+ yamlContent.append(line).append("\n");
+ }
+ }
+ FixtureYamlRuleConfiguration actual =
YamlEngine.unmarshal(yamlContent.toString().getBytes(),
FixtureYamlRuleConfiguration.class);
+ assertThat(actual.getName(), is("test"));
+ }
+
+ @Test
+ public void assertUnmarshalWithYamlContentClassType() {
+ FixtureYamlRuleConfiguration actual = YamlEngine.unmarshal("name:
test", FixtureYamlRuleConfiguration.class);
+ assertThat(actual.getName(), is("test"));
+ }
+
+ @Test
+ @SuppressWarnings("unchecked")
+ public void assertUnmarshalWithYamlContent() {
+ Map<String, Object> actual = (Map<String, Object>)
YamlEngine.unmarshal("name: test");
+ assertThat(actual.get("name").toString(), is("test"));
+ }
+
+ @Test
+ public void assertUnmarshalProperties() {
+ Properties actual = YamlEngine.unmarshalProperties("password:
pwd\nauthorizedSchemas: db1");
+ assertThat(actual.getProperty("authorizedSchemas"), is("db1"));
+ assertThat(actual.getProperty("password"), is("pwd"));
+ }
+
+ @Test
+ public void assertMarshal() {
+ FixtureYamlRuleConfiguration actual = new
FixtureYamlRuleConfiguration();
+ actual.setName("test");
+ assertThat(YamlEngine.marshal(actual), is("name: test\n"));
+ }
+}
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 c6ce4ce..bb8e2a6 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
@@ -20,15 +20,26 @@ package
org.apache.shardingsphere.infra.yaml.engine.representer;
import
org.apache.shardingsphere.infra.yaml.engine.fixture.DefaultYamlRepresenterFixture;
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 {
@@ -61,4 +72,19 @@ public final class ShardingSphereYamlRepresenterTest {
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));
+ }
}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/swapper/YamlRuleConfigurationSwapperEngineTest.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/swapper/YamlRuleConfigurationSwapperEngineTest.java
new file mode 100644
index 0000000..e2089a5
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/swapper/YamlRuleConfigurationSwapperEngineTest.java
@@ -0,0 +1,58 @@
+/*
+ * 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.swapper;
+
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.infra.yaml.config.YamlRuleConfiguration;
+import
org.apache.shardingsphere.infra.yaml.swapper.fixture.FixtureRuleConfiguration;
+import
org.apache.shardingsphere.infra.yaml.swapper.fixture.FixtureYamlRuleConfiguration;
+import org.junit.Test;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.junit.Assert.assertThat;
+
+public final class YamlRuleConfigurationSwapperEngineTest {
+
+ @Test
+ public void assertSwapToYamlConfigurations() {
+ FixtureRuleConfiguration ruleConfiguration = new
FixtureRuleConfiguration();
+ ruleConfiguration.setName("test");
+ Collection<YamlRuleConfiguration> actual = new
YamlRuleConfigurationSwapperEngine().swapToYamlConfigurations(Collections.singletonList(ruleConfiguration));
+ assertThat(actual.size(), is(1));
+ assertThat(((FixtureYamlRuleConfiguration)
actual.iterator().next()).getName(), is("test"));
+ }
+
+ @Test
+ public void assertSwapToRuleConfigurations() {
+ FixtureYamlRuleConfiguration yamlRuleConfiguration = new
FixtureYamlRuleConfiguration();
+ yamlRuleConfiguration.setName("test");
+ Collection<RuleConfiguration> actual = new
YamlRuleConfigurationSwapperEngine().swapToRuleConfigurations(Collections.singletonList(yamlRuleConfiguration));
+ assertThat(actual.size(), is(1));
+ assertThat(((FixtureRuleConfiguration)
actual.iterator().next()).getName(), is("test"));
+ }
+
+ @Test
+ public void assertGetYamlShortcuts() {
+ Map<String, Class<?>> actual =
YamlRuleConfigurationSwapperEngine.getYamlShortcuts();
+ assertThat(actual.size(), is(1));
+ }
+}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/swapper/fixture/FixtureConfigurationYamlSwapper.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/swapper/fixture/FixtureConfigurationYamlSwapper.java
new file mode 100644
index 0000000..1a10553
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/swapper/fixture/FixtureConfigurationYamlSwapper.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.yaml.swapper.fixture;
+
+import
org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper;
+
+public final class FixtureConfigurationYamlSwapper implements
YamlRuleConfigurationSwapper<FixtureYamlRuleConfiguration,
FixtureRuleConfiguration> {
+
+ @Override
+ public String getRuleTagName() {
+ return "FIXTURE";
+ }
+
+ @Override
+ public int getOrder() {
+ return 0;
+ }
+
+ @Override
+ public Class<FixtureRuleConfiguration> getTypeClass() {
+ return FixtureRuleConfiguration.class;
+ }
+
+ @Override
+ public FixtureYamlRuleConfiguration swapToYamlConfiguration(final
FixtureRuleConfiguration data) {
+ FixtureYamlRuleConfiguration result = new
FixtureYamlRuleConfiguration();
+ result.setName(data.getName());
+ return result;
+ }
+
+ @Override
+ public FixtureRuleConfiguration swapToObject(final
FixtureYamlRuleConfiguration yamlConfig) {
+ FixtureRuleConfiguration result = new FixtureRuleConfiguration();
+ result.setName(yamlConfig.getName());
+ return result;
+ }
+}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/swapper/fixture/FixtureRuleConfiguration.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/swapper/fixture/FixtureRuleConfiguration.java
new file mode 100644
index 0000000..4ecbb02
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/swapper/fixture/FixtureRuleConfiguration.java
@@ -0,0 +1,30 @@
+/*
+ * 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.swapper.fixture;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+
+@Getter
+@Setter
+public final class FixtureRuleConfiguration implements RuleConfiguration {
+
+ private String name;
+
+}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/swapper/fixture/FixtureYamlRuleConfiguration.java
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/swapper/fixture/FixtureYamlRuleConfiguration.java
new file mode 100644
index 0000000..cd1546f
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/java/org/apache/shardingsphere/infra/yaml/swapper/fixture/FixtureYamlRuleConfiguration.java
@@ -0,0 +1,35 @@
+/*
+ * 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.swapper.fixture;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.infra.config.RuleConfiguration;
+import org.apache.shardingsphere.infra.yaml.config.YamlRuleConfiguration;
+
+@Getter
+@Setter
+public final class FixtureYamlRuleConfiguration implements
YamlRuleConfiguration {
+
+ private String name;
+
+ @Override
+ public Class<? extends RuleConfiguration> getRuleConfigurationType() {
+ return FixtureRuleConfiguration.class;
+ }
+}
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
b/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
new file mode 100644
index 0000000..8d7b0d3
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.swapper.YamlRuleConfigurationSwapper
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.infra.yaml.swapper.fixture.FixtureConfigurationYamlSwapper
diff --git
a/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/yaml/fixture-rule.yaml
b/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/yaml/fixture-rule.yaml
new file mode 100644
index 0000000..794445c
--- /dev/null
+++
b/shardingsphere-infra/shardingsphere-infra-common/src/test/resources/yaml/fixture-rule.yaml
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+name: test