From 5abe94fc7c401208c2413e351bf4744b6fd15e57 Mon Sep 17 00:00:00 2001 From: terrymanu Date: Fri, 4 May 2018 00:08:27 +0800 Subject: [PATCH] for #601: remove FileUtil --- .../io/shardingjdbc/dbtest/StartTest.java | 27 +++++- .../shardingjdbc/dbtest/common/FileUtil.java | 83 ------------------- 2 files changed, 23 insertions(+), 87 deletions(-) delete mode 100644 sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/FileUtil.java diff --git a/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/StartTest.java b/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/StartTest.java index 3795b6ed43c2e..4d3564aefe905 100644 --- a/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/StartTest.java +++ b/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/StartTest.java @@ -18,7 +18,6 @@ package io.shardingjdbc.dbtest; import io.shardingjdbc.dbtest.asserts.AssertEngine; -import io.shardingjdbc.dbtest.common.FileUtil; import io.shardingjdbc.dbtest.config.AnalyzeConfig; import io.shardingjdbc.dbtest.config.bean.AssertDDLDefinition; import io.shardingjdbc.dbtest.config.bean.AssertDMLDefinition; @@ -37,11 +36,17 @@ import org.junit.runners.Parameterized.Parameters; import javax.xml.bind.JAXBException; -import java.io.File; import java.io.IOException; import java.net.URL; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; import java.util.ArrayList; import java.util.Collection; +import java.util.LinkedList; import java.util.List; import static org.junit.Assert.assertNotNull; @@ -66,8 +71,7 @@ public final class StartTest { public static Collection getParameters() throws IOException, JAXBException { URL integrateResources = StartTest.class.getClassLoader().getResource(INTEGRATION_RESOURCES_PATH); assertNotNull(integrateResources); - List paths = FileUtil.getAllFilePaths(new File(integrateResources.getPath()), "assert-", "xml"); - for (String each : paths) { + for (String each : getAssertFiles(integrateResources)) { AssertsDefinition assertsDefinition = AnalyzeConfig.analyze(each); if (StringUtils.isNotBlank(assertsDefinition.getBaseConfig())) { String[] dbs = StringUtils.split(assertsDefinition.getBaseConfig(), ","); @@ -90,6 +94,21 @@ public static Collection getParameters() throws IOException, JAXBExcep return RESULT_ASSERT; } + private static List getAssertFiles(final URL integrateResources) throws IOException { + final List result = new LinkedList<>(); + Files.walkFileTree(Paths.get(integrateResources.getPath()), new SimpleFileVisitor() { + + @Override + public FileVisitResult visitFile(final Path file, final BasicFileAttributes basicFileAttributes) { + if (file.getFileName().toString().startsWith("assert-") && file.getFileName().toString().endsWith(".xml")) { + result.add(file.toFile().getPath()); + } + return FileVisitResult.CONTINUE; + } + }); + return result; + } + private static void collateData(final List result, final String path, final List asserts) { if (asserts == null) { return; diff --git a/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/FileUtil.java b/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/FileUtil.java deleted file mode 100644 index bdba185172803..0000000000000 --- a/sharding-jdbc-core/src/test/java/io/shardingjdbc/dbtest/common/FileUtil.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * Copyright 1999-2015 dangdang.com. - *

- * Licensed 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 io.shardingjdbc.dbtest.common; - -import java.io.File; -import java.util.ArrayList; -import java.util.List; - -public class FileUtil { - - /** - * Get all the files in the directory. - * @param filePath File path - * @param prefixFile File name prefix - * @param suffix File name suffix - * @return All file paths - */ - public static List getAllFilePaths(final File filePath, final String prefixFile, final String suffix) { - List result = new ArrayList<>(); - File[] files = filePath.listFiles(); - if (files == null) { - return result; - } - for (File each : files) { - if (each.isDirectory()) { - getSubFilePaths(each, result, prefixFile, suffix); - } else { - getFiles(prefixFile, suffix, result, each); - - } - } - return result; - } - - private static void getFiles(final String prefixFile, final String suffix, final List filePaths, final File f) { - if (prefixFile != null) { - if (f.getName().startsWith(prefixFile)) { - if (suffix != null) { - if (f.getName().endsWith("." + suffix)) { - filePaths.add(f.getPath()); - } - } else { - filePaths.add(f.getPath()); - } - } - } else { - filePaths.add(f.getPath()); - } - } - - private static List getSubFilePaths(final File filePath, final List filePaths, final String prefixFile, - final String suffix) { - File[] files = filePath.listFiles(); - List result = filePaths; - if (files == null) { - return result; - } - for (File each : files) { - if (each.isDirectory()) { - getSubFilePaths(each, result, prefixFile, suffix); - } else { - getFiles(prefixFile, suffix, result, each); - } - } - return result; - } - -}