summaryrefslogtreecommitdiff
path: root/app/src/androidTest/java/de/test/antennapod/util/syndication/FeedDiscovererTest.java
blob: 2dda77524b4545d05d20b1daddab7a4c80b526f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package de.test.antennapod.util.syndication;

import androidx.test.InstrumentationRegistry;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Map;

import de.danoeh.antennapod.core.util.syndication.FeedDiscoverer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

/**
 * Test class for FeedDiscoverer
 */
public class FeedDiscovererTest {

    private FeedDiscoverer fd;

    private File testDir;

    @Before
    public void setUp() throws Exception {
        fd = new FeedDiscoverer();
        testDir = new File(InstrumentationRegistry.getTargetContext().getFilesDir(), "FeedDiscovererTest");
        testDir.mkdir();
        assertTrue(testDir.exists());
    }

    @After
    public void tearDown() throws Exception {
        FileUtils.deleteDirectory(testDir);
    }

    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));
        }
    }

    @Test
    public void testAlternateRSSWithTitleAbsolute() throws Exception {
        checkFindUrls(true, true, true, true, true);
    }

    @Test
    public void testAlternateRSSWithTitleRelative() throws Exception {
        checkFindUrls(true, true, true, false, true);
    }

    @Test
    public void testAlternateRSSNoTitleAbsolute() throws Exception {
        checkFindUrls(true, true, false, true, true);
    }

    @Test
    public void testAlternateRSSNoTitleRelative() throws Exception {
        checkFindUrls(true, true, false, false, true);
    }

    @Test
    public void testAlternateAtomWithTitleAbsolute() throws Exception {
        checkFindUrls(true, false, true, true, true);
    }

    @Test
    public void testFeedAtomWithTitleAbsolute() throws Exception {
        checkFindUrls(false, false, true, true, true);
    }

    @Test
    public void testAlternateRSSWithTitleAbsoluteFromFile() throws Exception {
        checkFindUrls(true, true, true, true, false);
    }
}