Fixed filename generator test

This commit is contained in:
ByteHamster 2019-10-20 22:09:07 +02:00
parent 381732c582
commit 3c8a650e5b
1 changed files with 57 additions and 55 deletions

View File

@ -1,7 +1,7 @@
package de.test.antennapod.util; package de.test.antennapod.util;
import androidx.test.InstrumentationRegistry; import android.content.Context;
import androidx.test.filters.LargeTest; import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.filters.SmallTest; import androidx.test.filters.SmallTest;
import android.text.TextUtils; import android.text.TextUtils;
@ -13,77 +13,79 @@ import org.junit.After;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
@SmallTest @SmallTest
public class FilenameGeneratorTest { public class FilenameGeneratorTest {
private static final String VALID1 = "abc abc"; private static final String VALID1 = "abc abc";
private static final String INVALID1 = "ab/c: <abc"; private static final String INVALID1 = "ab/c: <abc";
private static final String INVALID2 = "abc abc "; private static final String INVALID2 = "abc abc ";
public FilenameGeneratorTest() { public FilenameGeneratorTest() {
super(); super();
} }
@Test @Test
public void testGenerateFileName() throws IOException { public void testGenerateFileName() throws IOException {
String result = FileNameGenerator.generateFileName(VALID1); String result = FileNameGenerator.generateFileName(VALID1);
assertEquals(result, VALID1); assertEquals(result, VALID1);
createFiles(result); createFiles(result);
} }
@Test @Test
public void testGenerateFileName1() throws IOException { public void testGenerateFileName1() throws IOException {
String result = FileNameGenerator.generateFileName(INVALID1); String result = FileNameGenerator.generateFileName(INVALID1);
assertEquals(result, VALID1); assertEquals(result, VALID1);
createFiles(result); createFiles(result);
} }
@Test @Test
public void testGenerateFileName2() throws IOException { public void testGenerateFileName2() throws IOException {
String result = FileNameGenerator.generateFileName(INVALID2); String result = FileNameGenerator.generateFileName(INVALID2);
assertEquals(result, VALID1); assertEquals(result, VALID1);
createFiles(result); createFiles(result);
} }
@Test @Test
public void testFeedTitleContainsApostrophe() { public void testFeedTitleContainsApostrophe() {
String result = FileNameGenerator.generateFileName("Feed's Title ..."); String result = FileNameGenerator.generateFileName("Feed's Title ...");
assertEquals("Feeds Title", result); assertEquals("Feeds Title", result);
} }
@Test @Test
public void testFeedTitleContainsDash() { public void testFeedTitleContainsDash() {
String result = FileNameGenerator.generateFileName("Left - Right"); String result = FileNameGenerator.generateFileName("Left - Right");
assertEquals("Left - Right", result); assertEquals("Left - Right", result);
} }
@Test @Test
public void testInvalidInput() { public void testInvalidInput() {
String result = FileNameGenerator.generateFileName("???"); String result = FileNameGenerator.generateFileName("???");
assertTrue(!TextUtils.isEmpty(result)); assertFalse(TextUtils.isEmpty(result));
} }
/** /**
* Tests if files can be created. * Tests if files can be created.
* *
* @throws IOException * @throws IOException
*/ */
private void createFiles(String name) throws IOException { private void createFiles(String name) throws IOException {
File cache = InstrumentationRegistry.getContext().getExternalCacheDir(); File cache = InstrumentationRegistry.getInstrumentation().getTargetContext().getExternalCacheDir();
File testFile = new File(cache, name); File testFile = new File(cache, name);
testFile.mkdir(); testFile.mkdir();
assertTrue(testFile.exists()); assertTrue(testFile.exists());
testFile.delete(); testFile.delete();
assertTrue(testFile.createNewFile()); assertTrue(testFile.createNewFile());
} }
@After @After
public void tearDown() throws Exception { public void tearDown() {
File f = new File(InstrumentationRegistry.getContext().getExternalCacheDir(), VALID1); Context context = InstrumentationRegistry.getInstrumentation().getTargetContext();
f.delete(); File f = new File(context.getExternalCacheDir(), VALID1);
} f.delete();
}
} }