diff --git a/apache-rat-core/pom.xml b/apache-rat-core/pom.xml index 21f0c583c..061670044 100644 --- a/apache-rat-core/pom.xml +++ b/apache-rat-core/pom.xml @@ -263,5 +263,10 @@ jimfs test + + org.xmlunit + xmlunit-assertj3 + test + 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 226394df0..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,9 +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) -ClaimStatistic statistic = reporter.execute() +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/main/java/org/apache/rat/ConfigurationException.java b/apache-rat-core/src/main/java/org/apache/rat/ConfigurationException.java index d5add9fed..f8a94cb2d 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/ConfigurationException.java +++ b/apache-rat-core/src/main/java/org/apache/rat/ConfigurationException.java @@ -26,8 +26,8 @@ public class ConfigurationException extends RuntimeException { private static final long serialVersionUID = 7257245932787579431L; public static ConfigurationException from(final Exception e) { - if (e instanceof ConfigurationException) { - return (ConfigurationException) e; + if (e instanceof ConfigurationException exists) { + return exists; } return new ConfigurationException(e); } diff --git a/apache-rat-core/src/main/java/org/apache/rat/Defaults.java b/apache-rat-core/src/main/java/org/apache/rat/Defaults.java index 87c7ed8df..16850e7b0 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/Defaults.java +++ b/apache-rat-core/src/main/java/org/apache/rat/Defaults.java @@ -49,7 +49,8 @@ public final class Defaults { /** The default configuration file from the package. */ private static final URI DEFAULT_CONFIG_URI; /** The path to the default configuration file. */ - private static final String DEFAULT_CONFIG_PATH = "/org/apache/rat/default.xml"; + // sonar wants this to be configurable. + private static final String DEFAULT_CONFIG_PATH = "/org/apache/rat/default.xml"; // NOSONAR /** The default ARCHIVES processing style. */ public static final ReportConfiguration.Processing ARCHIVE_PROCESSING = ReportConfiguration.Processing.NOTIFICATION; /** The default STANDARD processing style. */ diff --git a/apache-rat-core/src/main/java/org/apache/rat/ImplementationException.java b/apache-rat-core/src/main/java/org/apache/rat/ImplementationException.java index 76bd2f7c9..17fc47243 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/ImplementationException.java +++ b/apache-rat-core/src/main/java/org/apache/rat/ImplementationException.java @@ -26,8 +26,8 @@ public class ImplementationException extends RuntimeException { private static final long serialVersionUID = 7257245932787579431L; public static ImplementationException makeInstance(final Exception e) { - if (e instanceof ImplementationException) { - return (ImplementationException) e; + if (e instanceof ImplementationException exists) { + return exists; } return new ImplementationException(e); } 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 d04398080..d3531b59a 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 @@ -34,7 +34,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; @@ -48,7 +47,7 @@ import org.apache.rat.document.FileDocument; import org.apache.rat.help.Licenses; import org.apache.rat.license.LicenseSetFactory; -import org.apache.rat.report.IReportable; +import org.apache.rat.report.Reportable; import org.apache.rat.report.claim.ClaimStatistic; import org.apache.rat.utils.DefaultLog; import org.apache.rat.utils.Log.Level; @@ -131,27 +130,22 @@ public static ReportConfiguration parseCommands(final File workingDirectory, fin 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" } - - 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; } @@ -183,14 +177,14 @@ public static ReportConfiguration createConfiguration(final ArgumentContext argu Optional