diff options
author | daniel oeh <daniel.oeh@gmail.com> | 2014-09-17 20:51:45 +0200 |
---|---|---|
committer | daniel oeh <daniel.oeh@gmail.com> | 2014-09-17 20:51:45 +0200 |
commit | 072639b5b22e816df9f78b5cd8a7d4e5379b6aff (patch) | |
tree | 341c574bd6eb64497470e7226b3222b0a7c5a824 /app/src/androidTest/java/de/test/antennapod/util/syndication | |
parent | 76add8ef68dbc9997e901f4c11c397f581e8eabe (diff) | |
download | AntennaPod-072639b5b22e816df9f78b5cd8a7d4e5379b6aff.zip |
Changed project structure
Switched from custom layout to standard gradle project structure
Diffstat (limited to 'app/src/androidTest/java/de/test/antennapod/util/syndication')
5 files changed, 386 insertions, 0 deletions
diff --git a/app/src/androidTest/java/de/test/antennapod/util/syndication/FeedDiscovererTest.java b/app/src/androidTest/java/de/test/antennapod/util/syndication/FeedDiscovererTest.java new file mode 100644 index 000000000..10414fd2a --- /dev/null +++ b/app/src/androidTest/java/de/test/antennapod/util/syndication/FeedDiscovererTest.java @@ -0,0 +1,109 @@ +package de.test.antennapod.util.syndication; + +import android.test.InstrumentationTestCase; +import de.danoeh.antennapod.util.syndication.FeedDiscoverer; +import org.apache.commons.io.FileUtils; +import org.apache.commons.io.IOUtils; + +import java.io.File; +import java.io.FileOutputStream; +import java.util.Map; + +/** + * Test class for FeedDiscoverer + */ +public class FeedDiscovererTest extends InstrumentationTestCase { + + private FeedDiscoverer fd; + + private File testDir; + + @Override + public void setUp() throws Exception { + super.setUp(); + fd = new FeedDiscoverer(); + testDir = getInstrumentation().getTargetContext().getExternalFilesDir("FeedDiscovererTest"); + testDir.mkdir(); + assertTrue(testDir.exists()); + } + + @Override + protected void tearDown() throws Exception { + FileUtils.deleteDirectory(testDir); + super.tearDown(); + } + + private String createTestHtmlString(String rel, String type, String href, String title) { + return String.format("<html><head><title>Test</title><link rel=\"%s\" type=\"%s\" href=\"%s\" title=\"%s\"></head><body></body></html>", + rel, type, href, title); + } + + private String createTestHtmlString(String rel, String type, String href) { + return String.format("<html><head><title>Test</title><link rel=\"%s\" type=\"%s\" href=\"%s\"></head><body></body></html>", + rel, type, href); + } + + private void checkFindUrls(boolean isAlternate, boolean isRss, boolean withTitle, boolean isAbsolute, boolean fromString) throws Exception { + final String title = "Test title"; + final String hrefAbs = "http://example.com/feed"; + final String hrefRel = "/feed"; + final String base = "http://example.com"; + + final String rel = (isAlternate) ? "alternate" : "feed"; + final String type = (isRss) ? "application/rss+xml" : "application/atom+xml"; + final String href = (isAbsolute) ? hrefAbs : hrefRel; + + Map<String, String> res; + String html = (withTitle) ? createTestHtmlString(rel, type, href, title) + : createTestHtmlString(rel, type, href); + if (fromString) { + res = fd.findLinks(html, base); + } else { + File testFile = new File(testDir, "feed"); + FileOutputStream out = new FileOutputStream(testFile); + IOUtils.write(html, out); + out.close(); + res = fd.findLinks(testFile, base); + } + + assertNotNull(res); + assertEquals(1, res.size()); + for (String key : res.keySet()) { + assertEquals(hrefAbs, key); + } + assertTrue(res.containsKey(hrefAbs)); + if (withTitle) { + assertEquals(title, res.get(hrefAbs)); + } else { + assertEquals(href, res.get(hrefAbs)); + } + } + + public void testAlternateRSSWithTitleAbsolute() throws Exception { + checkFindUrls(true, true, true, true, true); + } + + public void testAlternateRSSWithTitleRelative() throws Exception { + checkFindUrls(true, true, true, false, true); + } + + public void testAlternateRSSNoTitleAbsolute() throws Exception { + checkFindUrls(true, true, false, true, true); + } + + public void testAlternateRSSNoTitleRelative() throws Exception { + checkFindUrls(true, true, false, false, true); + } + + public void testAlternateAtomWithTitleAbsolute() throws Exception { + checkFindUrls(true, false, true, true, true); + } + + public void testFeedAtomWithTitleAbsolute() throws Exception { + checkFindUrls(false, false, true, true, true); + } + + public void testAlternateRSSWithTitleAbsoluteFromFile() throws Exception { + checkFindUrls(true, true, true, true, false); + } +} diff --git a/app/src/androidTest/java/de/test/antennapod/util/syndication/feedgenerator/AtomGenerator.java b/app/src/androidTest/java/de/test/antennapod/util/syndication/feedgenerator/AtomGenerator.java new file mode 100644 index 000000000..8ce6c08e4 --- /dev/null +++ b/app/src/androidTest/java/de/test/antennapod/util/syndication/feedgenerator/AtomGenerator.java @@ -0,0 +1,118 @@ +package de.test.antennapod.util.syndication.feedgenerator; + +import android.util.Xml; +import de.danoeh.antennapod.feed.Feed; +import de.danoeh.antennapod.feed.FeedItem; +import de.danoeh.antennapod.feed.FeedMedia; +import de.danoeh.antennapod.syndication.util.SyndDateUtils; +import org.xmlpull.v1.XmlSerializer; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * Creates Atom feeds. See FeedGenerator for more information. + */ +public class AtomGenerator implements FeedGenerator{ + + private static final String NS_ATOM = "http://www.w3.org/2005/Atom"; + + public static final long FEATURE_USE_RFC3339LOCAL = 1; + + @Override + public void writeFeed(Feed feed, OutputStream outputStream, String encoding, long flags) throws IOException { + if (feed == null) throw new IllegalArgumentException("feed = null"); + if (outputStream == null) throw new IllegalArgumentException("outputStream = null"); + if (encoding == null) throw new IllegalArgumentException("encoding = null"); + + XmlSerializer xml = Xml.newSerializer(); + xml.setOutput(outputStream, encoding); + xml.startDocument(encoding, null); + + xml.startTag(null, "feed"); + xml.attribute(null, "xmlns", NS_ATOM); + + // Write Feed data + if (feed.getIdentifyingValue() != null) { + xml.startTag(null, "id"); + xml.text(feed.getIdentifyingValue()); + xml.endTag(null, "id"); + } + if (feed.getTitle() != null) { + xml.startTag(null, "title"); + xml.text(feed.getTitle()); + xml.endTag(null, "title"); + } + if (feed.getLink() != null) { + xml.startTag(null, "link"); + xml.attribute(null, "rel", "alternate"); + xml.attribute(null, "href", feed.getLink()); + xml.endTag(null, "link"); + } + if (feed.getDescription() != null) { + xml.startTag(null, "subtitle"); + xml.text(feed.getDescription()); + xml.endTag(null, "subtitle"); + } + + if (feed.getPaymentLink() != null) { + GeneratorUtil.addPaymentLink(xml, feed.getPaymentLink(), false); + } + + // Write FeedItem data + if (feed.getItems() != null) { + for (FeedItem item : feed.getItems()) { + xml.startTag(null, "entry"); + + if (item.getIdentifyingValue() != null) { + xml.startTag(null, "id"); + xml.text(item.getIdentifyingValue()); + xml.endTag(null, "id"); + } + if (item.getTitle() != null) { + xml.startTag(null, "title"); + xml.text(item.getTitle()); + xml.endTag(null, "title"); + } + if (item.getLink() != null) { + xml.startTag(null, "link"); + xml.attribute(null, "rel", "alternate"); + xml.attribute(null, "href", item.getLink()); + xml.endTag(null, "link"); + } + if (item.getPubDate() != null) { + xml.startTag(null, "published"); + if ((flags & FEATURE_USE_RFC3339LOCAL) != 0) { + xml.text(SyndDateUtils.formatRFC3339Local(item.getPubDate())); + } else { + xml.text(SyndDateUtils.formatRFC3339UTC(item.getPubDate())); + } + xml.endTag(null, "published"); + } + if (item.getDescription() != null) { + xml.startTag(null, "content"); + xml.text(item.getDescription()); + xml.endTag(null, "content"); + } + if (item.getMedia() != null) { + FeedMedia media = item.getMedia(); + xml.startTag(null, "link"); + xml.attribute(null, "rel", "enclosure"); + xml.attribute(null, "href", media.getDownload_url()); + xml.attribute(null, "type", media.getMime_type()); + xml.attribute(null, "length", String.valueOf(media.getSize())); + xml.endTag(null, "link"); + } + + if (item.getPaymentLink() != null) { + GeneratorUtil.addPaymentLink(xml, item.getPaymentLink(), false); + } + + xml.endTag(null, "entry"); + } + } + + xml.endTag(null, "feed"); + xml.endDocument(); + } +} diff --git a/app/src/androidTest/java/de/test/antennapod/util/syndication/feedgenerator/FeedGenerator.java b/app/src/androidTest/java/de/test/antennapod/util/syndication/feedgenerator/FeedGenerator.java new file mode 100644 index 000000000..23518de87 --- /dev/null +++ b/app/src/androidTest/java/de/test/antennapod/util/syndication/feedgenerator/FeedGenerator.java @@ -0,0 +1,28 @@ +package de.test.antennapod.util.syndication.feedgenerator; + +import de.danoeh.antennapod.feed.Feed; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * Generates a machine-readable, platform-independent representation of a Feed object. + */ +public interface FeedGenerator { + + /** + * Creates a machine-readable, platform-independent representation of a given + * Feed object and writes it to the given OutputStream. + * <p/> + * The representation might not be compliant with its specification if the feed + * is missing certain attribute values. This is intentional because the FeedGenerator is + * used for creating test data. + * + * @param feed The feed that should be written. Must not be null. + * @param outputStream The output target that the feed will be written to. The outputStream is not closed after + * the method's execution Must not be null. + * @param encoding The encoding to use. Must not be null. + * @param flags Optional argument for enabling implementation-dependent features. + */ + public void writeFeed(Feed feed, OutputStream outputStream, String encoding, long flags) throws IOException; +} diff --git a/app/src/androidTest/java/de/test/antennapod/util/syndication/feedgenerator/GeneratorUtil.java b/app/src/androidTest/java/de/test/antennapod/util/syndication/feedgenerator/GeneratorUtil.java new file mode 100644 index 000000000..e7cbb1b42 --- /dev/null +++ b/app/src/androidTest/java/de/test/antennapod/util/syndication/feedgenerator/GeneratorUtil.java @@ -0,0 +1,21 @@ +package de.test.antennapod.util.syndication.feedgenerator; + +import org.xmlpull.v1.XmlSerializer; + +import java.io.IOException; + +/** + * Utility methods for FeedGenerator + */ +public class GeneratorUtil { + + public static void addPaymentLink(XmlSerializer xml, String paymentLink, boolean withNamespace) throws IOException { + String ns = (withNamespace) ? "http://www.w3.org/2005/Atom" : null; + xml.startTag(ns, "link"); + xml.attribute(null, "rel", "payment"); + xml.attribute(null, "title", "Flattr this!"); + xml.attribute(null, "href", paymentLink); + xml.attribute(null, "type", "text/html"); + xml.endTag(ns, "link"); + } +} diff --git a/app/src/androidTest/java/de/test/antennapod/util/syndication/feedgenerator/RSS2Generator.java b/app/src/androidTest/java/de/test/antennapod/util/syndication/feedgenerator/RSS2Generator.java new file mode 100644 index 000000000..6355d4bb9 --- /dev/null +++ b/app/src/androidTest/java/de/test/antennapod/util/syndication/feedgenerator/RSS2Generator.java @@ -0,0 +1,110 @@ +package de.test.antennapod.util.syndication.feedgenerator; + +import android.util.Xml; +import de.danoeh.antennapod.feed.Feed; +import de.danoeh.antennapod.feed.FeedItem; +import de.danoeh.antennapod.syndication.util.SyndDateUtils; +import org.xmlpull.v1.XmlSerializer; + +import java.io.IOException; +import java.io.OutputStream; + +/** + * Creates RSS 2.0 feeds. See FeedGenerator for more information. + */ +public class RSS2Generator implements FeedGenerator{ + + public static final long FEATURE_WRITE_GUID = 1; + + @Override + public void writeFeed(Feed feed, OutputStream outputStream, String encoding, long flags) throws IOException { + if (feed == null) throw new IllegalArgumentException("feed = null"); + if (outputStream == null) throw new IllegalArgumentException("outputStream = null"); + if (encoding == null) throw new IllegalArgumentException("encoding = null"); + + XmlSerializer xml = Xml.newSerializer(); + xml.setOutput(outputStream, encoding); + xml.startDocument(encoding, null); + + xml.setPrefix("atom", "http://www.w3.org/2005/Atom"); + xml.startTag(null, "rss"); + xml.attribute(null, "version", "2.0"); + xml.startTag(null, "channel"); + + // Write Feed data + if (feed.getTitle() != null) { + xml.startTag(null, "title"); + xml.text(feed.getTitle()); + xml.endTag(null, "title"); + } + if (feed.getDescription() != null) { + xml.startTag(null, "description"); + xml.text(feed.getDescription()); + xml.endTag(null, "description"); + } + if (feed.getLink() != null) { + xml.startTag(null, "link"); + xml.text(feed.getLink()); + xml.endTag(null, "link"); + } + if (feed.getLanguage() != null) { + xml.startTag(null, "language"); + xml.text(feed.getLanguage()); + xml.endTag(null, "language"); + } + + if (feed.getPaymentLink() != null) { + GeneratorUtil.addPaymentLink(xml, feed.getPaymentLink(), true); + } + + // Write FeedItem data + if (feed.getItems() != null) { + for (FeedItem item : feed.getItems()) { + xml.startTag(null, "item"); + + if (item.getTitle() != null) { + xml.startTag(null, "title"); + xml.text(item.getTitle()); + xml.endTag(null, "title"); + } + if (item.getDescription() != null) { + xml.startTag(null, "description"); + xml.text(item.getDescription()); + xml.endTag(null, "description"); + } + if (item.getLink() != null) { + xml.startTag(null, "link"); + xml.text(item.getLink()); + xml.endTag(null, "link"); + } + if (item.getPubDate() != null) { + xml.startTag(null, "pubDate"); + xml.text(SyndDateUtils.formatRFC822Date(item.getPubDate())); + xml.endTag(null, "pubDate"); + } + if ((flags & FEATURE_WRITE_GUID) != 0) { + xml.startTag(null, "guid"); + xml.text(item.getItemIdentifier()); + xml.endTag(null, "guid"); + } + if (item.getMedia() != null) { + xml.startTag(null, "enclosure"); + xml.attribute(null, "url", item.getMedia().getDownload_url()); + xml.attribute(null, "length", String.valueOf(item.getMedia().getSize())); + xml.attribute(null, "type", item.getMedia().getMime_type()); + xml.endTag(null, "enclosure"); + } + if (item.getPaymentLink() != null) { + GeneratorUtil.addPaymentLink(xml, item.getPaymentLink(), true); + } + + xml.endTag(null, "item"); + } + } + + xml.endTag(null, "channel"); + xml.endTag(null, "rss"); + + xml.endDocument(); + } +} |