From d1309cb1ad94d324193b39aaffdc03512bd5e763 Mon Sep 17 00:00:00 2001 From: Claude Warren Date: Sun, 22 Mar 2026 08:21:54 +0000 Subject: [PATCH 01/11] initial working code --- apache-rat-core/pom.xml | 25 +- .../main/java/org/apache/rat/CLIOption.java | 73 ++ .../org/apache/rat/CLIOptionCollection.java | 41 + .../java/org/apache/rat/OptionCollection.java | 12 +- .../apache/rat/OptionCollectionParser.java | 325 ++++++++ .../java/org/apache/rat/commandline/Arg.java | 703 +++++++++--------- .../rat/commandline/ArgumentContext.java | 7 +- .../org/apache/rat/help/AbstractHelp.java | 5 +- .../apache/rat/ui/AbstractCodeGenerator.java | 151 ++++ .../org/apache/rat/ui/ArgumentTracker.java | 239 ++++++ .../src/main/java/org/apache/rat/ui/UI.java | 36 + .../main/java/org/apache/rat/ui/UIOption.java | 224 ++++++ .../org/apache/rat/ui/UIOptionCollection.java | 299 ++++++++ .../apache/rat/ui/UpdatableOptionGroup.java | 85 +++ .../ui/UpdatableOptionGroupCollection.java | 110 +++ .../java/org/apache/rat/ui/package-info.java | 23 + .../org/apache/rat/ui/spi/UIProvider.java | 25 + .../org/apache/rat/ui/spi/package-info.java | 22 + .../org/apache/rat/commandline/ArgTests.java | 3 +- .../apache/rat/ui/ArgumentTrackerTest.java | 122 +++ .../apache/rat/ui/UIOptionCollectionTest.java | 160 ++++ .../rat/documentation/options/CLIOption.java | 2 +- pom.xml | 10 + 23 files changed, 2312 insertions(+), 390 deletions(-) create mode 100644 apache-rat-core/src/main/java/org/apache/rat/CLIOption.java create mode 100644 apache-rat-core/src/main/java/org/apache/rat/CLIOptionCollection.java create mode 100644 apache-rat-core/src/main/java/org/apache/rat/OptionCollectionParser.java create mode 100644 apache-rat-core/src/main/java/org/apache/rat/ui/AbstractCodeGenerator.java create mode 100644 apache-rat-core/src/main/java/org/apache/rat/ui/ArgumentTracker.java create mode 100644 apache-rat-core/src/main/java/org/apache/rat/ui/UI.java create mode 100644 apache-rat-core/src/main/java/org/apache/rat/ui/UIOption.java create mode 100644 apache-rat-core/src/main/java/org/apache/rat/ui/UIOptionCollection.java create mode 100644 apache-rat-core/src/main/java/org/apache/rat/ui/UpdatableOptionGroup.java create mode 100644 apache-rat-core/src/main/java/org/apache/rat/ui/UpdatableOptionGroupCollection.java create mode 100644 apache-rat-core/src/main/java/org/apache/rat/ui/package-info.java create mode 100644 apache-rat-core/src/main/java/org/apache/rat/ui/spi/UIProvider.java create mode 100644 apache-rat-core/src/main/java/org/apache/rat/ui/spi/package-info.java create mode 100644 apache-rat-core/src/test/java/org/apache/rat/ui/ArgumentTrackerTest.java create mode 100644 apache-rat-core/src/test/java/org/apache/rat/ui/UIOptionCollectionTest.java diff --git a/apache-rat-core/pom.xml b/apache-rat-core/pom.xml index 3984e84a9..a4c35b8c2 100644 --- a/apache-rat-core/pom.xml +++ b/apache-rat-core/pom.xml @@ -37,7 +37,6 @@ src/main/filtered-resources - org.apache.rat @@ -56,9 +55,6 @@ - - - org.apache.maven.plugins maven-jar-plugin @@ -177,8 +173,20 @@ - org.apache.rat - apache-rat-testdata + com.github.spotbugs + spotbugs-annotations + + + org.apache.velocity + velocity-engine-core + + + org.apache.velocity.tools + velocity-tools-generic + + + org.reflections + reflections test @@ -213,11 +221,6 @@ junit-jupiter-api test - - org.junit.vintage - junit-vintage-engine - test - org.junit.jupiter junit-jupiter-params 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 new file mode 100644 index 000000000..03b790440 --- /dev/null +++ b/apache-rat-core/src/main/java/org/apache/rat/CLIOption.java @@ -0,0 +1,73 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.rat; + +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; + +/** + * The CLI option definition. + */ +public final class CLIOption extends UIOption { + + public CLIOption(final UIOptionCollection collection, final Option option) { + super(collection, option, ArgumentTracker.extractKey(option)); + } + + @Override + public String getText() { + StringBuilder result = new StringBuilder(); + if (option.getLongOpt() != null) { + result.append("--").append(option.getLongOpt()); + if (option.getOpt() != null) { + result.append(" or -").append(option.getArgs()); + } + } else { + result.append("-").append(option.getArgs()); + } + return result.toString(); + } + + @Override + protected String cleanupName(final Option option) { + return ArgumentTracker.extractKey(option); + } + + @Override + public String getExample() { + StringBuilder sb = new StringBuilder("-"); + if (option.getLongOpt() != null) { + sb.append("-").append(option.getLongOpt()); + } else { + sb.append(option.getOpt()); + } + if (option.hasArg()) { + String argName = StringUtils.defaultIfBlank(option.getArgName(), "Arg"); + sb.append(" ").append(argName); + if (option.hasArgs()) { + sb.append(" [").append(argName).append("2 [").append(argName) + .append("3 [...]]] --"); + } + } + return sb.toString(); + } +} 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 new file mode 100644 index 000000000..feb1cbf51 --- /dev/null +++ b/apache-rat-core/src/main/java/org/apache/rat/CLIOptionCollection.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.rat; + +import org.apache.commons.cli.Option; +import org.apache.rat.ui.UIOptionCollection; + +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() { + super(new Builder().uiOption(HELP) + .mapper(CLIOption::new)); + } + + private static final class Builder extends UIOptionCollection.Builder { + private Builder() { + super(); + } + } +} 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 6ce2e1c2d..b16ebd092 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 @@ -140,9 +140,8 @@ public static ReportConfiguration parseCommands(final File workingDirectory, fin // for "commandLine" } - Arg.processLogLevel(commandLine); - ArgumentContext argumentContext = new ArgumentContext(workingDirectory, commandLine); + Arg.processLogLevel(argumentContext, CLIOptionCollection.INSTANCE); if (commandLine.hasOption(HELP)) { helpCmd.accept(opts); @@ -175,14 +174,15 @@ public static ReportConfiguration parseCommands(final File workingDirectory, fin * @see #parseCommands(File, String[], Consumer, boolean) */ static ReportConfiguration createConfiguration(final ArgumentContext argumentContext) { - argumentContext.processArgs(); + argumentContext.processArgs(CLIOptionCollection.INSTANCE); final ReportConfiguration configuration = argumentContext.getConfiguration(); final CommandLine commandLine = argumentContext.getCommandLine(); - if (Arg.DIR.isSelected()) { + if (CLIOptionCollection.INSTANCE.isSelected(Arg.DIR)) { try { - configuration.addSource(getReportable(commandLine.getParsedOptionValue(Arg.DIR.getSelected()), configuration)); + configuration.addSource(getReportable(commandLine.getParsedOptionValue( + CLIOptionCollection.INSTANCE.getSelected(Arg.DIR).get()), configuration)); } catch (ParseException e) { - throw new ConfigurationException("Unable to set parse " + Arg.DIR.getSelected(), e); + throw new ConfigurationException("Unable to set parse " + CLIOptionCollection.INSTANCE.getSelected(Arg.DIR).get(), e); } } for (String s : commandLine.getArgs()) { diff --git a/apache-rat-core/src/main/java/org/apache/rat/OptionCollectionParser.java b/apache-rat-core/src/main/java/org/apache/rat/OptionCollectionParser.java new file mode 100644 index 000000000..5798b7385 --- /dev/null +++ b/apache-rat-core/src/main/java/org/apache/rat/OptionCollectionParser.java @@ -0,0 +1,325 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one * + * or more contributor license agreements. See the NOTICE file * + * distributed with this work for additional information * + * regarding copyright ownership. The ASF licenses this file * + * to you under the Apache License, Version 2.0 (the * + * "License"); you may not use this file except in compliance * + * with the License. You may obtain a copy of the License at * + * * + * http://www.apache.org/licenses/LICENSE-2.0 * + * * + * Unless required by applicable law or agreed to in writing, * + * software distributed under the License is distributed on an * + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * + * KIND, either express or implied. See the License for the * + * specific language governing permissions and limitations * + * under the License. * + */ +package org.apache.rat; + +import java.io.File; +import java.io.IOException; +import java.io.PrintWriter; +import java.io.Serial; +import java.io.Serializable; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Comparator; +import java.util.function.Supplier; +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; +import org.apache.rat.api.Document; +import org.apache.rat.commandline.Arg; +import org.apache.rat.commandline.ArgumentContext; +import org.apache.rat.commandline.StyleSheets; +import org.apache.rat.config.exclusion.StandardCollection; +import org.apache.rat.document.DocumentName; +import org.apache.rat.document.DocumentNameMatcher; +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.claim.ClaimStatistic; +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; +import org.apache.rat.walker.DirectoryWalker; + +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; + +import static java.lang.String.format; + +/** + * Uses the AbstractOptionCollection to parse the command line options. + * contains utility methods to ReportConfiguration from the options and an array of arguments. + */ +@SuppressFBWarnings("EI_EXPOSE_REP2") +public final class OptionCollectionParser { + /** The OptionCollection that we are working with */ + private final UIOptionCollection uiOptionCollection; + + public OptionCollectionParser(final UIOptionCollection optionCollection) { + this.uiOptionCollection = optionCollection; + } + + /** The Option comparator to sort the help */ + public static final Comparator diff --git a/apache-rat-plugin/src/it/RAT-469/pom.xml b/apache-rat-plugin/src/it/RAT-469/pom.xml index 10ea7967d..19687a2d9 100644 --- a/apache-rat-plugin/src/it/RAT-469/pom.xml +++ b/apache-rat-plugin/src/it/RAT-469/pom.xml @@ -32,15 +32,11 @@ GPL3 true false - - pom.xml - - - STANDARD_PATTERNS - STANDARD_SCMS - MAVEN - IDEA - + pom.xml + STANDARD_PATTERNS + STANDARD_SCMS + MAVEN + IDEA diff --git a/apache-rat-plugin/src/it/RAT-508/pom.xml b/apache-rat-plugin/src/it/RAT-508/pom.xml index 6a66121c6..d0741e00b 100644 --- a/apache-rat-plugin/src/it/RAT-508/pom.xml +++ b/apache-rat-plugin/src/it/RAT-508/pom.xml @@ -34,12 +34,8 @@ true true false - - pom.xml - - - ALL - + pom.xml + ALL diff --git a/apache-rat-plugin/src/it/RAT-524/invoker.properties b/apache-rat-plugin/src/it/RAT-524/invoker.properties index 1c089b469..0a419ea70 100644 --- a/apache-rat-plugin/src/it/RAT-524/invoker.properties +++ b/apache-rat-plugin/src/it/RAT-524/invoker.properties @@ -13,4 +13,4 @@ # See the License for the specific language governing permissions and # limitations under the License. -invoker.goals = clean -X apache-rat:rat +invoker.goals = -X clean apache-rat:rat diff --git a/apache-rat-plugin/src/test/java/org/apache/rat/mp/OptionMojoTest.java b/apache-rat-plugin/src/test/java/org/apache/rat/mp/OptionMojoTest.java index 7b9e27e41..5268e7048 100644 --- a/apache-rat-plugin/src/test/java/org/apache/rat/mp/OptionMojoTest.java +++ b/apache-rat-plugin/src/test/java/org/apache/rat/mp/OptionMojoTest.java @@ -34,6 +34,7 @@ import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.io.CleanupMode; import org.junit.jupiter.api.io.TempDir; import org.junit.jupiter.params.ParameterizedTest; @@ -52,7 +53,7 @@ import static java.lang.String.format; import static org.junit.jupiter.api.Assertions.fail; - +@Disabled("Change in Maven requires rework") public class OptionMojoTest { @TempDir(cleanup = CleanupMode.NEVER) @@ -133,11 +134,6 @@ protected final ReportConfiguration generateConfig(List> throw new IOException(e.getMessage(), e); } } - - @Override - protected void helpTest() { - fail("Should not call help"); - } } public abstract static class SimpleMojoTestcase extends BetterAbstractMojoTestCase { diff --git a/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatCheckMojoTest.java b/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatCheckMojoTest.java index 1afb32eb3..686f4ca1f 100644 --- a/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatCheckMojoTest.java +++ b/apache-rat-plugin/src/test/java/org/apache/rat/mp/RatCheckMojoTest.java @@ -463,11 +463,8 @@ void rat107() throws Exception { final String[] expected = {}; final String[] notExpected = {}; //setVariableValueToObject(mojo, "excludeSubProjects", Boolean.FALSE); - mojo.setInputExcludeParsedScm("MAVEN"); - mojo.setInputExcludeParsedScm("idea"); - mojo.setInputExcludeParsedScm("eclipse"); + mojo.setInputExcludeParsedScms(new String[] {"MAVEN", "idea", "eclipse"}); mojo.execute(); - ensureRatReportIsCorrect(ratTxtFile, expected, notExpected); } } diff --git a/apache-rat-plugin/src/test/resources/unit/it5/pom.xml b/apache-rat-plugin/src/test/resources/unit/it5/pom.xml index 474872128..4c770efa7 100644 --- a/apache-rat-plugin/src/test/resources/unit/it5/pom.xml +++ b/apache-rat-plugin/src/test/resources/unit/it5/pom.xml @@ -28,12 +28,12 @@ @pom.version@ xml - .rat/customConfig.xml - - .rat/** - pom.xml - invoker.properties - + + .rat/customConfig.xml + + .rat/** + pom.xml + invoker.properties diff --git a/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Help.java b/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Help.java index 7c6256a17..3f42ed407 100644 --- a/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Help.java +++ b/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Help.java @@ -27,12 +27,14 @@ import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; +import org.apache.commons.collections4.list.TreeList; import org.apache.commons.lang3.StringUtils; import org.apache.rat.commandline.Arg; import org.apache.rat.config.exclusion.StandardCollection; +import org.apache.rat.documentation.options.AntOptionCollection; import org.apache.rat.help.AbstractHelp; -import org.apache.rat.documentation.options.AbstractOption; import org.apache.rat.documentation.options.AntOption; +import org.apache.rat.ui.UIOption; import org.apache.rat.utils.DefaultLog; import org.apache.rat.utils.Log; @@ -178,11 +180,9 @@ protected StringBuffer renderOptions(final StringBuffer sb, final int width, fin String descriptionTitle = " -- Description --"; int max = optionTitle.length(); int maxExample = exampleTitle.length(); - final List optList = options.getOptions().stream().filter(Option::hasLongOpt) - .map(AntOption::new).collect(Collectors.toList()); - if (getOptionComparator() != null) { - optList.sort(Comparator.comparing(AbstractOption::getName)); - } + final List optList = new ArrayList<>(); + AntOptionCollection.INSTANCE.getMappedOptions().forEach(optList::add); + optList.sort(Comparator.comparing(UIOption::getName)); List exampleList = new ArrayList<>(); for (final AntOption option : optList) { String argName = StringUtils.defaultIfEmpty(option.getArgName(), "value"); diff --git a/apache-rat-tasks/src/test/java/org/apache/rat/anttasks/GeneratedReportTest.java b/apache-rat-tasks/src/test/java/org/apache/rat/anttasks/GeneratedReportTest.java index ca86ee4e7..0e39d2f03 100644 --- a/apache-rat-tasks/src/test/java/org/apache/rat/anttasks/GeneratedReportTest.java +++ b/apache-rat-tasks/src/test/java/org/apache/rat/anttasks/GeneratedReportTest.java @@ -30,14 +30,19 @@ import java.util.List; import java.util.Map; import java.util.stream.Stream; + +import org.apache.commons.cli.Option; import org.apache.commons.lang3.StringUtils; import org.apache.rat.OptionCollection; import org.apache.rat.ReportConfiguration; import org.apache.rat.commandline.Arg; import org.apache.rat.commandline.StyleSheets; import org.apache.rat.document.DocumentName; +import org.apache.rat.documentation.options.AntOptionCollection; import org.apache.rat.license.LicenseSetFactory; import org.apache.rat.documentation.options.AntOption; +import org.apache.rat.ui.ArgumentTracker; +import org.apache.rat.utils.CasedString; import org.apache.rat.utils.DefaultLog; import org.apache.tools.ant.BuildEvent; import org.apache.tools.ant.BuildListener; @@ -53,62 +58,64 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; +import javax.sound.midi.Track; + import static java.lang.String.format; import static org.assertj.core.api.Assertions.assertThat; -public class GeneratedReportTest { +public class GeneratedReportTest { @TempDir static Path tempDir; private static final Map REQUIRED_ATTRIBUTES = new HashMap<>(); private static final Map REQUIRED_ELEMENTS = new HashMap<>(); - private static final Map ARG_TYPE_MAP = new HashMap<>(); - - static { - BuildType buildType = null; - for (OptionCollection.ArgumentType argType : OptionCollection.ArgumentType.values()) { - switch (argType) { - case FILE: - case DIRORARCHIVE: - buildType = new BuildType("") { - @Override - protected String getMethodFormat(final AntOption antOption) { - return ""; - } - }; - break; - case NONE: - buildType = new BuildType("") { - @Override - protected String getMethodFormat(final AntOption antOption) { - return ""; - } - }; - break; - case STANDARDCOLLECTION: - buildType = new BuildType("std"); - break; - case EXPRESSION: - buildType = new BuildType("expr"); - break; - case COUNTERPATTERN: - buildType = new BuildType("cntr"); - break; - case LICENSEID: - case FAMILYID: - buildType = new BuildType("lst"); - break; - default: - buildType = new BuildType("") { - @Override - protected String getMethodFormat(final AntOption antOption) { - return format("<%1$s>%%s", tag); - } - }; - } - ARG_TYPE_MAP.put(argType, buildType); - } - } + + +// static { +// BuildType buildType = null; +// for (OptionCollection.ArgumentType argType : OptionCollection.ArgumentType.values()) { +// switch (argType) { +// case FILE: +// case DIRORARCHIVE: +// buildType = new BuildType("") { +// @Override +// protected String getMethodFormat(final AntOption antOption) { +// return ""; +// } +// }; +// break; +// case NONE: +// buildType = new BuildType("") { +// @Override +// protected String getMethodFormat(final AntOption antOption) { +// return ""; +// } +// }; +// break; +// case STANDARDCOLLECTION: +// buildType = new BuildType("std"); +// break; +// case EXPRESSION: +// buildType = new BuildType("expr"); +// break; +// case COUNTERPATTERN: +// buildType = new BuildType("cntr"); +// break; +// case LICENSEID: +// case FAMILYID: +// buildType = new BuildType("lst"); +// break; +// default: +// buildType = new BuildType("") { +// @Override +// protected String getMethodFormat(final AntOption antOption) { +// return format("<%1$s>%%s", tag); +// } +// }; +// } +// ARG_TYPE_MAP.put(argType, buildType); +// } +// } /** * The prefix for the ant build.xml file. @@ -158,7 +165,7 @@ public void setup() throws IOException { } private static String configFile(String fileName) { - return format("", fileName); + return format("", fileName); } @ParameterizedTest(name = "{index} {0}") @@ -221,24 +228,23 @@ private void executeTarget(StringBuilder outputBuffer, StringBuilder errorBuffer static String targetName(AntOption option) { AntOption actualOption = option.getActualAntOption(); - return actualOption.getName() + (actualOption.isAttribute() ? "Attribute" :"Element"); + return actualOption.getName() + (actualOption.isAttribute() ? "Attribute" : "Element"); } /** * Generate the data for the tests. + * * @return the arguments for the tests. */ static Stream generatedData() { - List options = Arg.getOptions().getOptions().stream() - .filter(o -> !AntOption.getFilteredOptions().contains(o)).map(AntOption::new) - .toList(); + List options = AntOptionCollection.INSTANCE.getMappedOptions().toList(); List lst = new ArrayList<>(); for (AntOption option : options) { lst.add(createTest(option)); - option.convertedFrom().forEach(o -> lst.add(createTest(new AntOption(o)))); + option.convertedFrom().forEach(o -> lst.add(createTest(o))); } for (Arguments arguments : lst) { Object[] objects = arguments.get(); @@ -249,8 +255,8 @@ static Stream generatedData() { private static Arguments createTest(AntOption option) { AntOption actualOption = option.getActualAntOption(); - BuildType buildType = ARG_TYPE_MAP.get(option.getArgType()); - String xml = buildXml(actualOption, option, buildType.getXml(option)); + AntOptionCollection.BuildType buildType = option.buildType(); + String xml = buildXml(actualOption, option, buildType.getXml(option, getData(option))); return Arguments.of(buildType.testName(option), xml, option); } @@ -278,7 +284,7 @@ private static String buildXml(AntOption actualOption, AntOption option, String // if (actualOption.argCount() == 1) { // xml.append(format(" <%s %s=\"%s\" />%n", actualOption.getName(), createAttribute(option), getData(option))); // } else { - xml.append(format(" <%1$s>%2$s%n", actualOption.getName(), body)); + xml.append(format(" <%1$s>%2$s%n", actualOption.getName(), body)); // } } } @@ -293,17 +299,22 @@ private static String buildXml(AntOption actualOption, AntOption option, String return xml.toString(); } - private static String getData(AntOption option) { - String value = getData(option.getName()); + private static String getData(AntOption antOption) { + + String value = getData(ArgumentTracker.extractName(antOption.getOption()).toCase(CasedString.StringCase.PASCAL)); + + //String value = getData(antOption.getCasedName().toCase(CasedString.StringCase.PASCAL)); + if (value == null) { - if (!option.hasArg()) { + if (!antOption.hasArg()) { return "true"; } else { - throw new IllegalStateException("Missing " + option.getName()); + throw new IllegalStateException("Missing " + antOption.getName()); } } return value; } + private static String getData(String name) { try { return switch (name) { @@ -394,6 +405,7 @@ private static class AntTestListener implements BuildListener { private final int logLevel; private final StringBuilder logBuffer; private final StringBuilder fullLogBuffer; + /** * Constructs a test listener which will ignore log events * above the given level. @@ -482,41 +494,4 @@ public void write(int b) { buffer.append((char) b); } } - - public static class BuildType { - /** The configuration tag for this build type */ - protected final String tag; - /** If True adds the tag as the test extension */ - private final boolean addExt; - - BuildType(final String tag) { - this(tag, StringUtils.isNotEmpty(tag)); - } - - BuildType(final String tag, boolean addExt) { - this.tag = tag; - this.addExt = addExt; - } - - protected String getMultipleFormat(final AntOption antOption) { - return String.format(" <%1$s>%%s\n", tag); - } - - protected String getMethodFormat(final AntOption antOption) { - return antOption.hasArgs() ? getMultipleFormat(antOption) : String.format(" <%1$s>%%s\n", tag); - } - - public String testName(final AntOption antOption) { - return addExt ? format("%s_%s", antOption.getName(), antOption.getArgName()) : antOption.getName(); - } - - public String getXml(final AntOption antOption) { - AntOption delegateOption = antOption.getActualAntOption(); - if (delegateOption.isAttribute()) { - return ""; - } else { - return format(getMethodFormat(antOption), getData(antOption)); - } - } - } } diff --git a/apache-rat-tasks/src/test/java/org/apache/rat/anttasks/HelpTest.java b/apache-rat-tasks/src/test/java/org/apache/rat/anttasks/HelpTest.java index 5b6a6903f..7006f32ea 100644 --- a/apache-rat-tasks/src/test/java/org/apache/rat/anttasks/HelpTest.java +++ b/apache-rat-tasks/src/test/java/org/apache/rat/anttasks/HelpTest.java @@ -46,9 +46,9 @@ protected File getAntFile() { @Test public void testExecHelp() { buildRule.executeTarget("execHelp"); - System.out.println(buildRule.getOutput()); + // System.out.println(buildRule.getOutput()); assertThat(buildRule.getOutput()).contains(" "); - assertThat(buildRule.getOutput()).contains("File"); - assertThat(buildRule.getOutput()).contains("Deprecated for removal since 0.17: Use outputFamilies attribute instead."); + assertThat(buildRule.getOutput()).contains("File"); + assertThat(buildRule.getOutput()).contains("Deprecated for removal since 0.17: Use outputFamilies attribute"); } } diff --git a/apache-rat-tasks/src/test/java/org/apache/rat/anttasks/ReportOptionTest.java b/apache-rat-tasks/src/test/java/org/apache/rat/anttasks/ReportOptionTest.java index 9f66bc74d..08a2c407a 100644 --- a/apache-rat-tasks/src/test/java/org/apache/rat/anttasks/ReportOptionTest.java +++ b/apache-rat-tasks/src/test/java/org/apache/rat/anttasks/ReportOptionTest.java @@ -27,6 +27,7 @@ import org.apache.commons.lang3.SystemUtils; import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.Pair; +import org.apache.rat.documentation.options.AntOptionCollection; import org.apache.rat.test.AbstractConfigurationOptionsProvider; import org.apache.rat.OptionCollectionTest; import org.apache.rat.ReportConfiguration; @@ -119,11 +120,6 @@ protected ReportConfiguration generateConfig(final List> return reportConfiguration; } - @Override - protected void helpTest() { - fail("Should not be called"); - } - @Override public void helpLicenses() { TestingLog testLog = new TestingLog(); @@ -146,7 +142,7 @@ private class BuildTask extends AbstractRatAntTaskTest { final String name; BuildTask(Option option) { - this(new AntOption(option).getName()); + this(AntOptionCollection.INSTANCE.getMappedOption(option).get().getName()); } BuildTask() { @@ -162,7 +158,7 @@ public final void setUp(List> args) { Map attributes = new HashMap<>(); if (args.get(0).getKey() != null) { for (Pair pair : args) { - AntOption argOption = new AntOption(pair.getKey()); + AntOption argOption = AntOptionCollection.INSTANCE.getMappedOption(pair.getKey()).get(); if (argOption.isAttribute()) { String value = pair.getValue() == null ? "true" : pair.getValue()[0]; attributes.put(argOption.getName(), value); diff --git a/apache-rat-tools/pom.xml b/apache-rat-tools/pom.xml index 728694d89..132c5674c 100644 --- a/apache-rat-tools/pom.xml +++ b/apache-rat-tools/pom.xml @@ -90,6 +90,10 @@ + + org.apache.commons + commons-text + org.apache.rat apache-rat-core diff --git a/apache-rat-tools/src/main/java/org/apache/rat/documentation/options/AbstractOption.java b/apache-rat-tools/src/main/java/org/apache/rat/documentation/options/AbstractOption.java deleted file mode 100644 index 84335831c..000000000 --- a/apache-rat-tools/src/main/java/org/apache/rat/documentation/options/AbstractOption.java +++ /dev/null @@ -1,233 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.rat.documentation.options; - -import java.util.HashMap; -import java.util.Locale; -import java.util.Map; -import java.util.Optional; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -import org.apache.commons.cli.Option; -import org.apache.commons.lang3.StringUtils; -import org.apache.rat.OptionCollection; -import org.apache.rat.commandline.Arg; - -import static java.lang.String.format; - -/** - * Abstract class that provides the framework for UI-specific RAT options. - * In this context UI option means an option expressed in the specific UI, such as: - * @see AntOption - * @see MavenOption - * @see CLIOption - */ -public abstract class AbstractOption { - /** The pattern to match CLI options in text */ - protected static final Pattern PATTERN = Pattern.compile("-(-[a-z0-9]+)+"); - /** The actual UI-specific name for the option */ - protected final Option option; - /** The name for the option */ - protected final String name; - /** The argument type for this option */ - protected final OptionCollection.ArgumentType argumentType; - - /** - * Constructor. - * - * @param option The CLI option - * @param name the UI-specific name for the option. - */ - AbstractOption(final Option option, final String name) { - this.option = option; - this.name = name; - argumentType = option.hasArg() ? - option.getArgName() == null ? OptionCollection.ArgumentType.ARG : - OptionCollection.ArgumentType.valueOf(option.getArgName().toUpperCase(Locale.ROOT)) : - OptionCollection.ArgumentType.NONE; - } - - /** - * Gets the option this abstract option is wrapping. - * @return the original Option. - */ - public Option getOption() { - return option; - } - - /** - * Return default value. - * @return default value or {@code null} if no argument given. - */ - public String getDefaultValue() { - Arg arg = Arg.findArg(option); - return arg == null ? null : arg.defaultValue(); - } - - /** - * Provide means to wrap the given option depending on the UI-specific option implementation. - * @param option The CLI option - * @return the cleaned up option name. - */ - protected abstract String cleanupName(Option option); - - /** - * Gets an example of how to use this option in the native UI. - * @return An example of how to use this option in the native UI. - */ - public abstract String getExample(); - - /** - * Gets this option's cleaned up name. - * @return This option's cleaned up name. - */ - public String cleanupName() { - return cleanupName(option); - } - - /** - * Replaces CLI pattern options with implementation specific pattern options. - * @param str the string to clean. - * @return the string with CLI names replaced with implementation specific names. - */ - public String cleanup(final String str) { - String workingStr = str; - if (StringUtils.isNotBlank(workingStr)) { - Map maps = new HashMap<>(); - Matcher matcher = PATTERN.matcher(workingStr); - while (matcher.find()) { - String key = matcher.group(); - String optKey = key.substring(2); - Optional