This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 5789fc2c7d6 Fix the issue of returning a short path when reading a
nested directory. (#30361)
5789fc2c7d6 is described below
commit 5789fc2c7d609f99789cf8b2de76e6b55232ef81
Author: Cong Hu <[email protected]>
AuthorDate: Sat Mar 2 13:33:47 2024 +0800
Fix the issue of returning a short path when reading a nested directory.
(#30361)
---
.../ClasspathResourceDirectoryReader.java | 45 +++++++++-------------
.../ClasspathResourceDirectoryReaderTest.java | 16 ++++++--
.../src/test/resources/yaml/fixture/fixture.yaml | 16 ++++++++
3 files changed, 46 insertions(+), 31 deletions(-)
diff --git
a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/directory/ClasspathResourceDirectoryReader.java
b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/directory/ClasspathResourceDirectoryReader.java
index acf327c1ea5..5f7306f4a27 100644
---
a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/directory/ClasspathResourceDirectoryReader.java
+++
b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/directory/ClasspathResourceDirectoryReader.java
@@ -20,6 +20,7 @@ package org.apache.shardingsphere.infra.util.directory;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
+import java.io.File;
import java.io.IOException;
import java.net.JarURLConnection;
import java.net.URISyntaxException;
@@ -85,7 +86,6 @@ public class ClasspathResourceDirectoryReader {
/**
* Return a lazily populated Stream that contains the names of resources
in the provided directory. The Stream is recursive, meaning it includes
resources from all subdirectories as well.
- *
* <p>When the {@code directory} parameter is a file, the method can still
work.</p>
*
* @param directory directory
@@ -100,7 +100,6 @@ public class ClasspathResourceDirectoryReader {
/**
* Return a lazily populated Stream that contains the names of resources
in the provided directory. The Stream is recursive, meaning it includes
resources from all subdirectories as well.
- *
* <p>When the {@code directory} parameter is a file, the method can still
work.</p>
*
* @param classLoader class loader
@@ -120,7 +119,7 @@ public class ClasspathResourceDirectoryReader {
if (JAR_URL_PROTOCOLS.contains(directoryUrl.getProtocol())) {
return readDirectoryInJar(directory, directoryUrl);
} else {
- return readDirectoryInFileSystem(directoryUrl);
+ return readDirectoryInFileSystem(directory, directoryUrl);
}
});
}
@@ -130,13 +129,7 @@ public class ClasspathResourceDirectoryReader {
if (null == jar) {
return Stream.empty();
}
- return jar.stream().filter(jarEntry ->
jarEntry.getName().startsWith(directory) &&
!jarEntry.isDirectory()).map(JarEntry::getName).onClose(() -> {
- try {
- jar.close();
- } catch (final IOException ex) {
- throw new RuntimeException(ex);
- }
- });
+ return jar.stream().filter(each ->
each.getName().startsWith(directory) &&
!each.isDirectory()).map(JarEntry::getName);
}
@SneakyThrows(IOException.class)
@@ -161,32 +154,30 @@ public class ClasspathResourceDirectoryReader {
* `com.oracle.svm.core.jdk.resources.NativeImageResourceFileSystem` will
be automatically created during the life cycle of the context,
* so additional determination is required.
*
+ * @param directory directory
* @param directoryUrl directory url
* @return stream of resource name
*/
@SneakyThrows({IOException.class, URISyntaxException.class})
- private static Stream<String> readDirectoryInFileSystem(final URL
directoryUrl) {
- if ("resource".equals(directoryUrl.getProtocol())) {
- try {
- return loadFromDirectory(directoryUrl);
- } catch (FileSystemNotFoundException exception) {
- FileSystem nativeImageResourceFileSystem =
FileSystems.newFileSystem(directoryUrl.toURI(), Collections.emptyMap());
- return loadFromDirectory(directoryUrl).onClose(() -> {
- try {
- nativeImageResourceFileSystem.close();
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- });
- }
+ private static Stream<String> readDirectoryInFileSystem(final String
directory, final URL directoryUrl) {
+ try {
+ return loadFromDirectory(directory, directoryUrl);
+ } catch (final FileSystemNotFoundException ignore) {
+ FileSystem fileSystem =
FileSystems.newFileSystem(directoryUrl.toURI(), Collections.emptyMap());
+ return loadFromDirectory(directory, directoryUrl).onClose(() -> {
+ try {
+ fileSystem.close();
+ } catch (final IOException ex) {
+ throw new RuntimeException(ex);
+ }
+ });
}
- return loadFromDirectory(directoryUrl);
}
- private static Stream<String> loadFromDirectory(final URL directoryUrl)
throws URISyntaxException, IOException {
+ private static Stream<String> loadFromDirectory(final String directory,
final URL directoryUrl) throws URISyntaxException, IOException {
Path directoryPath = Paths.get(directoryUrl.toURI());
// noinspection resource
Stream<Path> walkStream = Files.find(directoryPath, Integer.MAX_VALUE,
(path, basicFileAttributes) -> !basicFileAttributes.isDirectory(),
FileVisitOption.FOLLOW_LINKS);
- return walkStream.map(path ->
path.subpath(directoryPath.getNameCount() - 1, path.getNameCount()).toString());
+ return walkStream.map(path -> directory + File.separator +
path.subpath(directoryPath.getNameCount(), path.getNameCount()));
}
}
diff --git
a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/directory/ClasspathResourceDirectoryReaderTest.java
b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/directory/ClasspathResourceDirectoryReaderTest.java
index 0ec67b6c6b0..5364eb7d0ba 100644
---
a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/directory/ClasspathResourceDirectoryReaderTest.java
+++
b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/directory/ClasspathResourceDirectoryReaderTest.java
@@ -32,18 +32,26 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
class ClasspathResourceDirectoryReaderTest {
@Test
- void assertIsDirectory() {
+ void assertIsDirectoryTest() {
assertTrue(ClasspathResourceDirectoryReader.isDirectory("yaml"));
+
assertTrue(ClasspathResourceDirectoryReader.isDirectory("yaml/fixture"));
assertFalse(ClasspathResourceDirectoryReader.isDirectory("yaml/accepted-class.yaml"));
assertFalse(ClasspathResourceDirectoryReader.isDirectory("nonexistent"));
}
@Test
- void read() {
+ void assertReadTest() {
List<String> resourceNameList =
ClasspathResourceDirectoryReader.read("yaml").collect(Collectors.toList());
- assertThat(resourceNameList.size(), is(4));
+ assertThat(resourceNameList.size(), is(5));
final String separator = File.separator;
assertThat(resourceNameList, hasItems("yaml" + separator +
"accepted-class.yaml", "yaml" + separator + "customized-obj.yaml", "yaml" +
separator + "empty-config.yaml",
- "yaml" + separator + "shortcuts-fixture.yaml"));
+ "yaml" + separator + "shortcuts-fixture.yaml",
"yaml/fixture/fixture.yaml"));
+ }
+
+ @Test
+ void assertReadNestedTest() {
+ List<String> resourceNameList =
ClasspathResourceDirectoryReader.read("yaml/fixture").collect(Collectors.toList());
+ assertThat(resourceNameList.size(), is(1));
+ assertThat(resourceNameList, hasItems("yaml/fixture/fixture.yaml"));
}
}
diff --git a/infra/util/src/test/resources/yaml/fixture/fixture.yaml
b/infra/util/src/test/resources/yaml/fixture/fixture.yaml
new file mode 100644
index 00000000000..b1312a0905c
--- /dev/null
+++ b/infra/util/src/test/resources/yaml/fixture/fixture.yaml
@@ -0,0 +1,16 @@
+#
+# 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.
+#