Index: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/AndroidFileSystemTest.java |
diff --git a/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/AndroidFileSystemTest.java b/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/AndroidFileSystemTest.java |
new file mode 100644 |
index 0000000000000000000000000000000000000000..17b6e7d3ee57d81995ccc0087f664fa9ea36e8b1 |
--- /dev/null |
+++ b/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/AndroidFileSystemTest.java |
@@ -0,0 +1,295 @@ |
+/* |
+ * This file is part of Adblock Plus <https://adblockplus.org/>, |
+ * Copyright (C) 2006-2017 eyeo GmbH |
+ * |
+ * Adblock Plus is free software: you can redistribute it and/or modify |
+ * it under the terms of the GNU General Public License version 3 as |
+ * published by the Free Software Foundation. |
+ * |
+ * Adblock Plus is distributed in the hope that it will be useful, |
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of |
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
+ * GNU General Public License for more details. |
+ * |
+ * You should have received a copy of the GNU General Public License |
+ * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. |
+ */ |
+ |
+package org.adblockplus.libadblockplus.tests; |
+ |
+import android.test.AndroidTestCase; |
+ |
+import org.adblockplus.libadblockplus.AdblockPlusException; |
+import org.adblockplus.libadblockplus.FileSystem; |
+import org.adblockplus.libadblockplus.FileSystemUtils; |
+import org.adblockplus.libadblockplus.android.AndroidFileSystem; |
+import org.junit.Test; |
+ |
+import java.io.File; |
+import java.io.FileNotFoundException; |
+import java.io.IOException; |
+ |
+public class AndroidFileSystemTest extends AndroidTestCase |
+{ |
+ protected final FileSystemUtils fileSystemUtils = new FileSystemUtils(); |
+ protected static final String DATA = "12345qwerty"; |
+ protected AndroidFileSystem fileSystem; |
+ |
+ @Override |
+ protected void setUp() throws Exception |
+ { |
+ fileSystem = new AndroidFileSystem(getContext().getCacheDir()); |
+ } |
+ |
+ protected String generateUniqueFilename() |
+ { |
+ return fileSystemUtils.generateUniqueFileName("file", ".tmp"); |
+ } |
+ |
+ protected File getFullPath(String path) |
+ { |
+ return new File(fileSystem.getBasePath(), path); |
+ } |
+ |
+ @Test |
+ public void testWriteNullFile() throws FileNotFoundException |
+ { |
+ String path = generateUniqueFilename(); |
+ File file = getFullPath(path); |
+ assertFalse(file.exists()); |
+ |
+ fileSystem.write(path, null); |
+ |
+ assertTrue(file.exists()); |
+ assertEquals("", fileSystemUtils.readFile(file)); |
+ } |
+ |
+ @Test |
+ public void testWriteEmptyFile() throws FileNotFoundException |
+ { |
+ String path = generateUniqueFilename(); |
+ File file = getFullPath(path); |
+ assertFalse(file.exists()); |
+ |
+ fileSystem.write(path, ""); |
+ |
+ assertTrue(file.exists()); |
+ assertEquals("", fileSystemUtils.readFile(file)); |
+ } |
+ |
+ @Test |
+ public void testWriteInvalidPath() |
+ { |
+ String path = "notExistingDirectory/" + generateUniqueFilename(); |
+ File file = getFullPath(path); |
+ assertFalse(file.exists()); |
+ File notExistingDirectoryFile = file.getParentFile(); |
+ assertFalse(notExistingDirectoryFile.exists()); |
+ |
+ try |
+ { |
+ fileSystem.write(path, DATA); |
+ fail("Exception should be thrown"); |
+ } |
+ catch (AdblockPlusException e) |
+ { |
+ // ignored |
+ } |
+ } |
+ |
+ @Test |
+ public void testWriteSingleLineData() throws FileNotFoundException |
+ { |
+ String path = generateUniqueFilename(); |
+ File file = getFullPath(path); |
+ assertFalse(file.exists()); |
+ |
+ final String singleLineData = DATA.replace("\n", ""); |
+ fileSystem.write(path, singleLineData); |
+ |
+ assertTrue(file.exists()); |
+ assertEquals(singleLineData, fileSystemUtils.readFile(file)); |
+ } |
+ |
+ @Test |
+ public void testWriteMultiLineData() throws FileNotFoundException |
+ { |
+ String path = generateUniqueFilename(); |
+ File file = getFullPath(path); |
+ assertFalse(file.exists()); |
+ |
+ final String multiLineData = DATA + "\n" + DATA; |
+ fileSystem.write(path, multiLineData); |
+ |
+ assertTrue(file.exists()); |
+ assertEquals(multiLineData, fileSystemUtils.readFile(file)); |
+ } |
+ |
+ @Test |
+ public void testReadEmptyFile() throws IOException |
+ { |
+ String path = generateUniqueFilename(); |
+ File file = getFullPath(path); |
+ assertFalse(file.exists()); |
+ |
+ fileSystemUtils.writeFile(file, ""); |
+ assertTrue(file.exists()); |
+ |
+ assertEquals("", fileSystem.read(path)); |
+ } |
+ |
+ @Test |
+ public void testReadSingleLineData() throws IOException |
+ { |
+ String path = generateUniqueFilename(); |
+ File file = getFullPath(path); |
+ assertFalse(file.exists()); |
+ |
+ final String singleLineData = DATA.replace("\n", ""); |
+ fileSystemUtils.writeFile(file, singleLineData); |
+ assertTrue(file.exists()); |
+ |
+ assertEquals(singleLineData, fileSystem.read(path)); |
+ } |
+ |
+ @Test |
+ public void testReadMultiLineData() throws IOException |
+ { |
+ String path = generateUniqueFilename(); |
+ File file = getFullPath(path); |
+ assertFalse(file.exists()); |
+ |
+ final String multiLineData = DATA + "\n" + DATA; |
+ fileSystemUtils.writeFile(file, multiLineData); |
+ assertTrue(file.exists()); |
+ |
+ assertEquals(multiLineData, fileSystem.read(path)); |
+ } |
+ |
+ @Test |
+ public void testMoveExistingFile() throws IOException |
+ { |
+ String fromPath = generateUniqueFilename(); |
+ File fromFile = getFullPath(fromPath); |
+ String toPath = generateUniqueFilename(); |
+ File toFile = getFullPath(toPath); |
+ fileSystemUtils.writeFile(fromFile, DATA); |
+ |
+ assertTrue(fromFile.exists()); |
+ assertFalse(toFile.exists()); |
+ |
+ fileSystem.move(fromPath, toPath); |
+ |
+ assertFalse(fromFile.exists()); |
+ assertTrue(toFile.exists()); |
+ assertEquals(DATA, fileSystemUtils.readFile(toFile)); |
+ } |
+ |
+ @Test |
+ public void testMoveNotExistingFile() |
+ { |
+ String fromPath = generateUniqueFilename(); |
+ File fromFile = getFullPath(fromPath); |
+ String toPath = generateUniqueFilename(); |
+ assertFalse(fromFile.exists()); |
+ |
+ try |
+ { |
+ fileSystem.move(fromPath, toPath); |
+ fail("Exception should be thrown"); |
+ } |
+ catch (AdblockPlusException e) |
+ { |
+ // ignored |
+ } |
+ } |
+ |
+ @Test |
+ public void testRemoveExistingFile() throws IOException |
+ { |
+ String path = generateUniqueFilename(); |
+ File file = getFullPath(path); |
+ fileSystemUtils.writeFile(file, DATA); |
+ |
+ assertTrue(file.exists()); |
+ fileSystem.remove(path); |
+ assertFalse(file.exists()); |
+ } |
+ |
+ @Test |
+ public void testRemoveNotExistingFile() |
+ { |
+ String path = generateUniqueFilename(); |
+ File file = getFullPath(path); |
+ assertFalse(file.exists()); |
+ |
+ fileSystem.remove(path); |
+ assertFalse(file.exists()); |
+ } |
+ |
+ @Test |
+ public void testStatExistingFile() throws IOException |
+ { |
+ String path = generateUniqueFilename(); |
+ File file = getFullPath(path); |
+ fileSystemUtils.writeFile(file, DATA); |
+ assertTrue(file.exists()); |
+ |
+ FileSystem.StatResult stat = fileSystem.stat(path); |
+ assertNotNull(stat); |
+ assertTrue(stat.exists()); |
+ assertFalse(stat.isDirectory()); |
+ assertTrue(stat.isFile()); |
+ assertTrue(stat.getLastModified() > 0); |
+ } |
+ |
+ @Test |
+ public void testStatExistingDirectory() |
+ { |
+ String path = generateUniqueFilename(); |
+ File file = getFullPath(path); |
+ file.mkdir(); |
+ |
+ FileSystem.StatResult stat = fileSystem.stat(path); |
+ assertNotNull(stat); |
+ assertTrue(stat.exists()); |
+ assertFalse(stat.isFile()); |
+ assertTrue(stat.isDirectory()); |
+ assertTrue(stat.getLastModified() > 0); |
+ } |
+ |
+ @Test |
+ public void testStatNotExistingFile() |
+ { |
+ String path = generateUniqueFilename(); |
+ File file = getFullPath(path); |
+ assertFalse(file.exists()); |
+ |
+ fileSystem.remove(path); |
+ assertFalse(file.exists()); |
+ FileSystem.StatResult notExistingStat = fileSystem.stat(path); |
+ assertFalse(notExistingStat.exists()); |
+ assertEquals(0l, notExistingStat.getLastModified()); |
+ } |
+ |
+ @Test |
+ public void testResolveAbsolute() |
+ { |
+ String path = generateUniqueFilename(); |
+ String absolutePath = fileSystem.resolve(path); |
+ String fullPath = getFullPath(path).getAbsolutePath(); |
+ String fullPathFromBasePath = fullPath |
+ .substring(fileSystem.getBasePath() |
+ .getAbsolutePath().length()); |
+ assertEquals(fullPathFromBasePath, absolutePath); |
+ } |
+ |
+ @Test |
+ public void testResolveRelative() |
+ { |
+ String relativePath = "./file"; |
+ String absolutePath = fileSystem.resolve(relativePath); |
+ assertNotNull(absolutePath); |
+ assertTrue(absolutePath.startsWith("/")); |
+ } |
+} |