diff --git a/apache-rat-core/src/it/java/org/apache/rat/ReportTest.java b/apache-rat-core/src/it/java/org/apache/rat/ReportTest.java index b695134a6..34cf3909d 100644 --- a/apache-rat-core/src/it/java/org/apache/rat/ReportTest.java +++ b/apache-rat-core/src/it/java/org/apache/rat/ReportTest.java @@ -80,7 +80,7 @@ * associated with the exception. * */ -public class ReportTest { +class ReportTest { private String[] asArgs(final List argsList) { return argsList.toArray(new String[0]); @@ -88,7 +88,7 @@ private String[] asArgs(final List argsList) { @ParameterizedTest(name = "{index} {0}") @MethodSource("args") - public void integrationTest(String testName, Document commandLineDoc) throws Exception { + void integrationTest(String testName, Document commandLineDoc) throws Exception { DefaultLog.getInstance().log(Log.Level.INFO, "Running test for " + testName); File baseDir = new File(commandLineDoc.getName().getName()).getParentFile(); @@ -119,9 +119,11 @@ public void integrationTest(String testName, Document commandLineDoc) throws Exc File expectedMsg = new File(baseDir, "expected-message.txt"); if (expectedMsg.exists()) { - String msg = IOUtils.readLines(new FileReader(expectedMsg)).get(0).trim(); - assertThrows(RatDocumentAnalysisException.class, () -> Report.main(asArgs(argsList)), - msg); + try (FileReader fr = new FileReader(expectedMsg)) { + String msg = IOUtils.readLines(fr).get(0).trim(); + assertThrows(RatDocumentAnalysisException.class, () -> Report.main(asArgs(argsList)), + msg); + } } else { Report.main(asArgs(argsList)); } @@ -142,7 +144,7 @@ public void integrationTest(String testName, Document commandLineDoc) throws Exc try { Object value = shell.run(groovyScript, new String[]{outputFile.getAbsolutePath(), logFile.getAbsolutePath()}); if (value != null) { - fail(String.format("%s", value)); + fail(String.format("%s: %s", testName, value)); } } catch (AssertionError e) { throw new AssertionError(String.format("%s: %s", testName, e.getMessage()), e); @@ -204,6 +206,7 @@ public static class FileLog implements Log { * * @param level the level to use when writing messages. */ + @Override public void setLevel(final Level level) { this.level = level; } diff --git a/apache-rat-core/src/it/resources/ReportTest/RAT_14/verify.groovy b/apache-rat-core/src/it/resources/ReportTest/RAT_14/verify.groovy index 0a46a22f9..c0027ba13 100644 --- a/apache-rat-core/src/it/resources/ReportTest/RAT_14/verify.groovy +++ b/apache-rat-core/src/it/resources/ReportTest/RAT_14/verify.groovy @@ -66,10 +66,9 @@ myArgs[3] = src.getAbsolutePath() ReportConfiguration configuration = OptionCollection.parseCommands(src, myArgs, { opts -> }) assertNotNull(configuration) -configuration.validate(DefaultLog.getInstance().&error) +configuration.validate() Reporter reporter = new Reporter(configuration) -Reporter.Output output = reporter.execute() -ClaimStatistic statistic = output.getStatistic() +ClaimStatistic statistic = reporter.execute().getStatistic() assertEquals(3, statistic.getCounter(ClaimStatistic.Counter.APPROVED)) assertEquals(2, statistic.getCounter(ClaimStatistic.Counter.ARCHIVES)) diff --git a/apache-rat-core/src/it/resources/ReportTest/RAT_362/expected-message.txt b/apache-rat-core/src/it/resources/ReportTest/RAT_362/expected-message.txt index bea4ba545..7e187ea8d 100644 --- a/apache-rat-core/src/it/resources/ReportTest/RAT_362/expected-message.txt +++ b/apache-rat-core/src/it/resources/ReportTest/RAT_362/expected-message.txt @@ -1 +1 @@ -Issues with UNAPPROVED +Issues with LICENSE_CATEGORIES, LICENSE_NAMES, STANDARDS diff --git a/apache-rat-core/src/it/resources/ReportTest/RAT_406/commandLine.txt b/apache-rat-core/src/it/resources/ReportTest/RAT_406/commandLine.txt index 0d6433f57..cec3b5e30 100644 --- a/apache-rat-core/src/it/resources/ReportTest/RAT_406/commandLine.txt +++ b/apache-rat-core/src/it/resources/ReportTest/RAT_406/commandLine.txt @@ -1,2 +1,3 @@ --licenses-denied DOJO +-- diff --git a/apache-rat-core/src/main/java/org/apache/rat/CLIOption.java b/apache-rat-core/src/main/java/org/apache/rat/CLIOption.java index 7b75cdb8e..395452158 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/CLIOption.java +++ b/apache-rat-core/src/main/java/org/apache/rat/CLIOption.java @@ -18,19 +18,21 @@ */ package org.apache.rat; +import java.util.function.Function; + import org.apache.commons.cli.Option; import org.apache.commons.lang3.StringUtils; import org.apache.rat.ui.ArgumentTracker; import org.apache.rat.ui.UIOption; -import org.apache.rat.ui.UIOptionCollection; +import org.apache.rat.utils.CasedString; /** * The CLI option definition. */ public final class CLIOption extends UIOption { - public CLIOption(final UIOptionCollection collection, final Option option) { - super(collection, option, ArgumentTracker.extractName(option)); + private CLIOption(final CLIBuilder builder) { + super(builder); } @Override @@ -70,4 +72,20 @@ public String getExample() { } return sb.toString(); } + + /** + * Builder for a CLI Option. + */ + public static class CLIBuilder extends UIOption.Builder { + + @Override + protected Function getNameFactory() { + return ArgumentTracker::extractName; + } + + @Override + protected CLIOption doBuild() { + return new CLIOption(this); + } + } } diff --git a/apache-rat-core/src/main/java/org/apache/rat/CLIOptionCollection.java b/apache-rat-core/src/main/java/org/apache/rat/CLIOptionCollection.java index 9abb0ca57..9b192b163 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/CLIOptionCollection.java +++ b/apache-rat-core/src/main/java/org/apache/rat/CLIOptionCollection.java @@ -21,20 +21,20 @@ import org.apache.commons.cli.Option; import org.apache.rat.ui.UIOptionCollection; +/** + * The collection of CLI Options. + */ public final class CLIOptionCollection extends UIOptionCollection { /** The Help option */ static final Option HELP = new Option("?", "help", false, "Print help for the RAT command line interface and exit."); - /** The instance of the collection */ - public static final CLIOptionCollection INSTANCE = new CLIOptionCollection(); - - private CLIOptionCollection() { + public CLIOptionCollection() { super(new Builder().uiOption(HELP)); } private static final class Builder extends UIOptionCollection.Builder { private Builder() { - super(CLIOption::new); + super(CLIOption.CLIBuilder::new); } } } diff --git a/apache-rat-core/src/main/java/org/apache/rat/OptionCollection.java b/apache-rat-core/src/main/java/org/apache/rat/OptionCollection.java index 2d4bdd636..96c4c8263 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/OptionCollection.java +++ b/apache-rat-core/src/main/java/org/apache/rat/OptionCollection.java @@ -35,7 +35,6 @@ import java.util.stream.Collectors; import org.apache.commons.cli.CommandLine; -import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; @@ -51,6 +50,8 @@ import org.apache.rat.license.LicenseSetFactory; import org.apache.rat.report.Reportable; import org.apache.rat.report.claim.ClaimStatistic; +import org.apache.rat.ui.ArgumentTracker; +import org.apache.rat.ui.UIOptionCollection; import org.apache.rat.utils.DefaultLog; import org.apache.rat.utils.Log.Level; import org.apache.rat.walker.ArchiveWalker; @@ -68,6 +69,11 @@ private OptionCollection() { // do not instantiate } + /** + * The collection of UI Options. + */ + private static UIOptionCollection baseOptionCollection = new CLIOptionCollection(); + /** * The Option comparator to sort the help. */ @@ -130,29 +136,23 @@ public static ReportConfiguration parseCommands(final File workingDirectory, fin */ public static ReportConfiguration parseCommands(final File workingDirectory, final String[] args, final Consumer helpCmd, final boolean noArgs) throws IOException { - Options opts = buildOptions(); - CommandLine commandLine; + ArgumentContext argumentContext; try { - commandLine = DefaultParser.builder().setDeprecatedHandler(DeprecationReporter.getLogReporter()) - .setAllowPartialMatching(true).build().parse(opts, args); + argumentContext = new ArgumentContext(workingDirectory, opts, args); } catch (ParseException e) { - DefaultLog.getInstance().error(e.getMessage()); - DefaultLog.getInstance().error("Please use the \"--help\" option to see a list of valid commands and options.", e); System.exit(1); return null; // dummy return (won't be reached) to avoid Eclipse complaint about possible NPE // for "commandLine" } + Arg.processLogLevel(argumentContext, baseOptionCollection); - ArgumentContext argumentContext = new ArgumentContext(workingDirectory, commandLine); - Arg.processLogLevel(argumentContext, CLIOptionCollection.INSTANCE); - - if (commandLine.hasOption(HELP)) { + if (argumentContext.getCommandLine().hasOption(HELP)) { helpCmd.accept(opts); return null; } - if (commandLine.hasOption(Arg.HELP_LICENSES.option())) { + if (argumentContext.getCommandLine().hasOption(Arg.HELP_LICENSES.option())) { new Licenses(createConfiguration(argumentContext), new PrintWriter(System.out, false, StandardCharsets.UTF_8)).printHelp(); return null; } @@ -178,25 +178,37 @@ public static ReportConfiguration parseCommands(final File workingDirectory, fin * @see #parseCommands(File, String[], Consumer, boolean) */ public static ReportConfiguration createConfiguration(final ArgumentContext argumentContext) { - argumentContext.processArgs(CLIOptionCollection.INSTANCE); - final ReportConfiguration configuration = argumentContext.getConfiguration(); - final CommandLine commandLine = argumentContext.getCommandLine(); - Optional