forked from yegor256/takes
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
yegor256#794 Introduce HmTextBody matchers
- Loading branch information
Izbassar Tolegen
committed
Feb 28, 2018
1 parent
c0fc037
commit 76917b2
Showing
6 changed files
with
400 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
src/main/java/org/takes/facets/hamcrest/AbstractHmTextBody.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/** | ||
* 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. It should show, what was | ||
* expected and what was actually in the body 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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 containsString matcher and default charset. | ||
* @param expected String to test against | ||
*/ | ||
public HmRqTextBody(final String expected) { | ||
this(Matchers.containsString(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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 containsString matcher and default charset. | ||
* @param expected String to test against | ||
*/ | ||
public HmRsTextBody(final String expected) { | ||
this(Matchers.containsString(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(); | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
src/test/java/org/takes/facets/hamcrest/HmRqTextBodyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* 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 contains text. | ||
*/ | ||
@Test | ||
public void testsBodyValueContainsText() { | ||
MatcherAssert.assertThat( | ||
new RqFake( | ||
Collections.<String>emptyList(), | ||
"Some text" | ||
), | ||
new HmRqTextBody("text") | ||
); | ||
} | ||
|
||
/** | ||
* HmRqTextBody can test if body doesn't contains text. | ||
*/ | ||
@Test | ||
public void testsBodyValueDoesNotContainsText() { | ||
MatcherAssert.assertThat( | ||
new RqFake( | ||
Collections.<String>emptyList(), | ||
"some" | ||
), | ||
Matchers.not(new HmRqTextBody("other")) | ||
); | ||
} | ||
} |
Oops, something went wrong.