From 881545c45419321d52d42a773a68c8a1d9d27392 Mon Sep 17 00:00:00 2001 From: Roman Proshin Date: Thu, 22 Mar 2018 15:40:37 +0300 Subject: [PATCH] #686: Add more tests for OutputTo --- .../java/org/cactoos/io/OutputToTest.java | 131 +++++++++++++++++- 1 file changed, 124 insertions(+), 7 deletions(-) diff --git a/src/test/java/org/cactoos/io/OutputToTest.java b/src/test/java/org/cactoos/io/OutputToTest.java index 6bfda3134a..74e1c2cfb7 100644 --- a/src/test/java/org/cactoos/io/OutputToTest.java +++ b/src/test/java/org/cactoos/io/OutputToTest.java @@ -24,16 +24,20 @@ 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) @@ -41,32 +45,145 @@ */ 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) + ); + } }