diff --git a/src/main/java/org/takes/rs/Body.java b/src/main/java/org/takes/rs/Body.java index 1c86e8916..16f5d483c 100644 --- a/src/main/java/org/takes/rs/Body.java +++ b/src/main/java/org/takes/rs/Body.java @@ -32,19 +32,14 @@ import java.nio.file.StandardCopyOption; import java.util.UUID; import java.util.concurrent.atomic.AtomicInteger; +import org.cactoos.Input; /** * The body of a response used by {@link RsWithBody}. * * @since 0.32 - * @todo #804:30min Make Body extends Cactoos's Input and replace input method - * with stream method. Once this is done, rewrite the tests (such as BodyTest) - * so that they do not use guava but Cactoos objects and cactoos-matchers - * InputHasContent. When there is no more need for the method input, - * remove it. */ -@SuppressWarnings("PMD.TooManyMethods") -interface Body { +interface Body extends Input { /** * Gives an {@code InputStream} corresponding to the content of * the body. @@ -52,7 +47,7 @@ interface Body { * @throws IOException in case the content of the body could not * be provided. */ - InputStream input() throws IOException; + InputStream stream() throws IOException; /** * Gives the length of the stream. @@ -81,7 +76,7 @@ final class Url implements Body { } @Override - public InputStream input() throws IOException { + public InputStream stream() throws IOException { return this.url.openStream(); } @@ -112,7 +107,7 @@ final class ByteArray implements Body { } @Override - public InputStream input() { + public InputStream stream() { return new ByteArrayInputStream(this.bytes); } @@ -147,7 +142,7 @@ final class Stream implements Body { } @Override - public InputStream input() throws IOException { + public InputStream stream() throws IOException { this.estimate(); return this.stream; } @@ -206,7 +201,7 @@ final class TempFile implements Body { } @Override - public InputStream input() throws IOException { + public InputStream stream() throws IOException { return Files.newInputStream(this.file().toPath()); } @@ -239,7 +234,7 @@ private File file() throws IOException { synchronized (this.file) { if (!this.file.exists()) { this.file.deleteOnExit(); - try (InputStream content = this.body.input()) { + try (InputStream content = this.body.stream()) { Files.copy( content, Paths.get(this.file.getAbsolutePath()), diff --git a/src/main/java/org/takes/rs/RsWithBody.java b/src/main/java/org/takes/rs/RsWithBody.java index 50beceeff..2ab18f037 100644 --- a/src/main/java/org/takes/rs/RsWithBody.java +++ b/src/main/java/org/takes/rs/RsWithBody.java @@ -148,7 +148,7 @@ public Iterable head() throws IOException { @Override public InputStream body() throws IOException { - return body.input(); + return body.stream(); } } ); diff --git a/src/test/java/org/takes/rs/BodyByteArrayTest.java b/src/test/java/org/takes/rs/BodyByteArrayTest.java new file mode 100644 index 000000000..1990acedd --- /dev/null +++ b/src/test/java/org/takes/rs/BodyByteArrayTest.java @@ -0,0 +1,69 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2014-2019 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.takes.rs; + +import org.cactoos.io.BytesOf; +import org.cactoos.io.LengthOf; +import org.hamcrest.MatcherAssert; +import org.hamcrest.core.IsEqual; +import org.junit.Test; +import org.takes.misc.Utf8String; + +/** + * Test case for {@link Body.ByteArray}. + * + * @since 1.15 + */ +public final class BodyByteArrayTest { + + /** + * Body.ByteArray can provide the expected input. + * @throws Exception If there is some problem inside. + */ + @Test + public void returnsCorrectInputWithByteArray() throws Exception { + final byte[] bytes = + new Utf8String("ByteArray returnsCorrectInput!").asBytes(); + MatcherAssert.assertThat( + "Body content of Body.ByteArray doesn't provide the correct bytes", + new BytesOf(new Body.ByteArray(bytes)).asBytes(), + new IsEqual<>(bytes) + ); + } + + /** + * Body.ByteArray can provide the expected length. + * @throws Exception If there is some problem inside. + */ + @Test + public void returnsCorrectLengthWithStream() throws Exception { + final byte[] bytes = + new Utf8String("Stream returnsCorrectLength!").asBytes(); + MatcherAssert.assertThat( + "Body content of Body.ByteArray doesn't have the correct length", + new LengthOf(new Body.ByteArray(bytes)).intValue(), + new IsEqual<>(bytes.length) + ); + } +} diff --git a/src/test/java/org/takes/rs/BodyStreamTest.java b/src/test/java/org/takes/rs/BodyStreamTest.java new file mode 100644 index 000000000..0f0f80b7c --- /dev/null +++ b/src/test/java/org/takes/rs/BodyStreamTest.java @@ -0,0 +1,74 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2014-2019 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.takes.rs; + +import org.cactoos.io.BytesOf; +import org.cactoos.io.InputOf; +import org.cactoos.io.LengthOf; +import org.hamcrest.MatcherAssert; +import org.hamcrest.core.IsEqual; +import org.junit.Test; +import org.takes.misc.Utf8String; + +/** + * Test case for {@link Body.Stream}. + * + * @since 1.15 + */ +public final class BodyStreamTest { + + /** + * Body.Stream can provide the expected input. + * @throws Exception If there is some problem inside. + */ + @Test + public void returnsCorrectInputWithStream() throws Exception { + final byte[] bytes = + new Utf8String("Stream returnsCorrectInput!").asBytes(); + MatcherAssert.assertThat( + "Body content of Body.Stream doesn't provide the correct bytes", + new BytesOf( + new Body.Stream(new InputOf(bytes).stream()) + ).asBytes(), + new IsEqual<>(bytes) + ); + } + + /** + * Body.Stream can provide the expected length. + * @throws Exception If there is some problem inside. + */ + @Test + public void returnsCorrectLengthWithStream() throws Exception { + final byte[] bytes = + new Utf8String("Stream returnsCorrectLength!").asBytes(); + MatcherAssert.assertThat( + "Body content of Body.Stream doesn't have the correct length", + new LengthOf( + new Body.Stream(new InputOf(bytes).stream()) + ).intValue(), + new IsEqual<>(bytes.length) + ); + } +} diff --git a/src/test/java/org/takes/rs/BodyTempFileTest.java b/src/test/java/org/takes/rs/BodyTempFileTest.java new file mode 100644 index 000000000..01b8c1f59 --- /dev/null +++ b/src/test/java/org/takes/rs/BodyTempFileTest.java @@ -0,0 +1,73 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2014-2019 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.takes.rs; + +import org.cactoos.io.BytesOf; +import org.cactoos.io.LengthOf; +import org.hamcrest.MatcherAssert; +import org.hamcrest.core.IsEqual; +import org.junit.Test; +import org.takes.misc.Utf8String; + +/** + * Test case for {@link Body.TempFile}. + * + * @since 1.15 + */ +public final class BodyTempFileTest { + + /** + * Body.TemFile can provide the expected input. + * @throws Exception If there is some problem inside. + */ + @Test + public void returnsCorrectInputWithStream() throws Exception { + final byte[] bytes = + new Utf8String("Stream returnsCorrectInput!").asBytes(); + MatcherAssert.assertThat( + "Body content of Body.TempFile doesn't provide the correct bytes", + new BytesOf( + new Body.TempFile(new Body.ByteArray(bytes)) + ).asBytes(), + new IsEqual<>(bytes) + ); + } + + /** + * Body.TemFile can provide the expected length. + * @throws Exception If there is some problem inside. + */ + @Test + public void returnsCorrectLengthWithTempFile() throws Exception { + final byte[] bytes = + new Utf8String("TempFile returnsCorrectLength!").asBytes(); + MatcherAssert.assertThat( + "Body content of Body.TempFile doesn't have the correct length", + new LengthOf( + new Body.TempFile(new Body.ByteArray(bytes)) + ).intValue(), + new IsEqual<>(bytes.length) + ); + } +} diff --git a/src/test/java/org/takes/rs/BodyTest.java b/src/test/java/org/takes/rs/BodyTest.java deleted file mode 100644 index 318d6c6ce..000000000 --- a/src/test/java/org/takes/rs/BodyTest.java +++ /dev/null @@ -1,188 +0,0 @@ -/* - * The MIT License (MIT) - * - * Copyright (c) 2014-2019 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.takes.rs; - -import com.google.common.io.ByteStreams; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.StandardCopyOption; -import org.hamcrest.MatcherAssert; -import org.hamcrest.Matchers; -import org.junit.Test; -import org.takes.misc.Utf8String; - -/** - * Test case for {@link Body}. - * - * @since 0.32 - */ -public final class BodyTest { - - /** - * Body.ByteArray can provide the expected input. - * @throws Exception If some problem inside. - */ - @Test - public void returnsCorrectInputWithByteArray() throws Exception { - final byte[] bytes = - new Utf8String("ByteArray returnsCorrectInput!").asBytes(); - MatcherAssert.assertThat( - ByteStreams.toByteArray(new Body.ByteArray(bytes).input()), - Matchers.equalTo(bytes) - ); - } - - /** - * Body.ByteArray can provide the expected length. - * @throws Exception If some problem inside. - */ - @Test - public void returnsCorrectLengthWithByteArray() throws Exception { - final byte[] bytes = - new Utf8String("ByteArray returnsCorrectLength!").asBytes(); - MatcherAssert.assertThat( - new Body.ByteArray(bytes).length(), - Matchers.equalTo(bytes.length) - ); - } - - /** - * Body.Stream can provide the expected input. - * @throws Exception If some problem inside. - */ - @Test - public void returnsCorrectInputWithStream() throws Exception { - final byte[] bytes = - new Utf8String("Stream returnsCorrectInput!").asBytes(); - MatcherAssert.assertThat( - ByteStreams.toByteArray( - new Body.Stream(new ByteArrayInputStream(bytes)).input() - ), - Matchers.equalTo(bytes) - ); - } - - /** - * Body.Stream can provide the expected length. - * @throws Exception If some problem inside. - */ - @Test - public void returnsCorrectLengthWithStream() throws Exception { - final byte[] bytes = - new Utf8String("Stream returnsCorrectLength!").asBytes(); - MatcherAssert.assertThat( - new Body.Stream(new ByteArrayInputStream(bytes)).length(), - Matchers.equalTo(bytes.length) - ); - } - - /** - * Body.TemFile can provide the expected input. - * @throws Exception If some problem inside. - */ - @Test - public void returnsCorrectInputWithTemFile() throws Exception { - final byte[] bytes = - new Utf8String("TempFile returnsCorrectInput!").asBytes(); - MatcherAssert.assertThat( - ByteStreams.toByteArray( - new Body.TempFile(new Body.ByteArray(bytes)).input() - ), - Matchers.equalTo(bytes) - ); - } - - /** - * Body.TemFile can provide the expected length. - * @throws Exception If some problem inside. - */ - @Test - public void returnsCorrectLengthWithTemFile() throws Exception { - final byte[] bytes = - new Utf8String("TempFile returnsCorrectLength!").asBytes(); - MatcherAssert.assertThat( - new Body.TempFile(new Body.ByteArray(bytes)).length(), - Matchers.equalTo(bytes.length) - ); - } - - /** - * Body.URL can provide the expected input. - * @throws Exception If some problem inside. - */ - @Test - public void returnsCorrectInputWithUrl() throws Exception { - final Path file = BodyTest.createTempFile(); - try { - final byte[] bytes = - new Utf8String("URL returnsCorrectInput!").asBytes(); - try (InputStream input = new ByteArrayInputStream(bytes)) { - Files.copy(input, file, StandardCopyOption.REPLACE_EXISTING); - } - try (InputStream input = - new Body.Url(file.toUri().toURL()).input()) { - MatcherAssert.assertThat( - ByteStreams.toByteArray(input), - Matchers.equalTo(bytes) - ); - } - } finally { - Files.deleteIfExists(file); - } - } - - /** - * Body.URL can provide the expected length. - * @throws Exception If some problem inside. - */ - @Test - public void returnsCorrectLengthWithUrl() throws Exception { - final Path file = BodyTest.createTempFile(); - try { - final byte[] bytes = - new Utf8String("URL returnsCorrectLength!").asBytes(); - try (InputStream input = new ByteArrayInputStream(bytes)) { - Files.copy(input, file, StandardCopyOption.REPLACE_EXISTING); - } - MatcherAssert.assertThat( - new Body.Url(file.toUri().toURL()).length(), - Matchers.equalTo(bytes.length) - ); - } finally { - Files.deleteIfExists(file); - } - } - - /** - * Creates a temporary file for testing purpose. - * @return A temporary file for the test. - * @throws IOException If the file could not be created - */ - private static Path createTempFile() throws IOException { - return Files.createTempFile(BodyTest.class.getName(), "tmp"); - } -} diff --git a/src/test/java/org/takes/rs/BodyUrlTest.java b/src/test/java/org/takes/rs/BodyUrlTest.java new file mode 100644 index 000000000..df999e821 --- /dev/null +++ b/src/test/java/org/takes/rs/BodyUrlTest.java @@ -0,0 +1,92 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2014-2019 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.takes.rs; + +import java.io.OutputStream; +import org.cactoos.Bytes; +import org.cactoos.io.BytesOf; +import org.cactoos.io.LengthOf; +import org.cactoos.io.OutputTo; +import org.cactoos.io.TeeInput; +import org.cactoos.io.TempFile; +import org.hamcrest.MatcherAssert; +import org.hamcrest.core.IsEqual; +import org.junit.Test; +import org.takes.misc.Utf8String; + +/** + * Test case for {@link Body.Url}. + * + * @since 1.15 + * @checkstyle ClassDataAbstractionCouplingCheck (500 lines) + */ +public final class BodyUrlTest { + + /** + * Body.URL can provide the expected input. + * @throws Exception If there is some problem inside. + */ + @Test + public void returnsCorrectInputWithUrl() throws Exception { + try (TempFile file = new TempFile()) { + final Bytes body = new Utf8String("URL returnsCorrectInput!"); + try (OutputStream outStream = + new OutputTo(file.value()).stream()) { + new LengthOf( + new TeeInput(body, new OutputTo(outStream)) + ).intValue(); + } + final Body.Url input = new Body.Url(file.value().toUri().toURL()); + MatcherAssert.assertThat( + "Body content of Body.Url doesn't provide the correct bytes", + new BytesOf(input).asBytes(), + new IsEqual<>(body.asBytes()) + ); + } + } + + /** + * Body.URL can provide the expected length. + * @throws Exception If there is some problem inside. + */ + @Test + public void returnsCorrectLengthWithUrl() throws Exception { + try (TempFile file = new TempFile()) { + final Bytes body = new Utf8String("URL returnsCorrectLength!"); + try (OutputStream outStream = + new OutputTo(file.value()).stream()) { + new LengthOf( + new TeeInput(body, new OutputTo(outStream)) + ).intValue(); + } + MatcherAssert.assertThat( + "Body content of Body.Url doesn't have the correct length", + new LengthOf( + new Body.Url(file.value().toUri().toURL()) + ).intValue(), + new IsEqual<>(body.asBytes().length) + ); + } + } +}