From e8590a35df0e1bc15430994953e066d4d65c75cd Mon Sep 17 00:00:00 2001 From: Roman Proshin Date: Wed, 21 Feb 2018 01:50:58 +0300 Subject: [PATCH 1/6] #631: TeeInput test class is incomplete --- .../org/cactoos/io/TeeInputPartATest.java | 979 ++++++++++++++++++ .../org/cactoos/io/TeeInputPartBTest.java | 699 +++++++++++++ .../java/org/cactoos/io/TeeInputTest.java | 95 -- 3 files changed, 1678 insertions(+), 95 deletions(-) create mode 100644 src/test/java/org/cactoos/io/TeeInputPartATest.java create mode 100644 src/test/java/org/cactoos/io/TeeInputPartBTest.java delete mode 100644 src/test/java/org/cactoos/io/TeeInputTest.java diff --git a/src/test/java/org/cactoos/io/TeeInputPartATest.java b/src/test/java/org/cactoos/io/TeeInputPartATest.java new file mode 100644 index 0000000000..23cd434758 --- /dev/null +++ b/src/test/java/org/cactoos/io/TeeInputPartATest.java @@ -0,0 +1,979 @@ +/** + * 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 java.util.function.Supplier; +import org.cactoos.text.TextOf; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link TeeInput}. Part A. + * @author Roman Proshin (roman@proshin.org) + * @version $Id$ + * @since 1.0 + * @checkstyle JavadocMethodCheck (1000 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (1000 lines) + */ +@SuppressWarnings( + {"PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals", "PMD.GodClass"} +) +public final class TeeInputPartATest { + + /** + * Test content for all the tests in this class. + */ + private static final String CONTENT = + "Hello, товарищ äÄ üÜ öÖ and ß"; + + @Test + public void copiesFromUrlToPath() throws IOException { + final Path input = Files.createTempFile("input", ".txt"); + Files.write( + input, + TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) + ); + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + input + .toUri() + .toURL(), + output + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromUrlToFile() throws IOException { + final Path input = Files.createTempFile("input", ".txt"); + Files.write( + input, + TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) + ); + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + input + .toUri() + .toURL(), + output.toFile() + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromUrlToOutput() throws IOException { + final Path input = Files.createTempFile("input", ".txt"); + Files.write( + input, + TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) + ); + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + input.toUri() + .toURL(), + new OutputTo(output) + ), + TeeInputPartATest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromUriToPath() throws IOException { + final Path input = Files.createTempFile("input", ".txt"); + Files.write( + input, + TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) + ); + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + input.toUri(), + output + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromUriToFile() throws IOException { + final Path input = Files.createTempFile("input", ".txt"); + Files.write( + input, + TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) + ); + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + input.toUri(), + output.toFile() + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromUriToOutput() throws IOException { + final Path input = Files.createTempFile("input", ".txt"); + Files.write( + input, + TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) + ); + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + input.toUri(), + new OutputTo(output) + ), + TeeInputPartATest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromPathToPath() throws IOException { + final Path input = Files.createTempFile("input", ".txt"); + Files.write( + input, + TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) + ); + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + input, + output + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromPathToFile() throws IOException { + final Path input = Files.createTempFile("input", ".txt"); + Files.write( + input, + TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) + ); + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + input, + output.toFile() + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromPathToOutput() throws IOException { + final Path input = Files.createTempFile("input", ".txt"); + Files.write( + input, + TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) + ); + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + input, + new OutputTo(output) + ), + TeeInputPartATest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromFileToFile() throws IOException { + final Path input = Files.createTempFile("input", ".txt"); + Files.write( + input, + TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) + ); + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + input.toFile(), + output.toFile() + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromFileToPath() throws IOException { + final Path input = Files.createTempFile("input", ".txt"); + Files.write( + input, + TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) + ); + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + input.toFile(), + output + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromFileToOutput() throws IOException { + final Path input = Files.createTempFile("input", ".txt"); + Files.write( + input, + TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) + ); + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + input.toFile(), + new OutputTo(output) + ), + TeeInputPartATest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromCharSequenceToFile() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + TeeInputPartATest.CONTENT, + output.toFile() + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromCharSequenceWithCharsetToFile() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + TeeInputPartATest.CONTENT, + output.toFile(), + StandardCharsets.UTF_8 + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromCharSequenceWithCharsetByNameToFile() + throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + TeeInputPartATest.CONTENT, + output.toFile(), + StandardCharsets.UTF_8.name() + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromCharSequenceToPath() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + TeeInputPartATest.CONTENT, + output + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromCharSequenceWithCharsetToPath() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + TeeInputPartATest.CONTENT, + output, + StandardCharsets.UTF_8 + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromCharSequenceWithCharsetByNameToPath() + throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + TeeInputPartATest.CONTENT, + output, + StandardCharsets.UTF_8.name() + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromCharSequenceToOutput() throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + TeeInputPartATest.CONTENT, + new OutputTo(output) + ), + TeeInputPartATest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromCharSequenceWithCharsetToOutput() throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + TeeInputPartATest.CONTENT, + new OutputTo(output), + StandardCharsets.UTF_8 + ), + TeeInputPartATest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromCharSequenceWithCharsetByNameToOutput() + throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + TeeInputPartATest.CONTENT, + new OutputTo(output), + StandardCharsets.UTF_8.name() + ), + TeeInputPartATest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromReaderToFile() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new ReaderOf(TeeInputPartATest.CONTENT), + output.toFile() + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromReaderWithSizeToFile() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new ReaderOf(TeeInputPartATest.CONTENT), + output.toFile(), + TeeInputPartATest.CONTENT.length() + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromReaderWithCharsetToFile() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new ReaderOf(TeeInputPartATest.CONTENT), + output.toFile(), + StandardCharsets.UTF_8 + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromReaderWithCharsetAndSizeToFile() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new ReaderOf(TeeInputPartATest.CONTENT), + output.toFile(), + StandardCharsets.UTF_8, + TeeInputPartATest.CONTENT.length() + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromReaderWithCharsetByNameToFile() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new ReaderOf(TeeInputPartATest.CONTENT), + output.toFile(), + StandardCharsets.UTF_8.name() + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromReaderWithCharsetByNameAndSizeToFile() + throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new ReaderOf(TeeInputPartATest.CONTENT), + output.toFile(), + StandardCharsets.UTF_8.name(), + TeeInputPartATest.CONTENT.length() + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromReaderToPath() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new ReaderOf(TeeInputPartATest.CONTENT), + output + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromReaderWithSizeToPath() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new ReaderOf(TeeInputPartATest.CONTENT), + output, + TeeInputPartATest.CONTENT.length() + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromReaderWithCharsetToPath() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new ReaderOf(TeeInputPartATest.CONTENT), + output, + StandardCharsets.UTF_8 + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromReaderWithCharsetAndSizeToPath() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new ReaderOf(TeeInputPartATest.CONTENT), + output, + StandardCharsets.UTF_8, + TeeInputPartATest.CONTENT.length() + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromReaderWithCharsetByNameToPath() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new ReaderOf(TeeInputPartATest.CONTENT), + output, + StandardCharsets.UTF_8.name() + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromReaderWithCharsetByNameAndSizeToPath() + throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new ReaderOf(TeeInputPartATest.CONTENT), + output, + StandardCharsets.UTF_8.name(), + TeeInputPartATest.CONTENT.length() + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromReaderToOutput() throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + new ReaderOf(TeeInputPartATest.CONTENT), + new OutputTo(output) + ), + TeeInputPartATest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromReaderWithSizeToOutput() throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + new ReaderOf(TeeInputPartATest.CONTENT), + new OutputTo(output), + TeeInputPartATest.CONTENT.length() + ), + TeeInputPartATest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromReaderWithCharsetToOutput() throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + new ReaderOf(TeeInputPartATest.CONTENT), + new OutputTo(output), + StandardCharsets.UTF_8 + ), + TeeInputPartATest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromReaderWithCharsetAndSizeToOutput() + throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + new ReaderOf(TeeInputPartATest.CONTENT), + new OutputTo(output), + StandardCharsets.UTF_8, + TeeInputPartATest.CONTENT.length() + ), + TeeInputPartATest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromReaderWithCharsetByNameToOutput() throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + new ReaderOf(TeeInputPartATest.CONTENT), + new OutputTo(output), + StandardCharsets.UTF_8.name() + ), + TeeInputPartATest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromReaderWithCharsetByNameAndSizeToOutput() + throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + new ReaderOf(TeeInputPartATest.CONTENT), + new OutputTo(output), + StandardCharsets.UTF_8.name(), + TeeInputPartATest.CONTENT.length() + ), + TeeInputPartATest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromByteArrayToPath() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8), + output + ), + TeeInputPartATest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + /** + * Asserts returned strings of the given TeeInput instance. + * + * @param input Instance of {@link TeeInput} class + * @param result String, that must be returned + * @param copied String, that must be copied into output + * @throws IOException if any IO error occurs + */ + private void assertThat(final TeeInput input, final String result, + final Supplier copied) throws IOException { + final String text = new TextOf(input).asString(); + MatcherAssert.assertThat( + "TeeInput result is different, than the input", + text, + Matchers.is(result) + ); + MatcherAssert.assertThat( + "Copied string is different, than the input string", + text, + Matchers.is(copied.get()) + ); + } +} diff --git a/src/test/java/org/cactoos/io/TeeInputPartBTest.java b/src/test/java/org/cactoos/io/TeeInputPartBTest.java new file mode 100644 index 0000000000..14b005b849 --- /dev/null +++ b/src/test/java/org/cactoos/io/TeeInputPartBTest.java @@ -0,0 +1,699 @@ +/** + * 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 java.util.function.Supplier; +import org.cactoos.text.TextOf; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.junit.Test; + +/** + * Test case for {@link TeeInput}. Part B. + * @author Roman Proshin (roman@proshin.org) + * @version $Id$ + * @since 1.0 + * @checkstyle JavadocMethodCheck (650 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (650 lines) + */ +@SuppressWarnings( + {"PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals", "PMD.GodClass"} +) +public final class TeeInputPartBTest { + + /** + * Test content for all the tests in this class. + */ + private static final String CONTENT = + "Hello, товарищ äÄ üÜ öÖ and ß"; + + @Test + public void copiesFromCharArrayWithCharsetToFile() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + TeeInputPartBTest.CONTENT.toCharArray(), + output.toFile(), + StandardCharsets.UTF_8 + ), + TeeInputPartBTest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromCharArrayWithCharsetByNameToFile() + throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + TeeInputPartBTest.CONTENT.toCharArray(), + output.toFile(), + StandardCharsets.UTF_8.name() + ), + TeeInputPartBTest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromCharArrayToOutput() throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + TeeInputPartBTest.CONTENT.toCharArray(), + new OutputTo(output) + ), + TeeInputPartBTest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromCharArrayWithCharsetToOutput() throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + TeeInputPartBTest.CONTENT.toCharArray(), + new OutputTo(output), + StandardCharsets.UTF_8 + ), + TeeInputPartBTest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromCharArrayWithCharsetByNameToOutput() + throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + TeeInputPartBTest.CONTENT.toCharArray(), + new OutputTo(output), + StandardCharsets.UTF_8.name() + ), + TeeInputPartBTest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromTextToPath() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new TextOf(TeeInputPartBTest.CONTENT), + output + ), + TeeInputPartBTest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromTextWithCharsetToPath() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new TextOf(TeeInputPartBTest.CONTENT), + output, + StandardCharsets.UTF_8 + ), + TeeInputPartBTest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromTextWithCharsetByNameToPath() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new TextOf(TeeInputPartBTest.CONTENT), + output, + StandardCharsets.UTF_8.name() + ), + TeeInputPartBTest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromTextToFile() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new TextOf(TeeInputPartBTest.CONTENT), + output.toFile() + ), + TeeInputPartBTest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromTextWithCharsetToFile() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new TextOf(TeeInputPartBTest.CONTENT), + output.toFile(), + StandardCharsets.UTF_8 + ), + TeeInputPartBTest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromTextWithCharsetByNameToFile() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new TextOf(TeeInputPartBTest.CONTENT), + output.toFile(), + StandardCharsets.UTF_8.name() + ), + TeeInputPartBTest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromTextToOutput() throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + new TextOf(TeeInputPartBTest.CONTENT), + new OutputTo(output) + ), + TeeInputPartBTest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromTextWithCharsetToOutput() throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + new TextOf(TeeInputPartBTest.CONTENT), + new OutputTo(output), + StandardCharsets.UTF_8 + ), + TeeInputPartBTest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromTextWithCharsetByNameToOutput() throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + new TextOf(TeeInputPartBTest.CONTENT), + new OutputTo(output), + StandardCharsets.UTF_8.name() + ), + TeeInputPartBTest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromBytesToPath() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new BytesOf(TeeInputPartBTest.CONTENT), + output + ), + TeeInputPartBTest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromBytesToFile() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new BytesOf(TeeInputPartBTest.CONTENT), + output.toFile() + ), + TeeInputPartBTest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromBytesToOutput() throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + new BytesOf(TeeInputPartBTest.CONTENT), + new OutputTo(output) + ), + TeeInputPartBTest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromInputToPath() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new InputOf(TeeInputPartBTest.CONTENT), + output + ), + TeeInputPartBTest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromInputToFile() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + new InputOf(TeeInputPartBTest.CONTENT), + output.toFile() + ), + TeeInputPartBTest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromInputToWriter() throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + new InputOf(TeeInputPartBTest.CONTENT), + new WriterTo(output) + ), + TeeInputPartBTest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromInputWithSizeToWriter() throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + new InputOf(TeeInputPartBTest.CONTENT), + new WriterTo(output), + TeeInputPartBTest.CONTENT.length() + ), + TeeInputPartBTest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromInputWithCharsetToWriter() throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + new InputOf(TeeInputPartBTest.CONTENT), + new WriterTo(output), + StandardCharsets.UTF_8 + ), + TeeInputPartBTest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromInputWithCharsetAndSizeToWriter() throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + new InputOf(TeeInputPartBTest.CONTENT), + new WriterTo(output), + StandardCharsets.UTF_8, + TeeInputPartBTest.CONTENT.length() + ), + TeeInputPartBTest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromInputWithCharsetByNameToWriter() throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + new InputOf(TeeInputPartBTest.CONTENT), + new WriterTo(output), + StandardCharsets.UTF_8.name() + ), + TeeInputPartBTest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromInputWithCharsetByNameAndSizeToWriter() + throws Exception { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + new InputOf(TeeInputPartBTest.CONTENT), + new WriterTo(output), + StandardCharsets.UTF_8.name(), + TeeInputPartBTest.CONTENT.length() + ), + TeeInputPartBTest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + @Test + public void copiesFromCharArrayToPath() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + TeeInputPartBTest.CONTENT.toCharArray(), + output + ), + TeeInputPartBTest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromCharArrayWithCharsetToPath() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + TeeInputPartBTest.CONTENT.toCharArray(), + output, + StandardCharsets.UTF_8 + ), + TeeInputPartBTest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromCharArrayWithCharsetByNameToPath() + throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + TeeInputPartBTest.CONTENT.toCharArray(), + output, + StandardCharsets.UTF_8.name() + ), + TeeInputPartBTest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromCharArrayToFile() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + TeeInputPartBTest.CONTENT.toCharArray(), + output.toFile() + ), + TeeInputPartBTest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromByteArrayToFile() throws IOException { + final Path output = Files.createTempFile("output", ".txt"); + this.assertThat( + new TeeInput( + TeeInputPartBTest.CONTENT.getBytes(StandardCharsets.UTF_8), + output.toFile() + ), + TeeInputPartBTest.CONTENT, + () -> { + try { + return new TextOf(output).asString(); + } catch (final IOException exception) { + throw new AssertionError( + "There was an IOException", + exception + ); + } + } + ); + } + + @Test + public void copiesFromByteArrayToOutput() throws IOException { + final ByteArrayOutputStream output = new ByteArrayOutputStream(); + this.assertThat( + new TeeInput( + TeeInputPartBTest.CONTENT.getBytes(StandardCharsets.UTF_8), + new OutputTo(output) + ), + TeeInputPartBTest.CONTENT, + () -> new String( + output.toByteArray(), + StandardCharsets.UTF_8 + ) + ); + } + + /** + * Asserts returned strings of the given TeeInput instance. + * + * @param input Instance of {@link TeeInput} class + * @param result String, that must be returned + * @param copied String, that must be copied into output + * @throws IOException if any IO error occurs + */ + private void assertThat(final TeeInput input, final String result, + final Supplier copied) throws IOException { + final String text = new TextOf(input).asString(); + MatcherAssert.assertThat( + "TeeInput result is different, than the input", + text, + Matchers.is(result) + ); + MatcherAssert.assertThat( + "Copied string is different, than the input string", + text, + Matchers.is(copied.get()) + ); + } +} 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 - ) - ); - } - ) - ) - ); - } -} From 7d4cb7cbdf7e1d53b0b2d151bdda123ec7a5c352 Mon Sep 17 00:00:00 2001 From: Roman Proshin Date: Wed, 21 Feb 2018 14:20:59 +0300 Subject: [PATCH 2/6] #631: TeeInput test class is incomplete: add two puzzles --- src/test/java/org/cactoos/io/TeeInputPartATest.java | 5 +++++ src/test/java/org/cactoos/io/TeeInputPartBTest.java | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/src/test/java/org/cactoos/io/TeeInputPartATest.java b/src/test/java/org/cactoos/io/TeeInputPartATest.java index 23cd434758..68068340ae 100644 --- a/src/test/java/org/cactoos/io/TeeInputPartATest.java +++ b/src/test/java/org/cactoos/io/TeeInputPartATest.java @@ -36,6 +36,11 @@ /** * Test case for {@link TeeInput}. Part A. + * + * @todo #631 Split this test class on small ones based on source of the + * TeeInput. I expect a number of small test classes for each source of + * TeeInput (e.g. TeeInputFromUriTest, TeeInputFromPathTest etc.) + * * @author Roman Proshin (roman@proshin.org) * @version $Id$ * @since 1.0 diff --git a/src/test/java/org/cactoos/io/TeeInputPartBTest.java b/src/test/java/org/cactoos/io/TeeInputPartBTest.java index 14b005b849..cdd14e2518 100644 --- a/src/test/java/org/cactoos/io/TeeInputPartBTest.java +++ b/src/test/java/org/cactoos/io/TeeInputPartBTest.java @@ -36,6 +36,11 @@ /** * Test case for {@link TeeInput}. Part B. + * + * @todo #631 Split this test class on small ones based on source of the + * TeeInput. I expect a number of small test classes for each source of + * TeeInput (e.g. TeeInputFromUriTest, TeeInputFromPathTest etc.) + * * @author Roman Proshin (roman@proshin.org) * @version $Id$ * @since 1.0 From 22a653a08a0fe9b251d07c84c8d32e30161eb61f Mon Sep 17 00:00:00 2001 From: Roman Proshin Date: Fri, 23 Feb 2018 00:16:16 +0300 Subject: [PATCH 3/6] #631: TeeInput test class is incomplete --- .../cactoos/matchers/TeeInputHasResult.java | 127 +++ .../cactoos/io/TeeInputFromByteArrayTest.java | 102 ++ .../org/cactoos/io/TeeInputFromBytesTest.java | 102 ++ .../cactoos/io/TeeInputFromCharArrayTest.java | 214 ++++ .../io/TeeInputFromCharSequenceTest.java | 214 ++++ .../org/cactoos/io/TeeInputFromFileTest.java | 118 +++ .../org/cactoos/io/TeeInputFromInputTest.java | 196 ++++ .../org/cactoos/io/TeeInputFromPathTest.java | 120 +++ .../cactoos/io/TeeInputFromReaderTest.java | 384 +++++++ .../org/cactoos/io/TeeInputFromTextTest.java | 211 ++++ .../org/cactoos/io/TeeInputFromUriTest.java | 119 +++ .../org/cactoos/io/TeeInputFromUrlTest.java | 125 +++ .../org/cactoos/io/TeeInputPartATest.java | 984 ------------------ .../org/cactoos/io/TeeInputPartBTest.java | 704 ------------- 14 files changed, 2032 insertions(+), 1688 deletions(-) create mode 100644 src/main/java/org/cactoos/matchers/TeeInputHasResult.java create mode 100644 src/test/java/org/cactoos/io/TeeInputFromByteArrayTest.java create mode 100644 src/test/java/org/cactoos/io/TeeInputFromBytesTest.java create mode 100644 src/test/java/org/cactoos/io/TeeInputFromCharArrayTest.java create mode 100644 src/test/java/org/cactoos/io/TeeInputFromCharSequenceTest.java create mode 100644 src/test/java/org/cactoos/io/TeeInputFromFileTest.java create mode 100644 src/test/java/org/cactoos/io/TeeInputFromInputTest.java create mode 100644 src/test/java/org/cactoos/io/TeeInputFromPathTest.java create mode 100644 src/test/java/org/cactoos/io/TeeInputFromReaderTest.java create mode 100644 src/test/java/org/cactoos/io/TeeInputFromTextTest.java create mode 100644 src/test/java/org/cactoos/io/TeeInputFromUriTest.java create mode 100644 src/test/java/org/cactoos/io/TeeInputFromUrlTest.java delete mode 100644 src/test/java/org/cactoos/io/TeeInputPartATest.java delete mode 100644 src/test/java/org/cactoos/io/TeeInputPartBTest.java diff --git a/src/main/java/org/cactoos/matchers/TeeInputHasResult.java b/src/main/java/org/cactoos/matchers/TeeInputHasResult.java new file mode 100644 index 0000000000..01482c218b --- /dev/null +++ b/src/main/java/org/cactoos/matchers/TeeInputHasResult.java @@ -0,0 +1,127 @@ +/** + * 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.matchers; + +import org.cactoos.Text; +import org.cactoos.io.TeeInput; +import org.cactoos.text.ComparableText; +import org.cactoos.text.FormattedText; +import org.cactoos.text.TextOf; +import org.cactoos.text.UncheckedText; +import org.hamcrest.Description; +import org.hamcrest.TypeSafeMatcher; + +/** + * Matcher for the {@link TeeInput}'s results. + * + * @author Roman Proshin (roman@proshin.org) + * @version $Id$ + * @since 1.0 + */ +public final class TeeInputHasResult extends TypeSafeMatcher { + + /** + * Format for matcher description. + */ + private static final String DESCRIPTION = + "TeeInput with result '%s' and copied value '%s'"; + /** + * Value that is expected to be returned from the given TeeInput. + */ + private final ComparableText expected; + /** + * Value that is expected to be copied into the output of TeeInput. + */ + private final ComparableText copied; + /** + * Actual value returned from the given TeeInput. + */ + private ComparableText actual; + + /** + * Ctor. + * @param expected Value that is expected to be returned from the TeeInput + * @param copied Value that is expected to be copied into the output of + * the TeeInput + */ + public TeeInputHasResult( + final String expected, + final Text copied) { + this( + new TextOf(expected), + copied + ); + } + + /** + * Ctor. + * @param expected Value that is expected to be returned from the TeeInput + * @param copied Value that is expected to be copied into the output of + * the TeeInput + */ + public TeeInputHasResult( + final Text expected, + final Text copied) { + super(); + this.expected = new ComparableText(expected); + this.copied = new ComparableText(copied); + this.actual = new ComparableText(new TextOf("")); + } + + @Override + public boolean matchesSafely(final TeeInput item) { + this.actual = new ComparableText(new TextOf(item)); + return + this.expected.compareTo(this.actual) == 0 + && this.expected.compareTo(this.copied) == 0; + } + + @Override + public void describeTo(final Description description) { + description.appendText( + new UncheckedText( + new FormattedText( + TeeInputHasResult.DESCRIPTION, + new UncheckedText(this.expected).asString(), + new UncheckedText(this.expected).asString() + ) + ).asString() + ); + } + + @Override + public void describeMismatchSafely( + final TeeInput item, + final Description description) { + description.appendText( + new UncheckedText( + new FormattedText( + TeeInputHasResult.DESCRIPTION, + new UncheckedText(this.actual).asString(), + new UncheckedText(this.copied).asString() + ) + ).asString() + ); + } +} 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..d32ad6aedb --- /dev/null +++ b/src/test/java/org/cactoos/io/TeeInputFromByteArrayTest.java @@ -0,0 +1,102 @@ +/** + * 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.matchers.TeeInputHasResult; +import org.cactoos.text.TextOf; +import org.hamcrest.MatcherAssert; +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 input = + "Hello, товарищ path äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input.getBytes(StandardCharsets.UTF_8), + output.toPath() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromByteArrayToFile() throws Exception { + final String input = + "Hello, товарищ file äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input.getBytes(StandardCharsets.UTF_8), + output + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromByteArrayToOutput() throws Exception { + final String input = + "Hello, товарищ output äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input.getBytes(StandardCharsets.UTF_8), + new OutputTo(output) + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } +} 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..40640e2ccd --- /dev/null +++ b/src/test/java/org/cactoos/io/TeeInputFromBytesTest.java @@ -0,0 +1,102 @@ +/** + * 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.matchers.TeeInputHasResult; +import org.cactoos.text.TextOf; +import org.hamcrest.MatcherAssert; +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 + * @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 input = + "Hello, товарищ path äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new BytesOf(input), + output.toPath() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromBytesToFile() throws IOException { + final String input = + "Hello, товарищ file äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new BytesOf(input), + output + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromBytesToOutput() throws IOException { + final String input = + "Hello, товарищ output äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new BytesOf(input), + new OutputTo(output) + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } +} diff --git a/src/test/java/org/cactoos/io/TeeInputFromCharArrayTest.java b/src/test/java/org/cactoos/io/TeeInputFromCharArrayTest.java new file mode 100644 index 0000000000..3da56cb1f1 --- /dev/null +++ b/src/test/java/org/cactoos/io/TeeInputFromCharArrayTest.java @@ -0,0 +1,214 @@ +/** + * 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 java.nio.charset.StandardCharsets; +import org.cactoos.matchers.TeeInputHasResult; +import org.cactoos.text.TextOf; +import org.hamcrest.MatcherAssert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +/** + * Test case for {@link TeeInput}. Cases for ctors which use char array as an + * input. + * @author Roman Proshin (roman@proshin.org) + * @version $Id$ + * @since 1.0 + * @checkstyle JavadocMethodCheck (215 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (215 lines) + */ +public final class TeeInputFromCharArrayTest { + + /** + * Temporary files generator. + */ + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void copiesFromCharArrayWithCharsetToFile() throws IOException { + final String input = + "Hello, товарищ file #1 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input.toCharArray(), + output, + StandardCharsets.UTF_8 + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromCharArrayWithCharsetByNameToFile() + throws IOException { + final String input = + "Hello, товарищ file #2 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input.toCharArray(), + output, + StandardCharsets.UTF_8.name() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromCharArrayToOutput() throws IOException { + final String input = + "Hello, товарищ output #1 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input.toCharArray(), + new OutputTo(output) + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromCharArrayWithCharsetToOutput() throws IOException { + final String input = + "Hello, товарищ output #2 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input.toCharArray(), + new OutputTo(output), + StandardCharsets.UTF_8 + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromCharArrayWithCharsetByNameToOutput() + throws IOException { + final String input = + "Hello, товарищ output #3 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input.toCharArray(), + new OutputTo(output), + StandardCharsets.UTF_8.name() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromCharArrayToPath() throws IOException { + final String input = + "Hello, товарищ path #1 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input.toCharArray(), + output.toPath() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromCharArrayWithCharsetToPath() throws IOException { + final String input = + "Hello, товарищ path #2 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input.toCharArray(), + output.toPath(), + StandardCharsets.UTF_8 + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromCharArrayWithCharsetByNameToPath() + throws IOException { + final String input = + "Hello, товарищ path #3 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input.toCharArray(), + output.toPath(), + StandardCharsets.UTF_8.name() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromCharArrayToFile() throws IOException { + final File output = this.folder.newFile(); + final String input = + "Hello, товарищ file äÄ üÜ öÖ and ß"; + MatcherAssert.assertThat( + new TeeInput( + input.toCharArray(), + output + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } +} diff --git a/src/test/java/org/cactoos/io/TeeInputFromCharSequenceTest.java b/src/test/java/org/cactoos/io/TeeInputFromCharSequenceTest.java new file mode 100644 index 0000000000..31539c584a --- /dev/null +++ b/src/test/java/org/cactoos/io/TeeInputFromCharSequenceTest.java @@ -0,0 +1,214 @@ +/** + * 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 java.nio.charset.StandardCharsets; +import org.cactoos.matchers.TeeInputHasResult; +import org.cactoos.text.TextOf; +import org.hamcrest.MatcherAssert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +/** + * Test case for {@link TeeInput}. Cases for ctors which use char sequence as + * an input. + * @author Roman Proshin (roman@proshin.org) + * @version $Id$ + * @since 1.0 + * @checkstyle JavadocMethodCheck (215 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (215 lines) + */ +public final class TeeInputFromCharSequenceTest { + + /** + * Temporary files generator. + */ + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void copiesFromCharSequenceToFile() throws IOException { + final String input = + "Hello, товарищ file #1 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input, + output + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromCharSequenceWithCharsetToFile() throws IOException { + final String input = + "Hello, товарищ file #2 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input, + output, + StandardCharsets.UTF_8 + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromCharSequenceWithCharsetByNameToFile() + throws IOException { + final String input = + "Hello, товарищ file #3 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input, + output, + StandardCharsets.UTF_8.name() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromCharSequenceToPath() throws IOException { + final String input = + "Hello, товарищ path #1 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input, + output + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromCharSequenceWithCharsetToPath() throws IOException { + final String input = + "Hello, товарищ path #2 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input, + output.toPath(), + StandardCharsets.UTF_8 + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromCharSequenceWithCharsetByNameToPath() + throws IOException { + final String input = + "Hello, товарищ path #3 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input, + output.toPath(), + StandardCharsets.UTF_8.name() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromCharSequenceToOutput() throws IOException { + final String input = + "Hello, товарищ output #1 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input, + new OutputTo(output) + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromCharSequenceWithCharsetToOutput() throws IOException { + final String input = + "Hello, товарищ output #2 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input, + new OutputTo(output), + StandardCharsets.UTF_8 + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromCharSequenceWithCharsetByNameToOutput() + throws IOException { + final String input = + "Hello, товарищ output #3 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input, + new OutputTo(output), + StandardCharsets.UTF_8.name() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } +} diff --git a/src/test/java/org/cactoos/io/TeeInputFromFileTest.java b/src/test/java/org/cactoos/io/TeeInputFromFileTest.java new file mode 100644 index 0000000000..72e3b3da0c --- /dev/null +++ b/src/test/java/org/cactoos/io/TeeInputFromFileTest.java @@ -0,0 +1,118 @@ +/** + * 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 java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import org.cactoos.matchers.TeeInputHasResult; +import org.cactoos.text.TextOf; +import org.hamcrest.MatcherAssert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +/** + * Test case for {@link TeeInput}. Cases for ctors which use file as an input. + * @author Roman Proshin (roman@proshin.org) + * @version $Id$ + * @since 1.0 + * @checkstyle JavadocMethodCheck (120 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (120 lines) + */ +public final class TeeInputFromFileTest { + + /** + * Temporary files generator. + */ + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void copiesFromFileToFile() throws IOException { + final String message = + "Hello, товарищ file #1 äÄ üÜ öÖ and ß"; + final File input = this.folder.newFile(); + Files.write( + input.toPath(), + message.getBytes(StandardCharsets.UTF_8) + ); + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input, + output + ), + new TeeInputHasResult( + message, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromFileToPath() throws IOException { + final String message = + "Hello, товарищ path #1 äÄ üÜ öÖ and ß"; + final File input = this.folder.newFile(); + Files.write( + input.toPath(), + message.getBytes(StandardCharsets.UTF_8) + ); + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input, + output.toPath() + ), + new TeeInputHasResult( + message, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromFileToOutput() throws IOException { + final String message = + "Hello, товарищ output #1 äÄ üÜ öÖ and ß"; + final File input = this.folder.newFile(); + Files.write( + input.toPath(), + message.getBytes(StandardCharsets.UTF_8) + ); + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input, + new OutputTo(output) + ), + new TeeInputHasResult( + message, + new TextOf(output) + ) + ); + } +} diff --git a/src/test/java/org/cactoos/io/TeeInputFromInputTest.java b/src/test/java/org/cactoos/io/TeeInputFromInputTest.java new file mode 100644 index 0000000000..9a719324d1 --- /dev/null +++ b/src/test/java/org/cactoos/io/TeeInputFromInputTest.java @@ -0,0 +1,196 @@ +/** + * 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 java.nio.charset.StandardCharsets; +import org.cactoos.matchers.TeeInputHasResult; +import org.cactoos.text.TextOf; +import org.hamcrest.MatcherAssert; +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.Input} as an input. + * @author Roman Proshin (roman@proshin.org) + * @version $Id$ + * @since 1.0 + * @checkstyle JavadocMethodCheck (200 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (200 lines) + */ +public final class TeeInputFromInputTest { + + /** + * Temporary files generator. + */ + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void copiesFromInputToPath() throws IOException { + final String input = + "Hello, товарищ path #1 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new InputOf(input), + output.toPath() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromInputToFile() throws IOException { + final String input = + "Hello, товарищ file #1 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new InputOf(input), + output + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromInputToWriter() throws IOException { + final String input = + "Hello, товарищ write #1 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new InputOf(input), + new WriterTo(output) + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromInputWithSizeToWriter() throws IOException { + final String input = + "Hello, товарищ writer #2 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new InputOf(input), + new WriterTo(output), + input.length() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromInputWithCharsetToWriter() throws IOException { + final String input = + "Hello, товарищ writer #3 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new InputOf(input), + new WriterTo(output), + StandardCharsets.UTF_8 + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromInputWithCharsetAndSizeToWriter() throws IOException { + final String input = + "Hello, товарищ writer #4 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new InputOf(input), + new WriterTo(output), + StandardCharsets.UTF_8, + input.length() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromInputWithCharsetByNameToWriter() throws IOException { + final String input = + "Hello, товарищ writer #5 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new InputOf(input), + new WriterTo(output), + StandardCharsets.UTF_8.name() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromInputWithCharsetByNameAndSizeToWriter() + throws Exception { + final String input = + "Hello, товарищ writer #6 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new InputOf(input), + new WriterTo(output), + StandardCharsets.UTF_8.name(), + input.length() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } +} diff --git a/src/test/java/org/cactoos/io/TeeInputFromPathTest.java b/src/test/java/org/cactoos/io/TeeInputFromPathTest.java new file mode 100644 index 0000000000..e3716877fb --- /dev/null +++ b/src/test/java/org/cactoos/io/TeeInputFromPathTest.java @@ -0,0 +1,120 @@ +/** + * 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 java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import org.cactoos.matchers.TeeInputHasResult; +import org.cactoos.text.TextOf; +import org.hamcrest.MatcherAssert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +/** + * Test case for {@link TeeInput}. Cases for ctors which use {@link Path} as + * an input. + * @author Roman Proshin (roman@proshin.org) + * @version $Id$ + * @since 1.0 + * @checkstyle JavadocMethodCheck (120 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (120 lines) + */ +public final class TeeInputFromPathTest { + + /** + * Temporary files generator. + */ + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void copiesFromPathToPath() throws IOException { + final String message = + "Hello, товарищ path #1 äÄ üÜ öÖ and ß"; + final File input = this.folder.newFile(); + Files.write( + input.toPath(), + message.getBytes(StandardCharsets.UTF_8) + ); + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input.toPath(), + output.toPath() + ), + new TeeInputHasResult( + message, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromPathToFile() throws IOException { + final String message = + "Hello, товарищ file #1 äÄ üÜ öÖ and ß"; + final File input = this.folder.newFile(); + Files.write( + input.toPath(), + message.getBytes(StandardCharsets.UTF_8) + ); + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input.toPath(), + output + ), + new TeeInputHasResult( + message, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromPathToOutput() throws IOException { + final String message = + "Hello, товарищ output #1 äÄ üÜ öÖ and ß"; + final File input = this.folder.newFile(); + Files.write( + input.toPath(), + message.getBytes(StandardCharsets.UTF_8) + ); + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input.toPath(), + new OutputTo(output) + ), + new TeeInputHasResult( + message, + new TextOf(output) + ) + ); + } +} diff --git a/src/test/java/org/cactoos/io/TeeInputFromReaderTest.java b/src/test/java/org/cactoos/io/TeeInputFromReaderTest.java new file mode 100644 index 0000000000..946048c629 --- /dev/null +++ b/src/test/java/org/cactoos/io/TeeInputFromReaderTest.java @@ -0,0 +1,384 @@ +/** + * 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 java.nio.charset.StandardCharsets; +import org.cactoos.matchers.TeeInputHasResult; +import org.cactoos.text.TextOf; +import org.hamcrest.MatcherAssert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +/** + * Test case for {@link TeeInput}. Cases for ctors which use + * {@link java.io.Reader} as an input. + * @author Roman Proshin (roman@proshin.org) + * @version $Id$ + * @since 1.0 + * @checkstyle JavadocMethodCheck (400 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (400 lines) + */ +@SuppressWarnings("PMD.TooManyMethods") +public final class TeeInputFromReaderTest { + + /** + * Temporary files generator. + */ + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void copiesFromReaderToFile() throws IOException { + final String input = + "Hello, товарищ file #1 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new ReaderOf(input), + output + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromReaderWithSizeToFile() throws IOException { + final String input = + "Hello, товарищ file #2 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new ReaderOf(input), + output, + input.length() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromReaderWithCharsetToFile() throws IOException { + final String input = + "Hello, товарищ file #3 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new ReaderOf(input), + output, + StandardCharsets.UTF_8 + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromReaderWithCharsetAndSizeToFile() throws IOException { + final String input = + "Hello, товарищ file #4 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new ReaderOf(input), + output, + StandardCharsets.UTF_8, + input.length() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromReaderWithCharsetByNameToFile() throws IOException { + final String input = + "Hello, товарищ file #5 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new ReaderOf(input), + output, + StandardCharsets.UTF_8.name() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromReaderWithCharsetByNameAndSizeToFile() + throws IOException { + final String input = + "Hello, товарищ file #6 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new ReaderOf(input), + output, + StandardCharsets.UTF_8.name(), + input.length() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromReaderToPath() throws IOException { + final String input = + "Hello, товарищ path #1 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new ReaderOf(input), + output.toPath() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromReaderWithSizeToPath() throws IOException { + final String input = + "Hello, товарищ path #2 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new ReaderOf(input), + output.toPath(), + input.length() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromReaderWithCharsetToPath() throws IOException { + final String input = + "Hello, товарищ path #3 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new ReaderOf(input), + output.toPath(), + StandardCharsets.UTF_8 + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromReaderWithCharsetAndSizeToPath() throws IOException { + final String input = + "Hello, товарищ path #4 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new ReaderOf(input), + output.toPath(), + StandardCharsets.UTF_8, + input.length() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromReaderWithCharsetByNameToPath() throws IOException { + final String input = + "Hello, товарищ path #5 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new ReaderOf(input), + output.toPath(), + StandardCharsets.UTF_8.name() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromReaderWithCharsetByNameAndSizeToPath() + throws IOException { + final String input = + "Hello, товарищ path #6 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new ReaderOf(input), + output.toPath(), + StandardCharsets.UTF_8.name(), + input.length() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromReaderToOutput() throws IOException { + final String input = + "Hello, товарищ output #1 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new ReaderOf(input), + new OutputTo(output) + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromReaderWithSizeToOutput() throws IOException { + final String input = + "Hello, товарищ output #2 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new ReaderOf(input), + new OutputTo(output), + input.length() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromReaderWithCharsetToOutput() throws IOException { + final String input = + "Hello, товарищ output #3 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new ReaderOf(input), + new OutputTo(output), + StandardCharsets.UTF_8 + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromReaderWithCharsetAndSizeToOutput() + throws IOException { + final String input = + "Hello, товарищ output #4 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new ReaderOf(input), + new OutputTo(output), + StandardCharsets.UTF_8, + input.length() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromReaderWithCharsetByNameToOutput() throws IOException { + final String input = + "Hello, товарищ output #5 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new ReaderOf(input), + new OutputTo(output), + StandardCharsets.UTF_8.name() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromReaderWithCharsetByNameAndSizeToOutput() + throws IOException { + final String input = + "Hello, товарищ output #6 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new ReaderOf(input), + new OutputTo(output), + StandardCharsets.UTF_8.name(), + input.length() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } +} diff --git a/src/test/java/org/cactoos/io/TeeInputFromTextTest.java b/src/test/java/org/cactoos/io/TeeInputFromTextTest.java new file mode 100644 index 0000000000..6041cac91e --- /dev/null +++ b/src/test/java/org/cactoos/io/TeeInputFromTextTest.java @@ -0,0 +1,211 @@ +/** + * 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 java.nio.charset.StandardCharsets; +import org.cactoos.matchers.TeeInputHasResult; +import org.cactoos.text.TextOf; +import org.hamcrest.MatcherAssert; +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.Text} as an input. + * @author Roman Proshin (roman@proshin.org) + * @version $Id$ + * @since 1.0 + * @checkstyle JavadocMethodCheck (215 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (215 lines) + */ +public final class TeeInputFromTextTest { + + /** + * Temporary files generator. + */ + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void copiesFromTextToPath() throws IOException { + final String input = + "Hello, товарищ path #1 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new TextOf(input), + output.toPath() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromTextWithCharsetToPath() throws IOException { + final String input = + "Hello, товарищ path #2 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new TextOf(input), + output.toPath(), + StandardCharsets.UTF_8 + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromTextWithCharsetByNameToPath() throws IOException { + final String input = + "Hello, товарищ path #3 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new TextOf(input), + output.toPath(), + StandardCharsets.UTF_8.name() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromTextToFile() throws IOException { + final String input = + "Hello, товарищ file #1 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new TextOf(input), + output + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromTextWithCharsetToFile() throws IOException { + final String input = + "Hello, товарищ file #2 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new TextOf(input), + output, + StandardCharsets.UTF_8 + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromTextWithCharsetByNameToFile() throws IOException { + final String input = + "Hello, товарищ file #3 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new TextOf(input), + output, + StandardCharsets.UTF_8.name() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromTextToOutput() throws IOException { + final String input = + "Hello, товарищ output #1 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new TextOf(input), + new OutputTo(output) + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromTextWithCharsetToOutput() throws IOException { + final String input = + "Hello, товарищ output #2 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new TextOf(input), + new OutputTo(output), + StandardCharsets.UTF_8 + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromTextWithCharsetByNameToOutput() throws IOException { + final String input = + "Hello, товарищ output #3 äÄ üÜ öÖ and ß"; + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + new TextOf(input), + new OutputTo(output), + StandardCharsets.UTF_8.name() + ), + new TeeInputHasResult( + input, + new TextOf(output) + ) + ); + } +} diff --git a/src/test/java/org/cactoos/io/TeeInputFromUriTest.java b/src/test/java/org/cactoos/io/TeeInputFromUriTest.java new file mode 100644 index 0000000000..8062fc318b --- /dev/null +++ b/src/test/java/org/cactoos/io/TeeInputFromUriTest.java @@ -0,0 +1,119 @@ +/** + * 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 java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import org.cactoos.matchers.TeeInputHasResult; +import org.cactoos.text.TextOf; +import org.hamcrest.MatcherAssert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +/** + * Test case for {@link TeeInput}. Cases for ctors which use + * {@link java.net.URI} as an input. + * @author Roman Proshin (roman@proshin.org) + * @version $Id$ + * @since 1.0 + * @checkstyle JavadocMethodCheck (120 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (120 lines) + */ +public final class TeeInputFromUriTest { + + /** + * Temporary files generator. + */ + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void copiesFromUriToPath() throws IOException { + final String message = + "Hello, товарищ path #1 äÄ üÜ öÖ and ß"; + final File input = this.folder.newFile(); + Files.write( + input.toPath(), + message.getBytes(StandardCharsets.UTF_8) + ); + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input.toURI(), + output + ), + new TeeInputHasResult( + message, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromUriToFile() throws IOException { + final String message = + "Hello, товарищ file #1 äÄ üÜ öÖ and ß"; + final File input = this.folder.newFile(); + Files.write( + input.toPath(), + message.getBytes(StandardCharsets.UTF_8) + ); + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input.toURI(), + output + ), + new TeeInputHasResult( + message, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromUriToOutput() throws IOException { + final String message = + "Hello, товарищ output #1 äÄ üÜ öÖ and ß"; + final File input = this.folder.newFile(); + Files.write( + input.toPath(), + message.getBytes(StandardCharsets.UTF_8) + ); + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input.toURI(), + new OutputTo(output) + ), + new TeeInputHasResult( + message, + new TextOf(output) + ) + ); + } +} diff --git a/src/test/java/org/cactoos/io/TeeInputFromUrlTest.java b/src/test/java/org/cactoos/io/TeeInputFromUrlTest.java new file mode 100644 index 0000000000..953154a1db --- /dev/null +++ b/src/test/java/org/cactoos/io/TeeInputFromUrlTest.java @@ -0,0 +1,125 @@ +/** + * 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 java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import org.cactoos.matchers.TeeInputHasResult; +import org.cactoos.text.TextOf; +import org.hamcrest.MatcherAssert; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +/** + * Test case for {@link TeeInput}. Cases for ctors which use + * {@link java.net.URL} as an input. + * @author Roman Proshin (roman@proshin.org) + * @version $Id$ + * @since 1.0 + * @checkstyle JavadocMethodCheck (125 lines) + * @checkstyle ClassDataAbstractionCouplingCheck (125 lines) + */ +public final class TeeInputFromUrlTest { + + /** + * Temporary files generator. + */ + @Rule + public TemporaryFolder folder = new TemporaryFolder(); + + @Test + public void copiesFromUrlToPath() throws IOException { + final String message = + "Hello, товарищ path #1 äÄ üÜ öÖ and ß"; + final File input = this.folder.newFile(); + Files.write( + input.toPath(), + message.getBytes(StandardCharsets.UTF_8) + ); + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input + .toURI() + .toURL(), + output.toPath() + ), + new TeeInputHasResult( + message, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromUrlToFile() throws IOException { + final String message = + "Hello, товарищ file #1 äÄ üÜ öÖ and ß"; + final File input = this.folder.newFile(); + Files.write( + input.toPath(), + message.getBytes(StandardCharsets.UTF_8) + ); + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input + .toURI() + .toURL(), + output + ), + new TeeInputHasResult( + message, + new TextOf(output) + ) + ); + } + + @Test + public void copiesFromUrlToOutput() throws IOException { + final String message = + "Hello, товарищ output #1 äÄ üÜ öÖ and ß"; + final File input = this.folder.newFile(); + Files.write( + input.toPath(), + message.getBytes(StandardCharsets.UTF_8) + ); + final File output = this.folder.newFile(); + MatcherAssert.assertThat( + new TeeInput( + input + .toURI() + .toURL(), + new OutputTo(output) + ), + new TeeInputHasResult( + message, + new TextOf(output) + ) + ); + } +} diff --git a/src/test/java/org/cactoos/io/TeeInputPartATest.java b/src/test/java/org/cactoos/io/TeeInputPartATest.java deleted file mode 100644 index 68068340ae..0000000000 --- a/src/test/java/org/cactoos/io/TeeInputPartATest.java +++ /dev/null @@ -1,984 +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 java.util.function.Supplier; -import org.cactoos.text.TextOf; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.Test; - -/** - * Test case for {@link TeeInput}. Part A. - * - * @todo #631 Split this test class on small ones based on source of the - * TeeInput. I expect a number of small test classes for each source of - * TeeInput (e.g. TeeInputFromUriTest, TeeInputFromPathTest etc.) - * - * @author Roman Proshin (roman@proshin.org) - * @version $Id$ - * @since 1.0 - * @checkstyle JavadocMethodCheck (1000 lines) - * @checkstyle ClassDataAbstractionCouplingCheck (1000 lines) - */ -@SuppressWarnings( - {"PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals", "PMD.GodClass"} -) -public final class TeeInputPartATest { - - /** - * Test content for all the tests in this class. - */ - private static final String CONTENT = - "Hello, товарищ äÄ üÜ öÖ and ß"; - - @Test - public void copiesFromUrlToPath() throws IOException { - final Path input = Files.createTempFile("input", ".txt"); - Files.write( - input, - TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) - ); - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - input - .toUri() - .toURL(), - output - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromUrlToFile() throws IOException { - final Path input = Files.createTempFile("input", ".txt"); - Files.write( - input, - TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) - ); - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - input - .toUri() - .toURL(), - output.toFile() - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromUrlToOutput() throws IOException { - final Path input = Files.createTempFile("input", ".txt"); - Files.write( - input, - TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) - ); - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - input.toUri() - .toURL(), - new OutputTo(output) - ), - TeeInputPartATest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromUriToPath() throws IOException { - final Path input = Files.createTempFile("input", ".txt"); - Files.write( - input, - TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) - ); - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - input.toUri(), - output - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromUriToFile() throws IOException { - final Path input = Files.createTempFile("input", ".txt"); - Files.write( - input, - TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) - ); - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - input.toUri(), - output.toFile() - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromUriToOutput() throws IOException { - final Path input = Files.createTempFile("input", ".txt"); - Files.write( - input, - TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) - ); - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - input.toUri(), - new OutputTo(output) - ), - TeeInputPartATest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromPathToPath() throws IOException { - final Path input = Files.createTempFile("input", ".txt"); - Files.write( - input, - TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) - ); - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - input, - output - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromPathToFile() throws IOException { - final Path input = Files.createTempFile("input", ".txt"); - Files.write( - input, - TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) - ); - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - input, - output.toFile() - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromPathToOutput() throws IOException { - final Path input = Files.createTempFile("input", ".txt"); - Files.write( - input, - TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) - ); - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - input, - new OutputTo(output) - ), - TeeInputPartATest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromFileToFile() throws IOException { - final Path input = Files.createTempFile("input", ".txt"); - Files.write( - input, - TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) - ); - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - input.toFile(), - output.toFile() - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromFileToPath() throws IOException { - final Path input = Files.createTempFile("input", ".txt"); - Files.write( - input, - TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) - ); - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - input.toFile(), - output - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromFileToOutput() throws IOException { - final Path input = Files.createTempFile("input", ".txt"); - Files.write( - input, - TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8) - ); - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - input.toFile(), - new OutputTo(output) - ), - TeeInputPartATest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromCharSequenceToFile() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - TeeInputPartATest.CONTENT, - output.toFile() - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromCharSequenceWithCharsetToFile() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - TeeInputPartATest.CONTENT, - output.toFile(), - StandardCharsets.UTF_8 - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromCharSequenceWithCharsetByNameToFile() - throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - TeeInputPartATest.CONTENT, - output.toFile(), - StandardCharsets.UTF_8.name() - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromCharSequenceToPath() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - TeeInputPartATest.CONTENT, - output - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromCharSequenceWithCharsetToPath() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - TeeInputPartATest.CONTENT, - output, - StandardCharsets.UTF_8 - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromCharSequenceWithCharsetByNameToPath() - throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - TeeInputPartATest.CONTENT, - output, - StandardCharsets.UTF_8.name() - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromCharSequenceToOutput() throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - TeeInputPartATest.CONTENT, - new OutputTo(output) - ), - TeeInputPartATest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromCharSequenceWithCharsetToOutput() throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - TeeInputPartATest.CONTENT, - new OutputTo(output), - StandardCharsets.UTF_8 - ), - TeeInputPartATest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromCharSequenceWithCharsetByNameToOutput() - throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - TeeInputPartATest.CONTENT, - new OutputTo(output), - StandardCharsets.UTF_8.name() - ), - TeeInputPartATest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromReaderToFile() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new ReaderOf(TeeInputPartATest.CONTENT), - output.toFile() - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromReaderWithSizeToFile() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new ReaderOf(TeeInputPartATest.CONTENT), - output.toFile(), - TeeInputPartATest.CONTENT.length() - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromReaderWithCharsetToFile() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new ReaderOf(TeeInputPartATest.CONTENT), - output.toFile(), - StandardCharsets.UTF_8 - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromReaderWithCharsetAndSizeToFile() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new ReaderOf(TeeInputPartATest.CONTENT), - output.toFile(), - StandardCharsets.UTF_8, - TeeInputPartATest.CONTENT.length() - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromReaderWithCharsetByNameToFile() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new ReaderOf(TeeInputPartATest.CONTENT), - output.toFile(), - StandardCharsets.UTF_8.name() - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromReaderWithCharsetByNameAndSizeToFile() - throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new ReaderOf(TeeInputPartATest.CONTENT), - output.toFile(), - StandardCharsets.UTF_8.name(), - TeeInputPartATest.CONTENT.length() - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromReaderToPath() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new ReaderOf(TeeInputPartATest.CONTENT), - output - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromReaderWithSizeToPath() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new ReaderOf(TeeInputPartATest.CONTENT), - output, - TeeInputPartATest.CONTENT.length() - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromReaderWithCharsetToPath() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new ReaderOf(TeeInputPartATest.CONTENT), - output, - StandardCharsets.UTF_8 - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromReaderWithCharsetAndSizeToPath() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new ReaderOf(TeeInputPartATest.CONTENT), - output, - StandardCharsets.UTF_8, - TeeInputPartATest.CONTENT.length() - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromReaderWithCharsetByNameToPath() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new ReaderOf(TeeInputPartATest.CONTENT), - output, - StandardCharsets.UTF_8.name() - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromReaderWithCharsetByNameAndSizeToPath() - throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new ReaderOf(TeeInputPartATest.CONTENT), - output, - StandardCharsets.UTF_8.name(), - TeeInputPartATest.CONTENT.length() - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromReaderToOutput() throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - new ReaderOf(TeeInputPartATest.CONTENT), - new OutputTo(output) - ), - TeeInputPartATest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromReaderWithSizeToOutput() throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - new ReaderOf(TeeInputPartATest.CONTENT), - new OutputTo(output), - TeeInputPartATest.CONTENT.length() - ), - TeeInputPartATest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromReaderWithCharsetToOutput() throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - new ReaderOf(TeeInputPartATest.CONTENT), - new OutputTo(output), - StandardCharsets.UTF_8 - ), - TeeInputPartATest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromReaderWithCharsetAndSizeToOutput() - throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - new ReaderOf(TeeInputPartATest.CONTENT), - new OutputTo(output), - StandardCharsets.UTF_8, - TeeInputPartATest.CONTENT.length() - ), - TeeInputPartATest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromReaderWithCharsetByNameToOutput() throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - new ReaderOf(TeeInputPartATest.CONTENT), - new OutputTo(output), - StandardCharsets.UTF_8.name() - ), - TeeInputPartATest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromReaderWithCharsetByNameAndSizeToOutput() - throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - new ReaderOf(TeeInputPartATest.CONTENT), - new OutputTo(output), - StandardCharsets.UTF_8.name(), - TeeInputPartATest.CONTENT.length() - ), - TeeInputPartATest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromByteArrayToPath() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - TeeInputPartATest.CONTENT.getBytes(StandardCharsets.UTF_8), - output - ), - TeeInputPartATest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - /** - * Asserts returned strings of the given TeeInput instance. - * - * @param input Instance of {@link TeeInput} class - * @param result String, that must be returned - * @param copied String, that must be copied into output - * @throws IOException if any IO error occurs - */ - private void assertThat(final TeeInput input, final String result, - final Supplier copied) throws IOException { - final String text = new TextOf(input).asString(); - MatcherAssert.assertThat( - "TeeInput result is different, than the input", - text, - Matchers.is(result) - ); - MatcherAssert.assertThat( - "Copied string is different, than the input string", - text, - Matchers.is(copied.get()) - ); - } -} diff --git a/src/test/java/org/cactoos/io/TeeInputPartBTest.java b/src/test/java/org/cactoos/io/TeeInputPartBTest.java deleted file mode 100644 index cdd14e2518..0000000000 --- a/src/test/java/org/cactoos/io/TeeInputPartBTest.java +++ /dev/null @@ -1,704 +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 java.util.function.Supplier; -import org.cactoos.text.TextOf; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.Test; - -/** - * Test case for {@link TeeInput}. Part B. - * - * @todo #631 Split this test class on small ones based on source of the - * TeeInput. I expect a number of small test classes for each source of - * TeeInput (e.g. TeeInputFromUriTest, TeeInputFromPathTest etc.) - * - * @author Roman Proshin (roman@proshin.org) - * @version $Id$ - * @since 1.0 - * @checkstyle JavadocMethodCheck (650 lines) - * @checkstyle ClassDataAbstractionCouplingCheck (650 lines) - */ -@SuppressWarnings( - {"PMD.TooManyMethods", "PMD.AvoidDuplicateLiterals", "PMD.GodClass"} -) -public final class TeeInputPartBTest { - - /** - * Test content for all the tests in this class. - */ - private static final String CONTENT = - "Hello, товарищ äÄ üÜ öÖ and ß"; - - @Test - public void copiesFromCharArrayWithCharsetToFile() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - TeeInputPartBTest.CONTENT.toCharArray(), - output.toFile(), - StandardCharsets.UTF_8 - ), - TeeInputPartBTest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromCharArrayWithCharsetByNameToFile() - throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - TeeInputPartBTest.CONTENT.toCharArray(), - output.toFile(), - StandardCharsets.UTF_8.name() - ), - TeeInputPartBTest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromCharArrayToOutput() throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - TeeInputPartBTest.CONTENT.toCharArray(), - new OutputTo(output) - ), - TeeInputPartBTest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromCharArrayWithCharsetToOutput() throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - TeeInputPartBTest.CONTENT.toCharArray(), - new OutputTo(output), - StandardCharsets.UTF_8 - ), - TeeInputPartBTest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromCharArrayWithCharsetByNameToOutput() - throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - TeeInputPartBTest.CONTENT.toCharArray(), - new OutputTo(output), - StandardCharsets.UTF_8.name() - ), - TeeInputPartBTest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromTextToPath() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new TextOf(TeeInputPartBTest.CONTENT), - output - ), - TeeInputPartBTest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromTextWithCharsetToPath() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new TextOf(TeeInputPartBTest.CONTENT), - output, - StandardCharsets.UTF_8 - ), - TeeInputPartBTest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromTextWithCharsetByNameToPath() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new TextOf(TeeInputPartBTest.CONTENT), - output, - StandardCharsets.UTF_8.name() - ), - TeeInputPartBTest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromTextToFile() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new TextOf(TeeInputPartBTest.CONTENT), - output.toFile() - ), - TeeInputPartBTest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromTextWithCharsetToFile() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new TextOf(TeeInputPartBTest.CONTENT), - output.toFile(), - StandardCharsets.UTF_8 - ), - TeeInputPartBTest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromTextWithCharsetByNameToFile() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new TextOf(TeeInputPartBTest.CONTENT), - output.toFile(), - StandardCharsets.UTF_8.name() - ), - TeeInputPartBTest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromTextToOutput() throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - new TextOf(TeeInputPartBTest.CONTENT), - new OutputTo(output) - ), - TeeInputPartBTest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromTextWithCharsetToOutput() throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - new TextOf(TeeInputPartBTest.CONTENT), - new OutputTo(output), - StandardCharsets.UTF_8 - ), - TeeInputPartBTest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromTextWithCharsetByNameToOutput() throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - new TextOf(TeeInputPartBTest.CONTENT), - new OutputTo(output), - StandardCharsets.UTF_8.name() - ), - TeeInputPartBTest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromBytesToPath() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new BytesOf(TeeInputPartBTest.CONTENT), - output - ), - TeeInputPartBTest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromBytesToFile() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new BytesOf(TeeInputPartBTest.CONTENT), - output.toFile() - ), - TeeInputPartBTest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromBytesToOutput() throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - new BytesOf(TeeInputPartBTest.CONTENT), - new OutputTo(output) - ), - TeeInputPartBTest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromInputToPath() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new InputOf(TeeInputPartBTest.CONTENT), - output - ), - TeeInputPartBTest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromInputToFile() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - new InputOf(TeeInputPartBTest.CONTENT), - output.toFile() - ), - TeeInputPartBTest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromInputToWriter() throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - new InputOf(TeeInputPartBTest.CONTENT), - new WriterTo(output) - ), - TeeInputPartBTest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromInputWithSizeToWriter() throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - new InputOf(TeeInputPartBTest.CONTENT), - new WriterTo(output), - TeeInputPartBTest.CONTENT.length() - ), - TeeInputPartBTest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromInputWithCharsetToWriter() throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - new InputOf(TeeInputPartBTest.CONTENT), - new WriterTo(output), - StandardCharsets.UTF_8 - ), - TeeInputPartBTest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromInputWithCharsetAndSizeToWriter() throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - new InputOf(TeeInputPartBTest.CONTENT), - new WriterTo(output), - StandardCharsets.UTF_8, - TeeInputPartBTest.CONTENT.length() - ), - TeeInputPartBTest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromInputWithCharsetByNameToWriter() throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - new InputOf(TeeInputPartBTest.CONTENT), - new WriterTo(output), - StandardCharsets.UTF_8.name() - ), - TeeInputPartBTest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromInputWithCharsetByNameAndSizeToWriter() - throws Exception { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - new InputOf(TeeInputPartBTest.CONTENT), - new WriterTo(output), - StandardCharsets.UTF_8.name(), - TeeInputPartBTest.CONTENT.length() - ), - TeeInputPartBTest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - @Test - public void copiesFromCharArrayToPath() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - TeeInputPartBTest.CONTENT.toCharArray(), - output - ), - TeeInputPartBTest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromCharArrayWithCharsetToPath() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - TeeInputPartBTest.CONTENT.toCharArray(), - output, - StandardCharsets.UTF_8 - ), - TeeInputPartBTest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromCharArrayWithCharsetByNameToPath() - throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - TeeInputPartBTest.CONTENT.toCharArray(), - output, - StandardCharsets.UTF_8.name() - ), - TeeInputPartBTest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromCharArrayToFile() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - TeeInputPartBTest.CONTENT.toCharArray(), - output.toFile() - ), - TeeInputPartBTest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromByteArrayToFile() throws IOException { - final Path output = Files.createTempFile("output", ".txt"); - this.assertThat( - new TeeInput( - TeeInputPartBTest.CONTENT.getBytes(StandardCharsets.UTF_8), - output.toFile() - ), - TeeInputPartBTest.CONTENT, - () -> { - try { - return new TextOf(output).asString(); - } catch (final IOException exception) { - throw new AssertionError( - "There was an IOException", - exception - ); - } - } - ); - } - - @Test - public void copiesFromByteArrayToOutput() throws IOException { - final ByteArrayOutputStream output = new ByteArrayOutputStream(); - this.assertThat( - new TeeInput( - TeeInputPartBTest.CONTENT.getBytes(StandardCharsets.UTF_8), - new OutputTo(output) - ), - TeeInputPartBTest.CONTENT, - () -> new String( - output.toByteArray(), - StandardCharsets.UTF_8 - ) - ); - } - - /** - * Asserts returned strings of the given TeeInput instance. - * - * @param input Instance of {@link TeeInput} class - * @param result String, that must be returned - * @param copied String, that must be copied into output - * @throws IOException if any IO error occurs - */ - private void assertThat(final TeeInput input, final String result, - final Supplier copied) throws IOException { - final String text = new TextOf(input).asString(); - MatcherAssert.assertThat( - "TeeInput result is different, than the input", - text, - Matchers.is(result) - ); - MatcherAssert.assertThat( - "Copied string is different, than the input string", - text, - Matchers.is(copied.get()) - ); - } -} From 16c4fe0ab8233804920d24b015a870777c54e6d9 Mon Sep 17 00:00:00 2001 From: Roman Proshin Date: Fri, 23 Feb 2018 18:38:09 +0300 Subject: [PATCH 4/6] #631: TeeInput test class is incomplete --- src/main/java/org/cactoos/io/TeeInput.java | 3 + .../cactoos/matchers/TeeInputHasResult.java | 127 ------ .../cactoos/io/TeeInputFromByteArrayTest.java | 62 +-- .../org/cactoos/io/TeeInputFromBytesTest.java | 64 +-- .../cactoos/io/TeeInputFromCharArrayTest.java | 214 ---------- .../io/TeeInputFromCharSequenceTest.java | 214 ---------- .../org/cactoos/io/TeeInputFromFileTest.java | 118 ------ .../org/cactoos/io/TeeInputFromInputTest.java | 196 --------- .../org/cactoos/io/TeeInputFromPathTest.java | 120 ------ .../cactoos/io/TeeInputFromReaderTest.java | 384 ------------------ .../org/cactoos/io/TeeInputFromTextTest.java | 211 ---------- .../org/cactoos/io/TeeInputFromUriTest.java | 119 ------ .../org/cactoos/io/TeeInputFromUrlTest.java | 125 ------ 13 files changed, 73 insertions(+), 1884 deletions(-) delete mode 100644 src/main/java/org/cactoos/matchers/TeeInputHasResult.java delete mode 100644 src/test/java/org/cactoos/io/TeeInputFromCharArrayTest.java delete mode 100644 src/test/java/org/cactoos/io/TeeInputFromCharSequenceTest.java delete mode 100644 src/test/java/org/cactoos/io/TeeInputFromFileTest.java delete mode 100644 src/test/java/org/cactoos/io/TeeInputFromInputTest.java delete mode 100644 src/test/java/org/cactoos/io/TeeInputFromPathTest.java delete mode 100644 src/test/java/org/cactoos/io/TeeInputFromReaderTest.java delete mode 100644 src/test/java/org/cactoos/io/TeeInputFromTextTest.java delete mode 100644 src/test/java/org/cactoos/io/TeeInputFromUriTest.java delete mode 100644 src/test/java/org/cactoos/io/TeeInputFromUrlTest.java diff --git a/src/main/java/org/cactoos/io/TeeInput.java b/src/main/java/org/cactoos/io/TeeInput.java index 943cb2f1a6..62c84f80fa 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 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/main/java/org/cactoos/matchers/TeeInputHasResult.java b/src/main/java/org/cactoos/matchers/TeeInputHasResult.java deleted file mode 100644 index 01482c218b..0000000000 --- a/src/main/java/org/cactoos/matchers/TeeInputHasResult.java +++ /dev/null @@ -1,127 +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.matchers; - -import org.cactoos.Text; -import org.cactoos.io.TeeInput; -import org.cactoos.text.ComparableText; -import org.cactoos.text.FormattedText; -import org.cactoos.text.TextOf; -import org.cactoos.text.UncheckedText; -import org.hamcrest.Description; -import org.hamcrest.TypeSafeMatcher; - -/** - * Matcher for the {@link TeeInput}'s results. - * - * @author Roman Proshin (roman@proshin.org) - * @version $Id$ - * @since 1.0 - */ -public final class TeeInputHasResult extends TypeSafeMatcher { - - /** - * Format for matcher description. - */ - private static final String DESCRIPTION = - "TeeInput with result '%s' and copied value '%s'"; - /** - * Value that is expected to be returned from the given TeeInput. - */ - private final ComparableText expected; - /** - * Value that is expected to be copied into the output of TeeInput. - */ - private final ComparableText copied; - /** - * Actual value returned from the given TeeInput. - */ - private ComparableText actual; - - /** - * Ctor. - * @param expected Value that is expected to be returned from the TeeInput - * @param copied Value that is expected to be copied into the output of - * the TeeInput - */ - public TeeInputHasResult( - final String expected, - final Text copied) { - this( - new TextOf(expected), - copied - ); - } - - /** - * Ctor. - * @param expected Value that is expected to be returned from the TeeInput - * @param copied Value that is expected to be copied into the output of - * the TeeInput - */ - public TeeInputHasResult( - final Text expected, - final Text copied) { - super(); - this.expected = new ComparableText(expected); - this.copied = new ComparableText(copied); - this.actual = new ComparableText(new TextOf("")); - } - - @Override - public boolean matchesSafely(final TeeInput item) { - this.actual = new ComparableText(new TextOf(item)); - return - this.expected.compareTo(this.actual) == 0 - && this.expected.compareTo(this.copied) == 0; - } - - @Override - public void describeTo(final Description description) { - description.appendText( - new UncheckedText( - new FormattedText( - TeeInputHasResult.DESCRIPTION, - new UncheckedText(this.expected).asString(), - new UncheckedText(this.expected).asString() - ) - ).asString() - ); - } - - @Override - public void describeMismatchSafely( - final TeeInput item, - final Description description) { - description.appendText( - new UncheckedText( - new FormattedText( - TeeInputHasResult.DESCRIPTION, - new UncheckedText(this.actual).asString(), - new UncheckedText(this.copied).asString() - ) - ).asString() - ); - } -} diff --git a/src/test/java/org/cactoos/io/TeeInputFromByteArrayTest.java b/src/test/java/org/cactoos/io/TeeInputFromByteArrayTest.java index d32ad6aedb..6e195b6e6f 100644 --- a/src/test/java/org/cactoos/io/TeeInputFromByteArrayTest.java +++ b/src/test/java/org/cactoos/io/TeeInputFromByteArrayTest.java @@ -25,9 +25,9 @@ import java.io.File; import java.nio.charset.StandardCharsets; -import org.cactoos.matchers.TeeInputHasResult; 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; @@ -51,52 +51,58 @@ public final class TeeInputFromByteArrayTest { @Test public void copiesFromByteArrayToPath() throws Exception { - final String input = + 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 TeeInput( - input.getBytes(StandardCharsets.UTF_8), - output.toPath() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) + new TextOf(output).asString(), + new IsEqual<>(message) ); } @Test public void copiesFromByteArrayToFile() throws Exception { - final String input = + 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 TeeInput( - input.getBytes(StandardCharsets.UTF_8), - output - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) + new TextOf(input).asString(), + new IsEqual<>(message) + ); + MatcherAssert.assertThat( + new TextOf(output).asString(), + new IsEqual<>(message) ); } @Test public void copiesFromByteArrayToOutput() throws Exception { - final String input = + 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 TeeInput( - input.getBytes(StandardCharsets.UTF_8), - new OutputTo(output) - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) + 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 index 40640e2ccd..811bdadba2 100644 --- a/src/test/java/org/cactoos/io/TeeInputFromBytesTest.java +++ b/src/test/java/org/cactoos/io/TeeInputFromBytesTest.java @@ -25,9 +25,9 @@ import java.io.File; import java.io.IOException; -import org.cactoos.matchers.TeeInputHasResult; 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; @@ -38,6 +38,8 @@ * @author Roman Proshin (roman@proshin.org) * @version $Id$ * @since 1.0 + * @todo #631 Create a new Matcher that will compare results of TeeInput as + * well as copied content to the original message. * @checkstyle JavadocMethodCheck (100 lines) * @checkstyle ClassDataAbstractionCouplingCheck (100 lines) */ @@ -51,52 +53,58 @@ public final class TeeInputFromBytesTest { @Test public void copiesFromBytesToPath() throws IOException { - final String input = + 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 TeeInput( - new BytesOf(input), - output.toPath() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) + new TextOf(output).asString(), + new IsEqual<>(message) ); } @Test public void copiesFromBytesToFile() throws IOException { - final String input = + final String message = "Hello, товарищ file äÄ üÜ öÖ and ß"; final File output = this.folder.newFile(); + final TeeInput input = new TeeInput( + new BytesOf(message), + output + ); MatcherAssert.assertThat( - new TeeInput( - new BytesOf(input), - output - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) + new TextOf(input).asString(), + new IsEqual<>(message) + ); + MatcherAssert.assertThat( + new TextOf(output).asString(), + new IsEqual<>(message) ); } @Test public void copiesFromBytesToOutput() throws IOException { - final String input = + 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 TeeInput( - new BytesOf(input), - new OutputTo(output) - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) + new TextOf(output).asString(), + new IsEqual<>(message) ); } } diff --git a/src/test/java/org/cactoos/io/TeeInputFromCharArrayTest.java b/src/test/java/org/cactoos/io/TeeInputFromCharArrayTest.java deleted file mode 100644 index 3da56cb1f1..0000000000 --- a/src/test/java/org/cactoos/io/TeeInputFromCharArrayTest.java +++ /dev/null @@ -1,214 +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.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import org.cactoos.matchers.TeeInputHasResult; -import org.cactoos.text.TextOf; -import org.hamcrest.MatcherAssert; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; - -/** - * Test case for {@link TeeInput}. Cases for ctors which use char array as an - * input. - * @author Roman Proshin (roman@proshin.org) - * @version $Id$ - * @since 1.0 - * @checkstyle JavadocMethodCheck (215 lines) - * @checkstyle ClassDataAbstractionCouplingCheck (215 lines) - */ -public final class TeeInputFromCharArrayTest { - - /** - * Temporary files generator. - */ - @Rule - public TemporaryFolder folder = new TemporaryFolder(); - - @Test - public void copiesFromCharArrayWithCharsetToFile() throws IOException { - final String input = - "Hello, товарищ file #1 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input.toCharArray(), - output, - StandardCharsets.UTF_8 - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromCharArrayWithCharsetByNameToFile() - throws IOException { - final String input = - "Hello, товарищ file #2 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input.toCharArray(), - output, - StandardCharsets.UTF_8.name() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromCharArrayToOutput() throws IOException { - final String input = - "Hello, товарищ output #1 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input.toCharArray(), - new OutputTo(output) - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromCharArrayWithCharsetToOutput() throws IOException { - final String input = - "Hello, товарищ output #2 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input.toCharArray(), - new OutputTo(output), - StandardCharsets.UTF_8 - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromCharArrayWithCharsetByNameToOutput() - throws IOException { - final String input = - "Hello, товарищ output #3 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input.toCharArray(), - new OutputTo(output), - StandardCharsets.UTF_8.name() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromCharArrayToPath() throws IOException { - final String input = - "Hello, товарищ path #1 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input.toCharArray(), - output.toPath() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromCharArrayWithCharsetToPath() throws IOException { - final String input = - "Hello, товарищ path #2 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input.toCharArray(), - output.toPath(), - StandardCharsets.UTF_8 - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromCharArrayWithCharsetByNameToPath() - throws IOException { - final String input = - "Hello, товарищ path #3 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input.toCharArray(), - output.toPath(), - StandardCharsets.UTF_8.name() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromCharArrayToFile() throws IOException { - final File output = this.folder.newFile(); - final String input = - "Hello, товарищ file äÄ üÜ öÖ and ß"; - MatcherAssert.assertThat( - new TeeInput( - input.toCharArray(), - output - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } -} diff --git a/src/test/java/org/cactoos/io/TeeInputFromCharSequenceTest.java b/src/test/java/org/cactoos/io/TeeInputFromCharSequenceTest.java deleted file mode 100644 index 31539c584a..0000000000 --- a/src/test/java/org/cactoos/io/TeeInputFromCharSequenceTest.java +++ /dev/null @@ -1,214 +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.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import org.cactoos.matchers.TeeInputHasResult; -import org.cactoos.text.TextOf; -import org.hamcrest.MatcherAssert; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; - -/** - * Test case for {@link TeeInput}. Cases for ctors which use char sequence as - * an input. - * @author Roman Proshin (roman@proshin.org) - * @version $Id$ - * @since 1.0 - * @checkstyle JavadocMethodCheck (215 lines) - * @checkstyle ClassDataAbstractionCouplingCheck (215 lines) - */ -public final class TeeInputFromCharSequenceTest { - - /** - * Temporary files generator. - */ - @Rule - public TemporaryFolder folder = new TemporaryFolder(); - - @Test - public void copiesFromCharSequenceToFile() throws IOException { - final String input = - "Hello, товарищ file #1 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input, - output - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromCharSequenceWithCharsetToFile() throws IOException { - final String input = - "Hello, товарищ file #2 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input, - output, - StandardCharsets.UTF_8 - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromCharSequenceWithCharsetByNameToFile() - throws IOException { - final String input = - "Hello, товарищ file #3 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input, - output, - StandardCharsets.UTF_8.name() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromCharSequenceToPath() throws IOException { - final String input = - "Hello, товарищ path #1 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input, - output - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromCharSequenceWithCharsetToPath() throws IOException { - final String input = - "Hello, товарищ path #2 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input, - output.toPath(), - StandardCharsets.UTF_8 - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromCharSequenceWithCharsetByNameToPath() - throws IOException { - final String input = - "Hello, товарищ path #3 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input, - output.toPath(), - StandardCharsets.UTF_8.name() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromCharSequenceToOutput() throws IOException { - final String input = - "Hello, товарищ output #1 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input, - new OutputTo(output) - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromCharSequenceWithCharsetToOutput() throws IOException { - final String input = - "Hello, товарищ output #2 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input, - new OutputTo(output), - StandardCharsets.UTF_8 - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromCharSequenceWithCharsetByNameToOutput() - throws IOException { - final String input = - "Hello, товарищ output #3 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input, - new OutputTo(output), - StandardCharsets.UTF_8.name() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } -} diff --git a/src/test/java/org/cactoos/io/TeeInputFromFileTest.java b/src/test/java/org/cactoos/io/TeeInputFromFileTest.java deleted file mode 100644 index 72e3b3da0c..0000000000 --- a/src/test/java/org/cactoos/io/TeeInputFromFileTest.java +++ /dev/null @@ -1,118 +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.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import org.cactoos.matchers.TeeInputHasResult; -import org.cactoos.text.TextOf; -import org.hamcrest.MatcherAssert; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; - -/** - * Test case for {@link TeeInput}. Cases for ctors which use file as an input. - * @author Roman Proshin (roman@proshin.org) - * @version $Id$ - * @since 1.0 - * @checkstyle JavadocMethodCheck (120 lines) - * @checkstyle ClassDataAbstractionCouplingCheck (120 lines) - */ -public final class TeeInputFromFileTest { - - /** - * Temporary files generator. - */ - @Rule - public TemporaryFolder folder = new TemporaryFolder(); - - @Test - public void copiesFromFileToFile() throws IOException { - final String message = - "Hello, товарищ file #1 äÄ üÜ öÖ and ß"; - final File input = this.folder.newFile(); - Files.write( - input.toPath(), - message.getBytes(StandardCharsets.UTF_8) - ); - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input, - output - ), - new TeeInputHasResult( - message, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromFileToPath() throws IOException { - final String message = - "Hello, товарищ path #1 äÄ üÜ öÖ and ß"; - final File input = this.folder.newFile(); - Files.write( - input.toPath(), - message.getBytes(StandardCharsets.UTF_8) - ); - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input, - output.toPath() - ), - new TeeInputHasResult( - message, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromFileToOutput() throws IOException { - final String message = - "Hello, товарищ output #1 äÄ üÜ öÖ and ß"; - final File input = this.folder.newFile(); - Files.write( - input.toPath(), - message.getBytes(StandardCharsets.UTF_8) - ); - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input, - new OutputTo(output) - ), - new TeeInputHasResult( - message, - new TextOf(output) - ) - ); - } -} diff --git a/src/test/java/org/cactoos/io/TeeInputFromInputTest.java b/src/test/java/org/cactoos/io/TeeInputFromInputTest.java deleted file mode 100644 index 9a719324d1..0000000000 --- a/src/test/java/org/cactoos/io/TeeInputFromInputTest.java +++ /dev/null @@ -1,196 +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.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import org.cactoos.matchers.TeeInputHasResult; -import org.cactoos.text.TextOf; -import org.hamcrest.MatcherAssert; -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.Input} as an input. - * @author Roman Proshin (roman@proshin.org) - * @version $Id$ - * @since 1.0 - * @checkstyle JavadocMethodCheck (200 lines) - * @checkstyle ClassDataAbstractionCouplingCheck (200 lines) - */ -public final class TeeInputFromInputTest { - - /** - * Temporary files generator. - */ - @Rule - public TemporaryFolder folder = new TemporaryFolder(); - - @Test - public void copiesFromInputToPath() throws IOException { - final String input = - "Hello, товарищ path #1 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new InputOf(input), - output.toPath() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromInputToFile() throws IOException { - final String input = - "Hello, товарищ file #1 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new InputOf(input), - output - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromInputToWriter() throws IOException { - final String input = - "Hello, товарищ write #1 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new InputOf(input), - new WriterTo(output) - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromInputWithSizeToWriter() throws IOException { - final String input = - "Hello, товарищ writer #2 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new InputOf(input), - new WriterTo(output), - input.length() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromInputWithCharsetToWriter() throws IOException { - final String input = - "Hello, товарищ writer #3 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new InputOf(input), - new WriterTo(output), - StandardCharsets.UTF_8 - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromInputWithCharsetAndSizeToWriter() throws IOException { - final String input = - "Hello, товарищ writer #4 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new InputOf(input), - new WriterTo(output), - StandardCharsets.UTF_8, - input.length() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromInputWithCharsetByNameToWriter() throws IOException { - final String input = - "Hello, товарищ writer #5 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new InputOf(input), - new WriterTo(output), - StandardCharsets.UTF_8.name() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromInputWithCharsetByNameAndSizeToWriter() - throws Exception { - final String input = - "Hello, товарищ writer #6 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new InputOf(input), - new WriterTo(output), - StandardCharsets.UTF_8.name(), - input.length() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } -} diff --git a/src/test/java/org/cactoos/io/TeeInputFromPathTest.java b/src/test/java/org/cactoos/io/TeeInputFromPathTest.java deleted file mode 100644 index e3716877fb..0000000000 --- a/src/test/java/org/cactoos/io/TeeInputFromPathTest.java +++ /dev/null @@ -1,120 +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.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import org.cactoos.matchers.TeeInputHasResult; -import org.cactoos.text.TextOf; -import org.hamcrest.MatcherAssert; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; - -/** - * Test case for {@link TeeInput}. Cases for ctors which use {@link Path} as - * an input. - * @author Roman Proshin (roman@proshin.org) - * @version $Id$ - * @since 1.0 - * @checkstyle JavadocMethodCheck (120 lines) - * @checkstyle ClassDataAbstractionCouplingCheck (120 lines) - */ -public final class TeeInputFromPathTest { - - /** - * Temporary files generator. - */ - @Rule - public TemporaryFolder folder = new TemporaryFolder(); - - @Test - public void copiesFromPathToPath() throws IOException { - final String message = - "Hello, товарищ path #1 äÄ üÜ öÖ and ß"; - final File input = this.folder.newFile(); - Files.write( - input.toPath(), - message.getBytes(StandardCharsets.UTF_8) - ); - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input.toPath(), - output.toPath() - ), - new TeeInputHasResult( - message, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromPathToFile() throws IOException { - final String message = - "Hello, товарищ file #1 äÄ üÜ öÖ and ß"; - final File input = this.folder.newFile(); - Files.write( - input.toPath(), - message.getBytes(StandardCharsets.UTF_8) - ); - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input.toPath(), - output - ), - new TeeInputHasResult( - message, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromPathToOutput() throws IOException { - final String message = - "Hello, товарищ output #1 äÄ üÜ öÖ and ß"; - final File input = this.folder.newFile(); - Files.write( - input.toPath(), - message.getBytes(StandardCharsets.UTF_8) - ); - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input.toPath(), - new OutputTo(output) - ), - new TeeInputHasResult( - message, - new TextOf(output) - ) - ); - } -} diff --git a/src/test/java/org/cactoos/io/TeeInputFromReaderTest.java b/src/test/java/org/cactoos/io/TeeInputFromReaderTest.java deleted file mode 100644 index 946048c629..0000000000 --- a/src/test/java/org/cactoos/io/TeeInputFromReaderTest.java +++ /dev/null @@ -1,384 +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.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import org.cactoos.matchers.TeeInputHasResult; -import org.cactoos.text.TextOf; -import org.hamcrest.MatcherAssert; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; - -/** - * Test case for {@link TeeInput}. Cases for ctors which use - * {@link java.io.Reader} as an input. - * @author Roman Proshin (roman@proshin.org) - * @version $Id$ - * @since 1.0 - * @checkstyle JavadocMethodCheck (400 lines) - * @checkstyle ClassDataAbstractionCouplingCheck (400 lines) - */ -@SuppressWarnings("PMD.TooManyMethods") -public final class TeeInputFromReaderTest { - - /** - * Temporary files generator. - */ - @Rule - public TemporaryFolder folder = new TemporaryFolder(); - - @Test - public void copiesFromReaderToFile() throws IOException { - final String input = - "Hello, товарищ file #1 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new ReaderOf(input), - output - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromReaderWithSizeToFile() throws IOException { - final String input = - "Hello, товарищ file #2 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new ReaderOf(input), - output, - input.length() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromReaderWithCharsetToFile() throws IOException { - final String input = - "Hello, товарищ file #3 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new ReaderOf(input), - output, - StandardCharsets.UTF_8 - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromReaderWithCharsetAndSizeToFile() throws IOException { - final String input = - "Hello, товарищ file #4 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new ReaderOf(input), - output, - StandardCharsets.UTF_8, - input.length() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromReaderWithCharsetByNameToFile() throws IOException { - final String input = - "Hello, товарищ file #5 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new ReaderOf(input), - output, - StandardCharsets.UTF_8.name() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromReaderWithCharsetByNameAndSizeToFile() - throws IOException { - final String input = - "Hello, товарищ file #6 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new ReaderOf(input), - output, - StandardCharsets.UTF_8.name(), - input.length() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromReaderToPath() throws IOException { - final String input = - "Hello, товарищ path #1 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new ReaderOf(input), - output.toPath() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromReaderWithSizeToPath() throws IOException { - final String input = - "Hello, товарищ path #2 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new ReaderOf(input), - output.toPath(), - input.length() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromReaderWithCharsetToPath() throws IOException { - final String input = - "Hello, товарищ path #3 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new ReaderOf(input), - output.toPath(), - StandardCharsets.UTF_8 - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromReaderWithCharsetAndSizeToPath() throws IOException { - final String input = - "Hello, товарищ path #4 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new ReaderOf(input), - output.toPath(), - StandardCharsets.UTF_8, - input.length() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromReaderWithCharsetByNameToPath() throws IOException { - final String input = - "Hello, товарищ path #5 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new ReaderOf(input), - output.toPath(), - StandardCharsets.UTF_8.name() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromReaderWithCharsetByNameAndSizeToPath() - throws IOException { - final String input = - "Hello, товарищ path #6 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new ReaderOf(input), - output.toPath(), - StandardCharsets.UTF_8.name(), - input.length() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromReaderToOutput() throws IOException { - final String input = - "Hello, товарищ output #1 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new ReaderOf(input), - new OutputTo(output) - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromReaderWithSizeToOutput() throws IOException { - final String input = - "Hello, товарищ output #2 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new ReaderOf(input), - new OutputTo(output), - input.length() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromReaderWithCharsetToOutput() throws IOException { - final String input = - "Hello, товарищ output #3 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new ReaderOf(input), - new OutputTo(output), - StandardCharsets.UTF_8 - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromReaderWithCharsetAndSizeToOutput() - throws IOException { - final String input = - "Hello, товарищ output #4 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new ReaderOf(input), - new OutputTo(output), - StandardCharsets.UTF_8, - input.length() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromReaderWithCharsetByNameToOutput() throws IOException { - final String input = - "Hello, товарищ output #5 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new ReaderOf(input), - new OutputTo(output), - StandardCharsets.UTF_8.name() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromReaderWithCharsetByNameAndSizeToOutput() - throws IOException { - final String input = - "Hello, товарищ output #6 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new ReaderOf(input), - new OutputTo(output), - StandardCharsets.UTF_8.name(), - input.length() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } -} diff --git a/src/test/java/org/cactoos/io/TeeInputFromTextTest.java b/src/test/java/org/cactoos/io/TeeInputFromTextTest.java deleted file mode 100644 index 6041cac91e..0000000000 --- a/src/test/java/org/cactoos/io/TeeInputFromTextTest.java +++ /dev/null @@ -1,211 +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.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import org.cactoos.matchers.TeeInputHasResult; -import org.cactoos.text.TextOf; -import org.hamcrest.MatcherAssert; -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.Text} as an input. - * @author Roman Proshin (roman@proshin.org) - * @version $Id$ - * @since 1.0 - * @checkstyle JavadocMethodCheck (215 lines) - * @checkstyle ClassDataAbstractionCouplingCheck (215 lines) - */ -public final class TeeInputFromTextTest { - - /** - * Temporary files generator. - */ - @Rule - public TemporaryFolder folder = new TemporaryFolder(); - - @Test - public void copiesFromTextToPath() throws IOException { - final String input = - "Hello, товарищ path #1 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new TextOf(input), - output.toPath() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromTextWithCharsetToPath() throws IOException { - final String input = - "Hello, товарищ path #2 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new TextOf(input), - output.toPath(), - StandardCharsets.UTF_8 - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromTextWithCharsetByNameToPath() throws IOException { - final String input = - "Hello, товарищ path #3 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new TextOf(input), - output.toPath(), - StandardCharsets.UTF_8.name() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromTextToFile() throws IOException { - final String input = - "Hello, товарищ file #1 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new TextOf(input), - output - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromTextWithCharsetToFile() throws IOException { - final String input = - "Hello, товарищ file #2 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new TextOf(input), - output, - StandardCharsets.UTF_8 - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromTextWithCharsetByNameToFile() throws IOException { - final String input = - "Hello, товарищ file #3 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new TextOf(input), - output, - StandardCharsets.UTF_8.name() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromTextToOutput() throws IOException { - final String input = - "Hello, товарищ output #1 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new TextOf(input), - new OutputTo(output) - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromTextWithCharsetToOutput() throws IOException { - final String input = - "Hello, товарищ output #2 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new TextOf(input), - new OutputTo(output), - StandardCharsets.UTF_8 - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromTextWithCharsetByNameToOutput() throws IOException { - final String input = - "Hello, товарищ output #3 äÄ üÜ öÖ and ß"; - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - new TextOf(input), - new OutputTo(output), - StandardCharsets.UTF_8.name() - ), - new TeeInputHasResult( - input, - new TextOf(output) - ) - ); - } -} diff --git a/src/test/java/org/cactoos/io/TeeInputFromUriTest.java b/src/test/java/org/cactoos/io/TeeInputFromUriTest.java deleted file mode 100644 index 8062fc318b..0000000000 --- a/src/test/java/org/cactoos/io/TeeInputFromUriTest.java +++ /dev/null @@ -1,119 +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.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import org.cactoos.matchers.TeeInputHasResult; -import org.cactoos.text.TextOf; -import org.hamcrest.MatcherAssert; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; - -/** - * Test case for {@link TeeInput}. Cases for ctors which use - * {@link java.net.URI} as an input. - * @author Roman Proshin (roman@proshin.org) - * @version $Id$ - * @since 1.0 - * @checkstyle JavadocMethodCheck (120 lines) - * @checkstyle ClassDataAbstractionCouplingCheck (120 lines) - */ -public final class TeeInputFromUriTest { - - /** - * Temporary files generator. - */ - @Rule - public TemporaryFolder folder = new TemporaryFolder(); - - @Test - public void copiesFromUriToPath() throws IOException { - final String message = - "Hello, товарищ path #1 äÄ üÜ öÖ and ß"; - final File input = this.folder.newFile(); - Files.write( - input.toPath(), - message.getBytes(StandardCharsets.UTF_8) - ); - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input.toURI(), - output - ), - new TeeInputHasResult( - message, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromUriToFile() throws IOException { - final String message = - "Hello, товарищ file #1 äÄ üÜ öÖ and ß"; - final File input = this.folder.newFile(); - Files.write( - input.toPath(), - message.getBytes(StandardCharsets.UTF_8) - ); - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input.toURI(), - output - ), - new TeeInputHasResult( - message, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromUriToOutput() throws IOException { - final String message = - "Hello, товарищ output #1 äÄ üÜ öÖ and ß"; - final File input = this.folder.newFile(); - Files.write( - input.toPath(), - message.getBytes(StandardCharsets.UTF_8) - ); - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input.toURI(), - new OutputTo(output) - ), - new TeeInputHasResult( - message, - new TextOf(output) - ) - ); - } -} diff --git a/src/test/java/org/cactoos/io/TeeInputFromUrlTest.java b/src/test/java/org/cactoos/io/TeeInputFromUrlTest.java deleted file mode 100644 index 953154a1db..0000000000 --- a/src/test/java/org/cactoos/io/TeeInputFromUrlTest.java +++ /dev/null @@ -1,125 +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.File; -import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import org.cactoos.matchers.TeeInputHasResult; -import org.cactoos.text.TextOf; -import org.hamcrest.MatcherAssert; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TemporaryFolder; - -/** - * Test case for {@link TeeInput}. Cases for ctors which use - * {@link java.net.URL} as an input. - * @author Roman Proshin (roman@proshin.org) - * @version $Id$ - * @since 1.0 - * @checkstyle JavadocMethodCheck (125 lines) - * @checkstyle ClassDataAbstractionCouplingCheck (125 lines) - */ -public final class TeeInputFromUrlTest { - - /** - * Temporary files generator. - */ - @Rule - public TemporaryFolder folder = new TemporaryFolder(); - - @Test - public void copiesFromUrlToPath() throws IOException { - final String message = - "Hello, товарищ path #1 äÄ üÜ öÖ and ß"; - final File input = this.folder.newFile(); - Files.write( - input.toPath(), - message.getBytes(StandardCharsets.UTF_8) - ); - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input - .toURI() - .toURL(), - output.toPath() - ), - new TeeInputHasResult( - message, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromUrlToFile() throws IOException { - final String message = - "Hello, товарищ file #1 äÄ üÜ öÖ and ß"; - final File input = this.folder.newFile(); - Files.write( - input.toPath(), - message.getBytes(StandardCharsets.UTF_8) - ); - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input - .toURI() - .toURL(), - output - ), - new TeeInputHasResult( - message, - new TextOf(output) - ) - ); - } - - @Test - public void copiesFromUrlToOutput() throws IOException { - final String message = - "Hello, товарищ output #1 äÄ üÜ öÖ and ß"; - final File input = this.folder.newFile(); - Files.write( - input.toPath(), - message.getBytes(StandardCharsets.UTF_8) - ); - final File output = this.folder.newFile(); - MatcherAssert.assertThat( - new TeeInput( - input - .toURI() - .toURL(), - new OutputTo(output) - ), - new TeeInputHasResult( - message, - new TextOf(output) - ) - ); - } -} From c27b67acdac735310e21ce3df10661410257cc1b Mon Sep 17 00:00:00 2001 From: Roman Proshin Date: Mon, 26 Feb 2018 10:29:54 +0300 Subject: [PATCH 5/6] #631: TeeInput test class is incomplete --- src/main/java/org/cactoos/io/TeeInput.java | 6 +++--- src/test/java/org/cactoos/io/TeeInputFromBytesTest.java | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/cactoos/io/TeeInput.java b/src/main/java/org/cactoos/io/TeeInput.java index 62c84f80fa..b7ca5570e6 100644 --- a/src/main/java/org/cactoos/io/TeeInput.java +++ b/src/main/java/org/cactoos/io/TeeInput.java @@ -45,9 +45,9 @@ * @author Yegor Bugayenko (yegor256@gmail.com) * @version $Id$ * @since 0.1 - * @todo #631 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. + * @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/TeeInputFromBytesTest.java b/src/test/java/org/cactoos/io/TeeInputFromBytesTest.java index 811bdadba2..d27eef3ac9 100644 --- a/src/test/java/org/cactoos/io/TeeInputFromBytesTest.java +++ b/src/test/java/org/cactoos/io/TeeInputFromBytesTest.java @@ -38,8 +38,8 @@ * @author Roman Proshin (roman@proshin.org) * @version $Id$ * @since 1.0 - * @todo #631 Create a new Matcher that will compare results of TeeInput as - * well as copied content to the original message. + * @todo #631:30min Create a new Matcher that will compare results of TeeInput + * as well as copied content to the original message. * @checkstyle JavadocMethodCheck (100 lines) * @checkstyle ClassDataAbstractionCouplingCheck (100 lines) */ From 0e7628a22fb7f5a95870782ac1552b46ef6e5e8d Mon Sep 17 00:00:00 2001 From: Roman Proshin Date: Tue, 27 Feb 2018 14:25:55 +0300 Subject: [PATCH 6/6] #631: TeeInput test class is incomplete: fix puzzle description --- src/test/java/org/cactoos/io/TeeInputFromBytesTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/test/java/org/cactoos/io/TeeInputFromBytesTest.java b/src/test/java/org/cactoos/io/TeeInputFromBytesTest.java index d27eef3ac9..57b7d3d942 100644 --- a/src/test/java/org/cactoos/io/TeeInputFromBytesTest.java +++ b/src/test/java/org/cactoos/io/TeeInputFromBytesTest.java @@ -39,7 +39,8 @@ * @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. + * 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) */