Skip to content

Commit

Permalink
(#939) Refactored AppendTo and AppendToTest
Browse files Browse the repository at this point in the history
  • Loading branch information
igor committed Jan 28, 2019
1 parent ccc8b4c commit 3f50e1e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 21 deletions.
10 changes: 0 additions & 10 deletions src/main/java/org/cactoos/io/AppendTo.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@
package org.cactoos.io;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import org.cactoos.Output;
import org.cactoos.text.FormattedText;

/**
* Output that appends content to a given file.
Expand Down Expand Up @@ -64,14 +62,6 @@ public AppendTo(final File src) {

@Override
public OutputStream stream() throws Exception {
if (!this.source.exists()) {
throw new IOException(
new FormattedText(
"Can't append to %s file. It does not exist",
this.source.getAbsolutePath()
).asString()
);
}
return Files.newOutputStream(
this.source.toPath(), StandardOpenOption.APPEND
);
Expand Down
20 changes: 9 additions & 11 deletions src/test/java/org/cactoos/io/AppendToTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
package org.cactoos.io;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import org.cactoos.text.FormattedText;
import java.nio.file.NoSuchFileException;
import org.cactoos.text.JoinedText;
import org.cactoos.text.RandomText;
import org.junit.Rule;
Expand All @@ -52,21 +51,20 @@ public final class AppendToTest {

/**
* Ensures that AppendTo is failing on a negative predicate result.
* @throws Exception if fails
*/
@Test
public void failsIfFileDoesNotExist() throws Exception {
// @checkstyle MagicNumber (1 line)
final File source = new File(new RandomText(5).asString());
public void failsIfFileDoesNotExist() {
final File source = new File(
new RandomText(
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h'
).asString()
);
new Assertion<>(
"Can't throw exception with proper message",
() -> () -> new AppendTo(source).stream(),
new Throws<>(
new FormattedText(
"Can't append to %s file. It does not exist",
source.getAbsolutePath()
).asString(),
IOException.class
source.getPath(),
NoSuchFileException.class
)
).affirm();
}
Expand Down

0 comments on commit 3f50e1e

Please sign in to comment.