Skip to content

Commit

Permalink
feat(objectionary#1246): load file content by path
Browse files Browse the repository at this point in the history
  • Loading branch information
volodya-lombrozo committed Oct 18, 2022
1 parent 63648bf commit e214317
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 12 deletions.
14 changes: 14 additions & 0 deletions eo-maven-plugin/src/main/java/org/eolang/maven/Home.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@
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;
Expand Down Expand Up @@ -184,6 +186,17 @@ public boolean exists(final Path path) {
return Files.exists(path(path));
}

/**
* Load bytes from file by path.
*
* @param path Path to the file
* @return Bytes of file
*/
public Bytes load(final Path path) throws IOException {
return new BytesOf(Files.readAllBytes(path(path)));
}


/**
* Path modification.
*
Expand All @@ -195,4 +208,5 @@ private static Path path(final Path path) {
return Paths.get(new String(bytes, StandardCharsets.UTF_8));
}


}
58 changes: 46 additions & 12 deletions eo-maven-plugin/src/test/java/org/eolang/maven/HomeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@

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;
Expand All @@ -51,34 +54,34 @@ void saves(final int size, @TempDir final Path temp) throws IOException {
final String content = new UncheckedText(new Randomized(size)).asString();
new Home().save(content, resolve);
MatcherAssert.assertThat(
new UncheckedText(new TextOf(resolve)).asString(),
Matchers.is(content)
new UncheckedText(new TextOf(resolve)).asString(),
Matchers.is(content)
);
}

@Test
void returnsRelativePathOfCurrentWorkingDirectory(@TempDir final Path temp) {
MatcherAssert.assertThat(
new Home(temp.resolve("dir")).rel(temp.resolve("dir/file.txt")),
Matchers.is("./file.txt")
new Home(temp.resolve("dir")).rel(temp.resolve("dir/file.txt")),
Matchers.is("./file.txt")
);
}

@Test
void existsTest(@TempDir final Path temp) throws IOException {
new Home().save("any content", temp.resolve("file.txt"));
MatcherAssert.assertThat(
new Home().exists(temp.resolve("file.txt")),
Matchers.is(true)
new Home().exists(temp.resolve("file.txt")),
Matchers.is(true)
);
}

@Test
void existsInDirTest(@TempDir final Path temp) throws IOException {
new Home(Paths.get("dir")).save("any content", temp.resolve("file.txt"));
MatcherAssert.assertThat(
new Home(Paths.get("dir")).exists(temp.resolve("file.txt")),
Matchers.is(true)
new Home(Paths.get("dir")).exists(temp.resolve("file.txt")),
Matchers.is(true)
);
}

Expand All @@ -89,8 +92,8 @@ void existsInDirDifferentEncryptionTest(@TempDir final Path temp) throws IOExcep
final String decoded = new String(bytes, StandardCharsets.UTF_16BE);
new Home(Paths.get("directory")).save("any content", temp.resolve(decoded));
MatcherAssert.assertThat(
new Home(Paths.get("directory")).exists(temp.resolve(filename)),
Matchers.is(true)
new Home(Paths.get("directory")).exists(temp.resolve(filename)),
Matchers.is(true)
);
}

Expand All @@ -101,8 +104,39 @@ void existsInDirWithSpecialSymbolsTest(@TempDir final Path temp) throws IOExcept
final String decoded = new String(bytes, "CP1252");
new Home(Paths.get("directory")).save("any content", temp.resolve(decoded));
MatcherAssert.assertThat(
new Home(Paths.get("directory")).exists(temp.resolve(filename)),
Matchers.is(true)
new Home(Paths.get("directory")).exists(temp.resolve(filename)),
Matchers.is(true)
);
}

@Test
void loadsBytesFromExistedFile(@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 actualText = new TextOf(bytes);
final TextOf expectedContent = new TextOf(content);
MatcherAssert.assertThat(actualText, Matchers.equalTo(expectedContent));
}

@Test
void loadFromAbsentFile(@TempDir final Path temp) {
final Home home = new Home();
final Path absentFile = temp.resolve("nonexistent");
try {
home.load(absentFile);
} catch (IOException e) {
final String actual = e.getMessage();
MatcherAssert.assertThat(e instanceof NoSuchFileException, Matchers.is(true));
MatcherAssert.assertThat(actual, Matchers.equalTo(absentFile.toString()));
}
}


}

0 comments on commit e214317

Please sign in to comment.