| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.connector.file; |
| 34 | | | |
| 35 | | | import java.io.File; |
| 36 | | | import java.io.FileInputStream; |
| 37 | | | import java.io.FileNotFoundException; |
| 38 | | | import java.io.IOException; |
| 39 | | | import java.io.RandomAccessFile; |
| 40 | | | import java.util.ArrayList; |
| 41 | | | import java.util.List; |
| 42 | | | import java.util.Properties; |
| 43 | | | import java.util.Random; |
| 44 | | | import javax.resource.ResourceException; |
| 45 | | | |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | 100 | (1) | public class FsConnectionInterfaceTest |
| 51 | | | extends FsTestCase |
| 52 | | | |
| 53 | | | { |
| 54 | 100 | | private static final Random RANDOM = new Random(); |
| 55 | 100 | | private final List mFiles = new ArrayList(); |
| 56 | | | |
| 57 | | | private File mFileA; |
| 58 | | | private File mFileB; |
| 59 | | | |
| 60 | | | {@inheritDoc} |
| 61 | | | public void setUp () |
| 62 | | | throws Exception |
| 63 | | | { |
| 64 | 100 | | super.setUp(); |
| 65 | 100 | | mFileA = File.createTempFile("AAA", "A"); |
| 66 | 100 | | mFileB = File.createTempFile("BBB", "B"); |
| 67 | 100 | | mFiles.add(mFileA); |
| 68 | 100 | | mFiles.add(mFileB); |
| 69 | 100 | | } |
| 70 | | | |
| 71 | | | |
| 72 | | | {@link } |
| 73 | | | |
| 74 | | | public void testIsExists () |
| 75 | | | { |
| 76 | 100 | | final FsConnection c = getConnection(); |
| 77 | 100 | | final String nonExisingFile = "ThisFileDoesNotExist"; |
| 78 | | | |
| 79 | | | try |
| 80 | | | { |
| 81 | 100 | | assertEquals("Check existing file failed.", |
| 82 | | | c.isExists(mFileA.toString()), mFileA.exists()); |
| 83 | 100 | | assertFalse("Check non existing file failed.", |
| 84 | | | c.isExists(nonExisingFile)); |
| 85 | | | |
| 86 | 100 | | c.close(); |
| 87 | | | } |
| 88 | 0 | | catch (ResourceException e) |
| 89 | | | { |
| 90 | 0 | | fail("Test is exist failed. " + e.getMessage()); |
| 91 | 100 | | } |
| 92 | 100 | | } |
| 93 | | | |
| 94 | | | |
| 95 | | | {@link } |
| 96 | | | |
| 97 | | | public void testRenameFile () |
| 98 | | | { |
| 99 | 100 | | final FsConnection c = getConnection(); |
| 100 | | | |
| 101 | | | try |
| 102 | | | { |
| 103 | 100 | | final File fileC = File.createTempFile("CCC", "C"); |
| 104 | 100 | (2) | fileC.delete(); |
| 105 | 100 | | c.renameFile(mFileA.toString(), fileC.toString()); |
| 106 | 100 | | mFiles.add(fileC); |
| 107 | 80 | | assertTrue("Rename failed.", !mFileA.exists() && fileC.exists()); |
| 108 | | | |
| 109 | 100 | | c.renameFile(fileC.toString(), mFileA.toString()); |
| 110 | 60 | | assertTrue("Rename back failed.", mFileA.exists() && !fileC.exists()); |
| 111 | | | |
| 112 | | | try |
| 113 | | | { |
| 114 | 0 | | c.renameFile(fileC.toString(), mFileA.toString()); |
| 115 | 0 | | fail("Rename of a non existing file should throw the " |
| 116 | | | + "ResourceException."); |
| 117 | | | } |
| 118 | 100 | | catch (ResourceException re) |
| 119 | | | { |
| 120 | | | |
| 121 | 0 | | } |
| 122 | | | |
| 123 | 100 | | c.close(); |
| 124 | | | } |
| 125 | 0 | | catch (Exception e) |
| 126 | | | { |
| 127 | 0 | | fail("Test is exist failed. " + e.getMessage()); |
| 128 | 100 | | } |
| 129 | 100 | | } |
| 130 | | | |
| 131 | | | |
| 132 | | | {@link } |
| 133 | | | |
| 134 | | | public void testList () |
| 135 | | | { |
| 136 | 100 | | final FsConnection c = getConnection(); |
| 137 | | | |
| 138 | | | try |
| 139 | | | { |
| 140 | 100 | | String [] list = c.listFiles(mFileA.getParent()); |
| 141 | 100 | | assertNotNull("listFiles returned null, expected an array.", list); |
| 142 | 75 | | assertTrue("The method listFile must return an array of size >= 1", |
| 143 | | | list.length > 0); |
| 144 | | | |
| 145 | 100 | | list = c.listFiles(mFileA.toString()); |
| 146 | 100 | | assertNull("The listFiles must return null if the method's argument is" |
| 147 | | | + " a regular file.", list); |
| 148 | | | |
| 149 | 100 | | c.close(); |
| 150 | | | } |
| 151 | 0 | | catch (ResourceException re) |
| 152 | | | { |
| 153 | 0 | | fail("Test list file failed. " + re.getMessage()); |
| 154 | 100 | | } |
| 155 | 100 | | } |
| 156 | | | |
| 157 | | | |
| 158 | | | {@link } |
| 159 | | | |
| 160 | | | public void testRafBasePath () |
| 161 | | | { |
| 162 | 100 | | final FsConnection c = getConnection(); |
| 163 | | | |
| 164 | | | try |
| 165 | | | { |
| 166 | 100 | | final RandomAccessFile rfa = c.getRandomAccessFile(mFileB.toString(), |
| 167 | | | "r"); |
| 168 | | | try |
| 169 | | | { |
| 170 | 100 | | rfa.length(); |
| 171 | 100 | | rfa.close(); |
| 172 | | | } |
| 173 | 0 | | catch (IOException e1) |
| 174 | | | { |
| 175 | 0 | | fail("Unexpected exception" + e1.toString()); |
| 176 | 100 | | } |
| 177 | 100 | | c.close(); |
| 178 | | | } |
| 179 | 0 | | catch (ResourceException re) |
| 180 | | | { |
| 181 | 0 | | fail("testRafBasePath failed. " + re.getMessage()); |
| 182 | | | } |
| 183 | 0 | | catch (FileNotFoundException e) |
| 184 | | | { |
| 185 | 0 | | fail("File " + mFileB.toString() + " was not found: " |
| 186 | | | + e.getMessage()); |
| 187 | 50 | | } |
| 188 | 100 | | } |
| 189 | | | |
| 190 | | | |
| 191 | | | |
| 192 | | | |
| 193 | | | |
| 194 | | | |
| 195 | | | public void testRafConnectionClosed () |
| 196 | | | { |
| 197 | 0 | | final FsConnection c = getConnection(); |
| 198 | | | |
| 199 | | | try |
| 200 | | | { |
| 201 | 0 | | final RandomAccessFile rfa = c.getRandomAccessFile(mFileB.toString(), |
| 202 | | | "r"); |
| 203 | | | |
| 204 | 0 | | c.close(); |
| 205 | | | |
| 206 | | | try |
| 207 | | | { |
| 208 | 0 | | rfa.length(); |
| 209 | 0 | | fail("Must throw a IOException"); |
| 210 | | | } |
| 211 | 100 | | catch (IOException e1) |
| 212 | | | { |
| 213 | | | |
| 214 | 0 | | } |
| 215 | | | } |
| 216 | 0 | | catch (ResourceException re) |
| 217 | | | { |
| 218 | 0 | | fail("testRafBasePath failed. " + re.getMessage()); |
| 219 | | | } |
| 220 | 0 | | catch (FileNotFoundException e) |
| 221 | | | { |
| 222 | 0 | | fail("File " + mFileB.toString() + " was not found: " |
| 223 | | | + e.getMessage()); |
| 224 | 50 | | } |
| 225 | 100 | | } |
| 226 | | | |
| 227 | | | |
| 228 | | | |
| 229 | | | |
| 230 | | | public void testCreateFile () |
| 231 | | | { |
| 232 | 100 | | final FsConnection c = getConnection(); |
| 233 | | | |
| 234 | | | try |
| 235 | | | { |
| 236 | 100 | | String tmp = c.createTempFile(); |
| 237 | 100 | | final File f = createFileInstance(tmp); |
| 238 | | | |
| 239 | 100 | | assertTrue("Expected temp file does not exist.", f.exists()); |
| 240 | 100 | (3) | f.delete(); |
| 241 | 100 | (4) | f.mkdir(); |
| 242 | | | |
| 243 | 100 | | tmp = c.createTempFile(f.toString()); |
| 244 | 100 | | final File f1 = createFileInstance(tmp); |
| 245 | 100 | | assertTrue("Expected temp file does not exist.", f1.exists()); |
| 246 | | | |
| 247 | 100 | | deleteFile(f1); |
| 248 | | | |
| 249 | 100 | | assertTrue("Could not create a new file '" + f1.toString() + "'.", |
| 250 | | | c.createFile(f1.toString())); |
| 251 | 100 | | f1.delete(); |
| 252 | 100 | | final File f2 = createFileInstance(f.toString() + File.separator + "A" |
| 253 | | | + File.separator + "B" + File.separator + "C"); |
| 254 | | | |
| 255 | 100 | | assertTrue("Could not create a new file '" + f2.toString() + "'.", |
| 256 | | | c.createFile(f2.toString())); |
| 257 | | | |
| 258 | 100 | | deleteFile(f); |
| 259 | 100 | | deleteFile(f2); |
| 260 | | | |
| 261 | 100 | | c.close(); |
| 262 | | | } |
| 263 | 0 | | catch (ResourceException e) |
| 264 | | | { |
| 265 | 0 | (5) | e.printStackTrace(); |
| 266 | 0 | | fail("Test 'create temp file' failed. " + e.getMessage()); |
| 267 | 100 | | } |
| 268 | 100 | | } |
| 269 | | | |
| 270 | | | |
| 271 | | | {@link } |
| 272 | | | |
| 273 | | | public void testRenameToTempFile () |
| 274 | | | { |
| 275 | 100 | | final FsConnection c = getConnection(); |
| 276 | | | |
| 277 | | | try |
| 278 | | | { |
| 279 | 100 | | final File file = File.createTempFile("CCC", "C"); |
| 280 | 100 | | mFiles.add(file); |
| 281 | | | |
| 282 | 100 | | final String renamed = c.renameToTempFile(file.getAbsolutePath()); |
| 283 | 100 | | assertNotNull("Method FsConnection.renameToTempFile must return a " |
| 284 | | | + "non-null filename.", renamed); |
| 285 | 50 | | assertFalse("The file to be renamed and temp file must differ.", |
| 286 | | | file.getAbsolutePath().compareTo(renamed) == 0); |
| 287 | | | |
| 288 | 0 | | final File renamedFile = createFileInstance(renamed); |
| 289 | 0 | | assertTrue("The renamed file does not exist.", renamedFile.exists()); |
| 290 | 0 | | assertFalse("The original file exists after it has been renamed.", |
| 291 | | | file.exists()); |
| 292 | | | |
| 293 | 0 | (6) | file.setReadOnly(); |
| 294 | | | |
| 295 | | | try |
| 296 | | | { |
| 297 | 0 | | c.renameToTempFile(file.getAbsolutePath()); |
| 298 | 0 | | fail("Renaming of a read-only file must throw a ResourceException"); |
| 299 | | | } |
| 300 | 100 | | catch (ResourceException re) |
| 301 | | | { |
| 302 | | | |
| 303 | 0 | | } |
| 304 | | | |
| 305 | 0 | | deleteFile(file); |
| 306 | | | |
| 307 | | | try |
| 308 | | | { |
| 309 | 0 | | c.renameToTempFile(file.getAbsolutePath()); |
| 310 | 0 | | fail("Renaming of a non-existing file must throw a " |
| 311 | | | + "ResourceException"); |
| 312 | | | } |
| 313 | 100 | | catch (ResourceException re) |
| 314 | | | { |
| 315 | | | |
| 316 | 0 | | } |
| 317 | | | |
| 318 | 100 | | c.close(); |
| 319 | | | } |
| 320 | 0 | | catch (Exception e) |
| 321 | | | { |
| 322 | 0 | | fail("Test 'RenameToTempFile' failed. " + e.getMessage()); |
| 323 | 100 | | } |
| 324 | 100 | | } |
| 325 | | | |
| 326 | | | |
| 327 | | | |
| 328 | | | |
| 329 | | | {@link } |
| 330 | | | |
| 331 | | | public void testFileInputStreamBasePath () |
| 332 | | | { |
| 333 | 100 | | final FsConnection c = getConnection(); |
| 334 | | | |
| 335 | | | try |
| 336 | | | { |
| 337 | 100 | | final FileInputStream fis = c.getFileInputStream(mFileB.toString()); |
| 338 | | | try |
| 339 | | | { |
| 340 | 100 | | fis.available(); |
| 341 | 100 | | fis.close(); |
| 342 | | | } |
| 343 | 0 | | catch (IOException e1) |
| 344 | | | { |
| 345 | 0 | | fail("Unexpected exception" + e1.toString()); |
| 346 | 100 | | } |
| 347 | 100 | | c.close(); |
| 348 | | | } |
| 349 | 0 | | catch (ResourceException re) |
| 350 | | | { |
| 351 | 0 | | fail("testRafBasePath failed. " + re.getMessage()); |
| 352 | | | } |
| 353 | 0 | | catch (FileNotFoundException e) |
| 354 | | | { |
| 355 | 0 | | fail("File " + mFileB.toString() + " was not found: " |
| 356 | | | + e.getMessage()); |
| 357 | 50 | | } |
| 358 | 100 | | } |
| 359 | | | |
| 360 | | | |
| 361 | | | {@link } |
| 362 | | | |
| 363 | | | public void testMoveFile () |
| 364 | | | { |
| 365 | | | try |
| 366 | | | { |
| 367 | 100 | | final FsConnection c = getConnection(); |
| 368 | 100 | | final File src = File.createTempFile("CCC", "C"); |
| 369 | 100 | | final File dest = new File((src.getParentFile()).getCanonicalPath() |
| 370 | | | + File.separator |
| 371 | | | + String.valueOf(RANDOM.nextInt(Integer.MAX_VALUE)) |
| 372 | | | + File.separator + "dest.tmp"); |
| 373 | 100 | | dest.deleteOnExit(); |
| 374 | 100 | | src.deleteOnExit(); |
| 375 | 100 | | c.moveFile(src.toString(), dest.toString()); |
| 376 | 100 | | assertTrue("Destination file does not exist", dest.exists()); |
| 377 | 100 | | assertFalse("Source file exists", src.exists()); |
| 378 | 100 | | c.deleteFile(dest.toString()); |
| 379 | 100 | | c.deleteFile(dest.getParentFile().toString()); |
| 380 | 100 | | c.close(); |
| 381 | | | } |
| 382 | 0 | | catch (IOException ioE) |
| 383 | | | { |
| 384 | 0 | | fail("testMoveFile failed. " + ioE.getMessage()); |
| 385 | | | } |
| 386 | 0 | | catch (ResourceException re) |
| 387 | | | { |
| 388 | 0 | | fail("testMoveFile failed. " + re.getMessage()); |
| 389 | 50 | | } |
| 390 | | | |
| 391 | 100 | | } |
| 392 | | | |
| 393 | | | {@inheritDoc} |
| 394 | | | public void tearDown () |
| 395 | | | throws Exception |
| 396 | | | { |
| 397 | 100 | | super.tearDown(); |
| 398 | 100 | (7) | for (int i = 0; i < mFiles.size(); i++) |
| 399 | | | { |
| 400 | 100 | | final File file = (File) mFiles.get(i); |
| 401 | 100 | | if (file != null && file.exists()) |
| 402 | | | { |
| 403 | 100 | | del(file); |
| 404 | | | } |
| 405 | | | } |
| 406 | 100 | | } |
| 407 | | | |
| 408 | | | |
| 409 | | | |
| 410 | | | |
| 411 | | | |
| 412 | | | |
| 413 | | | public void testTempDir () |
| 414 | | | { |
| 415 | 100 | | FsConnection fc = getConnection(); |
| 416 | 100 | | final File sysTmpDir = new File(System.getProperty("java.io.tmpdir")); |
| 417 | 100 | | String tmp = null; |
| 418 | 100 | | File tmpFile = null; |
| 419 | | | try |
| 420 | | | { |
| 421 | 100 | | tmp = fc.createTempFile(); |
| 422 | 100 | | tmpFile = new File(tmp).getParentFile(); |
| 423 | 100 | | assertEquals("The temp dir used by fs '" + tmpFile |
| 424 | | | + "' does not match the system temp dir '" + sysTmpDir + "'", |
| 425 | | | sysTmpDir, tmpFile); |
| 426 | 100 | | fc.deleteFile(tmp); |
| 427 | | | |
| 428 | | | |
| 429 | 100 | (8) | new File(tmp).mkdir(); |
| 430 | 100 | | final File newTd = new File(tmp).getParentFile(); |
| 431 | 100 | | fc.deleteFile(tmp); |
| 432 | 100 | | fc.close(); |
| 433 | | | |
| 434 | 100 | | final Properties props = new Properties(); |
| 435 | 100 | | props.put(FsConnectionFactory.PROP_TEMP_DIR, newTd.toString()); |
| 436 | | | |
| 437 | 100 | | fc = getConnection(props); |
| 438 | 100 | | tmp = fc.createTempFile(); |
| 439 | 100 | | tmpFile = new File(tmp).getParentFile(); |
| 440 | 100 | | assertEquals("The temp dir used by fs '" + tmpFile |
| 441 | | | + "' does not match the config temp dir '" + newTd + "'", |
| 442 | | | newTd, tmpFile); |
| 443 | 100 | | fc.deleteFile(tmp); |
| 444 | 100 | | fc.deleteFile(newTd.toString()); |
| 445 | 100 | | fc.close(); |
| 446 | | | } |
| 447 | 0 | | catch (ResourceException e) |
| 448 | | | { |
| 449 | 0 | (9) | e.printStackTrace(); |
| 450 | 0 | | fail("testTempDir failed. " + e.getMessage()); |
| 451 | 100 | | } |
| 452 | | | |
| 453 | 100 | | } |
| 454 | | | |
| 455 | | | private File createFileInstance (String file) |
| 456 | | | { |
| 457 | 100 | | final File f = new File(file); |
| 458 | 100 | | mFiles.add(f); |
| 459 | 100 | | return f; |
| 460 | | | } |
| 461 | | | |
| 462 | | | |
| 463 | | | |
| 464 | | | @param |
| 465 | | | |
| 466 | | | private void deleteFile (File file) |
| 467 | | | { |
| 468 | 100 | | del(file); |
| 469 | 100 | | mFiles.remove(file); |
| 470 | 100 | | } |
| 471 | | | |
| 472 | | | private void del (File file) |
| 473 | | | { |
| 474 | 100 | | if (file.isDirectory()) |
| 475 | | | { |
| 476 | 100 | | final File [] f = file.listFiles(); |
| 477 | 100 | | for (int i = 0; i < f.length; i++) |
| 478 | | | { |
| 479 | 100 | | del(f[i]); |
| 480 | | | } |
| 481 | | | } |
| 482 | | | |
| 483 | 100 | (10) | file.delete(); |
| 484 | 100 | | } |
| 485 | | | } |