From: David ‘Bombe’ Roden Date: Tue, 18 Mar 2014 05:46:16 +0000 (+0100) Subject: Add unit test for comic site filter. X-Git-Tag: v2~248 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=b64dbd900e6a671ec1a2749f3b7e2b9f0b250657;p=rhynodge.git Add unit test for comic site filter. --- diff --git a/src/test/java/net/pterodactylus/rhynodge/filters/ComicSiteFilterTest.java b/src/test/java/net/pterodactylus/rhynodge/filters/ComicSiteFilterTest.java new file mode 100644 index 0000000..a449708 --- /dev/null +++ b/src/test/java/net/pterodactylus/rhynodge/filters/ComicSiteFilterTest.java @@ -0,0 +1,102 @@ +package net.pterodactylus.rhynodge.filters; + +import static com.google.common.base.Optional.fromNullable; +import static java.util.Arrays.asList; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; + +import java.util.Collections; +import java.util.List; + +import net.pterodactylus.rhynodge.State; +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.FailedState; +import net.pterodactylus.rhynodge.states.HtmlState; + +import com.google.common.base.Optional; +import org.jsoup.nodes.Document; +import org.junit.Test; + +/** + * Unit test for {@link ComicSiteFilter}. + * + * @author David ‘Bombe’ Roden + */ +public class ComicSiteFilterTest { + + @Test(expected = IllegalArgumentException.class) + public void comicSiteFilterRequiresHtmlState() { + State state = new FailedState(); + ComicSiteFilter comicSiteFilter = new TestComicSiteFilter(null, null, null); + comicSiteFilter.filter(state); + } + + @Test + public void htmlWithoutTitleIsNotRecognizedAsComic() { + State state = new HtmlState("http://foo/", null); + ComicSiteFilter comicSiteFilter = new TestComicSiteFilter(null, null, null); + State newState = comicSiteFilter.filter(state); + assertThat(newState instanceof ComicState, is(true)); + } + + @Test + public void htmlWithTitleButWithoutImagesIsNotRecognizedAsComic() { + State state = new HtmlState("http://foo/", null); + ComicSiteFilter comicSiteFilter = new TestComicSiteFilter("Title", Collections.emptyList(), null); + State newState = comicSiteFilter.filter(state); + assertThat(newState instanceof ComicState, is(true)); + } + + @Test + public void everyUrlGetAStrip() { + State state = new HtmlState("http://foo/", null); + ComicSiteFilter comicSiteFilter = new TestComicSiteFilter("Title", asList("url1.gif", "url2.gif", "url3.gif"), asList("Comment 1", "Comment 2")); + State newState = comicSiteFilter.filter(state); + assertThat(newState instanceof ComicState, is(true)); + ComicState comicState = (ComicState) newState; + assertThat(comicState.comics(), hasSize(1)); + Comic comic = comicState.comics().get(0); + assertThat(comic.strips(), contains(new Strip("http://foo/url1.gif", "Comment 1"), new Strip("http://foo/url2.gif", "Comment 2"), new Strip("http://foo/url3.gif", ""))); + } + + @Test(expected = IllegalStateException.class) + public void illegalUrlThrowsException() { + State state = new HtmlState("http://foo/^", null); + ComicSiteFilter comicSiteFilter = new TestComicSiteFilter("Title", asList("url1.gif"), Collections.emptyList()); + comicSiteFilter.filter(state); + } + + private static class TestComicSiteFilter extends ComicSiteFilter { + + private final String title; + private final List urls; + private final List comments; + + public TestComicSiteFilter(String title, List urls, List comments) { + this.title = title; + this.urls = urls; + this.comments = comments; + } + + @Override + protected Optional extractTitle(Document document) { + return fromNullable(title); + } + + @Override + protected List extractImageUrls(Document document) { + return urls; + } + + @Override + protected List extractImageComments(Document document) { + return comments; + } + + } + +}