diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/CopyMojo.java b/eo-maven-plugin/src/main/java/org/eolang/maven/CopyMojo.java index 5397619001..40270d1cfd 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/CopyMojo.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/CopyMojo.java @@ -65,6 +65,7 @@ public final class CopyMojo extends SafeMojo { /** * Directory in which .eo files are located. + * * @checkstyle MemberNameCheck (7 lines) */ @Parameter( @@ -76,6 +77,7 @@ public final class CopyMojo extends SafeMojo { /** * Target directory with resources to be packaged in JAR. + * * @checkstyle MemberNameCheck (7 lines) */ @Parameter( @@ -87,6 +89,7 @@ public final class CopyMojo extends SafeMojo { /** * The version to use for 0.0.0 replacements. + * * @checkstyle MemberNameCheck (7 lines) */ @Parameter(property = "eo.version", required = true, defaultValue = "${project.version}") diff --git a/eo-maven-plugin/src/main/java/org/eolang/maven/Home.java b/eo-maven-plugin/src/main/java/org/eolang/maven/Home.java index 28c835d840..c25e27a649 100644 --- a/eo-maven-plugin/src/main/java/org/eolang/maven/Home.java +++ b/eo-maven-plugin/src/main/java/org/eolang/maven/Home.java @@ -31,8 +31,10 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import org.cactoos.Bytes; import org.cactoos.Input; import org.cactoos.Text; +import org.cactoos.bytes.BytesOf; import org.cactoos.io.InputOf; import org.cactoos.io.OutputTo; import org.cactoos.io.TeeInput; @@ -43,9 +45,6 @@ * Class for saving and loading files. * * @since 0.27 - * @todo #1105:30min create load function (it has to be able read by path) - * It should be able to load data from file - * We also need to add new unit test */ @SuppressWarnings("PMD.TooManyMethods") public class Home { @@ -63,6 +62,7 @@ public class Home { /** * Ctor. + * * @param path Path */ Home(final Path path) { @@ -71,6 +71,7 @@ public class Home { /** * Saving input. + * * @param input Input * @param path Path to file * @throws IOException If fails @@ -109,6 +110,7 @@ public void save(final Input input, final Path path) throws IOException { /** * Saving string. + * * @param str String * @param path Path to file * @throws IOException If fails @@ -119,6 +121,7 @@ public void save(final String str, final Path path) throws IOException { /** * Saving text. + * * @param txt Text * @param path Path to file * @throws IOException If fails @@ -129,6 +132,7 @@ public void save(final Text txt, final Path path) throws IOException { /** * Saving stream. + * * @param stream Input stream * @param path Path to file * @throws IOException If fails @@ -139,6 +143,7 @@ public void save(final InputStream stream, final Path path) throws IOException { /** * Saving bytes. + * * @param bytes Byte array * @param path Path to file * @throws IOException If fails @@ -149,6 +154,7 @@ public void save(final byte[] bytes, final Path path) throws IOException { /** * Make relative name from path. + * * @param file The path of the file or dir * @return Relative name to CWD */ @@ -167,6 +173,7 @@ public String rel(final Path file) { /** * Check if exists. + * * @param path Path * @return True if exists */ @@ -174,8 +181,21 @@ public boolean exists(final Path path) { return Files.exists(this.path(path)); } + /** + * Load bytes from file by path. + * + * @param path Path to the file + * @return Bytes of file + * @throws IOException if method can't find the file by path or + * if some exception happens during reading the file + */ + public Bytes load(final Path path) throws IOException { + return new BytesOf(Files.readAllBytes(this.path(path))); + } + /** * Path modification. + * * @param path Path * @return Modified path (without bad symbols) */ @@ -183,5 +203,4 @@ private static Path path(final Path path) { final byte[] bytes = path.toString().getBytes(StandardCharsets.UTF_8); return Paths.get(new String(bytes, StandardCharsets.UTF_8)); } - } diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/CopyMojoTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/CopyMojoTest.java index aa30a6cfb0..befc7abb40 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/CopyMojoTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/CopyMojoTest.java @@ -23,9 +23,8 @@ */ package org.eolang.maven; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; import java.nio.file.Path; +import org.cactoos.text.TextOf; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; import org.junit.jupiter.api.Test; @@ -58,7 +57,7 @@ void copiesSources(@TempDir final Path temp) throws Exception { Matchers.is(true) ); MatcherAssert.assertThat( - new String(Files.readAllBytes(out), StandardCharsets.UTF_8), + new TextOf(new Home().load(out)).asString(), Matchers.allOf( Matchers.containsString("+rt foo:"), Matchers.containsString("0.0.0"), diff --git a/eo-maven-plugin/src/test/java/org/eolang/maven/HomeTest.java b/eo-maven-plugin/src/test/java/org/eolang/maven/HomeTest.java index c3135d4b96..93ba7e939e 100644 --- a/eo-maven-plugin/src/test/java/org/eolang/maven/HomeTest.java +++ b/eo-maven-plugin/src/test/java/org/eolang/maven/HomeTest.java @@ -25,13 +25,16 @@ import java.io.IOException; import java.nio.charset.StandardCharsets; +import java.nio.file.NoSuchFileException; import java.nio.file.Path; import java.nio.file.Paths; +import org.cactoos.Bytes; import org.cactoos.text.Randomized; import org.cactoos.text.TextOf; import org.cactoos.text.UncheckedText; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.params.ParameterizedTest; @@ -105,4 +108,23 @@ void existsInDirWithSpecialSymbolsTest(@TempDir final Path temp) throws IOExcept Matchers.is(true) ); } + + @Test + void loadBytesFromExistingFile(@TempDir final Path temp) throws IOException { + final Home home = new Home(); + final String filename = "foo"; + final String content = "bar"; + final Path subfolder = temp.resolve("subfolder").resolve(filename); + home.save(content, subfolder); + final Bytes bytes = home.load(subfolder); + final TextOf text = new TextOf(bytes); + MatcherAssert.assertThat(text, Matchers.equalTo(new TextOf(content))); + } + + @Test + void loadFromAbsentFile(@TempDir final Path temp) { + final Home home = new Home(); + final Path absent = temp.resolve("nonexistent"); + Assertions.assertThrows(NoSuchFileException.class, () -> home.load(absent)); + } }