001/**
002 * Powerunit - A JDK1.8 test framework
003 * Copyright (C) 2014 Mathieu Boretti.
004 *
005 * This file is part of Powerunit
006 *
007 * Powerunit is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License as published by
009 * the Free Software Foundation, either version 3 of the License, or
010 * (at your option) any later version.
011 *
012 * Powerunit is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015 * GNU General Public License for more details.
016 * 
017 * You should have received a copy of the GNU General Public License
018 * along with Powerunit. If not, see <http://www.gnu.org/licenses/>.
019 */
020package ch.powerunit;
021
022import java.io.PrintStream;
023import java.lang.reflect.Method;
024import java.util.Arrays;
025
026import ch.powerunit.impl.DefaultPowerUnitRunnerImpl;
027import ch.powerunit.impl.DefaultTestResultListener;
028
029/**
030 * Main class that can be used to run test.
031 * 
032 * @author borettim
033 * @since 0.1.0
034 * @see #main(String[]) The main method to have the description of the
035 *      parameters.
036 */
037public class PowerUnitMainRunner {
038
039    private PowerUnitMainRunner() {
040    }
041
042    public static final PrintStream DEFAULT_OUT = System.out;
043
044    /**
045     * The main method.
046     * 
047     * @param args
048     *            The argument syntax is the following :
049     *            <ul>
050     *            <li><code>path className[:methodName],...</code></li>
051     *            </ul>
052     */
053    public static void main(String[] args) {
054        if (args.length != 2) {
055            DEFAULT_OUT.printf("The received argument is not valid : %1$s%n",
056                    Arrays.toString(args));
057            System.exit(-1);// NOSONAR
058        }
059        String outputPath = args[0];
060        String classes[] = args[1].split("\\s*,\\s*");
061        StringBuilder resumedSucess = new StringBuilder();
062        StringBuilder resumedFailure = new StringBuilder();
063        StringBuilder resumedSkipped = new StringBuilder();
064
065        boolean success = true;
066        for (String c : classes) {
067            DEFAULT_OUT.printf("Running test for %1$s%n", c);
068            try {
069                PowerUnitRunner r = null;
070                if (c.contains(":")) {
071                    String param[] = c.split("\\s*:\\s*");
072                    Class<?> cls = Class.forName(param[0]);
073                    Method m = cls.getMethod(param[1]);
074                    r = new DefaultPowerUnitRunnerImpl(cls, m);
075                } else {
076                    Class<?> cls = Class.forName(c);
077                    r = new DefaultPowerUnitRunnerImpl(cls);
078                }
079                DefaultTestResultListener def = new DefaultTestResultListener(
080                        outputPath, DEFAULT_OUT);
081                r.addListener(def);
082                r.run();// NOSONAR
083                success &= !def.isError();
084                resumedSucess.append(def.getResumedSucess());
085                resumedFailure.append(def.getResumedFailure());
086                resumedSkipped.append(def.getResumedSkipped());
087            } catch (ClassNotFoundException e) {
088                DEFAULT_OUT.printf("Unable to create the class %1$s%n", c);
089            } catch (NoSuchMethodException e) {
090                DEFAULT_OUT.printf(
091                        "Unable to create the find the method %1$s%n", c);
092            } catch (SecurityException e) {
093                DEFAULT_OUT
094                        .printf("Unable to create the test because of security issue %1$s%n",
095                                c);
096            } finally {
097                DEFAULT_OUT.printf("End running test for %1$s%n", c);
098            }
099        }
100        DEFAULT_OUT.print("\n\nSuccess tests:\n" + resumedSucess.toString()
101                + "\n\nSkipped tests:\n" + resumedSkipped.toString()
102                + "\n\nFailed tests:\n" + resumedFailure.toString() + "\n");
103        if (!success) {
104            System.exit(-1);// NOSONAR
105        }
106        System.exit(0);// NOSONAR
107    }
108
109}