All Downloads are FREE. Search and download functionalities are using the official Maven repository.

tech.kronicle.testutils.TestFileHelper Maven / Gradle / Ivy

Go to download

Kronicle tests-utils library that contains helper classes for testing Kronicle and its plugins

There is a newer version: 0.1.331
Show newest version
package tech.kronicle.testutils;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;

import static java.util.Objects.isNull;

public class TestFileHelper {

    public static String readTestFile(String name, Class type) {
        return readTestFile(getResourcesDir(name, type));
    }

    private static String readTestFile(Path file) {
        try {
            return Files.readString(file);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public static Path getResourcesDir(String name, Class type) {
        return getResourcesDir(type).resolve(name);
    }

    private static Path getResourcesDir(Class type) {
        return getProjectDir()
                .resolve("src/test/resources")
                .resolve(String.join("/", type.getName().split("\\.")));
    }

    public static Path getProjectDir() {
        Path dir = Path.of("").toAbsolutePath();

        while (!Files.exists(dir.resolve("build.gradle"))) {
            dir = dir.getParent();

            if (isNull(dir)) {
                throw new RuntimeException("Cannot find project directory");
            }
        }

        return dir;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy