/**
 * Copyright (c) 2016, German Neuroinformatics Node (G-Node)
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted under the terms of the BSD License. See
 * LICENSE file in the root of the Project.
 */

package org.g_node.srv;

import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.apache.commons.io.FileUtils;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * Unit tests for the {@link CtrlCheckService} class.
 *
 * @author Michael Sonntag (sonntag@bio.lmu.de)
 */
public class CtrlCheckServiceTest {

    private final String tmpRoot = System.getProperty("java.io.tmpdir");
    private final String testFolderName = "ctrlCheckServiceTest";
    private final String testFileName = "test.txt";
    private final Path testFileFolder = Paths.get(tmpRoot, testFolderName);

    /**
     * Create a test folder and the main test file in the java temp directory.
     * @throws Exception
     */
    @Before
    public void setUp() throws Exception {
        final File currTestFile = this.testFileFolder.resolve(this.testFileName).toFile();
        FileUtils.write(currTestFile, "This is a normal test file");
    }

    /**
     * Remove all created folders and files after the tests are done.
     * @throws Exception
     */
    @After
    public void tearDown() throws Exception {
        if (Files.exists(this.testFileFolder)) {
            FileUtils.deleteDirectory(this.testFileFolder.toFile());
        }
    }

    /**
     * Test if the method returns false when presented with a
     * String referencing a non existing file and returns true if it is
     * presented with String referencing an existing file.
     * @throws Exception
     */
    @Test
    public void testExistingFile() throws Exception {
        final String testNonExistingFilePath =
                this.testFileFolder
                        .resolve("IdoNotExist")
                        .toAbsolutePath().normalize().toString();

        assertThat(CtrlCheckService.isExistingFile(testNonExistingFilePath)).isFalse();

        final String testExistingFilePath =
                this.testFileFolder
                        .resolve(this.testFileName)
                        .toAbsolutePath().normalize().toString();

        assertThat(CtrlCheckService.isExistingFile(testExistingFilePath)).isTrue();
    }

}

@@ -318,20 +318,86 @@
 java.io.
-File
+ByteArrayOutputStream;%0Aimport java.io.File;%0Aimport java.io.PrintStream
 ;%0Aimport
@@ -473,16 +473,106 @@
 .Paths;%0A
+import java.util.Set;%0Aimport java.util.stream.Collectors;%0Aimport java.util.stream.Stream;%0A
 import o
@@ -882,16 +882,123 @@
 ceTest %7B
+%0A    private ByteArrayOutputStream outStream = new ByteArrayOutputStream();%0A    private PrintStream stdout;
 %0A%0A    pr
@@ -1426,32 +1426,177 @@
 ows Exception %7B%0A
+        this.stdout = System.out;%0A        this.outStream = new ByteArrayOutputStream();%0A        System.setOut(new PrintStream(this.outStream));%0A%0A
         final Fi
@@ -1906,32 +1906,69 @@
 ows Exception %7B%0A
+        System.setOut(this.stdout);%0A%0A
         if (File
@@ -2862,32 +2862,32 @@
 ().toString();%0A%0A
-
         assertTh
@@ -2961,10 +2961,1500 @@
 %0A    %7D%0A%0A
+    /**%0A     * Test that the method prints the correct error message if a given String is not%0A     * found in a given Set.%0A     * @throws Exception%0A     */%0A    @Test%0A    public void testSupportedInFileType() throws Exception %7B%0A        final Set%3CString%3E test = Stream.of(%22TXT%22, %22TTL%22).collect(Collectors.toSet());%0A%0A        final String supportedFileType = %22/some/path/someFile.txt%22;%0A        final String unsupportedFileType = %22/some/path/someFile.tex%22;%0A        final String unsupportedMissingFileType = %22/some/path/someFile%22;%0A        final String unsupportedLastEndingIsUsed = %22/some/path/someFile.txt.tex%22;%0A%0A        final String errorMessage = %22 cannot be read.%22;%0A%0A        assertThat(CtrlCheckService.isSupportedInFileType(supportedFileType, test)).isTrue();%0A%0A        assertThat(CtrlCheckService.isSupportedInFileType(unsupportedFileType, test)).isFalse();%0A        assertThat(this.outStream.toString().contains(%0A                String.join(%22%22, unsupportedFileType, errorMessage)%0A        ));%0A        assertThat(CtrlCheckService.isSupportedInFileType(unsupportedMissingFileType, test)).isFalse();%0A        assertThat(this.outStream.toString().contains(%0A                String.join(%22%22, unsupportedMissingFileType, errorMessage)%0A        ));%0A        assertThat(CtrlCheckService.isSupportedInFileType(unsupportedLastEndingIsUsed, test)).isFalse();%0A        assertThat(this.outStream.toString().contains(%0A                String.join(%22%22, unsupportedLastEndingIsUsed, errorMessage)%0A        ));%0A    %7D%0A%0A
 %7D%0A

Add CtrlCheckService supported file type test