package com.atlassian.bitbucket.util;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;
import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

public class FileUtilsTest {

    @Rule
    public TemporaryFolder folder = new TemporaryFolder();

    @Test(expected = IllegalArgumentException.class)
    public void testCreateTempDirWithInvalidPrefix() throws Exception {
        FileUtils.createTempDir("a", null, folder.getRoot());
    }

    @Test(expected = IOException.class)
    public void testCreateTempDirWithInvalidParent() throws Exception {
        FileUtils.createTempDir("foo", ".tmp", new File(folder.getRoot(), "invalid"));
    }

    @Test
    public void testCreateTempDirTwiceWithSamePrefixCreatesTwoDirectories() throws Exception {
        File a = FileUtils.createTempDir("foo", "", folder.getRoot());
        File b = FileUtils.createTempDir("foo", "", folder.getRoot());
        assertFalse("Same file", a.equals(b));
    }

    @Test
    public void testConstruct() throws Exception {
        assertEquals(new File("a", "b").getAbsoluteFile(), FileUtils.construct(new File("a"), "b").getAbsoluteFile());
        assertEquals(new File(new File("a", "b"), "c").getAbsoluteFile(), FileUtils.construct(new File("a"), "b", "c").getAbsoluteFile());
    }
}
