summaryrefslogtreecommitdiff
path: root/app/src/androidTest/java/de/test/antennapod/util/FilenameGeneratorTest.java
blob: 93e5bcb74d8545e8ef50a47717c10768988397c8 (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
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.apache.commons.lang3.StringUtils;
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.assertNotEquals;
import static org.junit.Assert.assertTrue;

@SmallTest
public class FilenameGeneratorTest {

    public FilenameGeneratorTest() {
        super();
    }

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

    @Test
    public void testGenerateFileName1() throws IOException {
        String result = FileNameGenerator.generateFileName("ab/c: <abc");
        assertEquals(result, "abc abc");
        createFiles(result);
    }

    @Test
    public void testGenerateFileName2() throws IOException {
        String result = FileNameGenerator.generateFileName("abc abc ");
        assertEquals(result, "abc abc");
        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 testFeedTitleContainsAccents() {
        String result = FileNameGenerator.generateFileName("Äàáâãå");
        assertEquals("Aaaaaa", result);
    }

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

    @Test
    public void testLongFilename() throws IOException {
        String longName = StringUtils.repeat("x", 20 + FileNameGenerator.MAX_FILENAME_LENGTH);
        String result = FileNameGenerator.generateFileName(longName);
        assertTrue(result.length() <= FileNameGenerator.MAX_FILENAME_LENGTH);
        createFiles(result);
    }

    @Test
    public void testLongFilenameNotEquals() {
        // Verify that the name is not just trimmed and different suffixes end up with the same name
        String longName = StringUtils.repeat("x", 20 + FileNameGenerator.MAX_FILENAME_LENGTH);
        String result1 = FileNameGenerator.generateFileName(longName + "a");
        String result2 = FileNameGenerator.generateFileName(longName + "b");
        assertNotEquals(result1, result2);
    }

    /**
     * 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());
    }

}