1 package net.pterodactylus.rhynodge.filters.torrents;
3 import java.io.IOException;
5 import net.pterodactylus.rhynodge.State;
6 import net.pterodactylus.rhynodge.states.HtmlState;
7 import net.pterodactylus.rhynodge.states.TorrentState;
8 import net.pterodactylus.rhynodge.states.TorrentState.TorrentFile;
10 import org.hamcrest.Description;
11 import org.hamcrest.Matcher;
12 import org.hamcrest.MatcherAssert;
13 import org.hamcrest.Matchers;
14 import org.hamcrest.TypeSafeDiagnosingMatcher;
15 import org.jsoup.Jsoup;
16 import org.jsoup.nodes.Document;
17 import org.junit.Test;
20 * Unit test for {@link TorrentzEuFilter}.
22 * @author <a href="mailto:bombe@pterodactylus.net">David ‘Bombe’ Roden</a>
24 public class TorrentzEuFilterTest {
26 private final TorrentzEuFilter torrentzEuFilter = new TorrentzEuFilter();
29 public void filterCanExtractAllTorrents() throws IOException {
30 Document document = Jsoup.parse(getClass().getResourceAsStream("torrentz-eu-results.html"), "UTF-8", "http://torrentz.eu/searchA?f=Ubuntu+ISO");
31 HtmlState htmlState = new HtmlState("http://torrentz.eu/searchA?f=Ubuntu+ISO", document);
32 State newState = torrentzEuFilter.filter(htmlState);
33 MatcherAssert.assertThat(newState, Matchers.notNullValue());
34 MatcherAssert.assertThat(newState, Matchers.instanceOf(TorrentState.class));
35 TorrentState torrentState = (TorrentState) newState;
36 MatcherAssert.assertThat(torrentState.torrentFiles(), Matchers.hasSize(2));
37 MatcherAssert.assertThat(torrentState.torrentFiles(), Matchers.contains(
38 isTorrent("Ubuntu 14 04 64Bit ISO File", "981 MB", 0, 1),
39 isTorrent("Ubuntu 14 04 LTS Desktop 64bit ISO", "964 MB", 75, 5)
43 private Matcher<TorrentFile> isTorrent(String name, String size, int seedCount, int leechCount) {
44 return new TypeSafeDiagnosingMatcher<TorrentFile>() {
46 protected boolean matchesSafely(TorrentFile torrentFile, Description mismatchDescription) {
47 if (!torrentFile.name().equals(name)) {
48 mismatchDescription.appendText("name is ").appendValue(torrentFile.name());
51 if (!torrentFile.size().equals(size)) {
52 mismatchDescription.appendText("size is ").appendValue(torrentFile.size());
55 if (torrentFile.seedCount() != seedCount) {
56 mismatchDescription.appendText("seed count is ").appendValue(torrentFile.seedCount());
59 if (torrentFile.leechCount() != leechCount) {
60 mismatchDescription.appendText("leech count is ").appendValue(torrentFile.leechCount());
67 public void describeTo(Description description) {
68 description.appendText("torrent named ").appendValue(name);
69 description.appendText(", size ").appendValue(size);
70 description.appendText(", seeds ").appendValue(seedCount);
71 description.appendText(", leechs ").appendValue(leechCount);