diff options
author | ByteHamster <ByteHamster@users.noreply.github.com> | 2021-01-24 12:20:09 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-01-24 12:20:09 +0100 |
commit | a36d66bf35e7660e678af342e1cf7e2be783c8ce (patch) | |
tree | 7fa67c294ed1ad3c5460b88ccec5ccbcb315a2e8 /core/src/test | |
parent | 72ab17f1533317c676d9182c92519fffe75ba5db (diff) | |
parent | ce48b186edf28ba03821e541c235497b99aba628 (diff) | |
download | AntennaPod-a36d66bf35e7660e678af342e1cf7e2be783c8ce.zip |
Merge pull request #4893 from damoasda/robolectric-feed-parser2
Run AtomParserTest and RssParserTest with Robolectric
Diffstat (limited to 'core/src/test')
8 files changed, 198 insertions, 0 deletions
diff --git a/core/src/test/java/de/danoeh/antennapod/core/syndication/handler/AtomParserTest.java b/core/src/test/java/de/danoeh/antennapod/core/syndication/handler/AtomParserTest.java new file mode 100644 index 000000000..82f7fcfca --- /dev/null +++ b/core/src/test/java/de/danoeh/antennapod/core/syndication/handler/AtomParserTest.java @@ -0,0 +1,70 @@ +package de.danoeh.antennapod.core.syndication.handler; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +import java.io.File; +import java.util.Date; + +import de.danoeh.antennapod.core.feed.Feed; +import de.danoeh.antennapod.core.feed.FeedItem; +import de.danoeh.antennapod.core.feed.FeedMedia; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** + * Tests for Atom feeds in FeedHandler. + */ +@RunWith(RobolectricTestRunner.class) +public class AtomParserTest { + + @Test + public void testAtomBasic() throws Exception { + File feedFile = FeedParserTestHelper.getFeedFile("feed-atom-testAtomBasic.xml"); + Feed feed = FeedParserTestHelper.runFeedParser(feedFile); + assertEquals(Feed.TYPE_ATOM1, feed.getType()); + assertEquals("title", feed.getTitle()); + assertEquals("http://example.com/feed", feed.getFeedIdentifier()); + assertEquals("http://example.com", feed.getLink()); + assertEquals("This is the description", feed.getDescription()); + assertEquals("http://example.com/payment", feed.getPaymentLink()); + assertEquals("http://example.com/picture", feed.getImageUrl()); + assertEquals(10, feed.getItems().size()); + for (int i = 0; i < feed.getItems().size(); i++) { + FeedItem item = feed.getItems().get(i); + assertEquals("http://example.com/item-" + i, item.getItemIdentifier()); + assertEquals("item-" + i, item.getTitle()); + assertNull(item.getDescription()); + assertNull(item.getContentEncoded()); + assertEquals("http://example.com/items/" + i, item.getLink()); + assertEquals(new Date(i * 60000), item.getPubDate()); + assertNull(item.getPaymentLink()); + assertEquals("http://example.com/picture", item.getImageLocation()); + // media + assertTrue(item.hasMedia()); + FeedMedia media = item.getMedia(); + //noinspection ConstantConditions + assertEquals("http://example.com/media-" + i, media.getDownload_url()); + assertEquals(1024 * 1024, media.getSize()); + assertEquals("audio/mp3", media.getMime_type()); + // chapters + assertNull(item.getChapters()); + } + } + + @Test + public void testLogoWithWhitespace() throws Exception { + File feedFile = FeedParserTestHelper.getFeedFile("feed-atom-testLogoWithWhitespace.xml"); + Feed feed = FeedParserTestHelper.runFeedParser(feedFile); + assertEquals("title", feed.getTitle()); + assertEquals("http://example.com/feed", feed.getFeedIdentifier()); + assertEquals("http://example.com", feed.getLink()); + assertEquals("This is the description", feed.getDescription()); + assertEquals("http://example.com/payment", feed.getPaymentLink()); + assertEquals("https://example.com/image.png", feed.getImageUrl()); + assertEquals(0, feed.getItems().size()); + } +} diff --git a/core/src/test/java/de/danoeh/antennapod/core/syndication/handler/FeedParserTestHelper.java b/core/src/test/java/de/danoeh/antennapod/core/syndication/handler/FeedParserTestHelper.java new file mode 100644 index 000000000..c02a7d209 --- /dev/null +++ b/core/src/test/java/de/danoeh/antennapod/core/syndication/handler/FeedParserTestHelper.java @@ -0,0 +1,35 @@ +package de.danoeh.antennapod.core.syndication.handler; + +import androidx.annotation.NonNull; + +import java.io.File; + +import de.danoeh.antennapod.core.feed.Feed; + +/** + * Tests for FeedHandler. + */ +public abstract class FeedParserTestHelper { + + /** + * Returns the File object for a file in the resources folder. + */ + @NonNull + static File getFeedFile(@NonNull String fileName) { + //noinspection ConstantConditions + return new File(FeedParserTestHelper.class.getClassLoader().getResource(fileName).getFile()); + } + + /** + * Runs the feed parser on the given file. + */ + @NonNull + static Feed runFeedParser(@NonNull File feedFile) throws Exception { + FeedHandler handler = new FeedHandler(); + Feed parsedFeed = new Feed("http://example.com/feed", null); + parsedFeed.setFile_url(feedFile.getAbsolutePath()); + parsedFeed.setDownloaded(true); + handler.parseFeed(parsedFeed); + return parsedFeed; + } +} diff --git a/core/src/test/java/de/danoeh/antennapod/core/syndication/handler/RssParserTest.java b/core/src/test/java/de/danoeh/antennapod/core/syndication/handler/RssParserTest.java new file mode 100644 index 000000000..1195520a6 --- /dev/null +++ b/core/src/test/java/de/danoeh/antennapod/core/syndication/handler/RssParserTest.java @@ -0,0 +1,86 @@ +package de.danoeh.antennapod.core.syndication.handler; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.robolectric.RobolectricTestRunner; + +import java.io.File; +import java.util.Date; + +import de.danoeh.antennapod.core.feed.Feed; +import de.danoeh.antennapod.core.feed.FeedItem; +import de.danoeh.antennapod.core.feed.FeedMedia; +import de.danoeh.antennapod.core.feed.MediaType; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +/** + * Tests for RSS feeds in FeedHandler. + */ +@RunWith(RobolectricTestRunner.class) +public class RssParserTest { + + @Test + public void testRss2Basic() throws Exception { + File feedFile = FeedParserTestHelper.getFeedFile("feed-rss-testRss2Basic.xml"); + Feed feed = FeedParserTestHelper.runFeedParser(feedFile); + assertEquals(Feed.TYPE_RSS2, feed.getType()); + assertEquals("title", feed.getTitle()); + assertEquals("en", feed.getLanguage()); + assertEquals("http://example.com", feed.getLink()); + assertEquals("This is the description", feed.getDescription()); + assertEquals("http://example.com/payment", feed.getPaymentLink()); + assertEquals("http://example.com/picture", feed.getImageUrl()); + assertEquals(10, feed.getItems().size()); + for (int i = 0; i < feed.getItems().size(); i++) { + FeedItem item = feed.getItems().get(i); + assertEquals("http://example.com/item-" + i, item.getItemIdentifier()); + assertEquals("item-" + i, item.getTitle()); + assertNull(item.getDescription()); + assertNull(item.getContentEncoded()); + assertEquals("http://example.com/items/" + i, item.getLink()); + assertEquals(new Date(i * 60000), item.getPubDate()); + assertNull(item.getPaymentLink()); + assertEquals("http://example.com/picture", item.getImageLocation()); + // media + assertTrue(item.hasMedia()); + FeedMedia media = item.getMedia(); + //noinspection ConstantConditions + assertEquals("http://example.com/media-" + i, media.getDownload_url()); + assertEquals(1024 * 1024, media.getSize()); + assertEquals("audio/mp3", media.getMime_type()); + // chapters + assertNull(item.getChapters()); + } + } + + @Test + public void testImageWithWhitespace() throws Exception { + File feedFile = FeedParserTestHelper.getFeedFile("feed-rss-testImageWithWhitespace.xml"); + Feed feed = FeedParserTestHelper.runFeedParser(feedFile); + assertEquals("title", feed.getTitle()); + assertEquals("http://example.com", feed.getLink()); + assertEquals("This is the description", feed.getDescription()); + assertEquals("http://example.com/payment", feed.getPaymentLink()); + assertEquals("https://example.com/image.png", feed.getImageUrl()); + assertEquals(0, feed.getItems().size()); + } + + @Test + public void testMediaContentMime() throws Exception { + File feedFile = FeedParserTestHelper.getFeedFile("feed-rss-testMediaContentMime.xml"); + Feed feed = FeedParserTestHelper.runFeedParser(feedFile); + assertEquals("title", feed.getTitle()); + assertEquals("http://example.com", feed.getLink()); + assertEquals("This is the description", feed.getDescription()); + assertEquals("http://example.com/payment", feed.getPaymentLink()); + assertNull(feed.getImageUrl()); + assertEquals(1, feed.getItems().size()); + FeedItem feedItem = feed.getItems().get(0); + //noinspection ConstantConditions + assertEquals(MediaType.VIDEO, feedItem.getMedia().getMediaType()); + assertEquals("https://www.example.com/file.mp4", feedItem.getMedia().getDownload_url()); + } +} diff --git a/core/src/test/resources/feed-atom-testAtomBasic.xml b/core/src/test/resources/feed-atom-testAtomBasic.xml new file mode 100644 index 000000000..cefc4f979 --- /dev/null +++ b/core/src/test/resources/feed-atom-testAtomBasic.xml @@ -0,0 +1 @@ +<?xml version='1.0' encoding='UTF-8' ?><feed xmlns="http://www.w3.org/2005/Atom"><id>http://example.com/feed</id><title>title</title><link rel="alternate" href="http://example.com" /><subtitle>This is the description</subtitle><logo>http://example.com/picture</logo><link rel="payment" href="http://example.com/payment" type="text/html" /><entry><id>http://example.com/item-0</id><title>item-0</title><link rel="alternate" href="http://example.com/items/0" /><published>1970-01-01T00:00:00Z</published><link rel="enclosure" href="http://example.com/media-0" type="audio/mp3" length="1048576" /></entry><entry><id>http://example.com/item-1</id><title>item-1</title><link rel="alternate" href="http://example.com/items/1" /><published>1970-01-01T00:01:00Z</published><link rel="enclosure" href="http://example.com/media-1" type="audio/mp3" length="1048576" /></entry><entry><id>http://example.com/item-2</id><title>item-2</title><link rel="alternate" href="http://example.com/items/2" /><published>1970-01-01T00:02:00Z</published><link rel="enclosure" href="http://example.com/media-2" type="audio/mp3" length="1048576" /></entry><entry><id>http://example.com/item-3</id><title>item-3</title><link rel="alternate" href="http://example.com/items/3" /><published>1970-01-01T00:03:00Z</published><link rel="enclosure" href="http://example.com/media-3" type="audio/mp3" length="1048576" /></entry><entry><id>http://example.com/item-4</id><title>item-4</title><link rel="alternate" href="http://example.com/items/4" /><published>1970-01-01T00:04:00Z</published><link rel="enclosure" href="http://example.com/media-4" type="audio/mp3" length="1048576" /></entry><entry><id>http://example.com/item-5</id><title>item-5</title><link rel="alternate" href="http://example.com/items/5" /><published>1970-01-01T00:05:00Z</published><link rel="enclosure" href="http://example.com/media-5" type="audio/mp3" length="1048576" /></entry><entry><id>http://example.com/item-6</id><title>item-6</title><link rel="alternate" href="http://example.com/items/6" /><published>1970-01-01T00:06:00Z</published><link rel="enclosure" href="http://example.com/media-6" type="audio/mp3" length="1048576" /></entry><entry><id>http://example.com/item-7</id><title>item-7</title><link rel="alternate" href="http://example.com/items/7" /><published>1970-01-01T00:07:00Z</published><link rel="enclosure" href="http://example.com/media-7" type="audio/mp3" length="1048576" /></entry><entry><id>http://example.com/item-8</id><title>item-8</title><link rel="alternate" href="http://example.com/items/8" /><published>1970-01-01T00:08:00Z</published><link rel="enclosure" href="http://example.com/media-8" type="audio/mp3" length="1048576" /></entry><entry><id>http://example.com/item-9</id><title>item-9</title><link rel="alternate" href="http://example.com/items/9" /><published>1970-01-01T00:09:00Z</published><link rel="enclosure" href="http://example.com/media-9" type="audio/mp3" length="1048576" /></entry></feed>
\ No newline at end of file diff --git a/core/src/test/resources/feed-atom-testLogoWithWhitespace.xml b/core/src/test/resources/feed-atom-testLogoWithWhitespace.xml new file mode 100644 index 000000000..f4886d56a --- /dev/null +++ b/core/src/test/resources/feed-atom-testLogoWithWhitespace.xml @@ -0,0 +1,2 @@ +<?xml version='1.0' encoding='UTF-8' ?><feed xmlns="http://www.w3.org/2005/Atom"><id>http://example.com/feed</id><title>title</title><link rel="alternate" href="http://example.com" /><subtitle>This is the description</subtitle><link rel="payment" href="http://example.com/payment" type="text/html" /><logo> https://example.com/image.png +</logo></feed>
\ No newline at end of file diff --git a/core/src/test/resources/feed-rss-testImageWithWhitespace.xml b/core/src/test/resources/feed-rss-testImageWithWhitespace.xml new file mode 100644 index 000000000..2be9401d2 --- /dev/null +++ b/core/src/test/resources/feed-rss-testImageWithWhitespace.xml @@ -0,0 +1,2 @@ +<?xml version='1.0' encoding='UTF-8' ?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>title</title><description>This is the description</description><link>http://example.com</link><language>en</language><atom:link rel="payment" href="http://example.com/payment" type="text/html" /><image><url> https://example.com/image.png +</url></image></channel></rss>
\ No newline at end of file diff --git a/core/src/test/resources/feed-rss-testMediaContentMime.xml b/core/src/test/resources/feed-rss-testMediaContentMime.xml new file mode 100644 index 000000000..a715abb37 --- /dev/null +++ b/core/src/test/resources/feed-rss-testMediaContentMime.xml @@ -0,0 +1 @@ +<?xml version='1.0' encoding='UTF-8' ?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>title</title><description>This is the description</description><link>http://example.com</link><language>en</language><atom:link rel="payment" href="http://example.com/payment" type="text/html" /><item xmlns:media="http://search.yahoo.com/mrss/"><media:content url="https://www.example.com/file.mp4" medium="video" /></item></channel></rss>
\ No newline at end of file diff --git a/core/src/test/resources/feed-rss-testRss2Basic.xml b/core/src/test/resources/feed-rss-testRss2Basic.xml new file mode 100644 index 000000000..dd771b61a --- /dev/null +++ b/core/src/test/resources/feed-rss-testRss2Basic.xml @@ -0,0 +1 @@ +<?xml version='1.0' encoding='UTF-8' ?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>title</title><description>This is the description</description><link>http://example.com</link><language>en</language><image><url>http://example.com/picture</url></image><atom:link rel="payment" href="http://example.com/payment" type="text/html" /><item><title>item-0</title><link>http://example.com/items/0</link><pubDate>01 Jan 70 01:00:00 +0100</pubDate><guid>http://example.com/item-0</guid><enclosure url="http://example.com/media-0" length="1048576" type="audio/mp3" /></item><item><title>item-1</title><link>http://example.com/items/1</link><pubDate>01 Jan 70 01:01:00 +0100</pubDate><guid>http://example.com/item-1</guid><enclosure url="http://example.com/media-1" length="1048576" type="audio/mp3" /></item><item><title>item-2</title><link>http://example.com/items/2</link><pubDate>01 Jan 70 01:02:00 +0100</pubDate><guid>http://example.com/item-2</guid><enclosure url="http://example.com/media-2" length="1048576" type="audio/mp3" /></item><item><title>item-3</title><link>http://example.com/items/3</link><pubDate>01 Jan 70 01:03:00 +0100</pubDate><guid>http://example.com/item-3</guid><enclosure url="http://example.com/media-3" length="1048576" type="audio/mp3" /></item><item><title>item-4</title><link>http://example.com/items/4</link><pubDate>01 Jan 70 01:04:00 +0100</pubDate><guid>http://example.com/item-4</guid><enclosure url="http://example.com/media-4" length="1048576" type="audio/mp3" /></item><item><title>item-5</title><link>http://example.com/items/5</link><pubDate>01 Jan 70 01:05:00 +0100</pubDate><guid>http://example.com/item-5</guid><enclosure url="http://example.com/media-5" length="1048576" type="audio/mp3" /></item><item><title>item-6</title><link>http://example.com/items/6</link><pubDate>01 Jan 70 01:06:00 +0100</pubDate><guid>http://example.com/item-6</guid><enclosure url="http://example.com/media-6" length="1048576" type="audio/mp3" /></item><item><title>item-7</title><link>http://example.com/items/7</link><pubDate>01 Jan 70 01:07:00 +0100</pubDate><guid>http://example.com/item-7</guid><enclosure url="http://example.com/media-7" length="1048576" type="audio/mp3" /></item><item><title>item-8</title><link>http://example.com/items/8</link><pubDate>01 Jan 70 01:08:00 +0100</pubDate><guid>http://example.com/item-8</guid><enclosure url="http://example.com/media-8" length="1048576" type="audio/mp3" /></item><item><title>item-9</title><link>http://example.com/items/9</link><pubDate>01 Jan 70 01:09:00 +0100</pubDate><guid>http://example.com/item-9</guid><enclosure url="http://example.com/media-9" length="1048576" type="audio/mp3" /></item></channel></rss>
\ No newline at end of file |