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 static org.junit.Assert.assertFalse;
17  import static org.junit.Assert.assertTrue;
18  
19  import java.io.File;
20  import java.io.IOException;
21  import java.util.ArrayList;
22  import java.util.List;
23  import java.util.Random;
24  
25  import ch.qos.logback.core.Context;
26  import ch.qos.logback.core.ContextBase;
27  import ch.qos.logback.core.testUtil.CoreTestConstants;
28  
29  import org.junit.After;
30  import org.junit.Before;
31  import org.junit.Test;
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      @Before
42      public void setUp() throws Exception {
43  
44      }
45  
46      @After
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 }