Skip to content

Commit

Permalink
Merge branch '__rultor'
Browse files Browse the repository at this point in the history
  • Loading branch information
rultor committed Mar 17, 2018
2 parents c1368ab + cab1502 commit 688b1a6
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 10 deletions.
55 changes: 47 additions & 8 deletions src/main/java/org/takes/facets/hamcrest/AbstractHmBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
package org.takes.facets.hamcrest;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;

Expand All @@ -43,11 +45,6 @@
* @todo #794:30min Current implementation of `AbstractHmBody` should be
* converted to `HmBytesBody` that will check equality of bytes. We can think
* of improving that class lately.
*
* @todo #485:30min Right now the describeTo doesn't properly
* show the reason behind mismatch. It should show expected
* bytes and actual bytes for better clarification for
* end user. Also describeMismatchSafely should be implemented.
*/
abstract class AbstractHmBody<T> extends TypeSafeMatcher<T> {

Expand All @@ -65,10 +62,32 @@ protected AbstractHmBody(final InputStream value) {
this.body = value;
}

// @todo #795:30min Right now the describeTo method do not covered
// with tests. Cover this method with unit test to increase coverage
// of the class.
@Override
public final void describeTo(final Description description) {
description.appendText("item: ")
.appendValue(this.body);
try {
description.appendText("body: ")
.appendText(Arrays.toString(AbstractHmBody.asBytes(this.body)));
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
}

@Override
protected void describeMismatchSafely(final T item,
final Description description) {
try {
description.appendText("body was: ")
.appendText(
Arrays.toString(
AbstractHmBody.asBytes(this.itemBody(item))
)
);
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
}

@Override
Expand Down Expand Up @@ -103,5 +122,25 @@ protected final boolean matchesSafely(final T item) {
* @throws IOException If some problem inside
*/
protected abstract InputStream itemBody(final T item) throws IOException;
}

/**
* InputStream as bytes.
* @param input Input
* @return Bytes
* @throws IOException If some problem inside
*/
@SuppressWarnings("PMD.AssignmentInOperand")
private static byte[] asBytes(final InputStream input) throws IOException {
input.reset();
try (final ByteArrayOutputStream output = new ByteArrayOutputStream()) {
// @checkstyle MagicNumberCheck (1 line)
final byte[] buffer = new byte[1024];
int read;
while ((read = input.read(buffer, 0, buffer.length)) != -1) {
output.write(buffer, 0, read);
}
output.flush();
return output.toByteArray();
}
}
}
24 changes: 23 additions & 1 deletion src/test/java/org/takes/facets/hamcrest/HmRqBodyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import java.util.Collections;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.StringDescription;
import org.junit.Test;
import org.takes.Request;
import org.takes.rq.RqFake;

/**
Expand Down Expand Up @@ -67,5 +69,25 @@ public void testsBodyValuesAreDifferent() {
Matchers.not(new HmRqBody("that"))
);
}
}

/**
* HmRqBody can describe mismatch in readable way.
*/
@Test
public void describesMismatchInReadableWay() {
final Request request = new RqFake(
Collections.<String>emptyList(),
"other"
);
final HmRqBody matcher = new HmRqBody("some");
matcher.matchesSafely(request);
final StringDescription description = new StringDescription();
matcher.describeMismatchSafely(request, description);
MatcherAssert.assertThat(
description.toString(),
Matchers.equalTo(
"body was: [111, 116, 104, 101, 114]"
)
);
}
}
24 changes: 23 additions & 1 deletion src/test/java/org/takes/facets/hamcrest/HmRsBodyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
import java.util.Collections;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.hamcrest.StringDescription;
import org.junit.Test;
import org.takes.Response;
import org.takes.rs.RsSimple;

/**
Expand Down Expand Up @@ -67,5 +69,25 @@ public void bodyValuesAreDifferent() {
Matchers.not(new HmRsBody("that"))
);
}
}

/**
* HmRsBody can describe mismatch in readable way.
*/
@Test
public void describesMismatchInReadableWay() {
final Response response = new RsSimple(
Collections.<String>emptyList(),
"other"
);
final HmRsBody matcher = new HmRsBody("some");
matcher.matchesSafely(response);
final StringDescription description = new StringDescription();
matcher.describeMismatchSafely(response, description);
MatcherAssert.assertThat(
description.toString(),
Matchers.equalTo(
"body was: [111, 116, 104, 101, 114]"
)
);
}
}

2 comments on commit 688b1a6

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 688b1a6 Mar 17, 2018

Choose a reason for hiding this comment

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

Puzzle 485-6f864529 disappeared from src/main/java/org/takes/facets/hamcrest/AbstractHmBody.java, that's why I closed #795. Please, remember that the puzzle was not necessarily removed in this particular commit. Maybe it happened earlier, but we discovered this fact only now.

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 688b1a6 Mar 17, 2018

Choose a reason for hiding this comment

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

Puzzle 795-077f3980 discovered in src/main/java/org/takes/facets/hamcrest/AbstractHmBody.java and submitted as #832. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.