// @@author A0147944U
package guitests;

import org.apache.commons.io.FileUtils;
import org.junit.Test;

import seedu.task.commons.core.LogsCenter;
import seedu.task.commons.util.ConfigUtil;
import seedu.task.logic.commands.BackupCommand;
import seedu.task.testutil.TestUtil;
import seedu.task.testutil.TypicalTestTasks;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.util.logging.Logger;

public class BackupCommandTest extends TaskManagerGuiTest {

    private static final Logger logger = LogsCenter.getLogger(ConfigUtil.class);

    public static final String filepath_A = TestUtil.getFilePathInSandboxFolder("backup_not_pre_existing");
    public static final String filepath_B = TestUtil.getFilePathInSandboxFolder("backup_pre_existing");
    public static final String filepath_C = "c:/inaccessible";
    public static final String filepath_D = "invalid:/drive";
    public static final String filepath_E = "c:/invalid>character";
    public static final String filepath_F = TestUtil.getFilePathInSandboxFolder("protected");

    @Test
    public void backup() {
        // Remove backup_not_pre_existing.xml if it exists
        File notSupposedToBeHere = new File(filepath_A+ ".xml");
        if (notSupposedToBeHere.exists()) {
            logger.info("'backup_not_pre_existing' exists");
            if(FileUtils.deleteQuietly(notSupposedToBeHere)) {
                logger.info("Not anymore");
            } else {
                logger.warning("Unable to delete backup_not_pre_existing");
            }
        } else {
            logger.info("'backup_not_pre_existing' does not exist ");
        }
        
        //verify an empty TaskManager can be backed up in a valid directory
        commandBox.runCommand("clear");
        assertListSize(0);
        assertResultMessage("Task manager has been cleared!");
        commandBox.runCommand("backup " + filepath_A);
        assertResultMessage(String.format(BackupCommand.MESSAGE_BACKUP_SUCCESS, filepath_A + ".xml", "created"));
        
        //verify a non-empty TaskManager can be backed up in a valid directory
        commandBox.runCommand("add Help Jim with his task, at 2016-10-25 9am");
        assertTrue(taskListPanel.isListMatching(TypicalTestTasks.taskH));
        commandBox.runCommand("backup " + filepath_B);
        assertResultMessage(String.format(BackupCommand.MESSAGE_BACKUP_SUCCESS, filepath_B + ".xml", "overwritten"));
        
        //verify TaskManager can be backed up in a valid directory onto an existing backup
        commandBox.runCommand("backup " + filepath_A);
        assertResultMessage(String.format(BackupCommand.MESSAGE_BACKUP_SUCCESS, filepath_A + ".xml", "overwritten"));

        /* Disabled as Travis is does not have an inaccessible directory, works locally on windows
        //verify a TaskManager can't be backed up in an inaccessible directory
        commandBox.runCommand("backup " + filepath_C);
        assertResultMessage(String.format(BackupCommand.MESSAGE_BACKUP_FAILURE, filepath_C + ".xml"));
        */
        
        /* Disabled as unable to test with FilePicker
        //verify if invalid directory given, FilePicker will be presented
        commandBox.runCommand("backup " + filepath_D);
        assertResultMessage(String.format(BackupCommand.MESSAGE_BACKUP_FAILURE, filepath_D + ".xml"));
        commandBox.runCommand("backup " + filepath_E);
        assertResultMessage(String.format(Messages.MESSAGE_INVALID_COMMAND_FORMAT, BackupCommand.MESSAGE_USAGE));
        */
        
        ///* Disabled as Travis does not respect read-only protection
        //verify a TaskManager will detect if a file is protected
        commandBox.runCommand("backup " + filepath_F);
        assertResultMessage(String.format(BackupCommand.MESSAGE_BACKUP_ERROR, filepath_F + ".xml"));
        //*/
    }
}

@@ -1207,16 +1207,17 @@
 lepath_A
+ 
 + %22.xml%22
@@ -1215,24 +1215,24 @@
  + %22.xml%22);%0A
-
         if (
@@ -1338,16 +1338,17 @@
       if
+ 
 (FileUti
@@ -1640,24 +1640,16 @@
       %7D%0A
-        
 %0A       
@@ -1643,32 +1643,33 @@
    %7D%0A%0A        //
+ 
 verify an empty 
@@ -2013,32 +2013,24 @@
 created%22));%0A
-        
 %0A        //v
@@ -2020,32 +2020,33 @@
 %22));%0A%0A        //
+ 
 verify a non-emp
@@ -2425,24 +2425,16 @@
 ten%22));%0A
-        
 %0A       
@@ -2432,24 +2432,25 @@
 %0A%0A        //
+ 
 verify TaskM
@@ -2501,16 +2501,27 @@
  onto an
+%0A        //
  existin
@@ -3548,78 +3548,9 @@
   //
-/* Disabled as Travis does not respect read-only protection%0A        //
+ 
 veri
@@ -3749,32 +3749,32 @@
 h_F + %22.xml%22));%0A
-
         //*/%0A   
@@ -3767,16 +3767,17 @@
       //
+ 
 */%0A    %7D

format BackupCommandTest