com.greenpepper.runner.CommandLineRunnerTest.java Source code

Java tutorial

Introduction

Here is the source code for com.greenpepper.runner.CommandLineRunnerTest.java

Source

/*
 * Copyright (c) 2006 Pyxis Technologies inc.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 /*
 * Copyright (c) 2006 Pyxis Technologies inc.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA,
 * or see the FSF site: http://www.fsf.org.
 */

package com.greenpepper.runner;

import static java.lang.String.format;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;

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

import com.greenpepper.AlternateCalculator;
import com.greenpepper.dialect.BracketSyntaxDialect;
import com.greenpepper.reflect.Fixture;
import com.greenpepper.reflect.PlainOldFixture;
import com.greenpepper.report.XmlReport;
import com.greenpepper.repository.FileSystemRepository;
import com.greenpepper.systemunderdevelopment.DefaultSystemUnderDevelopment;
import com.greenpepper.util.ExceptionUtils;
import com.greenpepper.util.IOUtil;
import com.greenpepper.util.cli.ParseException;

public class CommandLineRunnerTest {
    private File outputDir;

    @Before
    public void setUp() throws Exception {
        createOutputDirectory();
    }

    private void createOutputDirectory() {
        URL testDirectory = getClass().getResource(".");
        File parentDirectory = FileUtils.toFile(testDirectory);
        outputDir = new File(parentDirectory, "specs");
    }

    @After
    public void tearDown() throws Exception {
        deleteOutputDirectory();
        String input = getResourcePath("/specs/ABankSample.html");
        FileUtils.deleteQuietly(new File(input + ".out.html"));
    }

    private void deleteOutputDirectory() {
        if (outputDir != null)
            IOUtil.deleteDirectoryTree(outputDir);
    }

    @Test
    public void testCanRunASingleSpecificationAndProduceAReportFile() throws Exception {
        String input = getResourcePath("/specs/ABankSample.html");
        File outputFile = outputFile("report.html");

        new CommandLineRunner().run(input, outputFile.getAbsolutePath());

        assertFile(outputFile);
    }

    @Test
    public void testShouldNotOverrideInputFile() throws URISyntaxException, IOException, ParseException {
        String input = getResourcePath("/specs/ABankSample.html");

        new CommandLineRunner().run("-o", new File(input).getParent(), input);
        assertFile(new File(input));
        assertFile(new File(input + ".out"));
    }

    @Test
    public void testCanGenerateAUniqueReportFileNameFromSpecificationName() throws Exception {
        String input = getResourcePath("/specs/ABankSample.html");
        File expectedOutputFile = outputFile("ABankSample.html.xml");

        new CommandLineRunner().run("--xml", "-o", outputDir.getAbsolutePath(), input);
        assertFile(expectedOutputFile);
    }

    @Test
    public void testCanRunASuiteOfSpecificationsAndGenerateReports() throws Exception {
        String input = getResourcePath("/specs");
        new CommandLineRunner().run("-s", input, outputDir.getAbsolutePath());

        assertTrue(outputDir.isDirectory());
        File[] files = outputDir.listFiles();
        assertNotNull(files);
        assertEquals(3, files.length);
    }

    private void assertFile(File file) {
        assertTrue(file.getAbsolutePath() + " should exist", file.exists());
        long length = file.length();
        assertTrue(file.getAbsolutePath() + " should not be empty", length > 0);
    }

    private File outputFile(String fileName) {
        return new File(outputDir, fileName);
    }

    private String getResourcePath(String name) throws URISyntaxException {
        return CommandLineRunnerTest.class.getResource(name).getPath();
    }

    @Test
    public void testThatFixtureFactoryCanBeSpecifiedAlongWithArguments() throws Exception {
        File reportFile = outputFile("report.xml");
        String[] args = new String[] { "-f", CustomSystemUnderDevelopment.class.getName() + ";param1;param2",
                "--xml", getResourcePath("/WithACustomFixtureFactory.html"), reportFile.getAbsolutePath() };

        new CommandLineRunner().run(args);
        XmlReport parser = XmlReport.parse(new FileReader(reportFile));
        assertEquals(1, parser.getSuccess(0));
        assertEquals(1, parser.getFailure(0));
    }

    @Test
    public void testThatDebugModeAllowToSeeWholeStackTrace() throws Exception {

        String input = getResourcePath("/specs/ABankSample.html");
        File outputFile = outputFile("report.html");

        CommandLineRunner commandLineRunner = new CommandLineRunner();
        commandLineRunner.run("--debug", input, outputFile.getAbsolutePath());

        try {
            throw new Exception(new Throwable(""));
        } catch (Exception e) {
            assertTrue(countLines(ExceptionUtils.stackTrace(e.getCause(), "\n", 2)) > 2 + 1);
        }

    }

    @Test
    public void shouldUseACustomDialectForRunningTextTest() throws Exception {
        File reportFile = outputFile("report.xml");
        String[] args = new String[] { "--xml", "-f",
                CustomSystemUnderDevelopment.class.getName() + ";param1;param2", "-r",
                format("%s;%s;txt", FileSystemRepository.class.getName(), getResourcePath("/")), "-d",
                BracketSyntaxDialect.class.getName(), "WithDummyText.txt", reportFile.getAbsolutePath() };

        new CommandLineRunner().run(args);
        XmlReport parser = XmlReport.parse(new FileReader(reportFile));
        assertEquals(1, parser.getSuccess(0));
        assertEquals(1, parser.getFailure(0));
    }

    private int countLines(String val) {
        int result = 0;

        while (val.contains("\n")) {
            result++;
            val = val.substring(val.indexOf("\n") + 1);
        }

        return result;
    }

    public static class CustomSystemUnderDevelopment extends DefaultSystemUnderDevelopment {
        public CustomSystemUnderDevelopment(String... params) {
        }

        public Fixture getFixture(String name, String... params) throws Exception {
            return new PlainOldFixture(classForName(name).newInstance());
        }

        public void addImport(String packageName) {
        }

        public Class classForName(String fixtureName) throws Exception {
            if (fixtureName.equals("MyFixture"))
                return AlternateCalculator.class;
            else
                throw new Exception();
        }
    }

}