Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#1246 add Home.load() function #1344

Merged
merged 19 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
63648bf
refactor(#1246): reformat java docs
volodya-lombrozo Oct 18, 2022
e214317
feat(#1246): load file content by path
volodya-lombrozo Oct 18, 2022
c95e3ac
feat*(#1246): remove todo comment
volodya-lombrozo Oct 18, 2022
a1edc29
refactor(#1246): remove emoty lines
volodya-lombrozo Oct 18, 2022
416b0de
refactor(#1246): remove emoty lines
volodya-lombrozo Oct 18, 2022
dd41290
feat(#1246): remove instance of statement
volodya-lombrozo Oct 19, 2022
cb9060a
refactor(#1246): remove blank lines in imports
volodya-lombrozo Oct 19, 2022
b7102f6
refactor(#1246): remove blank lines in imports
volodya-lombrozo Oct 19, 2022
b24ceca
refactor(#1246): fix indentation problems
volodya-lombrozo Oct 19, 2022
8cc63d4
refactor(#1246): fix indentation problems
volodya-lombrozo Oct 19, 2022
c7fc275
refactor(#1246): remove empty lines
volodya-lombrozo Oct 19, 2022
737bfc4
refactor(#1246): fix names of variables
volodya-lombrozo Oct 19, 2022
9d66e83
refactor(#1246): fix names of variables
volodya-lombrozo Oct 19, 2022
a671977
refactor(#1246): add final modifier for exceptions
volodya-lombrozo Oct 19, 2022
36294a4
refactor(#1246): fix remain codestyle problems
volodya-lombrozo Oct 19, 2022
d6ab41a
refactor(#1246): loadBytesFromExistedFile -> loadBytesFromExistingFile
volodya-lombrozo Oct 19, 2022
3c631b0
feat(#1246): use Assertions.assertThrows
volodya-lombrozo Oct 19, 2022
ee5389d
feat(#1246): use load method in CopyMojoTest
volodya-lombrozo Oct 19, 2022
6a2b6fc
refactor(#1246): fix all linter mistakes
volodya-lombrozo Oct 19, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions eo-maven-plugin/src/main/java/org/eolang/maven/CopyMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public final class CopyMojo extends SafeMojo {

/**
* Directory in which .eo files are located.
*
* @checkstyle MemberNameCheck (7 lines)
*/
@Parameter(
Expand All @@ -76,6 +77,7 @@ public final class CopyMojo extends SafeMojo {

/**
* Target directory with resources to be packaged in JAR.
*
* @checkstyle MemberNameCheck (7 lines)
*/
@Parameter(
Expand All @@ -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}")
Expand Down
27 changes: 23 additions & 4 deletions eo-maven-plugin/src/main/java/org/eolang/maven/Home.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -63,6 +62,7 @@ public class Home {

/**
* Ctor.
*
* @param path Path
*/
Home(final Path path) {
Expand All @@ -71,6 +71,7 @@ public class Home {

/**
* Saving input.
*
* @param input Input
* @param path Path to file
* @throws IOException If fails
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
*/
Expand All @@ -167,21 +173,34 @@ public String rel(final Path file) {

/**
* Check if exists.
*
* @param path Path
* @return True if exists
*/
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)
*/
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));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"),
Expand Down
22 changes: 22 additions & 0 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,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;
Expand Down Expand Up @@ -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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@volodya-lombrozo it's better to use Assertions.assertThrows from JUnit 5

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. BTW, can we use JUnit assertions instead of MatcherAssert in orher places?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. BTW, can we use JUnit assertions instead of MatcherAssert in orher places?

@volodya-lombrozo we use them somewhere, but JUnit assertions are way less powerful than Hamcrest.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@volodya-lombrozo For consistency I'd use MatcherAssert everywhere for matching.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mximp Even for exceptions?

final Home home = new Home();
final Path absent = temp.resolve("nonexistent");
Assertions.assertThrows(NoSuchFileException.class, () -> home.load(absent));
}
}