diff --git a/src/main/java/org/cactoos/io/TeeInput.java b/src/main/java/org/cactoos/io/TeeInput.java index 943cb2f1a6..b7ca5570e6 100644 --- a/src/main/java/org/cactoos/io/TeeInput.java +++ b/src/main/java/org/cactoos/io/TeeInput.java @@ -45,6 +45,9 @@ * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @since 0.1 + * @todo #631:30min This class needs more test cases. Currently, only a set of + * ctors is covered by tests: ctors which use Bytes and byte array as an + * input. All other ctors should be covered too. */ public final class TeeInput implements Input { diff --git a/src/test/java/org/cactoos/io/TeeInputFromByteArrayTest.java b/src/test/java/org/cactoos/io/TeeInputFromByteArrayTest.java new file mode 100644 index 0000000000..6e195b6e6f --- /dev/null +++ b/src/test/java/org/cactoos/io/TeeInputFromByteArrayTest.java @@ -0,0 +1,108 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017-2018 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.io; + +import java.io.File; +import java.nio.charset.StandardCharsets; +import org.cactoos.text.TextOf; +import org.hamcrest.MatcherAssert; +import org.hamcrest.core.IsEqual; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +/** + * Test case for {@link TeeInput}. Cases for ctors which use byte array as an + * input. + * @author Roman Proshin (roman@proshin.org) + * @version $Id$ + * @since 1.0 + * @checkstyle JavadocMethodCheck (100 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (100 lines) + */ +public final class TeeInputFromByteArrayTest { + + /** + * Temporary files generator. + */ + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void copiesFromByteArrayToPath() throws Exception { + final String message = + "Hello, товарищ path äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + final TeeInput input = new TeeInput( + message.getBytes(StandardCharsets.UTF_8), + output.toPath() + ); + MatcherAssert.assertThat( + new TextOf(input).asString(), + new IsEqual<>(message) + ); + MatcherAssert.assertThat( + new TextOf(output).asString(), + new IsEqual<>(message) + ); + } + + @Test + public void copiesFromByteArrayToFile() throws Exception { + final String message = + "Hello, товарищ file äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + final TeeInput input = new TeeInput( + message.getBytes(StandardCharsets.UTF_8), + output + ); + MatcherAssert.assertThat( + new TextOf(input).asString(), + new IsEqual<>(message) + ); + MatcherAssert.assertThat( + new TextOf(output).asString(), + new IsEqual<>(message) + ); + } + + @Test + public void copiesFromByteArrayToOutput() throws Exception { + final String message = + "Hello, товарищ output äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + final TeeInput input = new TeeInput( + message.getBytes(StandardCharsets.UTF_8), + new OutputTo(output) + ); + MatcherAssert.assertThat( + new TextOf(input).asString(), + new IsEqual<>(message) + ); + MatcherAssert.assertThat( + new TextOf(output).asString(), + new IsEqual<>(message) + ); + } +} diff --git a/src/test/java/org/cactoos/io/TeeInputFromBytesTest.java b/src/test/java/org/cactoos/io/TeeInputFromBytesTest.java new file mode 100644 index 0000000000..57b7d3d942 --- /dev/null +++ b/src/test/java/org/cactoos/io/TeeInputFromBytesTest.java @@ -0,0 +1,111 @@ +/** + * The MIT License (MIT) + * + * Copyright (c) 2017-2018 Yegor Bugayenko + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.cactoos.io; + +import java.io.File; +import java.io.IOException; +import org.cactoos.text.TextOf; +import org.hamcrest.MatcherAssert; +import org.hamcrest.core.IsEqual; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +/** + * Test case for {@link TeeInput}. Cases for ctors which use + * {@link org.cactoos.Bytes} as an input. + * @author Roman Proshin (roman@proshin.org) + * @version $Id$ + * @since 1.0 + * @todo #631:30min Create a new Matcher that will compare results of TeeInput + * as well as copied content to the original message. Apply this new matcher + * for this test and for {@link TeeInputFromByteArrayTest}. + * @checkstyle JavadocMethodCheck (100 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (100 lines) + */ +public final class TeeInputFromBytesTest { + + /** + * Temporary files generator. + */ + @Rule + public final TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void copiesFromBytesToPath() throws IOException { + final String message = + "Hello, товарищ path äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + final TeeInput input = new TeeInput( + new BytesOf(message), + output.toPath() + ); + MatcherAssert.assertThat( + new TextOf(input).asString(), + new IsEqual<>(message) + ); + MatcherAssert.assertThat( + new TextOf(output).asString(), + new IsEqual<>(message) + ); + } + + @Test + public void copiesFromBytesToFile() throws IOException { + final String message = + "Hello, товарищ file äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + final TeeInput input = new TeeInput( + new BytesOf(message), + output + ); + MatcherAssert.assertThat( + new TextOf(input).asString(), + new IsEqual<>(message) + ); + MatcherAssert.assertThat( + new TextOf(output).asString(), + new IsEqual<>(message) + ); + } + + @Test + public void copiesFromBytesToOutput() throws IOException { + final String message = + "Hello, товарищ output äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + final TeeInput input = new TeeInput( + new BytesOf(message), + new OutputTo(output) + ); + MatcherAssert.assertThat( + new TextOf(input).asString(), + new IsEqual<>(message) + ); + MatcherAssert.assertThat( + new TextOf(output).asString(), + new IsEqual<>(message) + ); + } +} diff --git a/src/test/java/org/cactoos/io/TeeInputTest.java b/src/test/java/org/cactoos/io/TeeInputTest.java deleted file mode 100644 index 5e93a6dea7..0000000000 --- a/src/test/java/org/cactoos/io/TeeInputTest.java +++ /dev/null @@ -1,95 +0,0 @@ -/** - * The MIT License (MIT) - * - * Copyright (c) 2017-2018 Yegor Bugayenko - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included - * in all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ -package org.cactoos.io; - -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import org.cactoos.matchers.MatcherOf; -import org.cactoos.matchers.TextHasString; -import org.cactoos.text.TextOf; -import org.hamcrest.MatcherAssert; -import org.junit.Test; - -/** - * Test case for {@link TeeInput}. - * @author Yegor Bugayenko (yegor256@gmail.com) - * @version $Id$ - * @since 0.1 - * @checkstyle JavadocMethodCheck (500 lines) - * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) - */ -public final class TeeInputTest { - - @Test - public void copiesContent() { - final ByteArrayOutputStream baos = new ByteArrayOutputStream(); - final String content = "Hello, товарищ!"; - MatcherAssert.assertThat( - "Can't copy Input to Output and return Input", - new TextOf( - new TeeInput( - new InputOf(content), - new OutputTo(baos) - ) - ), - new TextHasString( - new MatcherOf<>( - str -> { - return new String( - baos.toByteArray(), StandardCharsets.UTF_8 - ).equals(str); - } - ) - ) - ); - } - - @Test - public void copiesToFile() throws IOException { - final Path temp = Files.createTempFile("cactoos", "txt"); - MatcherAssert.assertThat( - "Can't copy Input to File and return content", - new TextOf( - new BytesOf( - new TeeInput("Hello, друг!", temp) - ) - ), - new TextHasString( - new MatcherOf<>( - str -> { - return str.equals( - new String( - Files.readAllBytes(temp), - StandardCharsets.UTF_8 - ) - ); - } - ) - ) - ); - } -}