mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Jean-Noel Rouvignac
21.07.2015 3872f27722f9bf2f6a601a86bdc769aa933801d0
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
 * or http://forgerock.org/license/CDDLv1.0.html.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at legal-notices/CDDLv1_0.txt.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information:
 *      Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 *
 *
 *      Copyright 2006-2008 Sun Microsystems, Inc.
 *      Portions Copyright 2014-2015 ForgeRock AS
 */
package org.opends.quicksetup.util;
 
import static org.testng.Assert.*;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
 
import org.opends.quicksetup.ApplicationException;
import org.opends.quicksetup.Constants;
import org.opends.quicksetup.QuickSetupTestCase;
import org.opends.quicksetup.TestUtilities;
import org.opends.server.util.StaticUtils;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
 
/**
 * FileManager Tester.
 */
@SuppressWarnings("javadoc")
@Test(groups = {"slow"}, sequential=true)
public class FileManagerTest extends QuickSetupTestCase {
 
  private File fmWorkspace;
  private FileManager fileManager;
 
  @BeforeClass
  public void setUp() throws Exception {
    fileManager = new FileManager();
    fmWorkspace = new File(TestUtilities.getQuickSetupTestWorkspace(),
            "filemanagertests");
    if (fmWorkspace.exists()) {
      fileManager.deleteRecursively(fmWorkspace);
    }
    if (!fmWorkspace.mkdir()) {
      throw new IllegalStateException("cannot create FileManagerTest workspace");
    }
  }
 
  @AfterMethod
  public void cleanupWorkspace() throws Exception {
    String[] children = fmWorkspace.list();
    if (children != null) {
      for (String child : children) {
        fileManager.deleteRecursively(new File(fmWorkspace, child));
      }
    }
  }
 
  /**
   * Tests synchronized.
   * @throws Exception if something unexpected
   */
  @Test(enabled = false)
  public void testSynchronize() throws Exception {
    File s = new File(fmWorkspace, "s");
    File t = new File(fmWorkspace, "t");
 
    String childOfS = "child of s";
    String childOfT = "child of t";
 
    createSourceFiles(s, childOfS);
    createSourceFiles(t, childOfT);
 
    File t_d1 = new File(t, "d1");
    File t_f1a = new File(t_d1, "f1a");
    File t_f1b = new File(t_d1, "f1b");
 
    File t_d2 = new File(t, "d2");
    File t_f2a = new File(t_d2, "f2a");
    File t_d2b = new File(t_d2, "d2b");
    File t_f2b1 = new File(t_d2b, "f2b1");
 
    assertTrue(t_d1.exists());
    assertTrue(t_f1a.exists());
    assertTrue(t_f1b.exists());
 
    assertTrue(t_d2.exists());
    assertTrue(t_f2a.exists());
    assertTrue(t_d2b.exists());
    assertTrue(t_f2b1.exists());
 
    assertTrue(t_f2b1.delete());
    assertTrue(t_d2b.delete());
    assertTrue(t_f1a.delete());
 
    fileManager.synchronize(s, t);
 
    // Make sure files we deleted above were copied from the source dir
    assertTrue(t_f2b1.exists());
    assertEquals(childOfS, contentsOf(t_f2b1));
 
    assertTrue(t_d2b.exists());
 
    assertTrue(t_f1a.exists());
    assertEquals(childOfS, contentsOf(t_f1a));
 
    // Make sure files that pre-existed didn't get touched
    assertEquals(childOfT, contentsOf(t_f1b));
    assertEquals(childOfT, contentsOf(t_f2a));
  }
 
  /** Tests the rename. */
  @Test
  public void testRenameNonExistentTarget() throws Exception {
    File src = File.createTempFile("src", null);
    File target = new File(src.getParentFile(), "target");
    try {
      if (target.exists()) {
        target.delete();
        assertTrue(!target.exists());
      }
      fileManager.rename(src, target);
      assertTrue(!src.exists());
      assertTrue(target.exists());
    } finally {
      src.delete();
      target.delete();
    }
  }
 
  /**
   * Tests the {@link StaticUtils#renameFile(java.io.File, java.io.File)}
   * method.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test(enabled = false)
  public void testRenameFileExistentTarget() throws Exception {
    File src = File.createTempFile("src", null);
    File target = File.createTempFile("target", null);
    try {
      StaticUtils.renameFile(src, target);
      assertTrue(!src.exists());
      assertTrue(target.exists());
    } finally {
      src.delete();
      target.delete();
    }
  }
 
  /**
   * Tests the {@link StaticUtils#renameFile(java.io.File, java.io.File)}
   * method.  Renaming locked files is a problem on Windows but not so
   * much on other platforms.
   *
   * @throws Exception If the test failed unexpectedly.
   */
  @Test(enabled = false, groups={"windows"}, expectedExceptions=IOException.class)
  public void testRenameFileLockedTarget() throws Exception {
    File src = File.createTempFile("src", null);
    File target = File.createTempFile("target", null);
    FileChannel c = new RandomAccessFile(target, "rw").getChannel();
    FileLock lock = c.lock();
    try {
      StaticUtils.renameFile(src, target);
    } finally {
      lock.release();
      src.delete();
      target.delete();
    }
  }
 
  /**
   * Tests basic move.
   * @throws Exception if something unexpected
   */
  @Test(enabled = false)
  public void testMove() throws Exception {
 
    File fromDir = new File(fmWorkspace, "from");
    fromDir.mkdir();
 
    File toDir = new File(fmWorkspace, "to");
    toDir.mkdir();
 
    createSourceFiles(fromDir, "abc");
 
    fileManager.move(fromDir, toDir);
 
    assertFalse(fromDir.exists());
    assertTrue(new File(toDir, fromDir.getName()).exists());
  }
 
  /**
   * Tests basic move with filtering.
   * @throws Exception if something unexpected
   */
  @Test(enabled = false)
  public void testMove2() throws Exception {
    File fromDir = new File(fmWorkspace, "from");
    fromDir.mkdir();
 
    File toDir = new File(fmWorkspace, "to");
    toDir.mkdir();
 
    createSourceFiles(fromDir, "abc");
 
    // Create a filter that should reject the operation
    FileFilter filter = new FileFilter() {
      @Override
      public boolean accept(File pathname) {
        return false;
      }
    };
    fileManager.move(fromDir, toDir, filter);
 
    assertTrue(fromDir.exists());
    assertFalse(new File(toDir, fromDir.getName()).exists());
  }
 
  /**
   * Tests basic delete.
   * @throws Exception if something unexpected
   */
  @Test(enabled = false)
  public void testDelete() throws Exception {
    File dir = new File(fmWorkspace, "dir");
    dir.mkdir();
    assertTrue(dir.exists());
    fileManager.delete(dir);
    assertFalse(dir.exists());
 
    File file = new File(fmWorkspace, "file");
    writeContents(file, "abc");
    assertTrue(file.exists());
    fileManager.delete(file);
    assertFalse(file.exists());
  }
 
  /**
   * Tests basic delete with filtering.
   * @throws Exception if something unexpected
   */
  @Test(enabled = false)
  public void testDelete2() throws Exception {
 
    // Create a filter that should reject the operation
    FileFilter filter = new FileFilter() {
      @Override
      public boolean accept(File pathname) {
        return false;
      }
    };
 
    File dir = new File(fmWorkspace, "dir");
    dir.mkdir();
    assertTrue(dir.exists());
    fileManager.delete(dir, filter);
    assertTrue(dir.exists());
 
    File file = new File(fmWorkspace, "file");
    writeContents(file, "abc");
    assertTrue(file.exists());
    fileManager.delete(file, filter);
    assertTrue(file.exists());
  }
 
  /**
   * Test recursive delete.
   * @throws Exception if something unexpected
   */
  @Test(enabled = false)
  public void testDeleteRecursively() throws Exception {
    File dir = new File(fmWorkspace, "dir");
    createSourceFiles(dir, "abc");
    assertTrue(dir.exists());
    fileManager.deleteRecursively(dir);
    assertFalse(dir.exists());
  }
 
  /**
   * Test recursive delete with filtering.
   * @throws Exception if something unexpected
   */
  @Test(enabled = false)
  public void testDeleteRecursively1() throws Exception {
    File dir = new File(fmWorkspace, "dir");
    createSourceFiles(dir, "abc");
    File d2 = new File(dir, "d2");
    File d2b = new File(d2, "d2b");
    final File f2b1 = new File(d2b, "f2b1");
 
    // Test that a filter can stop a delete altogether
    FileFilter rejectOpFilter = new FileFilter() {
      @Override
      public boolean accept(File pathname) {
        return false;
      }
    };
    int childCount = countSelfAndAllChildren(dir);
    assertTrue(dir.exists());
    fileManager.deleteRecursively(dir, rejectOpFilter,
            FileManager.DeletionPolicy.DELETE_IMMEDIATELY);
    assertTrue(dir.exists());
    // Make sure we didn't lose any kids
    assertEquals(childCount, countSelfAndAllChildren(dir));
 
    // Test that using a filter to delete one file works
    FileFilter killChildFileFilter = new FileFilter() {
      @Override
      public boolean accept(File f) {
        return f.equals(f2b1);
      }
    };
    assertTrue(dir.exists());
    assertTrue(f2b1.exists());
    fileManager.deleteRecursively(dir, killChildFileFilter,
            FileManager.DeletionPolicy.DELETE_IMMEDIATELY);
    assertTrue(dir.exists());
    assertEquals((childCount -1), countSelfAndAllChildren(dir));
    assertFalse(f2b1.exists());
  }
 
  /**
   * Test basic copy.
   * @throws Exception if something unexpected
   */
  @Test(enabled = false)
  public void testCopy() throws Exception {
    File file = new File(fmWorkspace, "file");
    writeContents(file, "abc");
    File dir = new File(fmWorkspace, "dir");
    String[] children = dir.list();
    assertTrue(children == null || children.length == 0);
    fileManager.copy(file, dir);
    assertTrue(file.exists());
    assertEquals(dir.list().length, 1);
  }
 
  /**
   * Make sure things fail if target is a file and not a directory.
   * @throws Exception if something unexpected
   */
  @Test(enabled = false, expectedExceptions = ApplicationException.class)
  public void testCopy1() throws Exception {
    File file = new File(fmWorkspace, "file");
    writeContents(file, "abc");
    File file2 = new File(fmWorkspace, "file2");
    writeContents(file2, "abc");
    fileManager.copy(file, file2);
  }
 
  /**
   * Make sure things fail if target is a file and not a directory.
   * @throws Exception if something unexpected
   */
  @Test(enabled = false)
  public void testCopy2() throws Exception {
    File file = new File(fmWorkspace, "file");
    String NEW_CHILD_CONTENT = "new";
    writeContents(file, NEW_CHILD_CONTENT);
    File dir = new File(fmWorkspace, "dir");
    dir.mkdir();
    File dirChild = new File(dir, "file");
    String ORIGINAL_CHILD_CONTENT = "orinld";
    writeContents(dirChild, ORIGINAL_CHILD_CONTENT);
 
    // Try a copy without overwriting and make sure the original file didn't get replaced.
    fileManager.copy(file, dir, false);
    assertEquals(contentsOf(dirChild), ORIGINAL_CHILD_CONTENT);
 
    // New try a copy with overwrite true and make sure the original file got replaced.
    fileManager.copy(file, dir, true);
    assertEquals(contentsOf(dirChild), NEW_CHILD_CONTENT);
  }
 
  /**
   * Test copy recursively.
   * @throws Exception if something unexpected
   */
  @Test(enabled = false)
  public void testCopyRecursively() throws Exception {
    File source = new File(fmWorkspace, "source");
    createSourceFiles(source, "abc");
    int count = countSelfAndAllChildren(source);
 
    File dest = new File(fmWorkspace, "dest");
    fileManager.copyRecursively(source, dest);
 
    File copiedSource = new File(dest, "source");
    assertTrue(copiedSource.exists());
    assertEquals(count, countSelfAndAllChildren(copiedSource));
  }
 
  /**
   * Tests copy recursively with filtering.
   * @throws Exception if something unexpected
   */
  @Test(enabled = false)
  public void testCopyRecursively1() throws Exception {
    // Test that a filter can stop a delete altogether
    FileFilter rejectOpFilter = new FileFilter() {
      @Override
      public boolean accept(File pathname) {
        return false;
      }
    };
 
    File source = new File(fmWorkspace, "source");
    createSourceFiles(source, "abc");
    File d2 = new File(source, "d2");
    File d2b = new File(d2, "d2b");
    final File f2b1 = new File(d2b, "f2b1");
    File dest = new File(fmWorkspace, "dest");
 
    // Make sure a copy with a rejection filter doesn't do anything
    fileManager.copyRecursively(source, dest, rejectOpFilter);
 
    File copiedSource = new File(dest, "source");
    assertFalse(copiedSource.exists());
    assertEquals(countSelfAndAllChildren(dest), 1);
 
    // Test that using a filter to delete one file works
    FileFilter copyChildFileFilter = new FileFilter() {
      @Override
      public boolean accept(File f) {
        return f.equals(f2b1);
      }
    };
    fileManager.copyRecursively(source, dest, copyChildFileFilter);
    File copiedD2 = new File(copiedSource, "d2");
    File copiedD2b = new File(d2, "d2b");
    final File copiedF2b1 = new File(d2b, "f2b1");
    assertTrue(copiedSource.exists());
    assertEquals(countSelfAndAllChildren(copiedSource), 4);
    assertTrue(copiedD2.exists());
    assertEquals(countSelfAndAllChildren(copiedD2), 3);
    assertTrue(copiedD2b.exists());
    assertEquals(countSelfAndAllChildren(copiedD2b), 2);
    assertTrue(copiedF2b1.exists());
  }
 
  /**
   * Tests copy recursively with filtering and overwrite.
   * @throws Exception if something unexpected
   */
  @Test(enabled = false)
  public void testCopyRecursively2() throws Exception {
    File source = new File(fmWorkspace, "source");
    String FILES_TO_COPY = "to copy";
    createSourceFiles(source, FILES_TO_COPY);
    File d2 = new File(source, "d2");
    File d2b = new File(d2, "d2b");
    final File f2b1 = new File(d2b, "f2b1");
    File dest = new File(fmWorkspace, "dest");
 
    File copiedSource = new File(dest, "source");
    File copiedD2 = new File(copiedSource, "d2");
    copiedD2.mkdir();
    File copiedD2b = new File(d2, "d2b");
    copiedD2b.mkdir();
    final File copiedF2b1 = new File(d2b, "f2b1");
    String ORIGINAL = "original";
    writeContents(copiedF2b1, ORIGINAL);
 
    // Test that using a filter to delete one file works
    FileFilter copyChildFileFilter = new FileFilter() {
      @Override
      public boolean accept(File f) {
        return f.equals(f2b1);
      }
    };
    // With overwrite off make sure it doesn't get replaced
    fileManager.copyRecursively(source, dest, copyChildFileFilter, false);
    assertEquals(ORIGINAL, contentsOf(copiedF2b1));
 
    // Now with overwrite make sure it gets replaced.
    fileManager.copyRecursively(source, dest, copyChildFileFilter, true);
    assertEquals(ORIGINAL, contentsOf(copiedF2b1));
  }
 
  @DataProvider(name = "differTestData")
  public Object[][] differTestData() {
    return new Object[][] {
      new Object[] { "abc", "abc" },
      new Object[] { "abc", "xyz" },
      new Object[] { "abc", "abc\n" },
      new Object[] { "abc\n", "abc\n" },
      new Object[] { "abc\nabc", "abc\nabc" },
      new Object[] { "abc\nabc\nabc", "abc\nabc\nabc" }
    };
  }
 
  @Test(dataProvider = "differTestData")
  public void testFilesDiffer(String contents1, String contents2)
          throws Exception
  {
    File f1 = new File(fmWorkspace, "f1");
    File f2 = new File(fmWorkspace, "f2");
    writeContents(f1, contents1);
    writeContents(f2, contents2);
    if (!contents1.equals(contents2)) {
      assertTrue(fileManager.filesDiffer(f1, f2),
              "File contents '" + contents1 + "' and '" + contents2 + "' are " +
                      "not equal despite what FileManager claims");
    } else {
      assertFalse(fileManager.filesDiffer(f1, f2),
              "File contents '" + contents1 + "' and '" + contents2 + "' are " +
                      "equal despite what FileManager claims");
    }
    f1.delete();
    f2.delete();
  }
 
  /**
   * Creates a set of file for testing.
   * @param parent of the files.
   * @param childContent content of non-directory files.
   * @throws IOException if something unexpected
   */
  private void createSourceFiles(File parent, String childContent)
          throws IOException {
    if (!parent.exists()) {
      parent.mkdir();
    }
    File d1 = new File(parent, "d1");
    File f1a = new File(d1, "f1a");
    File f1b = new File(d1, "f1b");
 
    File d2 = new File(parent, "d2");
    File f2a = new File(d2, "f2a");
    File d2b = new File(d2, "d2b");
    File f2b1 = new File(d2b, "f2b1");
 
    d1.mkdir();
    d2.mkdir();
    d2b.mkdir();
 
    writeContents(f1a, childContent);
    writeContents(f1b, childContent);
    writeContents(f2a, childContent);
    writeContents(f2b1, childContent);
  }
 
  private void writeContents(File f, String content) throws IOException {
    if (!f.exists()) f.createNewFile();
    FileWriter fw = new FileWriter(f);
    fw.write(content);
    fw.flush();
    fw.close();
  }
 
  private String contentsOf(File f) throws IOException {
    BufferedReader br = new BufferedReader(new FileReader(f));
    StringBuilder sb = new StringBuilder();
    String s;
    s = br.readLine();
    while (s != null) {
      sb.append(s);
      s = br.readLine();
      if (s != null) {
        sb.append(Constants.LINE_SEPARATOR);
      }
    }
    br.close();
    return sb.toString();
  }
 
  private int countSelfAndAllChildren(File f) {
    int count = 0;
    if (f != null) {
      count = 1;
      if (f.isDirectory()) {
        File[] children = f.listFiles();
        if (children != null) {
          for (File child : children) {
            count += countSelfAndAllChildren(child);
          }
        }
      }
    }
    return count;
  }
}