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..43a25b4dd165dc3db246334220204971a9ea6848 |
--- /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-2016 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.android.AndroidFileSystem; |
+import org.adblockplus.libadblockplus.AdblockPlusException; |
+import org.adblockplus.libadblockplus.FileSystem; |
+import org.adblockplus.libadblockplus.FileSystemUtils; |
+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 final static String DATA = "12345qwerty"; |
diegocarloslima
2017/04/27 12:22:39
By our code style, the modifier order should be 's
anton
2017/04/28 08:25:45
Done.
|
+ 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 SINGLE_LINE_DATA = DATA.replace("\n", ""); |
diegocarloslima
2017/04/27 12:22:39
by the convention, only static final variables (cl
anton
2017/04/28 08:25:45
Done.
|
+ fileSystem.write(path, SINGLE_LINE_DATA); |
+ |
+ assertTrue(file.exists()); |
+ assertEquals(SINGLE_LINE_DATA, fileSystemUtils.readFile(file)); |
+ } |
+ |
+ @Test |
+ public void testWriteMultiLineData() throws FileNotFoundException |
+ { |
+ String path = generateUniqueFilename(); |
+ File file = getFullPath(path); |
+ assertFalse(file.exists()); |
+ |
+ final String MULTI_LINE_DATA = DATA + "\n" + DATA; |
diegocarloslima
2017/04/27 12:22:39
by the convention, only static final variables (cl
anton
2017/04/28 08:25:44
Done.
|
+ fileSystem.write(path, MULTI_LINE_DATA); |
+ |
+ assertTrue(file.exists()); |
+ assertEquals(MULTI_LINE_DATA, 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 SINGLE_LINE_DATA = DATA.replace("\n", ""); |
diegocarloslima
2017/04/27 12:22:39
by the convention, only static final variables (cl
anton
2017/04/28 08:25:44
Done.
|
+ fileSystemUtils.writeFile(file, SINGLE_LINE_DATA); |
+ assertTrue(file.exists()); |
+ |
+ assertEquals(SINGLE_LINE_DATA, fileSystem.read(path)); |
+ } |
+ |
+ @Test |
+ public void testReadMultiLineData() throws IOException |
+ { |
+ String path = generateUniqueFilename(); |
+ File file = getFullPath(path); |
+ assertFalse(file.exists()); |
+ |
+ final String MULTI_LINE_DATA = DATA + "\n" + DATA; |
diegocarloslima
2017/04/27 12:22:40
by the convention, only static final variables (cl
anton
2017/04/28 08:25:44
Done.
|
+ fileSystemUtils.writeFile(file, MULTI_LINE_DATA); |
+ assertTrue(file.exists()); |
+ |
+ assertEquals(MULTI_LINE_DATA, 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("/")); |
+ } |
+} |