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

Side by Side 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.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2017 eyeo GmbH
4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation.
8 *
9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package org.adblockplus.libadblockplus.tests;
19
20 import android.os.SystemClock;
21 import android.test.AndroidTestCase;
22 import android.util.Log;
23
24 import org.adblockplus.libadblockplus.AdblockPlusException;
25 import org.adblockplus.libadblockplus.AppInfo;
26 import org.adblockplus.libadblockplus.FileSystem;
27 import org.adblockplus.libadblockplus.FileSystemUtils;
28 import org.adblockplus.libadblockplus.FilterEngine;
29 import org.adblockplus.libadblockplus.JsEngine;
30 import org.adblockplus.libadblockplus.android.AndroidFileSystem;
31 import org.adblockplus.libadblockplus.android.AndroidWebRequest;
32 import org.adblockplus.libadblockplus.android.Utils;
33 import org.junit.Test;
34
35 import java.io.File;
36 import java.io.IOException;
37 import java.util.Arrays;
38
39 import static org.adblockplus.libadblockplus.FileSystemUtils.byteFromInt;
40
41 public class AndroidFileSystemTest extends AndroidTestCase
42 {
43 private static final String TAG = Utils.getTag(AndroidFileSystemTest.class);
44
45 protected static final String TEXT_DATA = "12345qwerty";
46 protected static final byte[] EMPTY_DATA_BYTES = {};
47 protected static final byte[] TEXT_DATA_BYTES = TEXT_DATA.getBytes();
48 protected static final byte[] BINARY_DATA_BYTES = new byte[]
49 {
50 byteFromInt(1),
51 byteFromInt(2),
52 byteFromInt(3)
53 };
54
55 protected final FileSystemUtils fileSystemUtils = new FileSystemUtils();
56 protected File basePathFile;
57 protected AndroidFileSystem fileSystem;
58
59 @Override
60 protected void setUp() throws Exception
61 {
62 String tmpFolder = FileSystemUtils.generateUniqueFileName("tmp", null);
63 basePathFile = new File(getContext().getCacheDir(), tmpFolder);
64 basePathFile.mkdirs();
65
66 fileSystem = new AndroidFileSystem(basePathFile);
67 }
68
69 protected String generateUniqueFilename()
70 {
71 return fileSystemUtils.generateUniqueFileName("file", ".tmp");
72 }
73
74 protected File getFullPath(String path)
75 {
76 return new File(fileSystem.getBasePath(), path);
77 }
78
79 @Test
80 public void testWriteReadBinaryFile() throws IOException
81 {
82 String path = generateUniqueFilename();
83 String resolvedPath = fileSystem.resolve(path);
84 File file = getFullPath(path);
85 assertFalse(file.exists());
86
87 fileSystem.write(resolvedPath, BINARY_DATA_BYTES);
88
89 assertTrue(file.exists());
90 assertTrue(Arrays.equals(BINARY_DATA_BYTES, fileSystemUtils.readFile(file))) ;
91
92 byte[] data = fileSystem.read(resolvedPath);
93 assertNotNull(data);
94 assertTrue(Arrays.equals(BINARY_DATA_BYTES, data));
95 }
96
97 @Test
98 public void testWriteNullFile() throws IOException
99 {
100 String path = generateUniqueFilename();
101 String resolvedPath = fileSystem.resolve(path);
102 File file = getFullPath(path);
103 assertFalse(file.exists());
104
105 fileSystem.write(resolvedPath, null);
106
107 assertTrue(file.exists());
108 assertTrue(Arrays.equals(EMPTY_DATA_BYTES, fileSystemUtils.readFile(file)));
109 }
110
111 @Test
112 public void testWriteEmptyFile() throws IOException
113 {
114 String path = generateUniqueFilename();
115 String resolvedPath = fileSystem.resolve(path);
116 File file = getFullPath(path);
117 assertFalse(file.exists());
118
119 fileSystem.write(resolvedPath, EMPTY_DATA_BYTES);
120
121 assertTrue(file.exists());
122 assertTrue(Arrays.equals(EMPTY_DATA_BYTES, fileSystemUtils.readFile(file)));
123 }
124
125 @Test
126 public void testWriteInvalidPath()
127 {
128 String path = "notExistingDirectory/" + generateUniqueFilename();
129 String resolvedPath = fileSystem.resolve(path);
130 File file = getFullPath(path);
131 assertFalse(file.exists());
132 File notExistingDirectoryFile = file.getParentFile();
133 assertFalse(notExistingDirectoryFile.exists());
134
135 try
136 {
137 fileSystem.write(resolvedPath, TEXT_DATA_BYTES);
138 fail("Exception should be thrown");
139 }
140 catch (AdblockPlusException e)
141 {
142 // ignored
143 }
144 }
145
146 @Test
147 public void testWriteSingleLineData() throws IOException
148 {
149 String path = generateUniqueFilename();
150 String resolvedPath = fileSystem.resolve(path);
151 File file = getFullPath(path);
152 assertFalse(file.exists());
153
154 final String singleLineData = TEXT_DATA.replace("\n", "");
155 fileSystem.write(resolvedPath, singleLineData.getBytes());
156
157 assertTrue(file.exists());
158 assertTrue(Arrays.equals(singleLineData.getBytes(), fileSystemUtils.readFile (file)));
159 }
160
161 @Test
162 public void testWriteMultiLineData() throws IOException
163 {
164 String path = generateUniqueFilename();
165 String resolvedPath = fileSystem.resolve(path);
166 File file = getFullPath(path);
167 assertFalse(file.exists());
168
169 final String multiLineData = TEXT_DATA + "\n" + TEXT_DATA;
170 fileSystem.write(resolvedPath, multiLineData.getBytes());
171
172 assertTrue(file.exists());
173 assertTrue(Arrays.equals(multiLineData.getBytes(), fileSystemUtils.readFile( file)));
174 }
175
176 @Test
177 public void testReadEmptyFile() throws IOException
178 {
179 String path = generateUniqueFilename();
180 String resolvedPath = fileSystem.resolve(path);
181 File file = getFullPath(path);
182 assertFalse(file.exists());
183
184 fileSystemUtils.writeFile(file, EMPTY_DATA_BYTES);
185 assertTrue(file.exists());
186
187 assertTrue(Arrays.equals(EMPTY_DATA_BYTES, fileSystem.read(resolvedPath)));
188 }
189
190 @Test
191 public void testReadSingleLineData() throws IOException
192 {
193 String path = generateUniqueFilename();
194 String resolvedPath = fileSystem.resolve(path);
195 File file = getFullPath(path);
196 assertFalse(file.exists());
197
198 final String singleLineData = TEXT_DATA.replace("\n", "");
199 fileSystemUtils.writeFile(file, singleLineData.getBytes());
200 assertTrue(file.exists());
201
202 assertTrue(Arrays.equals(singleLineData.getBytes(), fileSystem.read(resolved Path)));
203 }
204
205 @Test
206 public void testReadMultiLineData() throws IOException
207 {
208 String path = generateUniqueFilename();
209 String resolvedPath = fileSystem.resolve(path);
210 File file = getFullPath(path);
211 assertFalse(file.exists());
212
213 final String multiLineData = TEXT_DATA + "\n" + TEXT_DATA;
214 fileSystemUtils.writeFile(file, multiLineData.getBytes());
215 assertTrue(file.exists());
216
217 assertTrue(Arrays.equals(multiLineData.getBytes(), fileSystem.read(resolvedP ath)));
218 }
219
220 @Test
221 public void testMoveExistingFile() throws IOException
222 {
223 String fromPath = generateUniqueFilename();
224 String resolvedFromPath= fileSystem.resolve(fromPath);
225 File fromFile = getFullPath(fromPath);
226 String toPath = generateUniqueFilename();
227 String resolvedToPath = fileSystem.resolve(toPath);
228 File toFile = getFullPath(toPath);
229 fileSystemUtils.writeFile(fromFile, TEXT_DATA_BYTES);
230
231 assertTrue(fromFile.exists());
232 assertFalse(toFile.exists());
233
234 fileSystem.move(resolvedFromPath, resolvedToPath);
235
236 assertFalse(fromFile.exists());
237 assertTrue(toFile.exists());
238 assertTrue(Arrays.equals(TEXT_DATA_BYTES, fileSystemUtils.readFile(toFile))) ;
239 }
240
241 @Test
242 public void testMoveNotExistingFile()
243 {
244 String fromPath = generateUniqueFilename();
245 String resolvedFromPath = fileSystem.resolve(fromPath);
246 File fromFile = getFullPath(fromPath);
247 String toPath = generateUniqueFilename();
248 String resolvedToPath = fileSystem.resolve(toPath);
249 assertFalse(fromFile.exists());
250
251 try
252 {
253 fileSystem.move(resolvedFromPath, resolvedToPath);
254 fail("Exception should be thrown");
255 }
256 catch (AdblockPlusException e)
257 {
258 // ignored
259 }
260 }
261
262 @Test
263 public void testRemoveExistingFile() throws IOException
264 {
265 String path = generateUniqueFilename();
266 String resolvedPath = fileSystem.resolve(path);
267 File file = getFullPath(path);
268 fileSystemUtils.writeFile(file, TEXT_DATA_BYTES);
269
270 assertTrue(file.exists());
271 fileSystem.remove(resolvedPath);
272 assertFalse(file.exists());
273 }
274
275 @Test
276 public void testRemoveNotExistingFile()
277 {
278 String path = generateUniqueFilename();
279 String resolvedPath = fileSystem.resolve(path);
280 File file = getFullPath(path);
281 assertFalse(file.exists());
282
283 fileSystem.remove(resolvedPath);
284 assertFalse(file.exists());
285 }
286
287 @Test
288 public void testStatExistingFile() throws IOException
289 {
290 String path = generateUniqueFilename();
291 String resolvedPath = fileSystem.resolve(path);
292 File file = getFullPath(path);
293 fileSystemUtils.writeFile(file, TEXT_DATA_BYTES);
294 assertTrue(file.exists());
295
296 FileSystem.StatResult stat = fileSystem.stat(resolvedPath);
297 assertNotNull(stat);
298 assertTrue(stat.exists());
299 assertFalse(stat.isDirectory());
300 assertTrue(stat.isFile());
301 assertTrue(stat.getLastModified() > 0);
302 }
303
304 @Test
305 public void testStatExistingDirectory()
306 {
307 String path = generateUniqueFilename();
308 String resolvedPath = fileSystem.resolve(path);
309 File file = getFullPath(path);
310 file.mkdir();
311
312 FileSystem.StatResult stat = fileSystem.stat(resolvedPath);
313 assertNotNull(stat);
314 assertTrue(stat.exists());
315 assertFalse(stat.isFile());
316 assertTrue(stat.isDirectory());
317 assertTrue(stat.getLastModified() > 0);
318 }
319
320 @Test
321 public void testStatNotExistingFile()
322 {
323 String path = generateUniqueFilename();
324 String resolvedPath = fileSystem.resolve(path);
325 File file = getFullPath(path);
326 assertFalse(file.exists());
327
328 fileSystem.remove(resolvedPath);
329 assertFalse(file.exists());
330 FileSystem.StatResult notExistingStat = fileSystem.stat(resolvedPath);
331 assertFalse(notExistingStat.exists());
332 assertEquals(0l, notExistingStat.getLastModified());
333 }
334
335 @Test
336 public void testResolveAbsolute()
337 {
338 String path = generateUniqueFilename();
339 String resolvedPath = fileSystem.resolve(path);
340 String fullPath = getFullPath(path).getAbsolutePath();
341 assertEquals(fullPath, resolvedPath);
342 }
343
344 @Test
345 public void testResolveRelative()
346 {
347 String relativePath = "./folder/file2";
348 String resolvedPath = fileSystem.resolve(relativePath);
349 assertNotNull(resolvedPath);
350 assertEquals(new File(basePathFile, relativePath).getAbsolutePath(), resolve dPath);
351 }
352
353 private boolean readInvoked;
354 private boolean writeInvoked;
355 private boolean statInvoked;
356 private boolean resolveInvoked;
357
358 private final class TestAndroidFileSystem extends AndroidFileSystem
359 {
360 public TestAndroidFileSystem(File basePath)
361 {
362 super(basePath);
363 }
364
365 @Override
366 public byte[] read(String path)
367 {
368 readInvoked = true;
369 try
370 {
371 Log.d(TAG, "Reading from " + path);
372 String content = new String(FileSystemUtils.readFile(new File(path)), "U TF-8");
373 Log.d(TAG, "Read from " + path + ":\n" + content);
374 }
375 catch (IOException e)
376 {
377 throw new RuntimeException(e);
378 }
379
380 return super.read(path);
381 }
382
383 @Override
384 public void write(String path, byte[] data)
385 {
386 writeInvoked = true;
387 Log.d(TAG, "Write " + data.length + " bytes to " + path);
388 super.write(path, data);
389 }
390
391 @Override
392 public StatResult stat(String path)
393 {
394 statInvoked = true;
395 StatResult stat = super.stat(path);
396 Log.d(TAG, "Stat for " + path + " is " + stat);
397 return stat;
398 }
399
400 @Override
401 public String resolve(String path)
402 {
403 resolveInvoked = true;
404
405 String resolvedPath = super.resolve(path);
406 Log.d(TAG, "Resolve " + path + " to " + resolvedPath);
407 return resolvedPath;
408 }
409 }
410
411 @Test
412 public void testInvokeFromNativeCode()
413 {
414 readInvoked = false;
415 writeInvoked = false;
416 statInvoked = false;
417 resolveInvoked = false;
418
419 JsEngine jsEngine = new JsEngine(AppInfo.builder().build());
420 jsEngine.setDefaultLogSystem();
421 jsEngine.setFileSystem(new TestAndroidFileSystem(basePathFile));
422 jsEngine.setWebRequest(new AndroidWebRequest());
423
424 FilterEngine filterEngine = new FilterEngine(jsEngine);
425 SystemClock.sleep(10 * 1000);
426
427 assertTrue(readInvoked);
428 assertTrue(writeInvoked);
429 assertTrue(statInvoked);
430 assertTrue(resolveInvoked);
431 }
432 }
OLDNEW

Powered by Google App Engine
This is Rietveld