package com.atlassian.stash.internal.logback;

import org.apache.commons.io.FileUtils;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.File;

import static org.junit.Assert.assertEquals;

public class HomeDirectoryPropertyDefinerTest {

    @Rule
    public TemporaryFolder temporaryFolder = new TemporaryFolder();

    @Test
    public void testGetPropertyValue() throws Exception {
        File home = temporaryFolder.newFolder();

        HomeDirectoryPropertyDefiner propertyDefiner = newHomeDirectoryPropertyDefiner(home);
        assertEquals(home.getAbsolutePath(), propertyDefiner.getPropertyValue());
    }

    @Test(expected = IllegalStateException.class)
    public void testGetPropertyValueResolvingHomeToFile() throws Exception {
        File temp = temporaryFolder.newFolder();
        File home = new File(temp, "stash-home");
        FileUtils.writeStringToFile(home, "t");

        HomeDirectoryPropertyDefiner propertyDefiner = newHomeDirectoryPropertyDefiner(home);
        propertyDefiner.getPropertyValue();
    }

    private static HomeDirectoryPropertyDefiner newHomeDirectoryPropertyDefiner(final File dir) {
        return new HomeDirectoryPropertyDefiner() {
            @Override
            protected File resolveHomeDirectory() {
                return dir;
            }
        };
    }
}
