ottlinger commented on code in PR #257: URL: https://github.com/apache/creadur-rat/pull/257#discussion_r1614889147
########## apache-rat-tasks/src/test/java/org/apache/rat/anttasks/ReportOptionTest.java: ########## @@ -0,0 +1,431 @@ +/* + * 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.anttasks; + +import org.apache.commons.cli.Option; +import org.apache.commons.io.filefilter.FalseFileFilter; +import org.apache.commons.io.filefilter.IOFileFilter; +import org.apache.commons.io.filefilter.NotFileFilter; +import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.commons.lang3.tuple.Pair; +import org.apache.commons.text.WordUtils; +import org.apache.rat.IOptionsProvider; +import org.apache.rat.OptionCollection; +import org.apache.rat.OptionCollectionTest; +import org.apache.rat.ReportConfiguration; +import org.apache.rat.ReportTest; +import org.apache.rat.anttasks.BaseAntTask.AntOption; +import org.apache.rat.license.ILicense; +import org.apache.rat.license.LicenseSetFactory; +import org.apache.rat.tools.AntGenerator; +import org.apache.rat.utils.CasedString; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.extension.ExtensionContext; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.ArgumentsProvider; +import org.junit.jupiter.params.provider.ArgumentsSource; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.io.OutputStream; +import java.net.URL; +import java.nio.file.Files; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; +import java.util.SortedSet; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +import static java.lang.String.format; +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.fail; + +/** + * Tests to ensure the option setting works correctly. + */ +public class ReportOptionTest { + + static File baseDir = new File("target/optionTest"); + static ReportConfiguration reportConfiguration; + + + @BeforeAll + public static void makeDirs() { + baseDir.mkdirs(); + } + + @ParameterizedTest + @ArgumentsSource(OptionsProvider.class) + public void testOptionsUpdateConfig(String name, OptionCollectionTest.OptionTest test) throws Exception { + test.test(); + } + + public static class OptionTest extends Report { + + public OptionTest() {} + + @Override + public void execute() { + reportConfiguration = getConfiguration(); + } + } + + static class OptionsProvider implements ArgumentsProvider, IOptionsProvider { + + final AtomicBoolean helpCalled = new AtomicBoolean(false); + + final Map<Option, OptionCollectionTest.OptionTest> testMap = new HashMap<>(); + + File baseDir; + + public OptionsProvider() { + baseDir = new File("target/optionTools"); + baseDir.mkdirs(); + testMap.put(OptionCollection.ADD_LICENSE, this::addLicenseTest); + testMap.put(OptionCollection.ARCHIVE, this::archiveTest); + testMap.put(OptionCollection.STANDARD, this::standardTest); + testMap.put(OptionCollection.COPYRIGHT, this::copyrightTest); + testMap.put(OptionCollection.DRY_RUN, this::dryRunTest); + testMap.put(OptionCollection.EXCLUDE_CLI, this::excludeCliTest); + testMap.put(OptionCollection.EXCLUDE_FILE_CLI,this::excludeCliFileTest); + testMap.put(OptionCollection.FORCE, this::forceTest); + testMap.put(OptionCollection.LICENSES, this::licensesTest); + testMap.put(OptionCollection.LIST_LICENSES, this::listLicensesTest); + testMap.put(OptionCollection.LIST_FAMILIES, this::listFamiliesTest); + testMap.put(OptionCollection.NO_DEFAULTS, this::noDefaultsTest); + testMap.put(OptionCollection.OUT, this::outTest); + testMap.put(OptionCollection.SCAN_HIDDEN_DIRECTORIES, this::scanHiddenDirectoriesTest); + testMap.put(OptionCollection.STYLESHEET_CLI, this::styleSheetTest); + testMap.put(OptionCollection.XML, this::xmlTest); + } + + private ReportConfiguration generateConfig(Option option, String... args) { + List<Pair<String,String>> lst = new ArrayList<>(); + for (int i=0;i<args.length;i+=2) { + lst.add(ImmutablePair.of(args[i], args[i+1])); + } + BuildTask task = new BuildTask(option, lst); + task.setUp(); + task.buildRule.executeTarget(args[0]); + return reportConfiguration; + } + + private String antName(Option option) { + CasedString name = new CasedString(CasedString.StringCase.KEBAB, option.getLongOpt()); + return WordUtils.uncapitalize(name.toCase(CasedString.StringCase.CAMEL)); + } + + @Override + public void addLicenseTest() { + String name = antName(OptionCollection.ADD_LICENSE); + ReportConfiguration config = generateConfig(OptionCollection.ADD_LICENSE, name,"true"); + assertTrue(config.isAddingLicenses()); + config = generateConfig(OptionCollection.ADD_LICENSE,name, "false"); + assertFalse(config.isAddingLicenses()); + } + + @Override + public void archiveTest() { + String name = antName(OptionCollection.ARCHIVE); + for (ReportConfiguration.Processing proc : ReportConfiguration.Processing.values()) { + ReportConfiguration config = generateConfig(OptionCollection.ARCHIVE, name, proc.name()); + assertEquals(proc, config.getArchiveProcessing()); + } + } + + @Override + public void standardTest() { + String name = antName(OptionCollection.STANDARD); + for (ReportConfiguration.Processing proc : ReportConfiguration.Processing.values()) { + ReportConfiguration config = generateConfig(OptionCollection.STANDARD, name, proc.name()); + assertEquals(proc, config.getStandardProcessing()); + } + } + + @Override + public void copyrightTest() { + String name = antName(OptionCollection.COPYRIGHT); + ReportConfiguration config = generateConfig(OptionCollection.COPYRIGHT, name, "MyCopyright" ); + assertNull(config.getCopyrightMessage(), "Copyright without ADD_LICENCE should not work"); + config = generateConfig(OptionCollection.COPYRIGHT, name, "MyCopyright", antName(OptionCollection.ADD_LICENSE), "true" ); + assertEquals("MyCopyright", config.getCopyrightMessage()); + } + + @Override + public void dryRunTest() { + String name = antName(OptionCollection.DRY_RUN); + ReportConfiguration config = generateConfig(OptionCollection.DRY_RUN, name, "true" ); + assertTrue(config.isDryRun()); + config = generateConfig(OptionCollection.DRY_RUN,name, "false" ); + assertFalse(config.isDryRun()); + } + + @Override + public void excludeCliTest() { + String name = antName(OptionCollection.EXCLUDE_CLI); + ReportConfiguration config = generateConfig(OptionCollection.EXCLUDE_CLI, name, "*.foo", name, "[A-Z]\\.bar", name, "justbaz"); + execCliTest(config); + } + + private void execCliTest(ReportConfiguration config) { + IOFileFilter filter = config.getFilesToIgnore(); + assertThat(filter).isExactlyInstanceOf(NotFileFilter.class); + assertFalse(filter.accept(baseDir, "some.foo" ), "some.foo"); + assertFalse(filter.accept(baseDir, "B.bar"), "B.bar"); + assertFalse(filter.accept(baseDir, "justbaz" ), "justbaz"); + assertTrue(filter.accept(baseDir, "notbaz"), "notbaz"); + } + + @Override + public void excludeCliFileTest() { + File outputFile = new File(baseDir, "exclude.txt"); + try (FileWriter fw = new FileWriter(outputFile)) { + fw.write("*.foo"); + fw.write(System.lineSeparator()); + fw.write("[A-Z]\\.bar"); + fw.write(System.lineSeparator()); + fw.write("justbaz"); + fw.write(System.lineSeparator()); + } catch (IOException e) { + throw new RuntimeException(e); + } + String name = antName(OptionCollection.EXCLUDE_FILE_CLI); + ReportConfiguration config = generateConfig(OptionCollection.EXCLUDE_FILE_CLI, name, outputFile.getPath()); + execCliTest(config); + } + + @Override + public void forceTest() { + String name = antName(OptionCollection.FORCE); + ReportConfiguration config = generateConfig(OptionCollection.FORCE, name, "true"); + assertFalse(config.isAddingLicensesForced()); + config = generateConfig(OptionCollection.FORCE, name, "true", antName(OptionCollection.ADD_LICENSE), "true"); + assertTrue(config.isAddingLicensesForced()); + } + + + @Override + public void licensesTest() { + String name = antName(OptionCollection.LICENSES); + ReportConfiguration config = generateConfig(OptionCollection.LICENSES, name, "src/test/resources/OptionTools/One.xml", name, "src/test/resources/OptionTools/Two.xml"); + + SortedSet<ILicense> set = config.getLicenses(LicenseSetFactory.LicenseFilter.ALL); + assertTrue(set.size() > 2); + assertTrue(LicenseSetFactory.search("ONE", "ONE", set).isPresent()); + assertTrue(LicenseSetFactory.search("TWO", "TWO", set).isPresent()); + + config = generateConfig(OptionCollection.LICENSES, name, "src/test/resources/OptionTools/One.xml", name, "src/test/resources/OptionTools/Two.xml", antName(OptionCollection.NO_DEFAULTS), "true"); + + set = config.getLicenses(LicenseSetFactory.LicenseFilter.ALL); + assertEquals(2, set.size()); + assertTrue(LicenseSetFactory.search("ONE", "ONE", set).isPresent()); + assertTrue(LicenseSetFactory.search("TWO", "TWO", set).isPresent()); + + } + + @Override + public void listLicensesTest() { + String name = antName(OptionCollection.LIST_LICENSES); + for (LicenseSetFactory.LicenseFilter filter : LicenseSetFactory.LicenseFilter.values()) { + ReportConfiguration config = generateConfig(OptionCollection.LIST_LICENSES, name, filter.name()); + assertEquals(filter, config.listLicenses()); + } + } + + @Override + public void listFamiliesTest() { + String name = antName(OptionCollection.LIST_FAMILIES); + for (LicenseSetFactory.LicenseFilter filter : LicenseSetFactory.LicenseFilter.values()) { + ReportConfiguration config = generateConfig(OptionCollection.LIST_FAMILIES, name, filter.name()); + assertEquals(filter, config.listFamilies()); + } + } + + @Override + public void noDefaultsTest() { + String name = antName(OptionCollection.NO_DEFAULTS); + ReportConfiguration config = generateConfig(OptionCollection.NO_DEFAULTS,name, "true"); + assertTrue(config.getLicenses(LicenseSetFactory.LicenseFilter.ALL).isEmpty()); + config = generateConfig(OptionCollection.NO_DEFAULTS,name, "false"); + assertFalse(config.getLicenses(LicenseSetFactory.LicenseFilter.ALL).isEmpty()); + } + + @Override + public void outTest() { + File outFile = new File( baseDir, "outexample"); + String name = antName(OptionCollection.OUT); + ReportConfiguration config = generateConfig(OptionCollection.OUT, name, outFile.getAbsolutePath() ); + try (OutputStream os = config.getOutput().get()) { + os.write("Hello world".getBytes()); + } catch (IOException e) { + throw new RuntimeException(e); + } + try (BufferedReader reader = new BufferedReader( new InputStreamReader(Files.newInputStream(outFile.toPath())))) { + assertEquals("Hello world",reader.readLine()); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + @Override + public void scanHiddenDirectoriesTest() { + String name = antName(OptionCollection.SCAN_HIDDEN_DIRECTORIES); + ReportConfiguration config = generateConfig(OptionCollection.SCAN_HIDDEN_DIRECTORIES, name, "true"); + assertThat(config.getDirectoriesToIgnore()).isExactlyInstanceOf(FalseFileFilter.class); + } + + @Override + public void styleSheetTest() { + String name = antName(OptionCollection.STYLESHEET_CLI); + URL url = ReportTest.class.getResource("MatcherContainerResource.txt"); + if (url == null) { + fail("Could not locate 'MatcherContainerResource.txt'"); + } + for (String sheet : new String[]{"target/optionTools/stylesheet.xlt", "plain-rat", "missing-headers", "unapproved-licenses", url.getFile()}) { Review Comment: is this a typo or does the file just have in invalid name? stylesheet.xstl instead of xlt? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: dev-unsubscr...@creadur.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org