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 523d26d3e51 Use Files.readAllLines() to refactor test cases (#33036)
523d26d3e51 is described below
commit 523d26d3e5163ba8817dc8a2f900a09e4f32fb2a
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Sep 28 18:04:33 2024 +0800
Use Files.readAllLines() to refactor test cases (#33036)
---
.../infra/util/yaml/YamlEngineTest.java | 35 +++++++---------------
.../YamlShardingSphereDataSourceFactoryTest.java | 20 ++++---------
.../ConvertYamlConfigurationExecutorTest.java | 26 ++++++----------
.../ExportDatabaseConfigurationExecutorTest.java | 24 +++++----------
.../queryable/ExportStorageNodesExecutorTest.java | 22 +++++---------
5 files changed, 42 insertions(+), 85 deletions(-)
diff --git
a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/yaml/YamlEngineTest.java
b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/yaml/YamlEngineTest.java
index a0a5f0bf2fa..7071e001e67 100644
---
a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/yaml/YamlEngineTest.java
+++
b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/yaml/YamlEngineTest.java
@@ -21,11 +21,12 @@ import
org.apache.shardingsphere.infra.util.yaml.fixture.shortcuts.YamlShortcuts
import org.junit.jupiter.api.Test;
import org.yaml.snakeyaml.composer.ComposerException;
-import java.io.BufferedReader;
import java.io.File;
-import java.io.FileReader;
import java.io.IOException;
+import java.net.URISyntaxException;
import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Properties;
@@ -57,19 +58,19 @@ class YamlEngineTest {
}
@Test
- void assertUnmarshalWithYamlBytes() throws IOException {
+ void assertUnmarshalWithYamlBytes() throws IOException, URISyntaxException
{
URL url =
getClass().getClassLoader().getResource("yaml/shortcuts-fixture.yaml");
assertNotNull(url);
- String yamlContent = readContent(url);
+ String yamlContent = String.join(System.lineSeparator(),
Files.readAllLines(Paths.get(url.toURI())));
YamlShortcutsConfigurationFixture actual =
YamlEngine.unmarshal(yamlContent.getBytes(),
YamlShortcutsConfigurationFixture.class);
assertThat(actual.getName(), is("test"));
}
@Test
- void assertUnmarshalWithEmptyYamlBytes() throws IOException {
+ void assertUnmarshalWithEmptyYamlBytes() throws IOException,
URISyntaxException {
URL url =
getClass().getClassLoader().getResource("yaml/empty-config.yaml");
assertNotNull(url);
- String yamlContent = readContent(url);
+ String yamlContent = String.join(System.lineSeparator(),
Files.readAllLines(Paths.get(url.toURI())));
YamlShortcutsConfigurationFixture actual =
YamlEngine.unmarshal(yamlContent.getBytes(),
YamlShortcutsConfigurationFixture.class);
assertNotNull(actual);
assertTrue(actual.isEmpty());
@@ -108,10 +109,10 @@ class YamlEngineTest {
}
@Test
- void assertUnmarshalInvalidYaml() throws IOException {
+ void assertUnmarshalInvalidYaml() throws IOException, URISyntaxException {
URL url =
getClass().getClassLoader().getResource("yaml/accepted-class.yaml");
assertNotNull(url);
- String yamlContent = readContent(url);
+ String yamlContent = String.join(System.lineSeparator(),
Files.readAllLines(Paths.get(url.toURI())));
assertThrows(ComposerException.class, () ->
YamlEngine.unmarshal(yamlContent, Object.class));
}
@@ -121,21 +122,7 @@ class YamlEngineTest {
actual.setName("test");
YamlShortcutsConfigurationFixture actualAnother = new
YamlShortcutsConfigurationFixture();
actualAnother.setName("test");
- String res = "- !FIXTURE" + System.lineSeparator() + " name: test" +
System.lineSeparator() + "- !FIXTURE"
- + System.lineSeparator() + " name: test" +
System.lineSeparator();
- assertThat(YamlEngine.marshal(Arrays.asList(actual, actualAnother)),
is(res));
- }
-
- private String readContent(final URL url) throws IOException {
- StringBuilder result = new StringBuilder();
- try (
- FileReader fileReader = new FileReader(url.getFile());
- BufferedReader reader = new BufferedReader(fileReader)) {
- String line;
- while (null != (line = reader.readLine())) {
- result.append(line).append(System.lineSeparator());
- }
- }
- return result.toString();
+ String expected = "- !FIXTURE" + System.lineSeparator() + " name:
test" + System.lineSeparator() + "- !FIXTURE" + System.lineSeparator() + "
name: test" + System.lineSeparator();
+ assertThat(YamlEngine.marshal(Arrays.asList(actual, actualAnother)),
is(expected));
}
}
diff --git
a/jdbc/src/test/java/org/apache/shardingsphere/driver/api/yaml/YamlShardingSphereDataSourceFactoryTest.java
b/jdbc/src/test/java/org/apache/shardingsphere/driver/api/yaml/YamlShardingSphereDataSourceFactoryTest.java
index a5f581d67e6..81ab825aa38 100644
---
a/jdbc/src/test/java/org/apache/shardingsphere/driver/api/yaml/YamlShardingSphereDataSourceFactoryTest.java
+++
b/jdbc/src/test/java/org/apache/shardingsphere/driver/api/yaml/YamlShardingSphereDataSourceFactoryTest.java
@@ -24,11 +24,12 @@ import org.junit.jupiter.api.Test;
import org.mockito.internal.configuration.plugins.Plugins;
import javax.sql.DataSource;
-import java.io.BufferedReader;
import java.io.File;
-import java.io.FileReader;
import java.io.IOException;
+import java.net.URISyntaxException;
import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;
@@ -45,7 +46,7 @@ class YamlShardingSphereDataSourceFactoryTest {
}
@Test
- void assertCreateDataSourceWithBytes() throws SQLException, IOException {
+ void assertCreateDataSourceWithBytes() throws SQLException, IOException,
URISyntaxException {
assertDataSource(YamlShardingSphereDataSourceFactory.createDataSource(readFile(getYamlFileUrl()).getBytes()));
}
@@ -79,17 +80,8 @@ class YamlShardingSphereDataSourceFactoryTest {
return
Objects.requireNonNull(YamlShardingSphereDataSourceFactoryTest.class.getResource("/config/factory/database-for-factory-test.yaml"));
}
- private String readFile(final URL url) throws IOException {
- StringBuilder result = new StringBuilder();
- try (
- FileReader fileReader = new FileReader(url.getFile());
- BufferedReader reader = new BufferedReader(fileReader)) {
- String line;
- while (null != (line = reader.readLine())) {
- result.append(line).append(System.lineSeparator());
- }
- }
- return result.toString();
+ private String readFile(final URL url) throws IOException,
URISyntaxException {
+ return String.join(System.lineSeparator(),
Files.readAllLines(Paths.get(url.toURI())));
}
@SneakyThrows(ReflectiveOperationException.class)
diff --git
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationExecutorTest.java
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationExecutorTest.java
index 272ba82a3e2..985de029002 100644
---
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationExecutorTest.java
+++
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ConvertYamlConfigurationExecutorTest.java
@@ -28,11 +28,14 @@ import org.apache.shardingsphere.parser.rule.SQLParserRule;
import
org.apache.shardingsphere.parser.rule.builder.DefaultSQLParserRuleConfigurationBuilder;
import org.junit.jupiter.api.Test;
-import java.io.BufferedReader;
-import java.io.FileReader;
import java.io.IOException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.util.Collection;
import java.util.Objects;
+import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -97,21 +100,10 @@ class ConvertYamlConfigurationExecutorTest {
.forEach(each ->
assertNotNull(sqlParserRule.getSQLParserEngine(TypedSPILoader.getService(DatabaseType.class,
"MySQL")).parse(each, false)));
}
- @SneakyThrows(IOException.class)
+ @SneakyThrows({IOException.class, URISyntaxException.class})
private String loadExpectedRow(final String expectedFilePath) {
- StringBuilder result = new StringBuilder();
- String fileName =
Objects.requireNonNull(ConvertYamlConfigurationExecutorTest.class.getResource(expectedFilePath)).getFile();
- try (
- FileReader fileReader = new FileReader(fileName);
- BufferedReader reader = new BufferedReader(fileReader)) {
- String line;
- while (null != (line = reader.readLine())) {
- if (!line.startsWith("#")) {
- result.append(line).append(System.lineSeparator());
- }
- }
- result.append(System.lineSeparator());
- }
- return result.toString();
+ URL url =
Objects.requireNonNull(ConvertYamlConfigurationExecutorTest.class.getResource(expectedFilePath));
+ return Files.readAllLines(Paths.get(url.toURI())).stream().filter(each
-> !each.startsWith("#")).collect(Collectors.joining(System.lineSeparator()))
+ + System.lineSeparator() + System.lineSeparator();
}
}
diff --git
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportDatabaseConfigurationExecutorTest.java
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportDatabaseConfigurationExecutorTest.java
index 3acdada3591..1b0de173e3d 100644
---
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportDatabaseConfigurationExecutorTest.java
+++
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportDatabaseConfigurationExecutorTest.java
@@ -39,9 +39,11 @@ import
org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.jupiter.api.Test;
import javax.sql.DataSource;
-import java.io.BufferedReader;
-import java.io.FileReader;
import java.io.IOException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
@@ -134,20 +136,10 @@ class ExportDatabaseConfigurationExecutorTest {
return result;
}
- @SneakyThrows(IOException.class)
+ @SneakyThrows({IOException.class, URISyntaxException.class})
private String loadExpectedRow() {
- StringBuilder result = new StringBuilder();
- String fileName =
Objects.requireNonNull(ExportDatabaseConfigurationExecutorTest.class.getResource("/expected/export-database-configuration.yaml")).getFile();
- try (
- FileReader fileReader = new FileReader(fileName);
- BufferedReader reader = new BufferedReader(fileReader)) {
- String line;
- while (null != (line = reader.readLine())) {
- if (!line.startsWith("#") && !line.trim().isEmpty()) {
- result.append(line).append(System.lineSeparator());
- }
- }
- }
- return result.toString();
+ URL url =
Objects.requireNonNull(ConvertYamlConfigurationExecutorTest.class.getResource("/expected/export-database-configuration.yaml"));
+ return Files.readAllLines(Paths.get(url.toURI())).stream().filter(each
-> !each.startsWith("#") &&
!each.trim().isEmpty()).collect(Collectors.joining(System.lineSeparator()))
+ + System.lineSeparator();
}
}
diff --git
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportStorageNodesExecutorTest.java
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportStorageNodesExecutorTest.java
index 12b966ea919..acec7da7b06 100644
---
a/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportStorageNodesExecutorTest.java
+++
b/proxy/backend/core/src/test/java/org/apache/shardingsphere/proxy/backend/handler/distsql/ral/queryable/ExportStorageNodesExecutorTest.java
@@ -56,15 +56,18 @@ import org.mockito.junit.jupiter.MockitoSettings;
import org.mockito.quality.Strictness;
import javax.sql.DataSource;
-import java.io.BufferedReader;
-import java.io.FileReader;
import java.io.IOException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
+import java.util.stream.Collectors;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
@@ -190,18 +193,9 @@ class ExportStorageNodesExecutorTest {
return result;
}
- @SneakyThrows(IOException.class)
+ @SneakyThrows({IOException.class, URISyntaxException.class})
private String loadExpectedRow() {
- StringBuilder result = new StringBuilder();
- String fileName =
Objects.requireNonNull(ExportStorageNodesExecutorTest.class.getResource("/expected/export-storage-nodes.json")).getFile();
- try (
- FileReader fileReader = new FileReader(fileName);
- BufferedReader reader = new BufferedReader(fileReader)) {
- String line;
- while (null != (line = reader.readLine())) {
- result.append(line);
- }
- }
- return result.toString();
+ URL url =
Objects.requireNonNull(ConvertYamlConfigurationExecutorTest.class.getResource("/expected/export-storage-nodes.json"));
+ return Files.readAllLines(Paths.get(url.toURI())).stream().filter(each
-> !each.startsWith("#") &&
!each.trim().isEmpty()).collect(Collectors.joining(System.lineSeparator()));
}
}