OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
| 3 * Copyright (C) 2006-2016 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.test.AndroidTestCase; |
| 21 |
| 22 import org.adblockplus.android.AndroidFileSystem; |
| 23 import org.adblockplus.libadblockplus.AdblockPlusException; |
| 24 import org.adblockplus.libadblockplus.FileSystem; |
| 25 import org.adblockplus.libadblockplus.FileSystemUtils; |
| 26 import org.junit.Test; |
| 27 |
| 28 import java.io.File; |
| 29 import java.io.FileNotFoundException; |
| 30 import java.io.IOException; |
| 31 |
| 32 public class AndroidFileSystemTest extends AndroidTestCase |
| 33 { |
| 34 protected final FileSystemUtils fileSystemUtils = new FileSystemUtils(); |
| 35 protected final static String DATA = "12345qwerty"; |
| 36 protected AndroidFileSystem fileSystem; |
| 37 |
| 38 @Override |
| 39 protected void setUp() throws Exception |
| 40 { |
| 41 fileSystem = new AndroidFileSystem(getContext().getCacheDir()); |
| 42 } |
| 43 |
| 44 protected String generateUniqueFilename() |
| 45 { |
| 46 return fileSystemUtils.generateUniqueFileName("file", ".tmp"); |
| 47 } |
| 48 |
| 49 protected File getFullPath(String path) |
| 50 { |
| 51 return new File(fileSystem.getBasePath(), path); |
| 52 } |
| 53 |
| 54 @Test |
| 55 public void testWriteNullFile() throws FileNotFoundException |
| 56 { |
| 57 String path = generateUniqueFilename(); |
| 58 File file = getFullPath(path); |
| 59 assertFalse(file.exists()); |
| 60 |
| 61 fileSystem.write(path, null); |
| 62 |
| 63 assertTrue(file.exists()); |
| 64 assertEquals("", fileSystemUtils.readFile(file)); |
| 65 } |
| 66 |
| 67 @Test |
| 68 public void testWriteEmptyFile() throws FileNotFoundException |
| 69 { |
| 70 String path = generateUniqueFilename(); |
| 71 File file = getFullPath(path); |
| 72 assertFalse(file.exists()); |
| 73 |
| 74 fileSystem.write(path, ""); |
| 75 |
| 76 assertTrue(file.exists()); |
| 77 assertEquals("", fileSystemUtils.readFile(file)); |
| 78 } |
| 79 |
| 80 @Test |
| 81 public void testWriteInvalidPath() |
| 82 { |
| 83 String path = "notExistingDirectory/" + generateUniqueFilename(); |
| 84 File file = getFullPath(path); |
| 85 assertFalse(file.exists()); |
| 86 File notExistingDirectoryFile = file.getParentFile(); |
| 87 assertFalse(notExistingDirectoryFile.exists()); |
| 88 |
| 89 try |
| 90 { |
| 91 fileSystem.write(path, DATA); |
| 92 fail("Exception should be thrown"); |
| 93 } |
| 94 catch (AdblockPlusException e) |
| 95 { |
| 96 // ignored |
| 97 } |
| 98 } |
| 99 |
| 100 @Test |
| 101 public void testWriteSingleLineData() throws FileNotFoundException |
| 102 { |
| 103 String path = generateUniqueFilename(); |
| 104 File file = getFullPath(path); |
| 105 assertFalse(file.exists()); |
| 106 |
| 107 final String SINGLE_LINE_DATA = DATA.replace("\n", ""); |
| 108 fileSystem.write(path, SINGLE_LINE_DATA); |
| 109 |
| 110 assertTrue(file.exists()); |
| 111 assertEquals(SINGLE_LINE_DATA, fileSystemUtils.readFile(file)); |
| 112 } |
| 113 |
| 114 @Test |
| 115 public void testWriteMultiLineData() throws FileNotFoundException |
| 116 { |
| 117 String path = generateUniqueFilename(); |
| 118 File file = getFullPath(path); |
| 119 assertFalse(file.exists()); |
| 120 |
| 121 final String MULTI_LINE_DATA = DATA + "\n" + DATA; |
| 122 fileSystem.write(path, MULTI_LINE_DATA); |
| 123 |
| 124 assertTrue(file.exists()); |
| 125 assertEquals(MULTI_LINE_DATA, fileSystemUtils.readFile(file)); |
| 126 } |
| 127 |
| 128 @Test |
| 129 public void testReadEmptyFile() throws IOException |
| 130 { |
| 131 String path = generateUniqueFilename(); |
| 132 File file = getFullPath(path); |
| 133 assertFalse(file.exists()); |
| 134 |
| 135 fileSystemUtils.writeFile(file, ""); |
| 136 assertTrue(file.exists()); |
| 137 |
| 138 assertEquals("", fileSystem.read(path)); |
| 139 } |
| 140 |
| 141 @Test |
| 142 public void testReadSingleLineData() throws IOException |
| 143 { |
| 144 String path = generateUniqueFilename(); |
| 145 File file = getFullPath(path); |
| 146 assertFalse(file.exists()); |
| 147 |
| 148 final String SINGLE_LINE_DATA = DATA.replace("\n", ""); |
| 149 fileSystemUtils.writeFile(file, SINGLE_LINE_DATA); |
| 150 assertTrue(file.exists()); |
| 151 |
| 152 assertEquals(SINGLE_LINE_DATA, fileSystem.read(path)); |
| 153 } |
| 154 |
| 155 @Test |
| 156 public void testReadMultiLineData() throws IOException |
| 157 { |
| 158 String path = generateUniqueFilename(); |
| 159 File file = getFullPath(path); |
| 160 assertFalse(file.exists()); |
| 161 |
| 162 final String MULTI_LINE_DATA = DATA + "\n" + DATA; |
| 163 fileSystemUtils.writeFile(file, MULTI_LINE_DATA); |
| 164 assertTrue(file.exists()); |
| 165 |
| 166 assertEquals(MULTI_LINE_DATA, fileSystem.read(path)); |
| 167 } |
| 168 |
| 169 @Test |
| 170 public void testMoveExistingFile() throws IOException |
| 171 { |
| 172 String fromPath = generateUniqueFilename(); |
| 173 File fromFile = getFullPath(fromPath); |
| 174 String toPath = generateUniqueFilename(); |
| 175 File toFile = getFullPath(toPath); |
| 176 fileSystemUtils.writeFile(fromFile, DATA); |
| 177 |
| 178 assertTrue(fromFile.exists()); |
| 179 assertFalse(toFile.exists()); |
| 180 |
| 181 fileSystem.move(fromPath, toPath); |
| 182 |
| 183 assertFalse(fromFile.exists()); |
| 184 assertTrue(toFile.exists()); |
| 185 assertEquals(DATA, fileSystemUtils.readFile(toFile)); |
| 186 } |
| 187 |
| 188 @Test |
| 189 public void testMoveNotExistingFile() |
| 190 { |
| 191 String fromPath = generateUniqueFilename(); |
| 192 File fromFile = getFullPath(fromPath); |
| 193 String toPath = generateUniqueFilename(); |
| 194 assertFalse(fromFile.exists()); |
| 195 |
| 196 try |
| 197 { |
| 198 fileSystem.move(fromPath, toPath); |
| 199 fail("Exception should be thrown"); |
| 200 } |
| 201 catch (AdblockPlusException e) |
| 202 { |
| 203 // ignored |
| 204 } |
| 205 } |
| 206 |
| 207 @Test |
| 208 public void testRemoveExistingFile() throws IOException |
| 209 { |
| 210 String path = generateUniqueFilename(); |
| 211 File file = getFullPath(path); |
| 212 fileSystemUtils.writeFile(file, DATA); |
| 213 |
| 214 assertTrue(file.exists()); |
| 215 fileSystem.remove(path); |
| 216 assertFalse(file.exists()); |
| 217 } |
| 218 |
| 219 @Test |
| 220 public void testRemoveNotExistingFile() |
| 221 { |
| 222 String path = generateUniqueFilename(); |
| 223 File file = getFullPath(path); |
| 224 assertFalse(file.exists()); |
| 225 |
| 226 fileSystem.remove(path); |
| 227 assertFalse(file.exists()); |
| 228 } |
| 229 |
| 230 @Test |
| 231 public void testStatExistingFile() throws IOException |
| 232 { |
| 233 String path = generateUniqueFilename(); |
| 234 File file = getFullPath(path); |
| 235 fileSystemUtils.writeFile(file, DATA); |
| 236 assertTrue(file.exists()); |
| 237 |
| 238 FileSystem.StatResult stat = fileSystem.stat(path); |
| 239 assertNotNull(stat); |
| 240 assertTrue(stat.exists()); |
| 241 assertFalse(stat.isDirectory()); |
| 242 assertTrue(stat.isFile()); |
| 243 assertTrue(stat.getLastModified() > 0); |
| 244 } |
| 245 |
| 246 @Test |
| 247 public void testStatExistingDirectory() |
| 248 { |
| 249 String path = generateUniqueFilename(); |
| 250 File file = getFullPath(path); |
| 251 file.mkdir(); |
| 252 |
| 253 FileSystem.StatResult stat = fileSystem.stat(path); |
| 254 assertNotNull(stat); |
| 255 assertTrue(stat.exists()); |
| 256 assertFalse(stat.isFile()); |
| 257 assertTrue(stat.isDirectory()); |
| 258 assertTrue(stat.getLastModified() > 0); |
| 259 } |
| 260 |
| 261 @Test |
| 262 public void testStatNotExistingFile() |
| 263 { |
| 264 String path = generateUniqueFilename(); |
| 265 File file = getFullPath(path); |
| 266 assertFalse(file.exists()); |
| 267 |
| 268 fileSystem.remove(path); |
| 269 assertFalse(file.exists()); |
| 270 FileSystem.StatResult notExistingStat = fileSystem.stat(path); |
| 271 assertFalse(notExistingStat.exists()); |
| 272 assertEquals(0l, notExistingStat.getLastModified()); |
| 273 } |
| 274 |
| 275 @Test |
| 276 public void testResolveAbsolute() |
| 277 { |
| 278 String path = generateUniqueFilename(); |
| 279 String absolutePath = fileSystem.resolve(path); |
| 280 String fullPath = getFullPath(path).getAbsolutePath(); |
| 281 String fullPathFromBasePath = fullPath |
| 282 .substring(fileSystem.getBasePath() |
| 283 .getAbsolutePath().length()); |
| 284 assertEquals(fullPathFromBasePath, absolutePath); |
| 285 } |
| 286 |
| 287 @Test |
| 288 public void testResolveRelative() |
| 289 { |
| 290 String relativePath = "./file"; |
| 291 String absolutePath = fileSystem.resolve(relativePath); |
| 292 assertNotNull(absolutePath); |
| 293 assertTrue(absolutePath.startsWith("/")); |
| 294 } |
| 295 } |
OLD | NEW |