Java test code method collection

MyTestUtil.java


package domain.unofficial;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public class MyTestUtil {

    private static Path DELETE_PERMISSION_PATH = Paths.get("d:/tmp");

    public static void cat(final String path) throws IOException {
        readAllLines(path).forEach(System.out::println);
    }

    public static void dir(final String path) throws IOException {
        readPathsInfo(path).forEach(System.out::println);
    }

    public static List<String> readAllLines(final String path) throws IOException {
        return Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8);
    }

    public static List<String> readPathsInfo(final String targetPath) throws IOException {
        List<String> result = new ArrayList<>();
        Path target = Paths.get(targetPath);
        result.add(target.toString());
        for (Path p : Files.walk(target).filter(p -> !p.equals(target)).collect(Collectors.toList())) {
            LocalDateTime time = LocalDateTime.ofInstant(Files.getLastModifiedTime(p).toInstant(),
                    ZoneId.systemDefault());

            String dateTime = time.format(DateTimeFormatter.ISO_LOCAL_DATE_TIME).substring(0, 19);
            String attr = null;
            if (Files.isDirectory(p)) {
                attr = String.format("%-10s", "<DIR>");
            } else {
                attr = String.format("%10s", Long.toString(Files.size(p)));
            }
            String relativePath = target.relativize(p).toString();

            result.add(dateTime + "  " + attr + "  " + relativePath);
        }
        return result;
    }

    public static List<String> readAllPaths(final String path) throws IOException {
        return Files.walk(Paths.get(path)).map(Path::toString).collect(Collectors.toList());
    }

    public static void delete(final String targetPath) throws IOException {
        delete(targetPath, false);
    }

    public static void clear(final String targetPath) throws IOException {
        delete(targetPath, true);
    }

    private static void delete(final String targetPath, boolean isClear) throws IOException {
        Path target = Paths.get(targetPath);
        Predicate<Path> filter = null;
        if (isClear) {
            filter = (p) -> {
                return !p.equals(target);
            };
        } else {
            filter = (p) -> true;
        }
        Files.walk(target).sorted(Comparator.reverseOrder()).filter(filter)
                .filter(p -> p.startsWith(DELETE_PERMISSION_PATH)).map(Path::toFile).forEach(File::delete);
    }

    public static void copy(final String sourcePath, final String destinationPath) throws IOException {
        Path source = Paths.get(sourcePath);
        Path destination = Paths.get(destinationPath);

        if (Files.isRegularFile(source) && Files.isDirectory(destination)) {
            destination = destination.resolve(source.getFileName());
        }

        for (final Path sp : Files.walk(source).collect(Collectors.toList())) {
            Files.copy(sp, destination.resolve(source.relativize(sp)), StandardCopyOption.COPY_ATTRIBUTES);
        }
    }
}

Recommended Posts

Java test code method collection
Java parallelization code sample collection
Java method
java (method)
☾ Java / Collection
Java9 collection
Java method
[Java] method
[Java] method
Primality test Java
Script Java code
Java code TIPS
Java sample code 02
Java sample code 03
Java8 method reference
Java Reintroduction-Java Collection
[Java] forEach method
[Java] Collection and StringBuilder operation method comparison
Efficient test code
Java sample code 01
Java character code
java8 method reference
[Java] Random method
[Java] split method
[Java] Collection framework
[Java beginner] println method without collection type specification
Expired collection of java
RSpec test code execution
Java collection interview questions
Java learning 2 (learning calculation method)
Java learning memo (method)
About Java method binding
[Java ~ Method ~] Study memo (5)
About method splitting (Java)
Studying Java 8 (see method)
Java Unit Test Library-Artery-Sample
[Test code learning / output]
Java programming (class method)
[Java] Basic method notes
About method matchers used in model unit test code (RSpec)
Getting Started with Java Collection
[Java] JUnit4 test case example
[Java] New Thread generation method (2)
Digital signature sample code (JAVA)
Java GC method determination conditions
Code entry method in Qiita
Java Silver Study Method Memo
Create a java method [Memo] [java11]
Memorandum (task: model test code)
[Java] Comparator of Collection class
Fastest Primality Test C # Java C ++
2018 Java Proficiency Test for Newcomers-Basics-
[Java Silver] About equals method
What is a Java collection?
[Windows] Java code is garbled
[Rails] Test code using Rspec
[Java] Timer processing implementation method
[Java] Random number generation method (Random)
Java in Visual Studio Code
Implementation of unit test code
Java methods and method overloads