From: David ‘Bombe’ Roden Date: Wed, 4 Sep 2024 11:46:05 +0000 (+0200) Subject: ♻️ Extract InputStream matcher to its own file X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=ab6653f4308cc06c7b16514250836e5c59140454;p=jFCPlib.git ♻️ Extract InputStream matcher to its own file --- 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); + } + }; + } + +}