Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/AndroidFileSystemTest.java

Issue 29424615: Issue 4231 - Fix unstable FilterEngineTest.testSetRemoveFilterChangeCallback (Closed)
Patch Set: changed impl for reading file as bytes array, modified test. AndroidFileSystem now does not resolveā€¦ Created May 29, 2017, 11:26 a.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..8b693ac8f59709c320ebf5ea8851063d018f2548
--- /dev/null
+++ b/libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/AndroidFileSystemTest.java
@@ -0,0 +1,432 @@
+/*
+ * 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.os.SystemClock;
+import android.test.AndroidTestCase;
+import android.util.Log;
+
+import org.adblockplus.libadblockplus.AdblockPlusException;
+import org.adblockplus.libadblockplus.AppInfo;
+import org.adblockplus.libadblockplus.FileSystem;
+import org.adblockplus.libadblockplus.FileSystemUtils;
+import org.adblockplus.libadblockplus.FilterEngine;
+import org.adblockplus.libadblockplus.JsEngine;
+import org.adblockplus.libadblockplus.android.AndroidFileSystem;
+import org.adblockplus.libadblockplus.android.AndroidWebRequest;
+import org.adblockplus.libadblockplus.android.Utils;
+import org.junit.Test;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Arrays;
+
+import static org.adblockplus.libadblockplus.FileSystemUtils.byteFromInt;
+
+public class AndroidFileSystemTest extends AndroidTestCase
+{
+ private static final String TAG = Utils.getTag(AndroidFileSystemTest.class);
+
+ protected static final String TEXT_DATA = "12345qwerty";
+ protected static final byte[] EMPTY_DATA_BYTES = {};
+ protected static final byte[] TEXT_DATA_BYTES = TEXT_DATA.getBytes();
+ protected static final byte[] BINARY_DATA_BYTES = new byte[]
+ {
+ byteFromInt(1),
+ byteFromInt(2),
+ byteFromInt(3)
+ };
+
+ protected final FileSystemUtils fileSystemUtils = new FileSystemUtils();
+ protected File basePathFile;
+ protected AndroidFileSystem fileSystem;
+
+ @Override
+ protected void setUp() throws Exception
+ {
+ String tmpFolder = FileSystemUtils.generateUniqueFileName("tmp", null);
+ basePathFile = new File(getContext().getCacheDir(), tmpFolder);
+ basePathFile.mkdirs();
+
+ fileSystem = new AndroidFileSystem(basePathFile);
+ }
+
+ protected String generateUniqueFilename()
+ {
+ return fileSystemUtils.generateUniqueFileName("file", ".tmp");
+ }
+
+ protected File getFullPath(String path)
+ {
+ return new File(fileSystem.getBasePath(), path);
+ }
+
+ @Test
+ public void testWriteReadBinaryFile() throws IOException
+ {
+ String path = generateUniqueFilename();
+ String resolvedPath = fileSystem.resolve(path);
+ File file = getFullPath(path);
+ assertFalse(file.exists());
+
+ fileSystem.write(resolvedPath, BINARY_DATA_BYTES);
+
+ assertTrue(file.exists());
+ assertTrue(Arrays.equals(BINARY_DATA_BYTES, fileSystemUtils.readFile(file)));
+
+ byte[] data = fileSystem.read(resolvedPath);
+ assertNotNull(data);
+ assertTrue(Arrays.equals(BINARY_DATA_BYTES, data));
+ }
+
+ @Test
+ public void testWriteNullFile() throws IOException
+ {
+ String path = generateUniqueFilename();
+ String resolvedPath = fileSystem.resolve(path);
+ File file = getFullPath(path);
+ assertFalse(file.exists());
+
+ fileSystem.write(resolvedPath, null);
+
+ assertTrue(file.exists());
+ assertTrue(Arrays.equals(EMPTY_DATA_BYTES, fileSystemUtils.readFile(file)));
+ }
+
+ @Test
+ public void testWriteEmptyFile() throws IOException
+ {
+ String path = generateUniqueFilename();
+ String resolvedPath = fileSystem.resolve(path);
+ File file = getFullPath(path);
+ assertFalse(file.exists());
+
+ fileSystem.write(resolvedPath, EMPTY_DATA_BYTES);
+
+ assertTrue(file.exists());
+ assertTrue(Arrays.equals(EMPTY_DATA_BYTES, fileSystemUtils.readFile(file)));
+ }
+
+ @Test
+ public void testWriteInvalidPath()
+ {
+ String path = "notExistingDirectory/" + generateUniqueFilename();
+ String resolvedPath = fileSystem.resolve(path);
+ File file = getFullPath(path);
+ assertFalse(file.exists());
+ File notExistingDirectoryFile = file.getParentFile();
+ assertFalse(notExistingDirectoryFile.exists());
+
+ try
+ {
+ fileSystem.write(resolvedPath, TEXT_DATA_BYTES);
+ fail("Exception should be thrown");
+ }
+ catch (AdblockPlusException e)
+ {
+ // ignored
+ }
+ }
+
+ @Test
+ public void testWriteSingleLineData() throws IOException
+ {
+ String path = generateUniqueFilename();
+ String resolvedPath = fileSystem.resolve(path);
+ File file = getFullPath(path);
+ assertFalse(file.exists());
+
+ final String singleLineData = TEXT_DATA.replace("\n", "");
+ fileSystem.write(resolvedPath, singleLineData.getBytes());
+
+ assertTrue(file.exists());
+ assertTrue(Arrays.equals(singleLineData.getBytes(), fileSystemUtils.readFile(file)));
+ }
+
+ @Test
+ public void testWriteMultiLineData() throws IOException
+ {
+ String path = generateUniqueFilename();
+ String resolvedPath = fileSystem.resolve(path);
+ File file = getFullPath(path);
+ assertFalse(file.exists());
+
+ final String multiLineData = TEXT_DATA + "\n" + TEXT_DATA;
+ fileSystem.write(resolvedPath, multiLineData.getBytes());
+
+ assertTrue(file.exists());
+ assertTrue(Arrays.equals(multiLineData.getBytes(), fileSystemUtils.readFile(file)));
+ }
+
+ @Test
+ public void testReadEmptyFile() throws IOException
+ {
+ String path = generateUniqueFilename();
+ String resolvedPath = fileSystem.resolve(path);
+ File file = getFullPath(path);
+ assertFalse(file.exists());
+
+ fileSystemUtils.writeFile(file, EMPTY_DATA_BYTES);
+ assertTrue(file.exists());
+
+ assertTrue(Arrays.equals(EMPTY_DATA_BYTES, fileSystem.read(resolvedPath)));
+ }
+
+ @Test
+ public void testReadSingleLineData() throws IOException
+ {
+ String path = generateUniqueFilename();
+ String resolvedPath = fileSystem.resolve(path);
+ File file = getFullPath(path);
+ assertFalse(file.exists());
+
+ final String singleLineData = TEXT_DATA.replace("\n", "");
+ fileSystemUtils.writeFile(file, singleLineData.getBytes());
+ assertTrue(file.exists());
+
+ assertTrue(Arrays.equals(singleLineData.getBytes(), fileSystem.read(resolvedPath)));
+ }
+
+ @Test
+ public void testReadMultiLineData() throws IOException
+ {
+ String path = generateUniqueFilename();
+ String resolvedPath = fileSystem.resolve(path);
+ File file = getFullPath(path);
+ assertFalse(file.exists());
+
+ final String multiLineData = TEXT_DATA + "\n" + TEXT_DATA;
+ fileSystemUtils.writeFile(file, multiLineData.getBytes());
+ assertTrue(file.exists());
+
+ assertTrue(Arrays.equals(multiLineData.getBytes(), fileSystem.read(resolvedPath)));
+ }
+
+ @Test
+ public void testMoveExistingFile() throws IOException
+ {
+ String fromPath = generateUniqueFilename();
+ String resolvedFromPath= fileSystem.resolve(fromPath);
+ File fromFile = getFullPath(fromPath);
+ String toPath = generateUniqueFilename();
+ String resolvedToPath = fileSystem.resolve(toPath);
+ File toFile = getFullPath(toPath);
+ fileSystemUtils.writeFile(fromFile, TEXT_DATA_BYTES);
+
+ assertTrue(fromFile.exists());
+ assertFalse(toFile.exists());
+
+ fileSystem.move(resolvedFromPath, resolvedToPath);
+
+ assertFalse(fromFile.exists());
+ assertTrue(toFile.exists());
+ assertTrue(Arrays.equals(TEXT_DATA_BYTES, fileSystemUtils.readFile(toFile)));
+ }
+
+ @Test
+ public void testMoveNotExistingFile()
+ {
+ String fromPath = generateUniqueFilename();
+ String resolvedFromPath = fileSystem.resolve(fromPath);
+ File fromFile = getFullPath(fromPath);
+ String toPath = generateUniqueFilename();
+ String resolvedToPath = fileSystem.resolve(toPath);
+ assertFalse(fromFile.exists());
+
+ try
+ {
+ fileSystem.move(resolvedFromPath, resolvedToPath);
+ fail("Exception should be thrown");
+ }
+ catch (AdblockPlusException e)
+ {
+ // ignored
+ }
+ }
+
+ @Test
+ public void testRemoveExistingFile() throws IOException
+ {
+ String path = generateUniqueFilename();
+ String resolvedPath = fileSystem.resolve(path);
+ File file = getFullPath(path);
+ fileSystemUtils.writeFile(file, TEXT_DATA_BYTES);
+
+ assertTrue(file.exists());
+ fileSystem.remove(resolvedPath);
+ assertFalse(file.exists());
+ }
+
+ @Test
+ public void testRemoveNotExistingFile()
+ {
+ String path = generateUniqueFilename();
+ String resolvedPath = fileSystem.resolve(path);
+ File file = getFullPath(path);
+ assertFalse(file.exists());
+
+ fileSystem.remove(resolvedPath);
+ assertFalse(file.exists());
+ }
+
+ @Test
+ public void testStatExistingFile() throws IOException
+ {
+ String path = generateUniqueFilename();
+ String resolvedPath = fileSystem.resolve(path);
+ File file = getFullPath(path);
+ fileSystemUtils.writeFile(file, TEXT_DATA_BYTES);
+ assertTrue(file.exists());
+
+ FileSystem.StatResult stat = fileSystem.stat(resolvedPath);
+ assertNotNull(stat);
+ assertTrue(stat.exists());
+ assertFalse(stat.isDirectory());
+ assertTrue(stat.isFile());
+ assertTrue(stat.getLastModified() > 0);
+ }
+
+ @Test
+ public void testStatExistingDirectory()
+ {
+ String path = generateUniqueFilename();
+ String resolvedPath = fileSystem.resolve(path);
+ File file = getFullPath(path);
+ file.mkdir();
+
+ FileSystem.StatResult stat = fileSystem.stat(resolvedPath);
+ assertNotNull(stat);
+ assertTrue(stat.exists());
+ assertFalse(stat.isFile());
+ assertTrue(stat.isDirectory());
+ assertTrue(stat.getLastModified() > 0);
+ }
+
+ @Test
+ public void testStatNotExistingFile()
+ {
+ String path = generateUniqueFilename();
+ String resolvedPath = fileSystem.resolve(path);
+ File file = getFullPath(path);
+ assertFalse(file.exists());
+
+ fileSystem.remove(resolvedPath);
+ assertFalse(file.exists());
+ FileSystem.StatResult notExistingStat = fileSystem.stat(resolvedPath);
+ assertFalse(notExistingStat.exists());
+ assertEquals(0l, notExistingStat.getLastModified());
+ }
+
+ @Test
+ public void testResolveAbsolute()
+ {
+ String path = generateUniqueFilename();
+ String resolvedPath = fileSystem.resolve(path);
+ String fullPath = getFullPath(path).getAbsolutePath();
+ assertEquals(fullPath, resolvedPath);
+ }
+
+ @Test
+ public void testResolveRelative()
+ {
+ String relativePath = "./folder/file2";
+ String resolvedPath = fileSystem.resolve(relativePath);
+ assertNotNull(resolvedPath);
+ assertEquals(new File(basePathFile, relativePath).getAbsolutePath(), resolvedPath);
+ }
+
+ private boolean readInvoked;
+ private boolean writeInvoked;
+ private boolean statInvoked;
+ private boolean resolveInvoked;
+
+ private final class TestAndroidFileSystem extends AndroidFileSystem
+ {
+ public TestAndroidFileSystem(File basePath)
+ {
+ super(basePath);
+ }
+
+ @Override
+ public byte[] read(String path)
+ {
+ readInvoked = true;
+ try
+ {
+ Log.d(TAG, "Reading from " + path);
+ String content = new String(FileSystemUtils.readFile(new File(path)), "UTF-8");
+ Log.d(TAG, "Read from " + path + ":\n" + content);
+ }
+ catch (IOException e)
+ {
+ throw new RuntimeException(e);
+ }
+
+ return super.read(path);
+ }
+
+ @Override
+ public void write(String path, byte[] data)
+ {
+ writeInvoked = true;
+ Log.d(TAG, "Write " + data.length + " bytes to " + path);
+ super.write(path, data);
+ }
+
+ @Override
+ public StatResult stat(String path)
+ {
+ statInvoked = true;
+ StatResult stat = super.stat(path);
+ Log.d(TAG, "Stat for " + path + " is " + stat);
+ return stat;
+ }
+
+ @Override
+ public String resolve(String path)
+ {
+ resolveInvoked = true;
+
+ String resolvedPath = super.resolve(path);
+ Log.d(TAG, "Resolve " + path + " to " + resolvedPath);
+ return resolvedPath;
+ }
+ }
+
+ @Test
+ public void testInvokeFromNativeCode()
+ {
+ readInvoked = false;
+ writeInvoked = false;
+ statInvoked = false;
+ resolveInvoked = false;
+
+ JsEngine jsEngine = new JsEngine(AppInfo.builder().build());
+ jsEngine.setDefaultLogSystem();
+ jsEngine.setFileSystem(new TestAndroidFileSystem(basePathFile));
+ jsEngine.setWebRequest(new AndroidWebRequest());
+
+ FilterEngine filterEngine = new FilterEngine(jsEngine);
+ SystemClock.sleep(10 * 1000);
+
+ assertTrue(readInvoked);
+ assertTrue(writeInvoked);
+ assertTrue(statInvoked);
+ assertTrue(resolveInvoked);
+ }
+}

Powered by Google App Engine
This is Rietveld