From 90c93ad9bbdbfa0f892bee4f0f5527f5bb5d357a Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 21 Dec 2014 21:43:26 +0100 Subject: [PATCH] Factor out torrent matchers. --- .../rhynodge/filters/torrents/TorrentMatcher.java | 74 ++++++++++++++++++++++ .../filters/torrents/TorrentzEuFilterTest.java | 41 +----------- 2 files changed, 76 insertions(+), 39 deletions(-) create mode 100644 src/test/java/net/pterodactylus/rhynodge/filters/torrents/TorrentMatcher.java diff --git a/src/test/java/net/pterodactylus/rhynodge/filters/torrents/TorrentMatcher.java b/src/test/java/net/pterodactylus/rhynodge/filters/torrents/TorrentMatcher.java new file mode 100644 index 0000000..5b1017e --- /dev/null +++ b/src/test/java/net/pterodactylus/rhynodge/filters/torrents/TorrentMatcher.java @@ -0,0 +1,74 @@ +package net.pterodactylus.rhynodge.filters.torrents; + +import java.util.Objects; + +import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.Matchers; +import org.hamcrest.TypeSafeDiagnosingMatcher; + +/** + * {@link Matcher}s for {@link TorrentFile}s. + * + * @author David ‘Bombe’ Roden + */ +public class TorrentMatcher { + + public static Matcher isTorrent(String name, String size, int seedCount, int leechCount) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(TorrentFile torrentFile, Description mismatchDescription) { + if (!torrentFile.name().equals(name)) { + mismatchDescription.appendText("name is ").appendValue(torrentFile.name()); + return false; + } + if (!torrentFile.size().equals(size)) { + mismatchDescription.appendText("size is ").appendValue(torrentFile.size()); + return false; + } + if (torrentFile.seedCount() != seedCount) { + mismatchDescription.appendText("seed count is ").appendValue(torrentFile.seedCount()); + return false; + } + if (torrentFile.leechCount() != leechCount) { + mismatchDescription.appendText("leech count is ").appendValue(torrentFile.leechCount()); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("torrent named ").appendValue(name); + description.appendText(", size ").appendValue(size); + description.appendText(", seeds ").appendValue(seedCount); + description.appendText(", leechs ").appendValue(leechCount); + } + }; + } + + public static Matcher isTorrent(String name, String size, String magnetLink, int seedCount, int leechCount) { + return Matchers.allOf(isTorrent(name, size, seedCount, leechCount), torrentWithMagnetLink(magnetLink)); + } + + private static Matcher torrentWithMagnetLink(String magnetLink) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(TorrentFile item, Description mismatchDescription) { + if (!Objects.equals(item.magnetUri(), magnetLink)) { + mismatchDescription.appendText("magnet URI is ").appendValue(item.magnetUri()); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("magnet URI is ").appendValue(magnetLink); + } + }; + } + +} diff --git a/src/test/java/net/pterodactylus/rhynodge/filters/torrents/TorrentzEuFilterTest.java b/src/test/java/net/pterodactylus/rhynodge/filters/torrents/TorrentzEuFilterTest.java index 4720d9b..d7afc94 100644 --- a/src/test/java/net/pterodactylus/rhynodge/filters/torrents/TorrentzEuFilterTest.java +++ b/src/test/java/net/pterodactylus/rhynodge/filters/torrents/TorrentzEuFilterTest.java @@ -5,13 +5,9 @@ import java.io.IOException; import net.pterodactylus.rhynodge.State; import net.pterodactylus.rhynodge.states.HtmlState; import net.pterodactylus.rhynodge.states.TorrentState; -import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile; -import org.hamcrest.Description; -import org.hamcrest.Matcher; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.hamcrest.TypeSafeDiagnosingMatcher; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.junit.Test; @@ -35,42 +31,9 @@ public class TorrentzEuFilterTest { TorrentState torrentState = (TorrentState) newState; MatcherAssert.assertThat(torrentState.torrentFiles(), Matchers.hasSize(2)); MatcherAssert.assertThat(torrentState.torrentFiles(), Matchers.contains( - isTorrent("Ubuntu 14 04 64Bit ISO File", "981 MB", 0, 1), - isTorrent("Ubuntu 14 04 LTS Desktop 64bit ISO", "964 MB", 75, 5) + TorrentMatcher.isTorrent("Ubuntu 14 04 64Bit ISO File", "981 MB", 0, 1), + TorrentMatcher.isTorrent("Ubuntu 14 04 LTS Desktop 64bit ISO", "964 MB", 75, 5) )); } - private Matcher isTorrent(String name, String size, int seedCount, int leechCount) { - return new TypeSafeDiagnosingMatcher() { - @Override - protected boolean matchesSafely(TorrentFile torrentFile, Description mismatchDescription) { - if (!torrentFile.name().equals(name)) { - mismatchDescription.appendText("name is ").appendValue(torrentFile.name()); - return false; - } - if (!torrentFile.size().equals(size)) { - mismatchDescription.appendText("size is ").appendValue(torrentFile.size()); - return false; - } - if (torrentFile.seedCount() != seedCount) { - mismatchDescription.appendText("seed count is ").appendValue(torrentFile.seedCount()); - return false; - } - if (torrentFile.leechCount() != leechCount) { - mismatchDescription.appendText("leech count is ").appendValue(torrentFile.leechCount()); - return false; - } - return true; - } - - @Override - public void describeTo(Description description) { - description.appendText("torrent named ").appendValue(name); - description.appendText(", size ").appendValue(size); - description.appendText(", seeds ").appendValue(seedCount); - description.appendText(", leechs ").appendValue(leechCount); - } - }; - } - } -- 2.7.4