From ab6653f4308cc06c7b16514250836e5c59140454 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Wed, 4 Sep 2024 13:46:05 +0200 Subject: [PATCH] =?utf8?q?=E2=99=BB=EF=B8=8F=20Extract=20InputStream=20mat?= =?utf8?q?cher=20to=20its=20own=20file?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../pterodactylus/fcp/highlevel/FcpClientTest.java | 39 +------------------- .../fcp/test/InputStreamMatchers.java | 43 ++++++++++++++++++++++ 2 files changed, 45 insertions(+), 37 deletions(-) create mode 100644 src/test/java/net/pterodactylus/fcp/test/InputStreamMatchers.java diff --git a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java index 833a75b..b8028a6 100644 --- a/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java +++ b/src/test/java/net/pterodactylus/fcp/highlevel/FcpClientTest.java @@ -7,22 +7,19 @@ import net.pterodactylus.fcp.FcpMessage; import net.pterodactylus.fcp.GetFailed; import net.pterodactylus.fcp.NodeHello; import net.pterodactylus.fcp.SSKKeypair; -import org.hamcrest.Description; -import org.hamcrest.Matcher; -import org.hamcrest.TypeSafeDiagnosingMatcher; import org.junit.Rule; import org.junit.Test; import org.junit.rules.Timeout; import java.io.ByteArrayInputStream; import java.io.IOException; -import java.io.InputStream; import java.util.ArrayList; import java.util.List; import java.util.concurrent.atomic.AtomicReference; import java.util.function.BiConsumer; import java.util.function.Function; +import static net.pterodactylus.fcp.test.InputStreamMatchers.streamContaining; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; @@ -145,41 +142,9 @@ public class FcpClientTest { GetResult getResult = fcpClient.getURI("KSK@test"); assertThat(getResult.isSuccess(), equalTo(true)); assertThat(getResult.getContentLength(), equalTo(4L)); - assertThat(getResult.getInputStream(), contains('D', 'a', 't', 'a')); + assertThat(getResult.getInputStream(), streamContaining('D', 'a', 't', 'a')); } } - - private Matcher contains(int... content) { - return new TypeSafeDiagnosingMatcher() { - @Override - protected boolean matchesSafely(InputStream inputStream, Description mismatchDescription) { - try { - for (int index = 0; index < content.length; index++) { - int readByte = inputStream.read(); - if (readByte != content[index]) { - mismatchDescription.appendText("was ").appendValue(readByte).appendText(" at offset ").appendValue(index); - return false; - } - } - int eof = inputStream.read(); - if (eof != -1) { - mismatchDescription.appendText("contained more than ").appendValue(content.length).appendText(" bytes"); - return false; - } - } catch (IOException e) { - mismatchDescription.appendText("could not be read (").appendValue(e).appendText(")"); - return false; - } - return true; - } - - @Override - public void describeTo(Description description) { - description.appendText("is input stream containing ").appendValue(content); - } - }; - } - @Test public void generatingKeyPairSendsCorrectMessage() throws IOException, FcpException { FcpConnection fcpConnection = createFcpConnection(message -> { diff --git a/src/test/java/net/pterodactylus/fcp/test/InputStreamMatchers.java b/src/test/java/net/pterodactylus/fcp/test/InputStreamMatchers.java new file mode 100644 index 0000000..3e47a0b --- /dev/null +++ b/src/test/java/net/pterodactylus/fcp/test/InputStreamMatchers.java @@ -0,0 +1,43 @@ +package net.pterodactylus.fcp.test; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeDiagnosingMatcher; + +import java.io.IOException; +import java.io.InputStream; + +public class InputStreamMatchers { + + public static Matcher streamContaining(int... content) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(InputStream inputStream, Description mismatchDescription) { + try { + for (int index = 0; index < content.length; index++) { + int readByte = inputStream.read(); + if (readByte != content[index]) { + mismatchDescription.appendText("was ").appendValue(readByte).appendText(" at offset ").appendValue(index); + return false; + } + } + int eof = inputStream.read(); + if (eof != -1) { + mismatchDescription.appendText("contained more than ").appendValue(content.length).appendText(" bytes"); + return false; + } + } catch (IOException e) { + mismatchDescription.appendText("could not be read (").appendValue(e).appendText(")"); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("is input stream containing ").appendValue(content); + } + }; + } + +} -- 2.7.4