package com.atlassian.bitbucket.installer;

import com.atlassian.bitbucket.Product;
import com.install4j.api.Util;
import org.junit.Test;

import java.io.File;
import java.util.Locale;

import static org.junit.Assert.assertEquals;
import static org.junit.Assume.assumeTrue;

public class DefaultDirectoryResolverTest {

    private static final String VERSION = "1.0.0";

    @Test
    public void testMacOSX() throws Exception {
        assumeTrue("This test assumes Mac OS", Util.isMacOS());
        DefaultDirectoryResolver defaultDirectoryResolver = new DefaultDirectoryResolver(Platform.MACOSX, VERSION);

        File expectedHomeDirectory = new File(Util.getUserHome(), String.format("Atlassian%1$sApplicationData%1$s%2$s", File.separator, Product.NAME));
        assertEquals(expectedHomeDirectory, defaultDirectoryResolver.resolveHomeDirectory(true));

        File expectedInstallationDirectory = new File(Util.getUserHome(), String.format("Atlassian%1$s%2$s%1$s%3$s", File.separator, Product.NAME, VERSION));
        assertEquals(expectedInstallationDirectory, defaultDirectoryResolver.resolveInstallationDirectory(true));
    }

    @Test
    public void testWindowsForRegularUser() throws Exception {
        testWindows(false);
    }

    @Test
    public void testWindowsForAdmin() throws Exception {
        testWindows(true);
    }

    @Test
    public void testLinuxForRegularUser() throws Exception {
        testLinux(false);
    }

    @Test
    public void testLinuxForAdmin() throws Exception {
        testLinux(true);
    }

    private void testWindows(boolean isAdmin) {
        assumeTrue("This test assumes Windows", Util.isWindows());
        DefaultDirectoryResolver defaultDirectoryResolver = new DefaultDirectoryResolver(Platform.WINDOWS, VERSION);

        String prefix = isAdmin ? File.listRoots()[0].getAbsolutePath() : Util.getUserHome();
        File expectedHomeDirectory = new File(prefix, String.format("Atlassian%1$sApplicationData%1$s%2$s", File.separator, Product.NAME));
        assertEquals(expectedHomeDirectory,
                defaultDirectoryResolver.resolveHomeDirectory(isAdmin));

        File expectedInstallationDirectory = new File(prefix, String.format("Atlassian%1$s%2$s%1$s%3$s", File.separator, Product.NAME, VERSION));
        assertEquals(expectedInstallationDirectory, defaultDirectoryResolver.resolveInstallationDirectory(isAdmin));
    }

    private void testLinux(boolean isAdmin) {
        assumeTrue("This test assumes Linux", Util.isLinux());
        DefaultDirectoryResolver defaultDirectoryResolver = new DefaultDirectoryResolver(Platform.LINUX, VERSION);

        String homePrefix = isAdmin ? File.separator + "var" : Util.getUserHome();
        String installPrefix = isAdmin ? File.separator + "opt" : Util.getUserHome();
        File expectedHomeDirectory = new File(homePrefix, String.format("atlassian%1$sapplication-data%1$s%2$s", File.separator, Product.NAME.toLowerCase(Locale.US)))
                .getAbsoluteFile();
        assertEquals(expectedHomeDirectory, defaultDirectoryResolver.resolveHomeDirectory(isAdmin));

        File expectedInstallationDirectory =
                new File(installPrefix, String.format("atlassian%1$s%2$s%1$s%3$s", File.separator, Product.NAME.toLowerCase(Locale.US), VERSION)).getAbsoluteFile();
        assertEquals(expectedInstallationDirectory,
                defaultDirectoryResolver.resolveInstallationDirectory(isAdmin));
    }
}
