package com.softsizo.ui;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.Options;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.Before;
import org.junit.Test;
import com.softsizo.data.ConfigParameters;
import com.softsizo.test.mock.MockReportBuilder;
import com.softsizo.test.mock.MockRepositoryExplorer;
public class ConsoleFrontEndTest {
private ByteArrayOutputStream output;
private MockRepositoryExplorer repositoryExplorer;
private MockReportBuilder reportBuilder;
private ConsoleFrontEnd frontEnd;
private static String[] testArgs = { "-f", "15", "-t", "25", "-u", "serge", "-p",
"secret", "-o", "out-dir", "-r", "http://myrepo.com", "-i", "include-me",
"-e", "exclude-me"};
@Before
public void setUpConsoleFrontEnd(){
output = new ByteArrayOutputStream();
reportBuilder = new MockReportBuilder();
repositoryExplorer = new MockRepositoryExplorer();
frontEnd = new ConsoleFrontEnd();
frontEnd.setReportBuilder(reportBuilder);
frontEnd.setRepositoryExplorer(repositoryExplorer);
PrintStream printStream = new PrintStream(output);
frontEnd.setPrintStream(printStream);
}
@Before
public void turnOffLogger(){
Logger.getRootLogger().setLevel(Level.OFF);
}
@Test
public void testBuildConfigParameters() throws Exception {
Options options = ConsoleFrontEnd.buildOptions();
CommandLineParser parser = new GnuParser();
ConfigParameters configParameters;
CommandLine commandLine;
commandLine = parser.parse(options, testArgs);
configParameters = frontEnd
.buildConfigParameters(commandLine, options);
assertEquals(15, configParameters.getFromRevision());
assertEquals(25, configParameters.getToRevision());
assertEquals("serge", configParameters.getUsername());
assertEquals("secret", configParameters.getPassword());
assertEquals("out-dir", configParameters.getOutputDirectory());
assertEquals("http://myrepo.com", configParameters.getRepoUrl());
assertEquals("include-me", configParameters.getInclude());
assertEquals("exclude-me", configParameters.getExclude());
}
@Test
public void testRun() {
assertFalse(reportBuilder.isReportGenerated());
assertFalse(repositoryExplorer.isReportGenerated());
frontEnd.run(testArgs);
assertTrue(reportBuilder.isReportGenerated());
assertTrue(repositoryExplorer.isReportGenerated());
}
@Test
public void testPrintVersion() throws Exception {
frontEnd.printVersion();
assertTrue(output.toString().contains(ConsoleFrontEnd.APP_NAME + " v"));
}
@Test
public void testPrintVersionFromCommandLine() throws Exception {
Options options = ConsoleFrontEnd.buildOptions();
CommandLineParser parser = new GnuParser();
ConfigParameters configParameters;
CommandLine commandLine;
String [] args = { "-v" };
commandLine = parser.parse(options, args);
configParameters = frontEnd
.buildConfigParameters(commandLine, options);
assertNull(configParameters);
assertTrue(output.toString().contains(ConsoleFrontEnd.APP_NAME + " v"));
}
@Test
public void testPrintHelp() throws Exception {
Options options = ConsoleFrontEnd.buildOptions();
frontEnd.printHelp(options);
String help = output.toString();
assertTrue(help.contains("usage: " + ConsoleFrontEnd.APP_NAME));
assertTrue(help.contains("-f,--fromRevision"));
assertTrue(help.contains("-h,--help"));
assertTrue(help.contains("-o,--output"));
assertTrue(help.contains("-p,--password"));
assertTrue(help.contains("-r,--repository"));
assertTrue(help.contains("-t,--toRevision"));
assertTrue(help.contains("-u,--user"));
assertTrue(help.contains("-v,--version"));
assertTrue(help.contains("-i,--include"));
assertTrue(help.contains("-e,--exclude"));
}
@Test
public void testPrintHelpFromCommandLine() throws Exception {
Options options = ConsoleFrontEnd.buildOptions();
CommandLineParser parser = new GnuParser();
ConfigParameters configParameters;
CommandLine commandLine;
String [] args = { "-h" };
commandLine = parser.parse(options, args);
configParameters = frontEnd
.buildConfigParameters(commandLine, options);
assertNull(configParameters);
assertTrue(output.toString().contains("usage: " + ConsoleFrontEnd.APP_NAME));
}
@Test
public void buildConfigParametersDefaultValues() throws Exception{
String[] args = { "-r", "http://project/svn/trunk" };
Options options = ConsoleFrontEnd.buildOptions();
CommandLineParser parser = new GnuParser();
ConfigParameters configParameters;
CommandLine commandLine;
commandLine = parser.parse(options, args);
configParameters = frontEnd
.buildConfigParameters(commandLine, options);
assertEquals(ConfigParameters.INITIAL_REVISION, configParameters.getFromRevision());
assertEquals(ConfigParameters.HEAD_REVISION, configParameters.getToRevision());
assertNull(configParameters.getUsername());
assertEquals("", configParameters.getPassword());
assertEquals(ConfigParameters.OUTPUT_DIRECTORY_DEFAULT,
configParameters.getOutputDirectory());
assertEquals("http://project/svn/trunk", configParameters.getRepoUrl());
}
@Test
public void parseParameters() throws Exception {
String [] versionArgs = { "-v" };
String[] args = { "-r", "http://project/svn/trunk" };
Options options = ConsoleFrontEnd.buildOptions();
assertFalse(frontEnd.parseParameters(options, versionArgs));
assertTrue(frontEnd.parseParameters(options, args));
assertFalse(output.toString().contains("usage: " + ConsoleFrontEnd.APP_NAME));
}
@Test
public void parseParametersWithInvalidFromRevisionArgs() throws Exception {
String [] invalidFromRevisionArgs = { "-f", "0" };
Options options = ConsoleFrontEnd.buildOptions();
frontEnd.parseParameters(options, invalidFromRevisionArgs);
assertTrue(output.toString().contains("usage: " + ConsoleFrontEnd.APP_NAME));
}
@Test
public void parseParametersWithinvalidRevisionRangeArgs() throws Exception {
String[] invalidRevisionRangeArgs = { "-f", "3", "-t", "3" };
Options options = ConsoleFrontEnd.buildOptions();
frontEnd.parseParameters(options, invalidRevisionRangeArgs);
assertTrue(output.toString().contains("usage: " + ConsoleFrontEnd.APP_NAME));
}
@Test
public void parseParametersWithNoRepoUrlArgs() throws Exception {
String[] noRepoUrlArgs = { "-f", "3", "-t", "4" };
Options options = ConsoleFrontEnd.buildOptions();
frontEnd.parseParameters(options, noRepoUrlArgs);
assertTrue(output.toString().contains("usage: " + ConsoleFrontEnd.APP_NAME));
}
}