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 5, 2018
2 parents 3c86f77 + f6c48c0 commit 7fb5241
Show file tree
Hide file tree
Showing 6 changed files with 404 additions and 15 deletions.
17 changes: 2 additions & 15 deletions src/main/java/org/takes/facets/hamcrest/AbstractHmBody.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,21 +40,8 @@
* @param <T> Item type. Should be able to return own item
* @since 2.0
*
* @todo #485:30min Right now we can only check that InputStream
* have the same content as other InputStream. This is very
* limited usage. Task is to introduce `HmTextBody` that will
* make available to us to use useful string matchers from
* hamcrest. The usage will be like that:
* ```
* MatcherAssert.assertThat(
* response,
* new HmRsTextBody<>(Matchers.startsWith("<html>"))
* );
* ```
* The default constructor should use `Matcher.containsString`
* as default matcher, which is used for matching string to body.
* Current implementation of `AbstractHmBody` should be converted
* to `HmBytesBody` that will check equality of bytes. We can think
* @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
Expand Down
121 changes: 121 additions & 0 deletions src/main/java/org/takes/facets/hamcrest/AbstractHmTextBody.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2014-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.takes.facets.hamcrest;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import org.hamcrest.Description;
import org.hamcrest.Matcher;
import org.hamcrest.TypeSafeMatcher;

/**
* Text body matcher.
*
* <p>This "matcher" tests given item body, assuming that it has text content.
* <p>The class is immutable and thread-safe.
*
* @author Izbassar Tolegen (t.izbassar@gmail.com)
* @version $Id$
* @param <T> Item type. Should be able to return own body
* @since 2.0
*
* @todo #794:30min Implement describeMismatchSafely and cover mismatch
* descriptions with relevant test cases. Update describeTo implementation
* to be more informative and add relevant test cases for that. Both should
* show, what was expected, what was actually in the body and text description
* for clear understanding.
*/
public abstract class AbstractHmTextBody<T> extends TypeSafeMatcher<T> {

/**
* Body matcher.
*/
private final Matcher<String> body;

/**
* Charset of the text.
*/
private final Charset charset;

/**
* Ctor.
* @param body Body matcher.
* @param charset Charset of the text.
*/
public AbstractHmTextBody(final Matcher<String> body,
final Charset charset) {
super();
this.body = body;
this.charset = charset;
}

@Override
public final void describeTo(final Description description) {
description.appendDescriptionOf(this.body);
}

@Override
protected final boolean matchesSafely(final T item) {
try {
return this.body.matches(this.text(item));
} catch (final IOException ex) {
throw new IllegalStateException(ex);
}
}

/**
* Item's body.
* @param item Item to retrieve body from
* @return InputStream of body
* @throws IOException If some problem inside
*/
protected abstract InputStream itemBody(final T item) throws IOException;

/**
* Text from item.
* @param item Item
* @return Text contents of item
* @throws IOException If some problem inside
*/
@SuppressWarnings("PMD.AssignmentInOperand")
private String text(final T item) throws IOException {
final String text;
try (
final InputStream input = this.itemBody(item);
final ByteArrayOutputStream output = new ByteArrayOutputStream()
) {
// @checkstyle MagicNumberCheck (1 line)
final byte[] buffer = new byte[1024];
int len;
while ((len = input.read(buffer, 0, buffer.length)) != -1) {
output.write(buffer, 0, len);
}
output.flush();
text = new String(output.toByteArray(), this.charset);
}
return text;
}
}
75 changes: 75 additions & 0 deletions src/main/java/org/takes/facets/hamcrest/HmRqTextBody.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2014-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.takes.facets.hamcrest;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.takes.Request;

/**
* Request text body matcher.
*
* <p>This "matcher" tests given request body,
* assuming that it has text content.</p>
* <p>The class is immutable and thread-safe.
*
* @author Izbassar Tolegen (t.izbassar@gmail.com)
* @version $Id$
* @since 2.0
*/
public final class HmRqTextBody extends AbstractHmTextBody<Request> {

/**
* Ctor with equalTo matcher and default charset.
* @param expected String to test against
*/
public HmRqTextBody(final String expected) {
this(Matchers.equalTo(expected));
}

/**
* Ctor with charset set to default one.
* @param bdm Text body matcher
*/
public HmRqTextBody(final Matcher<String> bdm) {
this(bdm, Charset.defaultCharset());
}

/**
* Ctor.
* @param bdm Text body matcher
* @param charset Text body charset
*/
public HmRqTextBody(final Matcher<String> bdm, final Charset charset) {
super(bdm, charset);
}

@Override
public InputStream itemBody(final Request item) throws IOException {
return item.body();
}
}
75 changes: 75 additions & 0 deletions src/main/java/org/takes/facets/hamcrest/HmRsTextBody.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2014-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.takes.facets.hamcrest;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import org.hamcrest.Matcher;
import org.hamcrest.Matchers;
import org.takes.Response;

/**
* Response text body matcher.
*
* <p>This "matcher" tests given response body,
* assuming that it has text content.</p>
* <p>The class is immutable and thread-safe.
*
* @author Izbassar Tolegen (t.izbassar@gmail.com)
* @version $Id$
* @since 2.0
*/
public final class HmRsTextBody extends AbstractHmTextBody<Response> {

/**
* Ctor with equalTo matcher and default charset.
* @param expected String to test against
*/
public HmRsTextBody(final String expected) {
this(Matchers.equalTo(expected));
}

/**
* Ctor with charset set to default one.
* @param bdm Text body matcher
*/
public HmRsTextBody(final Matcher<String> bdm) {
this(bdm, Charset.defaultCharset());
}

/**
* Ctor.
* @param bdm Text body matcher
* @param charset Text body charset
*/
public HmRsTextBody(final Matcher<String> bdm, final Charset charset) {
super(bdm, charset);
}

@Override
public InputStream itemBody(final Response item) throws IOException {
return item.body();
}
}
69 changes: 69 additions & 0 deletions src/test/java/org/takes/facets/hamcrest/HmRqTextBodyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/**
* The MIT License (MIT)
*
* Copyright (c) 2014-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.takes.facets.hamcrest;

import java.util.Collections;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.takes.rq.RqFake;

/**
* Test case for {@link HmRqTextBody}.
*
* @author Izbassar Tolegen (t.izbassar@gmail.com)
* @version $Id$
* @since 2.0
*/
public final class HmRqTextBodyTest {

/**
* HmRqTextBody can test if body equals text.
*/
@Test
public void testsBodyValueContainsText() {
final String same = "Same text";
MatcherAssert.assertThat(
new RqFake(
Collections.<String>emptyList(),
same
),
new HmRqTextBody(same)
);
}

/**
* HmRqTextBody can test if body doesn't equal to text.
*/
@Test
public void testsBodyValueDoesNotContainsText() {
MatcherAssert.assertThat(
new RqFake(
Collections.<String>emptyList(),
"some"
),
Matchers.not(new HmRqTextBody("other"))
);
}
}

3 comments on commit 7fb5241

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 7fb5241 Mar 5, 2018

Choose a reason for hiding this comment

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

Puzzle 485-ca1df145 disappeared from src/main/java/org/takes/facets/hamcrest/AbstractHmBody.java, that's why I closed #794. 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 7fb5241 Mar 5, 2018

Choose a reason for hiding this comment

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

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

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on 7fb5241 Mar 5, 2018

Choose a reason for hiding this comment

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

Puzzle 794-9c47f19f discovered in src/main/java/org/takes/facets/hamcrest/AbstractHmBody.java and submitted as #820. 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.