aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorSamuel Tardieu <sam@rfc1149.net>2015-02-11 18:59:05 +0100
committerSamuel Tardieu <sam@rfc1149.net>2015-02-11 18:59:05 +0100
commit7ec2b8696f4dac0fc27075bfb0aa5846baa87168 (patch)
tree656a14a18fb5004e9809120c1be3f6504dcec010 /tests
parentc5d3092ce9136a6d5c2eb436e2b0e0719859d3d1 (diff)
downloadcgeo-7ec2b8696f4dac0fc27075bfb0aa5846baa87168.zip
cgeo-7ec2b8696f4dac0fc27075bfb0aa5846baa87168.tar.gz
cgeo-7ec2b8696f4dac0fc27075bfb0aa5846baa87168.tar.bz2
Add FileUtils tests
Diffstat (limited to 'tests')
-rw-r--r--tests/src/cgeo/geocaching/utils/FileUtilsTest.java52
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/src/cgeo/geocaching/utils/FileUtilsTest.java b/tests/src/cgeo/geocaching/utils/FileUtilsTest.java
new file mode 100644
index 0000000..17aed70
--- /dev/null
+++ b/tests/src/cgeo/geocaching/utils/FileUtilsTest.java
@@ -0,0 +1,52 @@
+package cgeo.geocaching.utils;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import cgeo.geocaching.files.LocalStorage;
+
+import junit.framework.TestCase;
+
+import android.content.Context;
+import android.test.ServiceTestCase;
+
+import java.io.File;
+import java.io.IOException;
+
+public class FileUtilsTest extends TestCase {
+
+ final File cacheDir = LocalStorage.getStorageDir("automated-tests");
+ final File baseFile = new File(cacheDir, "prefix.ext");
+ final File alternative1 = new File(cacheDir, "prefix_1.ext");
+ final File alternative2 = new File(cacheDir, "prefix_2.ext");
+
+ private Context getTestContext() {
+ try {
+ return (Context) ServiceTestCase.class.getMethod("getTestContext").invoke(this);
+ } catch (final Exception e) {
+ Log.e("Unable to get test context", e);
+ return null;
+ }
+ }
+
+ public void testGetUniqueNamedFile() throws IOException {
+ LocalStorage.deleteDirectory(cacheDir);
+ cacheDir.mkdirs();
+ try {
+ assertThat(FileUtils.getUniqueNamedFile(baseFile)).isEqualTo(baseFile);
+ baseFile.createNewFile();
+ assertThat(FileUtils.getUniqueNamedFile(baseFile)).isEqualTo(alternative1);
+ alternative1.createNewFile();
+ assertThat(FileUtils.getUniqueNamedFile(baseFile)).isEqualTo(alternative2);
+ assertThat(FileUtils.getUniqueNamedFile(baseFile)).isEqualTo(alternative2);
+ } finally {
+ LocalStorage.deleteDirectory(cacheDir);
+ }
+ }
+
+ public void testFileUrl() {
+ assertThat(FileUtils.isFileUrl("file:///tmp/foo/bar")).isTrue();
+ assertThat(FileUtils.isFileUrl("http://www.google.com")).isFalse();
+ assertThat(FileUtils.fileToUrl(new File("/tmp/foo/bar"))).isEqualTo("file:///tmp/foo/bar");
+ assertThat(FileUtils.urlToFile("file:///tmp/foo/bar").getPath()).isEqualTo("/tmp/foo/bar");
+ }
+}