This is an automated email from the ASF dual-hosted git repository. Claudenw pushed a commit to branch RAT-541_create-testhelper_data-package in repository https://gitbox.apache.org/repos/asf/creadur-rat.git
commit f77c0a53f555b4cbb2c2f8b97d2d7e9e9aebc616 Author: Claude Warren <[email protected]> AuthorDate: Sun May 10 10:23:40 2026 +0100 initial changes --- .../java/org/apache/rat/utils}/FileUtils.java | 23 +- .../org/apache/rat/ReporterOptionsProvider.java | 2 +- .../java/org/apache/rat/ReporterOptionsTest.java | 2 +- .../apache/rat/test/AbstractOptionsProvider.java | 2 +- .../testhelpers/data/AbstractTestDataProvider.java | 200 +++++++++++++++++ .../org/apache/rat/testhelpers/data/TestData.java | 242 +++++++++++++++++++++ .../apache/rat/testhelpers/data/ValidatorData.java | 113 ++++++++++ 7 files changed, 571 insertions(+), 13 deletions(-) diff --git a/apache-rat-core/src/test/java/org/apache/rat/testhelpers/FileUtils.java b/apache-rat-core/src/main/java/org/apache/rat/utils/FileUtils.java similarity index 81% rename from apache-rat-core/src/test/java/org/apache/rat/testhelpers/FileUtils.java rename to apache-rat-core/src/main/java/org/apache/rat/utils/FileUtils.java index 5681e797..50da3ecc 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/testhelpers/FileUtils.java +++ b/apache-rat-core/src/main/java/org/apache/rat/utils/FileUtils.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * * under the License. * */ -package org.apache.rat.testhelpers; +package org.apache.rat.utils; import java.io.File; import java.io.FileWriter; @@ -25,15 +25,17 @@ import java.io.PrintWriter; import java.util.Arrays; import java.util.Collections; -import static org.assertj.core.api.Fail.fail; -public class FileUtils { +public final class FileUtils { + private FileUtils() { + // do not instantiate + } /** * Creates a directory if it does not exist. * @param dir the directory to make. */ - public static void mkDir(File dir) { + public static void mkDir(final File dir) { boolean ignored = dir.mkdirs(); } @@ -41,7 +43,7 @@ public class FileUtils { * Deletes a file if it exists. * @param file the file to delete. */ - public static void delete(File file) { + public static void delete(final File file) { if (file.exists()) { if (file.isDirectory()) { try { @@ -62,15 +64,16 @@ public class FileUtils { * @param lines the lines to write into the file. * @return the new File. */ - static public File writeFile(File dir, final String name, final Iterable<String> lines) { + public static File writeFile(final File dir, final String name, final Iterable<String> lines) { if (dir == null) { - fail("base directory not specified"); + throw new IllegalArgumentException("base directory not specified"); } + mkDir(dir); File file = new File(dir, name); try (PrintWriter writer = new PrintWriter(new FileWriter(file))) { lines.forEach(writer::println); } catch (IOException e) { - fail(e.getMessage()); + throw new RuntimeException(e.getMessage(), e); } return file; } @@ -82,7 +85,7 @@ public class FileUtils { * @param lines the lines to write into the file. * @return the new File. */ - static public File writeFile(File dir, final String name, final String... lines) { + public static File writeFile(final File dir, final String name, final String... lines) { return writeFile(dir, name, Arrays.asList(lines)); } @@ -92,7 +95,7 @@ public class FileUtils { * @param name the name of the file. * @return the new file. */ - public static File writeFile(File dir, String name) { + public static File writeFile(final File dir, final String name) { return writeFile(dir, name, Collections.singletonList(name)); } } diff --git a/apache-rat-core/src/test/java/org/apache/rat/ReporterOptionsProvider.java b/apache-rat-core/src/test/java/org/apache/rat/ReporterOptionsProvider.java index a92cb87d..724b4919 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/ReporterOptionsProvider.java +++ b/apache-rat-core/src/test/java/org/apache/rat/ReporterOptionsProvider.java @@ -56,7 +56,7 @@ import org.apache.rat.report.claim.ClaimStatistic; import org.apache.rat.test.AbstractOptionsProvider; import org.apache.rat.test.utils.OptionFormatter; import org.apache.rat.test.utils.Resources; -import org.apache.rat.testhelpers.FileUtils; +import org.apache.rat.utils.FileUtils; import org.apache.rat.testhelpers.TestingLog; import org.apache.rat.testhelpers.TextUtils; import org.apache.rat.testhelpers.XmlUtils; diff --git a/apache-rat-core/src/test/java/org/apache/rat/ReporterOptionsTest.java b/apache-rat-core/src/test/java/org/apache/rat/ReporterOptionsTest.java index ccb82f3c..2694b4c9 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/ReporterOptionsTest.java +++ b/apache-rat-core/src/test/java/org/apache/rat/ReporterOptionsTest.java @@ -27,7 +27,7 @@ import javax.xml.xpath.XPathFactory; import org.apache.rat.api.RatException; import org.apache.rat.report.claim.ClaimStatistic; import org.apache.rat.test.AbstractConfigurationOptionsProvider; -import org.apache.rat.testhelpers.FileUtils; +import org.apache.rat.utils.FileUtils; import org.apache.rat.testhelpers.XmlUtils; import org.apache.rat.utils.DefaultLog; import org.apache.rat.utils.Log; diff --git a/apache-rat-core/src/test/java/org/apache/rat/test/AbstractOptionsProvider.java b/apache-rat-core/src/test/java/org/apache/rat/test/AbstractOptionsProvider.java index f4a18809..1f55d2ba 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/test/AbstractOptionsProvider.java +++ b/apache-rat-core/src/test/java/org/apache/rat/test/AbstractOptionsProvider.java @@ -170,7 +170,7 @@ public abstract class AbstractOptionsProvider implements ArgumentsProvider { } protected File writeFile(final String name, final Iterable<String> lines) { - return org.apache.rat.testhelpers.FileUtils.writeFile(baseDir, name, lines); + return org.apache.rat.utils.FileUtils.writeFile(baseDir, name, lines); } final protected DocumentName mkDocName(final String name) { diff --git a/apache-rat-core/src/test/java/org/apache/rat/testhelpers/data/AbstractTestDataProvider.java b/apache-rat-core/src/test/java/org/apache/rat/testhelpers/data/AbstractTestDataProvider.java new file mode 100644 index 00000000..94b23a35 --- /dev/null +++ b/apache-rat-core/src/test/java/org/apache/rat/testhelpers/data/AbstractTestDataProvider.java @@ -0,0 +1,200 @@ +/* + * 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.testhelpers.data; + + +import com.google.common.collect.ImmutableList; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; +import java.util.stream.Stream; +import org.apache.commons.cli.Option; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.rat.OptionCollectionParser; +import org.apache.rat.commandline.Arg; +import org.apache.rat.ui.AbstractOptionCollection; +import org.apache.rat.ui.ArgumentTracker; +import org.apache.rat.utils.DefaultLog; + +/** + * Generates a list of TestData for executing the Report. + * Use of this interface ensures consistent testing across the UIs. Each method + * tests an Option from {@link OptionCollectionParser} that must be implemented in the UI. + * This differes from the {@link OptionTestDataProvider} in that tests from this set + * expect that execptions will be thrown during execution, and tests the xml output. + */ +public abstract class AbstractTestDataProvider { + + /** The list of exclude args */ + static final String[] EXCLUDE_ARGS = {"*.foo", "%regex[[A-Z]\\.bar]", "justbaz"}; + /** the list of include args */ + static final String[] INCLUDE_ARGS = {"B.bar", "justbaz"}; + public final ImmutableList<ImmutablePair<Option, String[]>> NO_OPTIONS = ImmutableList.of(ImmutablePair.nullPair()); + + /** + * Generates a map of TestData indexed by the testName + * @param optionCollection the collection of options for the UI under test. + * @return the map of testName to Test Data. + */ + public final Map<String, TestData> getOptionTestMap(final AbstractOptionCollection<?> optionCollection) { + Map<String, TestData> map = new TreeMap<>(); + for (TestData test : getOptionTests(optionCollection)) { + map.put(test.getTestName(), test); + } + return map; + } + + /** + * Generates a list of test data for Option testing. + * This is different from UI testing as this is to test + * that the command line is properly parsed into a configuration. + * @param optionCollection the collection of options for the UI under test. + * @return a set of TestData for the tests. + */ + public final Set<TestData> getOptionTests(final AbstractOptionCollection<?> optionCollection) { + // the optionCollection establishes any changes to the Arg values. + List<Option> unsupportedOptions = new ArrayList<>(optionCollection.getUnsupportedOptions().getOptions()); + Set<TestData> result = new TreeSet<>(); + for (Arg arg : Arg.values()) { + if (!arg.isEmpty()) { + Stream<Option> options = arg.group().getOptions().stream().filter(opt -> !unsupportedOptions.contains(opt)); + switch (arg) { + case CONFIGURATION -> options.forEach(opt -> configTest(result, opt)); + case CONFIGURATION_NO_DEFAULTS -> options.forEach(opt -> configurationNoDefaultsTest(result, opt)); + case COUNTER_MIN -> options.forEach(opt -> counterMinTest(result, opt)); + case COUNTER_MAX -> options.forEach(opt -> counterMaxTest(result, opt)); + case DRY_RUN -> options.forEach(opt -> dryRunTest(result, opt)); + case EDIT_COPYRIGHT -> options.forEach(opt -> editCopyrightTest(result, opt)); + case EDIT_ADD -> options.forEach(opt -> editLicenseTest(result, opt)); + case EDIT_OVERWRITE -> options.forEach(opt -> editOverwriteTest(result, opt)); + case HELP_LICENSES -> options.forEach(opt -> helpLicenses(result, opt)); + case EXCLUDE -> options.forEach(opt -> inputExcludeTest(result, opt)); + case EXCLUDE_FILE -> options.forEach(opt -> inputExcludeFileTest(result, opt)); + case EXCLUDE_PARSE_SCM -> options.forEach(opt -> inputExcludeParsedScmTest(result, opt)); + case EXCLUDE_STD -> options.forEach(opt -> inputExcludeStdTest(result, opt)); + case EXCLUDE_SIZE -> options.forEach(opt -> inputExcludeSizeTest(result, opt)); + case INCLUDE -> options.forEach(opt -> inputIncludeTest(result, opt)); + case INCLUDE_FILE -> options.forEach(opt -> inputIncludeFileTest(result, opt)); + case INCLUDE_STD -> options.forEach(opt -> inputIncludeStdTest(result, opt)); + case SOURCE -> options.forEach(opt -> inputSourceTest(result, opt)); + case FAMILIES_APPROVED -> options.forEach(opt -> licenseFamiliesApprovedTest(result, opt)); + case FAMILIES_APPROVED_FILE -> options.forEach(opt -> licenseFamiliesApprovedFileTest(result, opt)); + case FAMILIES_DENIED -> options.forEach(opt -> licenseFamiliesDeniedTest(result, opt)); + case FAMILIES_DENIED_FILE -> options.forEach(opt -> licenseFamiliesDeniedFileTest(result, opt)); + case LICENSES_APPROVED -> options.forEach(opt -> licensesApprovedTest(result, opt)); + case LICENSES_APPROVED_FILE -> options.forEach(opt -> licensesApprovedFileTest(result, opt)); + case LICENSES_DENIED -> options.forEach(opt -> licensesDeniedTest(result, opt)); + case LICENSES_DENIED_FILE -> options.forEach(opt -> licensesDeniedFileTest(result, opt)); + case LOG_LEVEL -> options.forEach(opt -> logLevelTest(result, opt)); + case OUTPUT_ARCHIVE -> options.forEach(opt -> outputArchiveTest(result, opt)); + case OUTPUT_FAMILIES -> options.forEach(opt -> outputFamiliesTest(result, opt)); + case OUTPUT_FILE -> options.forEach(opt -> outputFileTest(result, opt)); + case OUTPUT_LICENSES -> options.forEach(opt -> outputLicensesTest(result, opt)); + case OUTPUT_STANDARD -> options.forEach(opt -> outputStandardTest(result, opt)); + case OUTPUT_STYLE -> options.forEach(opt -> outputStyleTest(result, opt)); + } + } + } + validate(result); + return result; + } + + private void validate(Set<TestData> result) { + Set<Option> options = new HashSet<>(Arg.getOptions().getOptions()); + result.forEach(testData -> options.remove(testData.getOption())); + // TODO fix this once deprecated options are removed + options.forEach(opt -> DefaultLog.getInstance().warn("Option " + ArgumentTracker.extractKey(opt) + " was not tested.")); + //assertThat(options).describedAs("All options are not accounted for.").isEmpty(); + } + + protected abstract void inputExcludeFileTest(final Set<TestData> result, final Option option); + + protected abstract void inputExcludeTest(final Set<TestData> result, final Option option); + + protected abstract void inputExcludeStdTest(final Set<TestData> result, final Option option); + + protected abstract void inputExcludeParsedScmTest(final Set<TestData> result, final Option option); + + protected abstract void inputExcludeSizeTest(final Set<TestData> result, final Option option); + + protected abstract void inputIncludeFileTest(final Set<TestData> result, final Option option); + + protected abstract void inputIncludeTest(final Set<TestData> result, final Option option); + + protected abstract void inputIncludeStdTest(final Set<TestData> result, final Option option); + + protected abstract void inputSourceTest(final Set<TestData> result, final Option option); + + protected abstract void helpLicenses(final Set<TestData> result, final Option option); + + protected abstract void licensesApprovedFileTest(final Set<TestData> result, final Option option); + + protected abstract void licensesApprovedTest(final Set<TestData> result, final Option option); + + protected abstract void licensesDeniedTest(final Set<TestData> result, final Option option); + + protected abstract void licensesDeniedFileTest(final Set<TestData> result, final Option option); + + protected abstract void licenseFamiliesApprovedFileTest(final Set<TestData> result, final Option option); + + protected abstract void licenseFamiliesApprovedTest(final Set<TestData> result, final Option option); + + protected abstract void licenseFamiliesDeniedFileTest(final Set<TestData> result, final Option option); + + protected abstract void licenseFamiliesDeniedTest(final Set<TestData> result, final Option option); + + protected abstract void counterMaxTest(final Set<TestData> result, final Option option); + + protected abstract void counterMinTest(final Set<TestData> result, final Option option); + + /** + * Add results to the result list. + * @param result the result list. + * @param option configuration option we are testing. + */ + protected abstract void configTest(final Set<TestData> result, final Option option); + + protected abstract void configurationNoDefaultsTest(final Set<TestData> result, final Option option); + + protected abstract void dryRunTest(final Set<TestData> result, final Option option); + + protected abstract void editCopyrightTest(final Set<TestData> result, final Option option); + + protected abstract void editLicenseTest(final Set<TestData> result, final Option option); + + protected abstract void editOverwriteTest(final Set<TestData> result, final Option option); + + protected abstract void logLevelTest(final Set<TestData> result, final Option option); + + protected abstract void outputArchiveTest(final Set<TestData> result, final Option option); + + protected abstract void outputFamiliesTest(final Set<TestData> result, final Option option); + + protected abstract void outputFileTest(final Set<TestData> result, final Option option); + + protected abstract void outputLicensesTest(final Set<TestData> result, final Option option); + + protected abstract void outputStandardTest(final Set<TestData> result, final Option option); + + protected abstract void outputStyleTest(final Set<TestData> result, final Option option); +} diff --git a/apache-rat-core/src/test/java/org/apache/rat/testhelpers/data/TestData.java b/apache-rat-core/src/test/java/org/apache/rat/testhelpers/data/TestData.java new file mode 100644 index 00000000..c1fcefdb --- /dev/null +++ b/apache-rat-core/src/test/java/org/apache/rat/testhelpers/data/TestData.java @@ -0,0 +1,242 @@ +/* + * 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.testhelpers.data; + +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.function.Consumer; +import javax.xml.xpath.XPathExpressionException; +import org.apache.commons.cli.Option; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.commons.lang3.tuple.Pair; +import org.apache.commons.text.WordUtils; +import org.apache.rat.report.claim.ClaimStatistic; +import org.apache.rat.testhelpers.XmlUtils; +import org.apache.rat.ui.ArgumentTracker; +import org.apache.rat.utils.CasedString; +import org.apache.rat.utils.FileUtils; +import org.w3c.dom.Document; + +import static org.assertj.core.api.Assertions.assertThat; + +/** + * The definition of a test. + */ +public final class TestData implements Comparable<TestData> { + /** if set, the expected exception from the test. */ + private Exception expectedException; + /** The sub name of the test */ + private final String name; + /** The command line for the test */ + private final List<ImmutablePair<Option, String[]>> commandLine; + /** A function to setup the test in a specific path */ + private final Consumer<Path> setupFiles; + /** A function to test the results of the test. */ + private final Consumer<ValidatorData> validator; + + /** + * Constructs the Test data + * @param name the sub name of the test. May not be {@code null} but may be an empty string. Should + * be specified in Camel case for multiple words. + * @param commandLine The command line for the test. May not be {@code null} but may consist of a single {@link ImmutablePair#nullPair()}. + * @param setupFiles the method to set up the files for the test. May not be {@code null}. + * @param validator the validator for the results of the test. May not be {@code null}. + */ + public TestData(String name, List<ImmutablePair<Option, String[]>> commandLine, + Consumer<Path> setupFiles, + Consumer<ValidatorData> validator) { + Objects.requireNonNull(name, " name cannot be null"); + Objects.requireNonNull(commandLine, "commandLine cannot be null"); + Objects.requireNonNull(setupFiles, "setupFiles cannot be null"); + Objects.requireNonNull(validator, "validator cannot be null"); + if (name.contains("/")) { + throw new IllegalArgumentException("name may not contain '/', use camel case instead"); + } + if (commandLine.isEmpty()) { + throw new IllegalArgumentException("commandLine may not be empty but contain an ImmutablePair.nullPair()"); + } + this.name = name; + this.commandLine = commandLine; + this.setupFiles = setupFiles; + this.validator = validator; + this.expectedException = null; + } + + @Override + public String toString() { + return getTestName(); + } + + /** + * Sets the exception expected from this test. + * @param expectedException the expected exception. + */ + void setException(Exception expectedException) { + this.expectedException = expectedException; + } + + /** + * The option for the test. This is the first option specified in the command line. + * If the command line is empty this returns {@code null}. + * @return the first option in the command line or {@code null} if there is no option. + */ + public Option getOption() { + return commandLine.get(0).getLeft(); + } + + /** + * Get the sub name for this test. + * @return the sub name for this test. + */ + public String getName() { + return name; + } + + /** + * Gets the expected excepton or {@code null} if not exception is expected. + * @return the expected excepton or {@code null} if not exception is expected. + */ + public Exception getExpectedException() { + return expectedException; + } + + /** + * Determines if the test is expecting an exception. + * @return {@code true} if the test is expecting to throw an exception. + */ + public boolean expectingException() { + return expectedException != null; + } + + /** + * Gets the command line as the string objects that are normally parsed by the + * command line parser. + * @return the command line strings. + */ + public String[] getCommandLine() { + return getCommandLine(null); + } + + /** + * Gets the command line as the string objects that are normally parsed by the + * command line parser. The result will include "--" to terminate a trailing multi + * argument option. + * @param workingDir the directory to add to the command line. May be {@code null}. + * @return the command line strings. + */ + public String[] getCommandLine(String workingDir) { + List<String> args = new ArrayList<>(commandLine.size()); + final boolean[] lastWasMultiArg = {false}; + commandLine.forEach(pair -> { + if (pair.getKey() != null) { + if (pair.getKey().hasLongOpt()) { + args.add("--" + pair.getKey().getLongOpt()); + } else { + args.add("-" + pair.getKey().getOpt()); + } + if (pair.getValue() != null) { + args.addAll(Arrays.asList(pair.getValue())); + } + lastWasMultiArg[0] = pair.getKey().hasArgs(); + } else { + lastWasMultiArg[0] = false; + } + }); + if (lastWasMultiArg[0]) { + args.add("--"); + } + if (workingDir != null) { + args.add(workingDir); + } + return args.toArray(new String[0]); + } + + /** + * Get the arguments for the command line. + * @return the argumentsfor the command line as Option, String[] pairs. + */ + public List<? extends Pair<Option, String[]>> getArgs() { + return commandLine; + } + + /** + * Gets the validator for the test. + * @return the validator for the test. + */ + public Consumer<ValidatorData> getValidator() { + return validator; + } + + /** + * Sets up the files for the test. + * @param path the path to use as the base directory. Subdirectories and files may be added + * to this path. + */ + public void setupFiles(Path path) { + FileUtils.mkDir(path.toFile()); + setupFiles.accept(path); + } + + /** + * Gets the test name. This is the option concatenated with the name. + * @return the unique test name + */ + public String getTestName() { + new ArrayList<>(); + String result = null; + if (getOption() == null) { + result = name + "_DefaultTest"; + } else { + result = new CasedString(CasedString.StringCase.KEBAB, ArgumentTracker.extractKey(getOption())).toString(); + if (!name.isEmpty()) { + result += "/" + name; + } + } + return result; + } + + /** + * Gets the test name as a class name. This is based on the option concatenated with the name. + * @return the unique Java class name + */ + public String getClassName() { + if (getOption() == null) { + return WordUtils.capitalize(name) + "_DefaultTest"; + } else { + List<String> parts = new ArrayList<>(Arrays.asList(new CasedString(CasedString.StringCase.KEBAB, ArgumentTracker.extractKey(getOption())).getSegments())); + if (!name.isEmpty()) { + parts.addAll(Arrays.asList(new CasedString(CasedString.StringCase.CAMEL, name).getSegments())); + } + return CasedString.StringCase.PASCAL.assemble(parts.toArray(new String[0])); + } + } + + @Override + public int compareTo(TestData other) { + return getTestName().compareTo(other.getTestName()); + } + +} diff --git a/apache-rat-core/src/test/java/org/apache/rat/testhelpers/data/ValidatorData.java b/apache-rat-core/src/test/java/org/apache/rat/testhelpers/data/ValidatorData.java new file mode 100644 index 00000000..55185d66 --- /dev/null +++ b/apache-rat-core/src/test/java/org/apache/rat/testhelpers/data/ValidatorData.java @@ -0,0 +1,113 @@ +/* + * 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.testhelpers.data; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.nio.file.Path; +import java.nio.file.Paths; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import org.apache.rat.ConfigurationException; +import org.apache.rat.ReportConfiguration; +import org.apache.rat.Reporter; +import org.apache.rat.api.RatException; +import org.apache.rat.configuration.XMLConfig; +import org.apache.rat.configuration.XMLConfigurationWriter; +import org.apache.rat.document.DocumentName; +import org.apache.rat.report.claim.ClaimStatistic; +import org.apache.rat.report.xml.writer.XmlWriter; +import org.w3c.dom.Document; +import org.xml.sax.InputSource; +import org.xml.sax.SAXException; + +import static org.apache.rat.configuration.XMLConfigurationReader.nodeListConsumer; + +/** + * The validator for test data. + */ +public final class ValidatorData { + /** The report output */ + private final Reporter.Output output; + /** the base directory where the test setup was created */ + private final Path baseDir; + + /** + * Constructor. + * @param output The report output. + * @param baseDir the directory where the test setup was created. + */ + public ValidatorData(final Reporter.Output output, final String baseDir) { + this.output = output; + this.baseDir = Paths.get(baseDir); + } + + /** + * Gets the directory where the test setup was created as a DocumentName. + * @return the DocumentName for the baseDir directory. + */ + public DocumentName getBaseName() { + return DocumentName.builder(baseDir.toFile()).build(); + } + + /** + * Creates a DocumentName for a file name. The root of the document Name will be the baseDir. + * @param fileName the file name to create a document name for. + * @return the Document name for the file. + */ + public DocumentName mkDocName(String fileName) { + return DocumentName.builder().setBaseName(baseDir.toFile()).setName(fileName).build(); + } + + public Reporter.Output getOutput() { + return output; + } + /** + * Gets the document that was generated during execution. + * @return the document that was generated during execution. + */ + public Document getDocument() { + return output.getDocument(); + } + + /** + * Gets the claim statistics from the run + * @return the ClaimStatistic from the run. + */ + public ClaimStatistic getStatistic() { + return output.getStatistic(); + } + + /** + * Gets the configuration from the run. + * @return the configuration from the run. + */ + public ReportConfiguration getConfiguration() { + return output.getConfiguration(); + } + + public Path getBaseDir() { + return baseDir; + } + +}
