summaryrefslogtreecommitdiff
path: root/app/src/androidTest/java/de/test/antennapod/util/FilenameGeneratorTest.java
blob: d33eb55b80906dae650ba70c95020ba51ddac906 (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
package de.test.antennapod.util;

import android.content.Context;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
import android.text.TextUtils;

import java.io.File;
import java.io.IOException;

import de.danoeh.antennapod.core.util.FileNameGenerator;
import org.junit.After;
import org.junit.Test;

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

@SmallTest
public class FilenameGeneratorTest {

    private static final String VALID1 = "abc abc";
    private static final String INVALID1 = "ab/c: <abc";
    private static final String INVALID2 = "abc abc ";

    public FilenameGeneratorTest() {
        super();
    }

    @Test
    public void testGenerateFileName() throws IOException {
        String result = FileNameGenerator.generateFileName(VALID1);
        assertEquals(result, VALID1);
        createFiles(result);
    }

    @Test
    public void testGenerateFileName1() throws IOException {
        String result = FileNameGenerator.generateFileName(INVALID1);
        assertEquals(result, VALID1);
        createFiles(result);
    }

    @Test
    public void testGenerateFileName2() throws IOException {
        String result = FileNameGenerator.generateFileName(INVALID2);
        assertEquals(result, VALID1);
        createFiles(result);
    }

    @Test
    public void testFeedTitleContainsApostrophe() {
        String result = FileNameGenerator.generateFileName("Feed's Title ...");
        assertEquals("Feeds Title", result);
    }

    @Test
    public void testFeedTitleContainsDash() {
        String result = FileNameGenerator.generateFileName("Left - Right");
        assertEquals("Left - Right", result);
    }

    @Test
    public void testInvalidInput() {
        String result = FileNameGenerator.generateFileName("???");
        assertFalse(TextUtils.isEmpty(result));
    }

    /**
     * Tests if files can be created.
     *
     * @throws IOException
     */
    private void createFiles(String name) throws IOException {
        File cache = InstrumentationRegistry.getInstrumentation().getTargetContext().getExternalCacheDir();
        File testFile = new File(cache, name);
        testFile.mkdir();
        assertTrue(testFile.exists());
        testFile.delete();
        assertTrue(testFile.createNewFile());

    }

    @After
    public void tearDown() {
        Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
        File f = new File(context.getExternalCacheDir(), VALID1);
        f.delete();
    }

}