From 8db60b473c6723b6bffdb98a6fb98c5af95aa04f Mon Sep 17 00:00:00 2001 From: "Erik C. Thauvin" Date: Thu, 27 Jun 2024 20:33:04 -0700 Subject: [PATCH] Log error messages and throw exit status exceptions to stop processing --- .../rife/bld/extension/TestNgOperation.java | 108 ++++++++++-------- 1 file changed, 63 insertions(+), 45 deletions(-) diff --git a/src/main/java/rife/bld/extension/TestNgOperation.java b/src/main/java/rife/bld/extension/TestNgOperation.java index cf2ea52..4ce9979 100644 --- a/src/main/java/rife/bld/extension/TestNgOperation.java +++ b/src/main/java/rife/bld/extension/TestNgOperation.java @@ -18,6 +18,7 @@ package rife.bld.extension; import rife.bld.BaseProject; import rife.bld.operations.TestOperation; +import rife.bld.operations.exceptions.ExitStatusException; import java.io.File; import java.io.IOException; @@ -137,6 +138,23 @@ public class TestNgOperation extends TestOperation return this; } + @Override + public void execute() throws IOException, InterruptedException, ExitStatusException { + if (project_ == null) { + if (LOGGER.isLoggable(Level.SEVERE) && !silent()) { + LOGGER.severe("A project must be specified."); + } + throw new ExitStatusException(ExitStatusException.EXIT_FAILURE); + } else if (packages_.isEmpty() && suites_.isEmpty() && methods_.isEmpty()) { + if (LOGGER.isLoggable(Level.SEVERE) && !silent()) { + LOGGER.severe("At least an XML suite, package or method is required."); + } + throw new ExitStatusException(ExitStatusException.EXIT_FAILURE); + } else { + super.execute(); + } + } + /** * Part of the {@link #execute execute} operation, constructs the command list to use for building the process. * @@ -144,53 +162,53 @@ public class TestNgOperation extends TestOperation */ @Override protected List executeConstructProcessCommandList() { - if (project_ == null) { - LOGGER.severe("A project must be specified."); - } else if (packages_.isEmpty() && suites_.isEmpty()) { - LOGGER.severe("At least one package or XML suite is required."); - } - - if (!options_.containsKey("-d")) { - options_.put("-d", Path.of(project_.buildDirectory().getPath(), "test-output").toString()); - } - final List args = new ArrayList<>(); - args.add(javaTool()); - args.addAll(javaOptions()); - args.add("-cp"); - if (testClasspath_.isEmpty()) { - args.add(String.format("%s:%s:%s:%s", Path.of(project_.libTestDirectory().getPath(), "*"), - Path.of(project_.libCompileDirectory().getPath(), "*"), project_.buildMainDirectory(), - project_.buildTestDirectory())); - } else { - args.add(String.join(":", testClasspath_)); - } - - args.add("org.testng.TestNG"); - - options_.forEach((k, v) -> { - args.add(k); - args.add(v); - }); - - if (!suites_.isEmpty()) { - args.addAll(suites_); - } else if (!options_.containsKey("-testclass")) { - try { - args.add(writeDefaultSuite().getPath()); - } catch (IOException ioe) { - LOGGER.log(Level.SEVERE, "An IO error occurred while accessing the default testng.xml file", ioe); + if (project_ != null) { + if (!options_.containsKey("-d")) { + options_.put("-d", new File(project_.buildDirectory(), "test-output").getAbsolutePath()); } - } - if (LOGGER.isLoggable(Level.FINE)) { - LOGGER.fine(String.join(" ", args)); - } + args.add(javaTool()); + args.addAll(javaOptions()); - if (LOGGER.isLoggable(Level.INFO)) { - LOGGER.info(String.format("Report will be saved in file://%s", - new File(options_.get("-d")).toURI().getPath())); + args.add("-cp"); + if (testClasspath_.isEmpty()) { + args.add(String.format("%s:%s:%s:%s", new File(project_.libTestDirectory(), "*"), + new File(project_.libCompileDirectory(), "*"), project_.buildMainDirectory(), + project_.buildTestDirectory())); + } else { + args.add(String.join(":", testClasspath_)); + } + + args.add("org.testng.TestNG"); + + options_.forEach((k, v) -> { + args.add(k); + args.add(v); + }); + + if (!suites_.isEmpty()) { + args.addAll(suites_); + } else if (!options_.containsKey("-testclass")) { + try { + args.add(writeDefaultSuite().getAbsolutePath()); + } catch (IOException ioe) { + if (LOGGER.isLoggable(Level.SEVERE) && !silent()) { + LOGGER.severe("An IO error occurred while accessing the default testng.xml file: " + + ioe.getMessage()); + } + throw new RuntimeException(ioe); + } + } + + + if (LOGGER.isLoggable(Level.FINE) && !silent()) { + LOGGER.fine(String.join(" ", args)); + } + if (LOGGER.isLoggable(Level.INFO) && !silent()) { + LOGGER.info(String.format("Report will be saved in file://%s", + new File(options_.get("-d")).toURI().getPath())); } return args; @@ -233,7 +251,7 @@ public class TestNgOperation extends TestOperation } /** - * Should TestNG generate results on a per suite basis by creating a sub directory for each suite and dumping + * Should TestNG generate results on a per-suite basis by creating a subdirectory for each suite and dumping * results into it. * *

Default is {@code false}

. @@ -628,7 +646,7 @@ public class TestNgOperation extends TestOperation */ public TestNgOperation shareThreadPoolForDataProviders(boolean shareThreadPoolForDataProviders) { if (shareThreadPoolForDataProviders) { - options_.put("-shareThreadPoolForDataProviders", String.valueOf(shareThreadPoolForDataProviders)); + options_.put("-shareThreadPoolForDataProviders", "true"); } return this; } @@ -943,7 +961,7 @@ public class TestNgOperation extends TestOperation */ public TestNgOperation useGlobalThreadPool(boolean useGlobalThreadPool) { if (useGlobalThreadPool) { - options_.put("-useGlobalThreadPool", String.valueOf(useGlobalThreadPool)); + options_.put("-useGlobalThreadPool", "true"); } return this; }