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

Delta Between Two Patch Sets: libadblockplus-android-tests/src/org/adblockplus/libadblockplus/tests/AndroidFileSystemTest.java

Issue 29347315: Issue 4231 - Fix unstable FilterEngineTest.testSetRemoveFilterChangeCallback (Closed)
Left Patch Set: Introduced file system abstraction to java and using LazyFileSystem for filter tests Created July 10, 2016, 10:47 a.m.
Right Patch Set: made helper methods static, fixed 'remove' for fs callback Created Dec. 13, 2016, 9:32 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 /* 1 /*
2 * This file is part of Adblock Plus <https://adblockplus.org/>, 2 * This file is part of Adblock Plus <https://adblockplus.org/>,
3 * Copyright (C) 2006-2016 Eyeo GmbH 3 * Copyright (C) 2006-2016 Eyeo GmbH
4 * 4 *
5 * Adblock Plus is free software: you can redistribute it and/or modify 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 6 * it under the terms of the GNU General Public License version 3 as
7 * published by the Free Software Foundation. 7 * published by the Free Software Foundation.
8 * 8 *
9 * Adblock Plus is distributed in the hope that it will be useful, 9 * Adblock Plus is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details. 12 * GNU General Public License for more details.
13 * 13 *
14 * You should have received a copy of the GNU General Public License 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/>. 15 * along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
16 */ 16 */
17 17
18 package org.adblockplus.libadblockplus.tests; 18 package org.adblockplus.libadblockplus.tests;
19 19
20 import android.test.AndroidTestCase; 20 import android.test.AndroidTestCase;
21 21
22 import org.adblockplus.android.AndroidFileSystem; 22 import org.adblockplus.android.AndroidFileSystem;
23 import org.adblockplus.libadblockplus.AdblockPlusException; 23 import org.adblockplus.libadblockplus.AdblockPlusException;
24 import org.adblockplus.libadblockplus.FileSystem; 24 import org.adblockplus.libadblockplus.FileSystem;
25 import org.adblockplus.libadblockplus.FileSystemHelper; 25 import org.adblockplus.libadblockplus.FileSystemUtils;
26 import org.junit.Test; 26 import org.junit.Test;
27 27
28 import java.io.File; 28 import java.io.File;
29 import java.io.FileNotFoundException; 29 import java.io.FileNotFoundException;
30 import java.io.IOException; 30 import java.io.IOException;
31 31
32 public class AndroidFileSystemTest extends AndroidTestCase 32 public class AndroidFileSystemTest extends AndroidTestCase
33 { 33 {
34 protected final FileSystemHelper fileSystemHelper = new FileSystemHelper(); 34 protected final FileSystemUtils fileSystemUtils = new FileSystemUtils();
35 protected final static String DATA = "12345qwerty"; 35 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.
36 protected AndroidFileSystem fileSystem; 36 protected AndroidFileSystem fileSystem;
37 37
38 @Override 38 @Override
39 protected void setUp() throws Exception 39 protected void setUp() throws Exception
40 { 40 {
41 fileSystem = new AndroidFileSystem(getContext().getCacheDir()); 41 fileSystem = new AndroidFileSystem(getContext().getCacheDir());
42 } 42 }
43 43
44 protected String generateUniqueFilename() 44 protected String generateUniqueFilename()
45 { 45 {
46 return fileSystemHelper.generateUniqueFileName("file", ".tmp"); 46 return fileSystemUtils.generateUniqueFileName("file", ".tmp");
47 } 47 }
48 48
49 protected File getFullPath(String path) 49 protected File getFullPath(String path)
50 { 50 {
51 return new File(fileSystem.getBasePath(), path); 51 return new File(fileSystem.getBasePath(), path);
52 } 52 }
53 53
54 @Test 54 @Test
55 public void testWriteNullFile() throws FileNotFoundException 55 public void testWriteNullFile() throws FileNotFoundException
56 { 56 {
57 String path = generateUniqueFilename(); 57 String path = generateUniqueFilename();
58 File file = getFullPath(path); 58 File file = getFullPath(path);
59 assertFalse(file.exists()); 59 assertFalse(file.exists());
60 60
61 fileSystem.write(path, null); 61 fileSystem.write(path, null);
62 62
63 assertTrue(file.exists()); 63 assertTrue(file.exists());
64 assertEquals("", fileSystemHelper.readFile(file)); 64 assertEquals("", fileSystemUtils.readFile(file));
65 } 65 }
66 66
67 @Test 67 @Test
68 public void testWriteEmptyFile() throws FileNotFoundException 68 public void testWriteEmptyFile() throws FileNotFoundException
69 { 69 {
70 String path = generateUniqueFilename(); 70 String path = generateUniqueFilename();
71 File file = getFullPath(path); 71 File file = getFullPath(path);
72 assertFalse(file.exists()); 72 assertFalse(file.exists());
73 73
74 fileSystem.write(path, ""); 74 fileSystem.write(path, "");
75 75
76 assertTrue(file.exists()); 76 assertTrue(file.exists());
77 assertEquals("", fileSystemHelper.readFile(file)); 77 assertEquals("", fileSystemUtils.readFile(file));
78 } 78 }
79 79
80 @Test 80 @Test
81 public void testWriteInvalidPath() 81 public void testWriteInvalidPath()
82 { 82 {
83 String path = "notExistingDirectory/" + generateUniqueFilename(); 83 String path = "notExistingDirectory/" + generateUniqueFilename();
84 File file = getFullPath(path); 84 File file = getFullPath(path);
85 assertFalse(file.exists()); 85 assertFalse(file.exists());
86 File notExistingDirectoryFile = file.getParentFile(); 86 File notExistingDirectoryFile = file.getParentFile();
87 assertFalse(notExistingDirectoryFile.exists()); 87 assertFalse(notExistingDirectoryFile.exists());
88 88
89 try 89 try
90 { 90 {
91 fileSystem.write(path, DATA); 91 fileSystem.write(path, DATA);
92 fail("Exception should be thrown"); 92 fail("Exception should be thrown");
93 } 93 }
94 catch (AdblockPlusException e) 94 catch (AdblockPlusException e)
95 { 95 {
96 // ignored 96 // ignored
97 } 97 }
98 } 98 }
99 99
100 @Test 100 @Test
101 public void testWriteSingleLineData() throws FileNotFoundException 101 public void testWriteSingleLineData() throws FileNotFoundException
102 { 102 {
103 String path = generateUniqueFilename(); 103 String path = generateUniqueFilename();
104 File file = getFullPath(path); 104 File file = getFullPath(path);
105 assertFalse(file.exists()); 105 assertFalse(file.exists());
106 106
107 final String SINGLE_LINE_DATA = DATA.replace("\n", ""); 107 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.
108 fileSystem.write(path, SINGLE_LINE_DATA); 108 fileSystem.write(path, SINGLE_LINE_DATA);
109 109
110 assertTrue(file.exists()); 110 assertTrue(file.exists());
111 assertEquals(SINGLE_LINE_DATA, fileSystemHelper.readFile(file)); 111 assertEquals(SINGLE_LINE_DATA, fileSystemUtils.readFile(file));
112 } 112 }
113 113
114 @Test 114 @Test
115 public void testWriteMultiLineData() throws FileNotFoundException 115 public void testWriteMultiLineData() throws FileNotFoundException
116 { 116 {
117 String path = generateUniqueFilename(); 117 String path = generateUniqueFilename();
118 File file = getFullPath(path); 118 File file = getFullPath(path);
119 assertFalse(file.exists()); 119 assertFalse(file.exists());
120 120
121 final String MULTI_LINE_DATA = DATA + "\n" + DATA; 121 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.
122 fileSystem.write(path, MULTI_LINE_DATA); 122 fileSystem.write(path, MULTI_LINE_DATA);
123 123
124 assertTrue(file.exists()); 124 assertTrue(file.exists());
125 assertEquals(MULTI_LINE_DATA, fileSystemHelper.readFile(file)); 125 assertEquals(MULTI_LINE_DATA, fileSystemUtils.readFile(file));
126 } 126 }
127 127
128 @Test 128 @Test
129 public void testReadEmptyFile() throws IOException 129 public void testReadEmptyFile() throws IOException
130 { 130 {
131 String path = generateUniqueFilename(); 131 String path = generateUniqueFilename();
132 File file = getFullPath(path); 132 File file = getFullPath(path);
133 assertFalse(file.exists()); 133 assertFalse(file.exists());
134 134
135 fileSystemHelper.writeFile(file, ""); 135 fileSystemUtils.writeFile(file, "");
136 assertTrue(file.exists()); 136 assertTrue(file.exists());
137 137
138 assertEquals("", fileSystem.read(path)); 138 assertEquals("", fileSystem.read(path));
139 } 139 }
140 140
141 @Test 141 @Test
142 public void testReadSingleLineData() throws IOException 142 public void testReadSingleLineData() throws IOException
143 { 143 {
144 String path = generateUniqueFilename(); 144 String path = generateUniqueFilename();
145 File file = getFullPath(path); 145 File file = getFullPath(path);
146 assertFalse(file.exists()); 146 assertFalse(file.exists());
147 147
148 final String SINGLE_LINE_DATA = DATA.replace("\n", ""); 148 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.
149 fileSystemHelper.writeFile(file, SINGLE_LINE_DATA); 149 fileSystemUtils.writeFile(file, SINGLE_LINE_DATA);
150 assertTrue(file.exists()); 150 assertTrue(file.exists());
151 151
152 assertEquals(SINGLE_LINE_DATA, fileSystem.read(path)); 152 assertEquals(SINGLE_LINE_DATA, fileSystem.read(path));
153 } 153 }
154 154
155 @Test 155 @Test
156 public void testReadMultiLineData() throws IOException 156 public void testReadMultiLineData() throws IOException
157 { 157 {
158 String path = generateUniqueFilename(); 158 String path = generateUniqueFilename();
159 File file = getFullPath(path); 159 File file = getFullPath(path);
160 assertFalse(file.exists()); 160 assertFalse(file.exists());
161 161
162 final String MULTI_LINE_DATA = DATA + "\n" + DATA; 162 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.
163 fileSystemHelper.writeFile(file, MULTI_LINE_DATA); 163 fileSystemUtils.writeFile(file, MULTI_LINE_DATA);
164 assertTrue(file.exists()); 164 assertTrue(file.exists());
165 165
166 assertEquals(MULTI_LINE_DATA, fileSystem.read(path)); 166 assertEquals(MULTI_LINE_DATA, fileSystem.read(path));
167 } 167 }
168 168
169 @Test 169 @Test
170 public void testMoveExistingFile() throws IOException 170 public void testMoveExistingFile() throws IOException
171 { 171 {
172 String fromPath = generateUniqueFilename(); 172 String fromPath = generateUniqueFilename();
173 File fromFile = getFullPath(fromPath); 173 File fromFile = getFullPath(fromPath);
174 String toPath = generateUniqueFilename(); 174 String toPath = generateUniqueFilename();
175 File toFile = getFullPath(toPath); 175 File toFile = getFullPath(toPath);
176 fileSystemHelper.writeFile(fromFile, DATA); 176 fileSystemUtils.writeFile(fromFile, DATA);
177 177
178 assertTrue(fromFile.exists()); 178 assertTrue(fromFile.exists());
179 assertFalse(toFile.exists()); 179 assertFalse(toFile.exists());
180 180
181 fileSystem.move(fromPath, toPath); 181 fileSystem.move(fromPath, toPath);
182 182
183 assertFalse(fromFile.exists()); 183 assertFalse(fromFile.exists());
184 assertTrue(toFile.exists()); 184 assertTrue(toFile.exists());
185 assertEquals(DATA, fileSystemHelper.readFile(toFile)); 185 assertEquals(DATA, fileSystemUtils.readFile(toFile));
186 } 186 }
187 187
188 @Test 188 @Test
189 public void testMoveNotExistingFile() 189 public void testMoveNotExistingFile()
190 { 190 {
191 String fromPath = generateUniqueFilename(); 191 String fromPath = generateUniqueFilename();
192 File fromFile = getFullPath(fromPath); 192 File fromFile = getFullPath(fromPath);
193 String toPath = generateUniqueFilename(); 193 String toPath = generateUniqueFilename();
194 assertFalse(fromFile.exists()); 194 assertFalse(fromFile.exists());
195 195
196 try 196 try
197 { 197 {
198 fileSystem.move(fromPath, toPath); 198 fileSystem.move(fromPath, toPath);
199 fail("Exception should be thrown"); 199 fail("Exception should be thrown");
200 } 200 }
201 catch (AdblockPlusException e) 201 catch (AdblockPlusException e)
202 { 202 {
203 // ignored 203 // ignored
204 } 204 }
205 } 205 }
206 206
207 @Test 207 @Test
208 public void testRemoveExistingFile() throws IOException 208 public void testRemoveExistingFile() throws IOException
209 { 209 {
210 String path = generateUniqueFilename(); 210 String path = generateUniqueFilename();
211 File file = getFullPath(path); 211 File file = getFullPath(path);
212 fileSystemHelper.writeFile(file, DATA); 212 fileSystemUtils.writeFile(file, DATA);
213 213
214 assertTrue(file.exists()); 214 assertTrue(file.exists());
215 fileSystem.remove(path); 215 fileSystem.remove(path);
216 assertFalse(file.exists()); 216 assertFalse(file.exists());
217 } 217 }
218 218
219 @Test 219 @Test
220 public void testRemoveNotExistingFile() 220 public void testRemoveNotExistingFile()
221 { 221 {
222 String path = generateUniqueFilename(); 222 String path = generateUniqueFilename();
223 File file = getFullPath(path); 223 File file = getFullPath(path);
224 assertFalse(file.exists()); 224 assertFalse(file.exists());
225 225
226 fileSystem.remove(path); 226 fileSystem.remove(path);
227 assertFalse(file.exists()); 227 assertFalse(file.exists());
228 } 228 }
229 229
230 @Test 230 @Test
231 public void testStatExistingFile() throws IOException 231 public void testStatExistingFile() throws IOException
232 { 232 {
233 String path = generateUniqueFilename(); 233 String path = generateUniqueFilename();
234 File file = getFullPath(path); 234 File file = getFullPath(path);
235 fileSystemHelper.writeFile(file, DATA); 235 fileSystemUtils.writeFile(file, DATA);
236 assertTrue(file.exists()); 236 assertTrue(file.exists());
237 237
238 FileSystem.StatResult stat = fileSystem.stat(path); 238 FileSystem.StatResult stat = fileSystem.stat(path);
239 assertNotNull(stat); 239 assertNotNull(stat);
240 assertTrue(stat.exists()); 240 assertTrue(stat.exists());
241 assertFalse(stat.isDirectory()); 241 assertFalse(stat.isDirectory());
242 assertTrue(stat.isFile()); 242 assertTrue(stat.isFile());
243 assertTrue(stat.getLastModified() > 0); 243 assertTrue(stat.getLastModified() > 0);
244 } 244 }
245 245
246 @Test 246 @Test
247 public void testStatExistingDirectory() 247 public void testStatExistingDirectory()
248 { 248 {
249 String path = generateUniqueFilename(); 249 String path = generateUniqueFilename();
250 File file = getFullPath(path); 250 File file = getFullPath(path);
251 file.mkdir(); 251 file.mkdir();
252 252
253 FileSystem.StatResult stat = fileSystem.stat(path); 253 FileSystem.StatResult stat = fileSystem.stat(path);
254 assertNotNull(stat); 254 assertNotNull(stat);
255 assertTrue(stat.exists()); 255 assertTrue(stat.exists());
256 assertFalse(stat.isFile()); 256 assertFalse(stat.isFile());
257 assertTrue(stat.isDirectory()); 257 assertTrue(stat.isDirectory());
258 assertTrue(stat.getLastModified() > 0); 258 assertTrue(stat.getLastModified() > 0);
259 } 259 }
260 260
261 @Test 261 @Test
262 public void testStatNotExistingFile() 262 public void testStatNotExistingFile()
263 { 263 {
264 String path = generateUniqueFilename(); 264 String path = generateUniqueFilename();
265 File file = getFullPath(path); 265 File file = getFullPath(path);
266 assertFalse(file.exists()); 266 assertFalse(file.exists());
267 267
268 fileSystem.remove(path); 268 fileSystem.remove(path);
269 assertFalse(file.exists()); 269 assertFalse(file.exists());
270 FileSystem.StatResult notExistingStat = fileSystem.stat(path); 270 FileSystem.StatResult notExistingStat = fileSystem.stat(path);
271 assertFalse(notExistingStat.exists()); 271 assertFalse(notExistingStat.exists());
272 assertEquals(0l, notExistingStat.getLastModified()); 272 assertEquals(0l, notExistingStat.getLastModified());
273 } 273 }
274 274
275 @Test 275 @Test
276 public void testResolveAbsolute() 276 public void testResolveAbsolute()
277 { 277 {
278 String path = generateUniqueFilename(); 278 String path = generateUniqueFilename();
279 String absolutePath = fileSystem.resolve(path); 279 String absolutePath = fileSystem.resolve(path);
280 String fullPath = getFullPath(path).getAbsolutePath(); 280 String fullPath = getFullPath(path).getAbsolutePath();
281 String fullPathFromBasePath = fullPath 281 String fullPathFromBasePath = fullPath
282 .substring(fileSystem.getBasePath() 282 .substring(fileSystem.getBasePath()
283 .getAbsolutePath().length()); 283 .getAbsolutePath().length());
284 assertEquals(fullPathFromBasePath, absolutePath); 284 assertEquals(fullPathFromBasePath, absolutePath);
285 } 285 }
286 286
287 @Test 287 @Test
288 public void testResolveRelative() 288 public void testResolveRelative()
289 { 289 {
290 String relativePath = "./file"; 290 String relativePath = "./file";
291 String absolutePath = fileSystem.resolve(relativePath); 291 String absolutePath = fileSystem.resolve(relativePath);
292 assertNotNull(absolutePath); 292 assertNotNull(absolutePath);
293 assertTrue(absolutePath.startsWith("/")); 293 assertTrue(absolutePath.startsWith("/"));
294 } 294 }
295 } 295 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld