/**
 * 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;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

import static org.assertj.core.api.Assertions.assertThat;

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

/**
 * Unit tests for the main App. Output and Error streams are redirected
 * from the console to a different PrintStream and reset after tests are finished
 * to avoid mixing tool error messages with actual test error messages.
 *
 * @author Michael Sonntag (sonntag@bio.lmu.de)
 */
public class AppTest {

    private ByteArrayOutputStream outStream;
    private PrintStream stdout;

    /**
     * Redirect Error and Out stream.
     * @throws Exception
     */
    @Before
    public void setUp() throws Exception {

        this.stdout = System.out;
        this.outStream = new ByteArrayOutputStream();
        System.setOut(new PrintStream(this.outStream));

        Logger rootLogger = Logger.getRootLogger();
        rootLogger.setLevel(Level.INFO);
        rootLogger.addAppender(
                new ConsoleAppender(
                        new PatternLayout("[%-5p] %m%n")
                )
        );
    }

    /**
     * Reset Out stream to the console after the tests are done.
     * @throws Exception
     */
    @After
    public void tearDown() throws Exception {
        System.setOut(this.stdout);
    }

    @Test
    public void testMainEmptyArgs() throws Exception {
        final String[] emptyArgs = new String[0];
        App.main(emptyArgs);
        assertThat(this.outStream.toString()).contains("[ERROR] No existing report tool selected!");
    }

    @Test
    public void testMainInvalidArgs() throws Exception {
        final String[] invalidArgs = new String[1];
        invalidArgs[0] = "iDoNotExist";
        App.main(invalidArgs);
        assertThat(this.outStream.toString()).contains("[ERROR] No existing report tool selected!");
    }

}

@@ -2319,10 +2319,2061 @@
 %0A    %7D%0A%0A
+    @Test%0A    public void testMainLKTMissingRequiredOptions() throws Exception %7B%0A        final String useCase = %22lkt%22;%0A        final String missingOptMessage = %22Missing required option:%22;%0A        final String missingArgMessage = %22Missing argument for option: %22;%0A        final String argVal = %22argumentValue%22;%0A%0A        String%5B%5D missingOptionArgs = new String%5B1%5D;%0A        missingOptionArgs%5B0%5D = useCase;%0A        App.main(missingOptionArgs);%0A        assertThat(this.outStream.toString()).contains(%22Missing required options:%22);%0A%0A        missingOptionArgs = new String%5B2%5D;%0A        missingOptionArgs%5B0%5D = useCase;%0A        missingOptionArgs%5B1%5D = %22-i%22;%0A        App.main(missingOptionArgs);%0A        assertThat(this.outStream.toString()).contains(String.join(%22%22, missingArgMessage, %22i%22));%0A%0A        missingOptionArgs = new String%5B2%5D;%0A        missingOptionArgs%5B0%5D = useCase;%0A        missingOptionArgs%5B1%5D = %22-r%22;%0A        App.main(missingOptionArgs);%0A        assertThat(this.outStream.toString()).contains(String.join(%22%22, missingArgMessage, %22r%22));%0A%0A        missingOptionArgs = new String%5B3%5D;%0A        missingOptionArgs%5B0%5D = useCase;%0A        missingOptionArgs%5B1%5D = %22-i%22;%0A        missingOptionArgs%5B2%5D = argVal;%0A        App.main(missingOptionArgs);%0A        assertThat(this.outStream.toString()).contains(missingOptMessage);%0A%0A        missingOptionArgs = new String%5B3%5D;%0A        missingOptionArgs%5B0%5D = useCase;%0A        missingOptionArgs%5B1%5D = %22-r%22;%0A        missingOptionArgs%5B2%5D = argVal;%0A        App.main(missingOptionArgs);%0A        assertThat(this.outStream.toString()).contains(missingOptMessage);%0A    %7D%0A%0A    @Test%0A    public void testMainHelp() throws Exception %7B%0A        final String useCase = %22lkt%22;%0A        final String argVal = %22val%22;%0A%0A        final String%5B%5D helpArgs = new String%5B6%5D;%0A        helpArgs%5B0%5D = useCase;%0A        helpArgs%5B1%5D = %22-h%22;%0A        helpArgs%5B2%5D = %22-i%22;%0A        helpArgs%5B3%5D = argVal;%0A        helpArgs%5B4%5D = %22-r%22;%0A        helpArgs%5B5%5D = argVal;%0A        App.main(helpArgs);%0A        assertThat(this.outStream.toString()).contains(%22usage: Help%22);%0A    %7D%0A%0A
 %7D%0A

Add tests for required cli options