001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.util;
015
016import static org.junit.Assert.assertFalse;
017import static org.junit.Assert.assertTrue;
018
019import java.io.File;
020import java.io.IOException;
021import java.util.ArrayList;
022import java.util.List;
023import java.util.Random;
024
025import ch.qos.logback.core.Context;
026import ch.qos.logback.core.ContextBase;
027import ch.qos.logback.core.testUtil.CoreTestConstants;
028
029import org.junit.After;
030import org.junit.Before;
031import org.junit.Test;
032
033public class FileUtilTest {
034
035    Context context = new ContextBase();
036    FileUtil fileUtil = new FileUtil(context);
037    List<File> cleanupList = new ArrayList<File>();
038    // test-output folder is not always clean
039    int diff = new Random().nextInt(10000);
040
041    @Before
042    public void setUp() throws Exception {
043
044    }
045
046    @After
047    public void tearDown() throws Exception {
048        for (File f : cleanupList) {
049            f.delete();
050        }
051    }
052
053    @Test
054    public void checkParentCreationInquiryAndSubsequentCreation() {
055        File file = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "/fu" + diff + "/testing.txt");
056        // these will be deleted later
057        cleanupList.add(file);
058        cleanupList.add(file.getParentFile());
059
060        assertFalse(file.getParentFile().exists());
061        assertTrue(FileUtil.createMissingParentDirectories(file));
062        assertTrue(file.getParentFile().exists());
063    }
064
065    @Test
066    public void checkDeeperParentCreationInquiryAndSubsequentCreation() {
067
068        File file = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "/fu" + diff + "/bla/testing.txt");
069        // these will be deleted later
070        cleanupList.add(file);
071        cleanupList.add(file.getParentFile());
072        cleanupList.add(file.getParentFile().getParentFile());
073
074        assertFalse(file.getParentFile().exists());
075        assertTrue(FileUtil.createMissingParentDirectories(file));
076        assertTrue(file.getParentFile().exists());
077    }
078
079    @Test
080    public void basicCopyingWorks() throws IOException {
081        String dir = CoreTestConstants.OUTPUT_DIR_PREFIX + "/fu" + diff;
082
083        File dirFile = new File(dir);
084        dirFile.mkdir();
085
086        String src = CoreTestConstants.TEST_INPUT_PREFIX + "compress1.copy";
087        String target = CoreTestConstants.OUTPUT_DIR_PREFIX + "/fu" + diff + "/copyingWorks.txt";
088
089        fileUtil.copy(src, target);
090        Compare.compare(src, target);
091    }
092
093    @Test
094    public void createParentDirIgnoresExistingDir() {
095        String target = CoreTestConstants.OUTPUT_DIR_PREFIX + "/fu" + diff + "/testing.txt";
096        File file = new File(target);
097        cleanupList.add(file);
098        file.mkdirs();
099        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}