From 8afea1d02219d66403108a23ccba04929126e24b Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 20 Feb 2015 06:42:54 +0100 Subject: [PATCH] Fix Sinfest filter and add a test --- .../filters/comics/SinfestComicFilter.java | 27 +- .../filters/comics/SinfestComicFilterTest.java | 82 ++++++ .../rhynodge/filters/comics/sinfest.html | 274 +++++++++++++++++++++ 3 files changed, 363 insertions(+), 20 deletions(-) create mode 100644 src/test/java/net/pterodactylus/rhynodge/filters/comics/SinfestComicFilterTest.java create mode 100644 src/test/resources/net/pterodactylus/rhynodge/filters/comics/sinfest.html diff --git a/src/main/java/net/pterodactylus/rhynodge/filters/comics/SinfestComicFilter.java b/src/main/java/net/pterodactylus/rhynodge/filters/comics/SinfestComicFilter.java index 76089e3..f94c355 100644 --- a/src/main/java/net/pterodactylus/rhynodge/filters/comics/SinfestComicFilter.java +++ b/src/main/java/net/pterodactylus/rhynodge/filters/comics/SinfestComicFilter.java @@ -19,6 +19,7 @@ package net.pterodactylus.rhynodge.filters.comics; import java.util.Collections; import java.util.List; +import java.util.stream.Collectors; import net.pterodactylus.rhynodge.filters.ComicSiteFilter; @@ -39,32 +40,18 @@ public class SinfestComicFilter extends ComicSiteFilter { @Override protected Optional extractTitle(Document document) { - Elements imageCell = document.select("table#AutoNumber2 tr:eq(1) img"); + Elements imageCell = selectImageAttributes(document); return imageCell.hasAttr("alt") ? Optional.of(imageCell.attr("alt")) : Optional.absent(); } @Override protected List extractImageUrls(Document document) { - Elements imageCell = document.select("table#AutoNumber2 tr:eq(1) img"); - return imageCell.hasAttr("src") ? FluentIterable.from(imageCell).transform(new Function>() { - - @Override - public Optional apply(Element elements) { - return elements.hasAttr("src") ? Optional.of(elements.attr("src")) : Optional.absent(); - } - }).filter(new Predicate>() { - - @Override - public boolean apply(Optional input) { - return input.isPresent(); - } - }).transform(new Function, String>() { + Elements imageCells = selectImageAttributes(document); + return imageCells.stream().map(cell -> cell.attr("src")).collect(Collectors.toList()); + } - @Override - public String apply(Optional input) { - return input.get(); - } - }).toList() : Collections.emptyList(); + private Elements selectImageAttributes(Document document) { + return document.select("tbody.style5 img"); } @Override diff --git a/src/test/java/net/pterodactylus/rhynodge/filters/comics/SinfestComicFilterTest.java b/src/test/java/net/pterodactylus/rhynodge/filters/comics/SinfestComicFilterTest.java new file mode 100644 index 0000000..dc20d36 --- /dev/null +++ b/src/test/java/net/pterodactylus/rhynodge/filters/comics/SinfestComicFilterTest.java @@ -0,0 +1,82 @@ +package net.pterodactylus.rhynodge.filters.comics; + +import java.io.IOException; + +import net.pterodactylus.rhynodge.Filter; +import net.pterodactylus.rhynodge.State; +import net.pterodactylus.rhynodge.filters.ResourceLoader; +import net.pterodactylus.rhynodge.states.ComicState; +import net.pterodactylus.rhynodge.states.ComicState.Comic; +import net.pterodactylus.rhynodge.states.ComicState.Strip; +import net.pterodactylus.rhynodge.states.HtmlState; + +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.MatcherAssert; +import org.hamcrest.Matchers; +import org.hamcrest.TypeSafeDiagnosingMatcher; +import org.jsoup.nodes.Document; +import org.junit.Test; + + +/** + * Unit test for {@link SinfestComicFilter}. + * + * @author David ‘Bombe’ Roden + */ +public class SinfestComicFilterTest { + + private final Filter sinfestFilter = new SinfestComicFilter(); + private final HtmlState htmlState; + + public SinfestComicFilterTest() throws IOException { + Document document = ResourceLoader.loadDocument(SinfestComicFilter.class, "sinfest.html", "http://www.sinfest.net/"); + htmlState = new HtmlState("http://www.sinfest.net/", document); + } + + @Test + public void canParseComicsFromHtml() { + State state = sinfestFilter.filter(htmlState); + MatcherAssert.assertThat(state, Matchers.instanceOf(ComicState.class)); + } + + @Test + public void imageUrlsAreParsedCorrectly() { + ComicState comicState = (ComicState) sinfestFilter.filter(htmlState); + MatcherAssert.assertThat(comicState.comics(), Matchers.contains(matchesComic("Search 9", "http://www.sinfest.net/btphp/comics/2015-02-20.gif", ""))); + } + + private Matcher matchesComic(String title, String url, String comment) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(Comic comic, Description mismatchDescription) { + if (!comic.title().equals(title)) { + mismatchDescription.appendText("comic is named ").appendValue(comic.title()); + return false; + } + if (comic.strips().size() != 1) { + mismatchDescription.appendText("comic has ").appendValue(comic.strips().size()).appendText(" strips"); + return false; + } + Strip strip = comic.strips().get(0); + if (!strip.imageUrl().equals(url)) { + mismatchDescription.appendText("image url is ").appendValue(strip.imageUrl()); + return false; + } + if (!strip.comment().equals(comment)) { + mismatchDescription.appendText("comment is ").appendValue(strip.comment()); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("comic named ").appendValue(title); + description.appendText(" at ").appendValue(url); + description.appendText(" with comment ").appendValue(comment); + } + }; + } + +} diff --git a/src/test/resources/net/pterodactylus/rhynodge/filters/comics/sinfest.html b/src/test/resources/net/pterodactylus/rhynodge/filters/comics/sinfest.html new file mode 100644 index 0000000..9de933f --- /dev/null +++ b/src/test/resources/net/pterodactylus/rhynodge/filters/comics/sinfest.html @@ -0,0 +1,274 @@ + + + + Sinfest + + + +
+ + + + + + +
+
+
+ + + + + + + +
+ By Tatsuya Ishida
+ + + + + +
HOME +  |  + ARCHIVE +  |  + FORUM +  |  + BOOKS +  |  + SEARCH

+ + + + + + +
 + + + +
February 20, 2015: Search 9
Search 9
+
+ +
+ +
+
+
+ + +
+ + + + +
+ + + + + + +
+
+ +
+ + + + + + + + +
+ + + + + + + +
+
+
+ + +   + + + + + + + +
+
June 1, 2014
+
I know my kingdom awaits and they've forgiven my mistakes
+
Posted by Tatsuya Ishida
+

Hi. New design.
+That is all.
+-T.

+
+ +
+
+
+
+ + +
 + + + + + +
+ +
+ + + + + + + + + +
+
+
+ +
+ + +
+
+ © COPYRIGHT 2000-2015 + BY TATSUYA + ISHIDA/MUSEWORKS. CONTACT +
+ Powered by + + + btPHP 1.3.2
+ +
page generated in 0.02347 seconds
+
+ +
+ +
+ + + + + + + + \ No newline at end of file -- 2.7.4