Sammlung von Java-Testcode-Methoden

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

Sammlung von Java-Testcode-Methoden
Java Parallel Code Sample Collection
Java-Methode
Java (Methode)
Java9-Sammlung
Java-Methode
[Java] -Methode
[Java] -Methode
Primzahlbeurteilung Java
Führen Sie Java-Code skriptweise aus
Java-Code-TIPPS
Java-Beispielcode 02
Java-Beispielcode 03
Java8-Methodenreferenz
Java Reintroduction-Java Collection
[Java] forEach-Methode
[Java] Vergleich von Sammlungs- und StringBuilder-Operationsmethoden
Java-Beispielcode 01
Java-Zeichencode
Java8-Methodenreferenz
[Java] Zufällige Methode
[Java] Split-Methode
[Java] Sammlungsframework
[Java-Anfänger] println-Methode ohne Angabe des Sammlungstyps
Abgelaufene Java-Sammlung
Ausführung des RSpec-Testcodes
Fragen zum Java-Sammlungsinterview
Java Learning 2 (Lernen Sie die Berechnungsmethode)
Java-Lernnotiz (Methode)
Informationen zur Bindung von Java-Methoden
Informationen zu Aufteilungsmethoden (Java)
Java 8 studieren (siehe Methode)
Java Unit Test Library-Arterien-Probe
[Lernen / Ausgeben von Testcode]
Java-Programmierung (Klassenmethode)
[Java] Grundlegende Methodenhinweise
Erste Schritte mit Java Collection
[Java] JUnit4-Testfallbeispiel
[Java] Neue Thread-Generierungsmethode (2)
Beispielcode für elektronische Signatur (JAVA)
Über die Entscheidungsbedingung der Java GC-Methode
So füllen Sie den Code mit Qiita aus
Java Silver Lernmethode Memo
Erstellen Sie eine Java-Methode [Memo] [java11]
[Java] Komparator der Collection-Klasse
Schnellstes Primzahl-Beurteilungsprogramm C # Java C ++
2018 Java Proficiency Test für Newcomer-Basics-
[Java Silver] Über gleich Methode
Was ist eine Java-Sammlung?
[Windows] Java-Code ist verstümmelt
[Rails] Testcode mit Rspec
[Java] Implementierungsmethode für die Timer-Verarbeitung
[Java] Zufällige Generierungsmethode (Zufällig)
Java mit Visual Studio Code
Java-Methoden und Methodenüberladungen