summaryrefslogtreecommitdiff
path: root/qpid/java/common/src/test/java/org/apache/qpid/util/FileUtilsTest.java
blob: 7eba5f092ef5628ae531863b1aadc9d04531e672 (plain)
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
610
611
612
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */
package org.apache.qpid.util;

import junit.framework.TestCase;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.List;

public class FileUtilsTest extends TestCase
{
    private static final String COPY = "-Copy";
    private static final String SUB = "-Sub";

    /**
     * Additional test for the copy method.
     * Ensures that the directory count did increase by more than 1 after the copy.
     */
    public void testCopyFile()
    {
        final String TEST_DATA = "FileUtilsTest-testCopy-TestDataTestDataTestDataTestDataTestDataTestData";
        String fileName = "FileUtilsTest-testCopy";
        String fileNameCopy = fileName + COPY;

        File[] beforeCopyFileList = null;

        //Create initial file
        File test = createTestFile(fileName, TEST_DATA);

        try
        {
            //Check number of files before copy
            beforeCopyFileList = test.getAbsoluteFile().getParentFile().listFiles();
            int beforeCopy = beforeCopyFileList.length;

            //Perform Copy
            File destination = new File(fileNameCopy);
            FileUtils.copy(test, destination);
            //Ensure the JVM cleans up if cleanup failues
            destination.deleteOnExit();

            //Retrieve counts after copy
            int afterCopy = test.getAbsoluteFile().getParentFile().listFiles().length;

            int afterCopyFromCopy = new File(fileNameCopy).getAbsoluteFile().getParentFile().listFiles().length;

            // Validate the copy counts
            assertEquals("The file listing from the original and the copy differ in length.", afterCopy, afterCopyFromCopy);
            assertEquals("The number of files did not increase.", beforeCopy + 1, afterCopy);
            assertEquals("The number of files did not increase.", beforeCopy + 1, afterCopyFromCopy);

            //Validate copy
            // Load content
            String copiedFileContent = FileUtils.readFileAsString(fileNameCopy);
            assertEquals(TEST_DATA, copiedFileContent);
        }
        finally // Ensure clean
        {
            //Clean up
            assertTrue("Unable to cleanup", FileUtils.deleteFile(fileNameCopy));

            //Check file list after cleanup
            File[] afterCleanup = new File(test.getAbsoluteFile().getParent()).listFiles();
            checkFileLists(beforeCopyFileList, afterCleanup);

            //Remove original file
            assertTrue("Unable to cleanup", test.delete());
        }
    }

    /**
     * Create and Copy the following structure:
     *
     * testDirectory --+
     * +-- testSubDirectory --+
     * +-- testSubFile
     * +-- File
     *
     * to testDirectory-Copy
     *
     * Validate that the file count in the copy is correct and contents of the copied files is correct.
     */
    public void testCopyRecursive()
    {
        final String TEST_DATA = "FileUtilsTest-testDirectoryCopy-TestDataTestDataTestDataTestDataTestDataTestData";
        String fileName = "FileUtilsTest-testCopy";
        String TEST_DIR = "testDirectoryCopy";

        //Create Initial Structure
        File testDir = new File(TEST_DIR);

        //Check number of files before copy
        File[] beforeCopyFileList = testDir.getAbsoluteFile().getParentFile().listFiles();

        try
        {
            //Create Directories
            assertTrue("Test directory already exists cannot test.", !testDir.exists());

            if (!testDir.mkdir())
            {
                fail("Unable to make test Directory");
            }

            File testSubDir = new File(TEST_DIR + File.separator + TEST_DIR + SUB);
            if (!testSubDir.mkdir())
            {
                fail("Unable to make test sub Directory");
            }

            //Create Files
            createTestFile(testDir.toString() + File.separator + fileName, TEST_DATA);
            createTestFile(testSubDir.toString() + File.separator + fileName + SUB, TEST_DATA);

            //Ensure the JVM cleans up if cleanup failues
            testSubDir.deleteOnExit();
            testDir.deleteOnExit();

            //Perform Copy
            File copyDir = new File(testDir.toString() + COPY);
            try
            {
                FileUtils.copyRecursive(testDir, copyDir);
            }
            catch (FileNotFoundException e)
            {
                fail(e.getMessage());
            }
            catch (FileUtils.UnableToCopyException e)
            {
                fail(e.getMessage());
            }

            //Validate Copy
            assertEquals("Copied directory should only have one file and one directory in it.", 2, copyDir.listFiles().length);

            //Validate Copy File Contents
            String copiedFileContent = FileUtils.readFileAsString(copyDir.toString() + File.separator + fileName);
            assertEquals(TEST_DATA, copiedFileContent);

            //Validate Name of Sub Directory
            assertTrue("Expected subdirectory is not a directory", new File(copyDir.toString() + File.separator + TEST_DIR + SUB).isDirectory());

            //Assert that it contains only one item
            assertEquals("Copied sub directory should only have one directory in it.", 1, new File(copyDir.toString() + File.separator + TEST_DIR + SUB).listFiles().length);

            //Validate content of Sub file
            copiedFileContent = FileUtils.readFileAsString(copyDir.toString() + File.separator + TEST_DIR + SUB + File.separator + fileName + SUB);
            assertEquals(TEST_DATA, copiedFileContent);
        }
        finally
        {
            //Clean up source and copy directory.
            assertTrue("Unable to cleanup", FileUtils.delete(testDir, true));
            assertTrue("Unable to cleanup", FileUtils.delete(new File(TEST_DIR + COPY), true));

            //Check file list after cleanup
            File[] afterCleanup = testDir.getAbsoluteFile().getParentFile().listFiles();
            checkFileLists(beforeCopyFileList, afterCleanup);
        }
    }

    /**
     * Helper method to create a test file with a string content
     *
     * @param fileName  The fileName to use in the creation
     * @param test_data The data to store in the file
     *
     * @return The File reference
     */
    private File createTestFile(String fileName, String test_data)
    {
        File test = new File(fileName);

        try
        {
            test.createNewFile();
            //Ensure the JVM cleans up if cleanup failues
            test.deleteOnExit();
        }
        catch (IOException e)
        {
            fail(e.getMessage());
        }

        BufferedWriter writer = null;
        try
        {
            writer = new BufferedWriter(new FileWriter(test));
            try
            {
                writer.write(test_data);
            }
            catch (IOException e)
            {
                fail(e.getMessage());
            }
        }
        catch (IOException e)
        {
            fail(e.getMessage());
        }
        finally
        {
            try
            {
                if (writer != null)
                {
                    writer.close();
                }
            }
            catch (IOException e)
            {
                fail(e.getMessage());
            }
        }

        return test;
    }

    /** Test that deleteFile only deletes the specified file */
    public void testDeleteFile()
    {
        File test = new File("FileUtilsTest-testDelete");
        //Record file count in parent directory to check it is not changed by delete
        String path = test.getAbsolutePath();
        File[] filesBefore = new File(path.substring(0, path.lastIndexOf(File.separator))).listFiles();
        int fileCountBefore = filesBefore.length;

        try
        {
            test.createNewFile();
            //Ensure the JVM cleans up if cleanup failues
            test.deleteOnExit();
        }
        catch (IOException e)
        {
            fail(e.getMessage());
        }

        assertTrue("File does not exists", test.exists());
        assertTrue("File is not a file", test.isFile());

        //Check that file creation can be seen on disk
        int fileCountCreated = new File(path.substring(0, path.lastIndexOf(File.separator))).listFiles().length;
        assertEquals("File creation was no registered", fileCountBefore + 1, fileCountCreated);

        //Perform Delete
        assertTrue("Unable to cleanup", FileUtils.deleteFile("FileUtilsTest-testDelete"));

        assertTrue("File exists after delete", !test.exists());

        //Check that after deletion the file count is now accurate
        File[] filesAfter = new File(path.substring(0, path.lastIndexOf(File.separator))).listFiles();
        int fileCountAfter = filesAfter.length;
        assertEquals("File creation was no registered", fileCountBefore, fileCountAfter);

        checkFileLists(filesBefore, filesAfter);
    }

    public void testDeleteNonExistentFile()
    {
        File test = new File("FileUtilsTest-testDelete-" + System.currentTimeMillis());

        assertTrue("File exists", !test.exists());
        assertFalse("File is a directory", test.isDirectory());

        assertTrue("Delete Succeeded ", !FileUtils.delete(test, true));
    }

    public void testDeleteNull()
    {
        try
        {
            FileUtils.delete(null, true);
            fail("Delete with null value should throw NPE.");
        }
        catch (NullPointerException npe)
        {
            // expected path
        }
    }

    /**
     * Given two lists of File arrays ensure they are the same length and all entries in Before are in After
     *
     * @param filesBefore File[]
     * @param filesAfter  File[]
     */
    private void checkFileLists(File[] filesBefore, File[] filesAfter)
    {
        assertNotNull("Before file list cannot be null", filesBefore);
        assertNotNull("After file list cannot be null", filesAfter);

        assertEquals("File lists are unequal", filesBefore.length, filesAfter.length);

        for (File fileBefore : filesBefore)
        {
            boolean found = false;

            for (File fileAfter : filesAfter)
            {
                if (fileBefore.getAbsolutePath().equals(fileAfter.getAbsolutePath()))
                {
                    found = true;
                    break;
                }
            }

            assertTrue("File'" + fileBefore.getName() + "' was not in directory afterwards", found);
        }
    }

    public void testNonRecursiveNonEmptyDirectoryDeleteFails()
    {
        String directoryName = "FileUtilsTest-testRecursiveDelete";
        File test = new File(directoryName);

        //Record file count in parent directory to check it is not changed by delete
        String path = test.getAbsolutePath();
        File[] filesBefore = new File(path.substring(0, path.lastIndexOf(File.separator))).listFiles();
        int fileCountBefore = filesBefore.length;

        assertTrue("Directory exists", !test.exists());

        test.mkdir();

        //Create a file in the directory
        String fileName = test.getAbsolutePath() + File.separatorChar + "testFile";
        File subFile = new File(fileName);
        try
        {
            subFile.createNewFile();
            //Ensure the JVM cleans up if cleanup failues
            subFile.deleteOnExit();
        }
        catch (IOException e)
        {
            fail(e.getMessage());
        }
        //Ensure the JVM cleans up if cleanup failues
        // This must be after the subFile as the directory must be empty before
        // the delete is performed
        test.deleteOnExit();

        //Try and delete the non-empty directory
        assertFalse("Non Empty Directory was successfully deleted.", FileUtils.deleteDirectory(directoryName));

        //Check directory is still there
        assertTrue("Directory was deleted.", test.exists());

        // Clean up
        assertTrue("Unable to cleanup", FileUtils.delete(test, true));

        //Check that after deletion the file count is now accurate
        File[] filesAfter = new File(path.substring(0, path.lastIndexOf(File.separator))).listFiles();
        int fileCountAfter = filesAfter.length;
        assertEquals("File creation was no registered", fileCountBefore, fileCountAfter);

        checkFileLists(filesBefore, filesAfter);
    }

    /** Test that an empty directory can be deleted with deleteDirectory */
    public void testEmptyDirectoryDelete()
    {
        String directoryName = "FileUtilsTest-testRecursiveDelete";
        File test = new File(directoryName);

        //Record file count in parent directory to check it is not changed by delete
        String path = test.getAbsolutePath();
        File[] filesBefore = new File(path.substring(0, path.lastIndexOf(File.separator))).listFiles();
        int fileCountBefore = filesBefore.length;

        assertTrue("Directory exists", !test.exists());

        test.mkdir();
        //Ensure the JVM cleans up if cleanup failues
        test.deleteOnExit();

        //Try and delete the empty directory
        assertTrue("Non Empty Directory was successfully deleted.", FileUtils.deleteDirectory(directoryName));

        //Check directory is still there
        assertTrue("Directory was deleted.", !test.exists());

        //Check that after deletion the file count is now accurate
        File[] filesAfter = new File(path.substring(0, path.lastIndexOf(File.separator))).listFiles();
        int fileCountAfter = filesAfter.length;
        assertEquals("File creation was no registered", fileCountBefore, fileCountAfter);

        checkFileLists(filesBefore, filesAfter);

    }

    /** Test that deleteDirectory on a non empty directory to complete */
    public void testNonEmptyDirectoryDelete()
    {
        String directoryName = "FileUtilsTest-testRecursiveDelete";
        File test = new File(directoryName);

        assertTrue("Directory exists", !test.exists());

        //Record file count in parent directory to check it is not changed by delete
        String path = test.getAbsolutePath();
        File[] filesBefore = new File(path.substring(0, path.lastIndexOf(File.separator))).listFiles();
        int fileCountBefore = filesBefore.length;

        test.mkdir();

        //Create a file in the directory
        String fileName = test.getAbsolutePath() + File.separatorChar + "testFile";
        File subFile = new File(fileName);
        try
        {
            subFile.createNewFile();
            //Ensure the JVM cleans up if cleanup failues
            subFile.deleteOnExit();
        }
        catch (IOException e)
        {
            fail(e.getMessage());
        }

        // Ensure the JVM cleans up if cleanup failues
        // This must be after the subFile as the directory must be empty before
        // the delete is performed
        test.deleteOnExit();

        //Try and delete the non-empty directory non-recursively
        assertFalse("Non Empty Directory was successfully deleted.", FileUtils.delete(test, false));

        //Check directory is still there
        assertTrue("Directory was deleted.", test.exists());

        // Clean up
        assertTrue("Unable to cleanup", FileUtils.delete(test, true));

        //Check that after deletion the file count is now accurate
        File[] filesAfter = new File(path.substring(0, path.lastIndexOf(File.separator))).listFiles();
        int fileCountAfter = filesAfter.length;
        assertEquals("File creation was no registered", fileCountBefore, fileCountAfter);

        checkFileLists(filesBefore, filesAfter);

    }

    /** Test that a recursive delete successeds */
    public void testRecursiveDelete()
    {
        String directoryName = "FileUtilsTest-testRecursiveDelete";
        File test = new File(directoryName);

        assertTrue("Directory exists", !test.exists());

        //Record file count in parent directory to check it is not changed by delete
        String path = test.getAbsolutePath();
        File[] filesBefore = new File(path.substring(0, path.lastIndexOf(File.separator))).listFiles();
        int fileCountBefore = filesBefore.length;

        test.mkdir();

        createSubDir(directoryName, 2, 4);

        //Ensure the JVM cleans up if cleanup failues
        // This must be after the sub dir creation as the delete order is
        // recorded and the directory must be empty to be deleted.
        test.deleteOnExit();

        assertFalse("Non recursive delete was able to directory", FileUtils.delete(test, false));

        assertTrue("File does not exist after non recursive delete", test.exists());

        assertTrue("Unable to cleanup", FileUtils.delete(test, true));

        assertTrue("File  exist after recursive delete", !test.exists());

        //Check that after deletion the file count is now accurate
        File[] filesAfter = new File(path.substring(0, path.lastIndexOf(File.separator))).listFiles();
        int fileCountAfter = filesAfter.length;
        assertEquals("File creation was no registered", fileCountBefore, fileCountAfter);

        checkFileLists(filesBefore, filesAfter);

    }

    private void createSubDir(String path, int directories, int files)
    {
        File directory = new File(path);

        assertTrue("Directory" + path + " does not exists", directory.exists());

        for (int dir = 0; dir < directories; dir++)
        {
            String subDirName = path + File.separatorChar + "sub" + dir;
            File subDir = new File(subDirName);

            subDir.mkdir();

            createSubDir(subDirName, directories - 1, files);
            //Ensure the JVM cleans up if cleanup failues
            // This must be after the sub dir creation as the delete order is
            // recorded and the directory must be empty to be deleted.
            subDir.deleteOnExit();
        }

        for (int file = 0; file < files; file++)
        {
            String subDirName = path + File.separatorChar + "file" + file;
            File subFile = new File(subDirName);
            try
            {
                subFile.createNewFile();
                //Ensure the JVM cleans up if cleanup failues
                subFile.deleteOnExit();
            }
            catch (IOException e)
            {
                fail(e.getMessage());
            }
        }
    }

    public static final String SEARCH_STRING = "testSearch";

    /**
     * Test searchFile(File file, String search) will find a match when it
     * exists.
     *
     * @throws java.io.IOException if unable to perform test setup
     */
    public void testSearchSucceed() throws IOException
    {
        File _logfile = File.createTempFile("FileUtilsTest-testSearchSucceed", ".out");

        prepareFileForSearchTest(_logfile);

        List<String> results = FileUtils.searchFile(_logfile, SEARCH_STRING);

        assertNotNull("Null result set returned", results);

        assertEquals("Results do not contain expected count", 1, results.size());
    }

    /**
     * Test searchFile(File file, String search) will not find a match when the
     * test string does not exist.
     *
     * @throws java.io.IOException if unable to perform test setup
     */
    public void testSearchFail() throws IOException
    {
        File _logfile = File.createTempFile("FileUtilsTest-testSearchFail", ".out");

        prepareFileForSearchTest(_logfile);

        List<String> results = FileUtils.searchFile(_logfile, "Hello");

        assertNotNull("Null result set returned", results);

        //Validate we only got one message
        if (results.size() > 0)
        {
            System.err.println("Unexpected messages");

            for (String msg : results)
            {
                System.err.println(msg);
            }
        }

        assertEquals("Results contains data when it was not expected",
                     0, results.size());
    }

    /**
     * Write the SEARCH_STRING in to the given file.
     *
     * @param logfile The file to write the SEARCH_STRING into
     *
     * @throws IOException if an error occurs
     */
    private void prepareFileForSearchTest(File logfile) throws IOException
    {
        BufferedWriter writer = new BufferedWriter(new FileWriter(logfile));
        writer.append(SEARCH_STRING);
        writer.flush();
        writer.close();
    }

}