package com.occamlab.te;

import static org.junit.Assert.assertFalse;

import java.io.File;
import java.io.IOException;
import java.util.List;

import javax.xml.parsers.ParserConfigurationException;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import org.xml.sax.SAXException;

public class SetupOptionsTest {

    static String TE_BASE = "TE_BASE";
    static boolean createdDir;
    static String prevTEBase;

    @BeforeClass
    public static void setSystemProperty() {
        File dir = new File(System.getProperty("user.home")
                + System.getProperty("file.separator") + "teamengine-test");
        if (!dir.exists()) {
            createdDir = dir.mkdir();
        }
        prevTEBase = System.setProperty(TE_BASE, dir.getAbsolutePath());
    }

    @AfterClass
    public static void restoreSystemProperty() {
        if (createdDir) {
            File dir = new File(System.getProperty(TE_BASE));
            File[] children = dir.listFiles();
            for (int i = 0; i < children.length; i++) {
                children[i].delete();
            }
            dir.delete();
        }
        if (null != prevTEBase) {
            System.setProperty(TE_BASE, prevTEBase);
        } else {
            System.clearProperty(TE_BASE);
        }
    }

    @Test
    public void getBaseDir() {
        File baseDir = SetupOptions.getBaseConfigDirectory();
        Assert.assertNotNull("File reference is null.", baseDir);
        Assert.assertTrue(
                "Expected a directory at " + baseDir.getAbsolutePath(),
                baseDir.isDirectory());
    }

    @Test
    public void getWorkDir() {
        SetupOptions iut = new SetupOptions();
        File workDir = iut.getWorkDir();
        Assert.assertTrue(
                "Expected a directory at " + workDir.getAbsolutePath(),
                workDir.isDirectory());
    }

    @Test
    public void getScriptSources() {
        SetupOptions iut = new SetupOptions();
        List<File> sources = iut.getSources();
        Assert.assertEquals("Unexpected size", 0, sources.size());
    }

    @Test
    public void testRecordingInfoWithoutConfig() throws ParserConfigurationException, SAXException, IOException {
        assertFalse(new File(SetupOptions.getBaseConfigDirectory(), "config.xml").exists());
        //checking recording info should work even if config.xml doesn't exist
        SetupOptions.recordingInfo("foo");
    }
}

@@ -142,16 +142,40 @@
 l.List;%0D
+%0Aimport java.util.UUID;%0D
 %0A%0D%0Aimpor
@@ -449,16 +449,42 @@
 BASE%22;%0D%0A
+    static File baseDir;%0D%0A
     stat
@@ -604,38 +604,37 @@
 ty() %7B%0D%0A        
-File d
+baseD
 ir = new File(Sy
@@ -666,122 +666,42 @@
 me%22)
-%0D%0A                + System.getProperty(%22file.separator%22) + %22teamengine-test%22);%0D%0A        if (!dir.exists()) %7B%0D%0A    
+, UUID.randomUUID().toString());%0D%0A
     
@@ -717,17 +717,21 @@
 edDir = 
-d
+baseD
 ir.mkdir
@@ -731,35 +731,24 @@
 r.mkdir();%0D%0A
-        %7D%0D%0A
         prev
@@ -784,17 +784,21 @@
 E_BASE, 
-d
+baseD
 ir.getAb
@@ -1334,24 +1334,65 @@
 %0A        %7D%0D%0A
+        System.clearProperty(%22Record%22);%0D%0A
     %7D%0D%0A%0D%0A   
@@ -1587,34 +1587,16 @@
 ertTrue(
-%0D%0A                
 %22Expecte
@@ -1642,33 +1642,16 @@
 ePath(),
-%0D%0A               
  baseDir
@@ -1836,34 +1836,16 @@
 ertTrue(
-%0D%0A                
 %22Expecte
@@ -1891,33 +1891,16 @@
 ePath(),
-%0D%0A               
  workDir
@@ -2286,66 +2286,42 @@
     
-assertFalse(new File(SetupOptions.getBaseConfigDirectory()
+File configFile = new File(baseDir
 , %22c
@@ -2335,18 +2335,8 @@
 ml%22)
-.exists())
 ;%0D%0A 
@@ -2346,88 +2346,63 @@
     
-//checking recording info should work even if config.xml doesn't exist%0D%0A        
+assertFalse(configFile.exists());%0D%0A        assertFalse(
 Setu
@@ -2430,16 +2430,17 @@
 o(%22foo%22)
+)
 ;%0D%0A    %7D

Create TE_BASE dir with random name