Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Jan 19, 2019
2 parents 8c91d47 + 4a39ffb commit e161aee
Show file tree
Hide file tree
Showing 7 changed files with 317 additions and 202 deletions.
21 changes: 8 additions & 13 deletions src/main/java/org/takes/rs/Body.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,22 @@
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.
* @return The content of the 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.
Expand Down Expand Up @@ -81,7 +76,7 @@ final class Url implements Body {
}

@Override
public InputStream input() throws IOException {
public InputStream stream() throws IOException {
return this.url.openStream();
}

Expand Down Expand Up @@ -112,7 +107,7 @@ final class ByteArray implements Body {
}

@Override
public InputStream input() {
public InputStream stream() {
return new ByteArrayInputStream(this.bytes);
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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());
}

Expand Down Expand Up @@ -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()),
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/takes/rs/RsWithBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public Iterable<String> head() throws IOException {

@Override
public InputStream body() throws IOException {
return body.input();
return body.stream();
}
}
);
Expand Down
69 changes: 69 additions & 0 deletions src/test/java/org/takes/rs/BodyByteArrayTest.java
Original file line number Diff line number Diff line change
@@ -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)
);
}
}
74 changes: 74 additions & 0 deletions src/test/java/org/takes/rs/BodyStreamTest.java
Original file line number Diff line number Diff line change
@@ -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)
);
}
}
73 changes: 73 additions & 0 deletions src/test/java/org/takes/rs/BodyTempFileTest.java
Original file line number Diff line number Diff line change
@@ -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)
);
}
}
Loading

1 comment on commit e161aee

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on e161aee Jan 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 804-84fdf636 disappeared from src/main/java/org/takes/rs/Body.java, that's why I closed #887. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

Please sign in to comment.