Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Apr 4, 2018
2 parents 6dddbfe + 881545c commit f08a43d
Showing 1 changed file with 124 additions and 7 deletions.
131 changes: 124 additions & 7 deletions src/test/java/org/cactoos/io/OutputToTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,49 +24,166 @@
package org.cactoos.io;

import java.io.IOException;
import java.nio.file.Files;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import org.cactoos.matchers.InputHasContent;
import org.hamcrest.MatcherAssert;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

/**
* Test case for {@link OutputTo}.
*
* @author Yegor Bugayenko (yegor256@gmail.com)
* @author Roman Proshin (roman@proshin.org)
* @version $Id$
* @since 0.15
* @checkstyle JavadocMethodCheck (500 lines)
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
*/
public final class OutputToTest {

/**
* Temporary folder.
*/
@Rule
public TemporaryFolder folder = new TemporaryFolder();

@Test
public void writesSimplePathContent() throws IOException {
final Path temp = Files.createTempDirectory("cactoos-1");
public void writesIntoPath() throws IOException {
final Path temp = this.folder.newFolder("cactoos-1").toPath();
final Path path = temp.resolve("one/two/three/file.txt");
final String content = "Hello, товарищ!";
new LengthOf(new TeeInput(content, new OutputTo(path))).intValue();
MatcherAssert.assertThat(
"Can't write path content",
"Can't write into path",
new InputOf(path),
new InputHasContent(content)
);
}

@Test
public void writesSimpleFileContent() throws IOException {
final Path temp = Files.createTempDirectory("cactoos-2");
public void writesIntoFile() throws IOException {
final Path temp = this.folder.newFolder("cactoos-2").toPath();
final Path path = temp.resolve("a/b/c/file.txt");
final String txt = "Hello, друг!";
new LengthOf(
new TeeInput(txt, new SyncOutput(new OutputTo(path.toFile())))
).intValue();
MatcherAssert.assertThat(
"Can't write file content",
"Can't write into file",
new InputOf(path.toFile()),
new InputHasContent(txt)
);
}

@Test
public void writesIntoWriter() {
final String txt = "Hello, writer!";
final StringWriter output = new StringWriter();
new LengthOf(new TeeInput(txt, new OutputTo(output))).intValue();
MatcherAssert.assertThat(
"Can't write into writer",
new InputOf(output.getBuffer()),
new InputHasContent(txt)
);
}

@Test
public void writesIntoWriterWithCharset() {
final String txt = "Hello, writer with charset!";
final StringWriter output = new StringWriter();
new LengthOf(
new TeeInput(txt, new OutputTo(output, StandardCharsets.UTF_8))
).intValue();
MatcherAssert.assertThat(
"Can't write into writer with charset",
new InputOf(output.getBuffer()),
new InputHasContent(txt)
);
}

@Test
public void writesIntoWriterWithCharsetByName() {
final String txt = "Hello, writer with charset by name!";
final StringWriter output = new StringWriter();
new LengthOf(
new TeeInput(txt, new OutputTo(output, StandardCharsets.UTF_8))
).intValue();
MatcherAssert.assertThat(
"Can't write into writer with charset by name",
new InputOf(output.getBuffer()),
new InputHasContent(txt)
);
}

@Test
public void writesIntoWriterWithCharsetAndSize() {
final String txt = "Hello, writer with charset and size!";
final StringWriter output = new StringWriter();
new LengthOf(
new TeeInput(
txt,
new OutputTo(output, StandardCharsets.UTF_8, 1)
)
).intValue();
MatcherAssert.assertThat(
"Can't write into writer with charset and size",
new InputOf(output.getBuffer()),
new InputHasContent(txt)
);
}

@Test
public void writesIntoWriterWithSize() {
final String txt = "Hello, writer with size!";
final StringWriter output = new StringWriter();
new LengthOf(
new TeeInput(
txt,
new OutputTo(output, 1)
)
).intValue();
MatcherAssert.assertThat(
"Can't write into writer with size",
new InputOf(output.getBuffer()),
new InputHasContent(txt)
);
}

@Test
public void writesIntoWriterWithCharsetByNameAndSize() {
final String txt = "Hello, writer with charset by name and size!";
final StringWriter output = new StringWriter();
new LengthOf(
new TeeInput(
txt,
new OutputTo(output, StandardCharsets.UTF_8.name(), 1)
)
).intValue();
MatcherAssert.assertThat(
"Can't write into writer with charset by name and size",
new InputOf(output.getBuffer()),
new InputHasContent(txt)
);
}

@Test
public void writesIntoWriterWithDecoderAndSize() {
final String txt = "Hello, writer with decoder and size!";
final StringWriter output = new StringWriter();
new LengthOf(
new TeeInput(
txt,
new OutputTo(output, StandardCharsets.UTF_8.newDecoder(), 1)
)
).intValue();
MatcherAssert.assertThat(
"Can't write into writer with decoder and size",
new InputOf(output.getBuffer()),
new InputHasContent(txt)
);
}
}

0 comments on commit f08a43d

Please sign in to comment.