public static void main()

in src/main/org/apache/tools/ant/taskdefs/optional/junit/JUnitTestRunner.java [910:1048]


    public static void main(final String[] args) throws IOException {
        String[] methods = null;
        boolean haltError = false;
        boolean haltFail = false;
        boolean stackfilter = true;
        final Properties props = new Properties();
        boolean showOut = false;
        boolean outputToFormat = true;
        boolean logFailedTests = true;
        boolean logTestListenerEvents = false;
        boolean skipNonTests = false;
        /* Ant id of thread running this unit test, 0 in single-threaded mode */
        int antThreadID = 0;

        if (args.length == 0) {
            System.err.println("required argument TestClassName missing");
            System.exit(ERRORS);
        }

        if (args[0].startsWith(Constants.TESTSFILE)) {
            multipleTests = true;
            args[0] = args[0].substring(Constants.TESTSFILE.length());
        }

        for (String arg : args) {
            if (arg.startsWith(Constants.METHOD_NAMES)) {
                try {
                    final String methodsList = arg.substring(Constants.METHOD_NAMES.length());
                    methods = JUnitTest.parseTestMethodNamesList(methodsList);
                } catch (final IllegalArgumentException ex) {
                    System.err.println("Invalid specification of test method names: " + arg);
                    System.exit(ERRORS);
                }
            } else if (arg.startsWith(Constants.HALT_ON_ERROR)) {
                haltError = Project.toBoolean(arg.substring(Constants.HALT_ON_ERROR.length()));
            } else if (arg.startsWith(Constants.HALT_ON_FAILURE)) {
                haltFail = Project.toBoolean(arg.substring(Constants.HALT_ON_FAILURE.length()));
            } else if (arg.startsWith(Constants.FILTERTRACE)) {
                stackfilter = Project.toBoolean(arg.substring(Constants.FILTERTRACE.length()));
            } else if (arg.startsWith(Constants.CRASHFILE)) {
                crashFile = arg.substring(Constants.CRASHFILE.length());
                registerTestCase(Constants.BEFORE_FIRST_TEST);
            } else if (arg.startsWith(Constants.FORMATTER)) {
                try {
                    createAndStoreFormatter(arg.substring(Constants.FORMATTER.length()));
                } catch (final BuildException be) {
                    System.err.println(be.getMessage());
                    System.exit(ERRORS);
                }
            } else if (arg.startsWith(Constants.PROPSFILE)) {
                final InputStream in = Files.newInputStream(Paths.get(arg
                        .substring(Constants.PROPSFILE.length())));
                props.load(in);
                in.close();
            } else if (arg.startsWith(Constants.SHOWOUTPUT)) {
                showOut = Project.toBoolean(arg.substring(Constants.SHOWOUTPUT.length()));
            } else if (arg.startsWith(Constants.LOGTESTLISTENEREVENTS)) {
                logTestListenerEvents = Project.toBoolean(
                    arg.substring(Constants.LOGTESTLISTENEREVENTS.length()));
            } else if (arg.startsWith(Constants.OUTPUT_TO_FORMATTERS)) {
                outputToFormat = Project.toBoolean(
                    arg.substring(Constants.OUTPUT_TO_FORMATTERS.length()));
            } else if (arg.startsWith(Constants.LOG_FAILED_TESTS)) {
                logFailedTests = Project.toBoolean(
                    arg.substring(Constants.LOG_FAILED_TESTS.length()));
            } else if (arg.startsWith(Constants.SKIP_NON_TESTS)) {
                skipNonTests = Project.toBoolean(
                    arg.substring(Constants.SKIP_NON_TESTS.length()));
            } else if (arg.startsWith(Constants.THREADID)) {
                antThreadID = Integer.parseInt(arg.substring(Constants.THREADID.length()));
            }
        }

        // Add/overlay system properties on the properties from the Ant project
        props.putAll(System.getProperties());

        int returnCode = SUCCESS;
        if (multipleTests) {
            try (final BufferedReader reader = new BufferedReader(new FileReader(args[0]))) {
                int code = 0;
                boolean errorOccurred = false;
                boolean failureOccurred = false;
                String line = null;
                while ((line = reader.readLine()) != null) {
                    final StringTokenizer st = new StringTokenizer(line, ",");
                    final String testListSpec = st.nextToken();
                    final int colonIndex = testListSpec.indexOf(':');
                    String testCaseName;
                    String[] testMethodNames;
                    if (colonIndex == -1) {
                        testCaseName = testListSpec;
                        testMethodNames = null;
                    } else {
                        testCaseName = testListSpec.substring(0, colonIndex);
                        testMethodNames = JUnitTest.parseTestMethodNamesList(
                                                    testListSpec
                                                    .substring(colonIndex + 1)
                                                    .replace('+', ','));
                    }
                    final JUnitTest t = new JUnitTest(testCaseName);
                    t.setTodir(new File(st.nextToken()));
                    t.setOutfile(st.nextToken());
                    t.setProperties(props);
                    t.setSkipNonTests(skipNonTests);
                    t.setThread(antThreadID);
                    code = launch(t, testMethodNames, haltError, stackfilter, haltFail,
                                  showOut, outputToFormat,
                                  logTestListenerEvents);
                    errorOccurred = (code == ERRORS);
                    failureOccurred = (code != SUCCESS);
                    if (errorOccurred || failureOccurred) {
                        if ((errorOccurred && haltError) || (failureOccurred && haltFail)) {
                            registerNonCrash();
                            System.exit(code);
                        } else {
                            if (code > returnCode) {
                                returnCode = code;
                            }
                            if (logFailedTests) {
                                System.out.println("TEST " + t.getName() + " FAILED");
                            }
                        }
                    }
                }
            } catch (final IOException e) {
                e.printStackTrace(); //NOSONAR
            }
        } else {
            final JUnitTest t = new JUnitTest(args[0]);
            t.setThread(antThreadID);
            t.setProperties(props);
            t.setSkipNonTests(skipNonTests);
            returnCode = launch(t, methods, haltError, stackfilter, haltFail, showOut,
                    outputToFormat, logTestListenerEvents);
        }

        registerNonCrash();
        System.exit(returnCode);
    }