View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.util;
15  
16  import java.io.File;
17  import java.io.IOException;
18  import java.util.ArrayList;
19  import java.util.List;
20  import java.util.Random;
21  
22  import ch.qos.logback.core.Context;
23  import ch.qos.logback.core.ContextBase;
24  import ch.qos.logback.core.testUtil.CoreTestConstants;
25  
26  import org.junit.jupiter.api.AfterEach;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  
30  import static org.junit.jupiter.api.Assertions.assertFalse;
31  import static org.junit.jupiter.api.Assertions.assertTrue;
32  
33  public class FileUtilTest {
34  
35      Context context = new ContextBase();
36      FileUtil fileUtil = new FileUtil(context);
37      List<File> cleanupList = new ArrayList<File>();
38      // test-output folder is not always clean
39      int diff = new Random().nextInt(10000);
40  
41      @BeforeEach
42      public void setUp() throws Exception {
43  
44      }
45  
46      @AfterEach
47      public void tearDown() throws Exception {
48          for (File f : cleanupList) {
49              f.delete();
50          }
51      }
52  
53      @Test
54      public void checkParentCreationInquiryAndSubsequentCreation() {
55          File file = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "/fu" + diff + "/testing.txt");
56          // these will be deleted later
57          cleanupList.add(file);
58          cleanupList.add(file.getParentFile());
59  
60          assertFalse(file.getParentFile().exists());
61          assertTrue(FileUtil.createMissingParentDirectories(file));
62          assertTrue(file.getParentFile().exists());
63      }
64  
65      @Test
66      public void checkDeeperParentCreationInquiryAndSubsequentCreation() {
67  
68          File file = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "/fu" + diff + "/bla/testing.txt");
69          // these will be deleted later
70          cleanupList.add(file);
71          cleanupList.add(file.getParentFile());
72          cleanupList.add(file.getParentFile().getParentFile());
73  
74          assertFalse(file.getParentFile().exists());
75          assertTrue(FileUtil.createMissingParentDirectories(file));
76          assertTrue(file.getParentFile().exists());
77      }
78  
79      @Test
80      public void basicCopyingWorks() throws IOException {
81          String dir = CoreTestConstants.OUTPUT_DIR_PREFIX + "/fu" + diff;
82  
83          File dirFile = new File(dir);
84          dirFile.mkdir();
85  
86          String src = CoreTestConstants.TEST_INPUT_PREFIX + "compress1.copy";
87          String target = CoreTestConstants.OUTPUT_DIR_PREFIX + "/fu" + diff + "/copyingWorks.txt";
88  
89          fileUtil.copy(src, target);
90          Compare.compare(src, target);
91      }
92  
93      @Test
94      public void createParentDirIgnoresExistingDir() {
95          String target = CoreTestConstants.OUTPUT_DIR_PREFIX + "/fu" + diff + "/testing.txt";
96          File file = new File(target);
97          cleanupList.add(file);
98          file.mkdirs();
99          assertTrue(file.getParentFile().exists());
100         assertTrue(FileUtil.createMissingParentDirectories(file));
101     }
102 
103     @Test
104     public void createParentDirAcceptsNoParentSpecified() {
105         File file = new File("testing.txt");
106         assertTrue(FileUtil.createMissingParentDirectories(file));
107     }
108 }