From a4cc18b6a729b4b659bb794c8289950f9e5330f0 Mon Sep 17 00:00:00 2001 From: Claude Warren Date: Tue, 10 Dec 2024 17:28:32 +0000 Subject: [PATCH 01/22] Fixes to correctly detect additional valid licenses --- .../main/java/org/apache/rat/Defaults.java | 2 +- .../org/apache/rat/ReportConfiguration.java | 11 ++++++-- .../java/org/apache/rat/api/MetaData.java | 19 ++++++-------- .../config/exclusion/plexus/MatchPattern.java | 2 +- .../apache/rat/license/LicenseSetFactory.java | 16 ++++++++---- .../org/apache/rat/policy/DefaultPolicy.java | 25 ++++++------------- .../rat/report/xml/XmlReportFactory.java | 2 +- .../apache/rat/policy/DefaultPolicyTest.java | 15 +++++------ .../org/apache/rat/mp/AbstractRatMojo.java | 7 +++--- 9 files changed, 50 insertions(+), 49 deletions(-) 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 bc7ef27d2..6d3cfdba7 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 @@ -135,7 +135,7 @@ private static LicenseSetFactory readConfigFiles(final Collection uris) { } LicenseSetFactory result = new LicenseSetFactory(licenses); - approvedLicenseCategories.forEach(result::addLicenseCategory); + approvedLicenseCategories.forEach(result::approveLicenseCategory); return result; } diff --git a/apache-rat-core/src/main/java/org/apache/rat/ReportConfiguration.java b/apache-rat-core/src/main/java/org/apache/rat/ReportConfiguration.java index a2056b74e..aead13eb5 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/ReportConfiguration.java +++ b/apache-rat-core/src/main/java/org/apache/rat/ReportConfiguration.java @@ -442,7 +442,6 @@ public void setFrom(final Defaults defaults) { if (getStyleSheet() == null) { setStyleSheet(StyleSheets.PLAIN.getStyleSheet()); } - defaults.getStandardExclusion().forEach(this::addExcludedCollection); } @@ -602,7 +601,7 @@ public void addApprovedLicenseCategory(final ILicenseFamily approvedILicenseFami * @param familyCategory the category to add. */ public void addApprovedLicenseCategory(final String familyCategory) { - licenseSetFactory.addLicenseCategory(familyCategory); + licenseSetFactory.approveLicenseCategory(familyCategory); } /** @@ -795,6 +794,14 @@ public ClaimValidator getClaimValidator() { return claimValidator; } + /** + * Gets the enclosed LicenseSetFactory. + * @return the license set factory. + */ + public LicenseSetFactory getLicenseSetFactory() { + return licenseSetFactory; + } + /** * Validates that the configuration is valid. * @param logger String consumer to log warning messages to. diff --git a/apache-rat-core/src/main/java/org/apache/rat/api/MetaData.java b/apache-rat-core/src/main/java/org/apache/rat/api/MetaData.java index 1499764ae..053fff189 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/api/MetaData.java +++ b/apache-rat-core/src/main/java/org/apache/rat/api/MetaData.java @@ -19,15 +19,12 @@ package org.apache.rat.api; import java.nio.charset.Charset; -import java.util.HashSet; -import java.util.Set; import java.util.SortedSet; import java.util.TreeSet; import java.util.function.Predicate; import java.util.stream.Stream; import org.apache.rat.license.ILicense; -import org.apache.rat.license.ILicenseFamily; import org.apache.tika.mime.MediaType; /** @@ -38,7 +35,7 @@ public class MetaData { /** The list of matched licenses */ private final SortedSet matchedLicenses; /** The list of License Family Categories that are approved */ - private final Set approvedLicenses; + private Predicate approvalPredicate; /** The charset for this document */ private Charset charset; /** The media type for this document */ @@ -52,7 +49,7 @@ public class MetaData { */ public MetaData() { this.matchedLicenses = new TreeSet<>(); - this.approvedLicenses = new HashSet<>(); + this.approvalPredicate = x -> true; } /** @@ -97,11 +94,10 @@ public boolean detectedLicense() { /** * Sets the set of approved licenses. - * @param approvedLicenseFamilies the set of approved license families. + * @param approvalPredicate the predicate to validate licenses. */ - public void setApprovedLicenses(final Set approvedLicenseFamilies) { - licenses().filter(lic -> approvedLicenseFamilies.contains(lic.getLicenseFamily())) - .forEach(lic -> approvedLicenses.add(lic.getId())); + public void setApprovalPredicate(final Predicate approvalPredicate) { + this.approvalPredicate = approvalPredicate; } /** @@ -126,7 +122,7 @@ public Stream approvedLicenses() { * @return {@code true} if the license is in the list of approved licenses, {@code false} otherwise. */ public boolean isApproved(final ILicense license) { - return approvedLicenses.contains(license.getId()); + return approvalPredicate.test(license); } /** @@ -172,6 +168,7 @@ public void removeLicenses(final Predicate filter) { @Override public String toString() { - return String.format("MetaData[%s license, %s approved]", matchedLicenses.size(), approvedLicenses.size()); + return String.format("MetaData[%s license, %s approved]", matchedLicenses.size(), + matchedLicenses.stream().filter(approvalPredicate).count()); } } diff --git a/apache-rat-core/src/main/java/org/apache/rat/config/exclusion/plexus/MatchPattern.java b/apache-rat-core/src/main/java/org/apache/rat/config/exclusion/plexus/MatchPattern.java index 99b07ddad..c43836ecd 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/config/exclusion/plexus/MatchPattern.java +++ b/apache-rat-core/src/main/java/org/apache/rat/config/exclusion/plexus/MatchPattern.java @@ -83,7 +83,7 @@ boolean matchPath(final String str, final char[][] strDirs, final boolean isCase } else { result = SelectorUtils.matchAntPathPattern(getTokenizedPathChars(), strDirs, isCaseSensitive); } - if (DefaultLog.getInstance().isEnabled(Log.Level.DEBUG)) { + if (result && DefaultLog.getInstance().isEnabled(Log.Level.DEBUG)) { DefaultLog.getInstance().debug(format("%s match %s -> %s", this, str, result)); } return result; diff --git a/apache-rat-core/src/main/java/org/apache/rat/license/LicenseSetFactory.java b/apache-rat-core/src/main/java/org/apache/rat/license/LicenseSetFactory.java index e417af542..7f08ec9f3 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/license/LicenseSetFactory.java +++ b/apache-rat-core/src/main/java/org/apache/rat/license/LicenseSetFactory.java @@ -238,7 +238,7 @@ public void addFamily(final ILicenseFamily.Builder builder) { * Adds a license family category (id) to the list of approved licenses * @param familyCategory the category to add. */ - public void addLicenseCategory(final String familyCategory) { + public void approveLicenseCategory(final String familyCategory) { approvedLicenseCategories.add(ILicenseFamily.makeCategory(familyCategory)); } @@ -275,21 +275,27 @@ private boolean isApprovedCategory(final ILicenseFamily family) { return approvedLicenseCategories.contains(family.getFamilyCategory()) && !removedLicenseCategories.contains(family.getFamilyCategory()); } + /** + * Gets a predicate to filter for approved licenses. + * @return a predicate that returns {@code true} if the license is approved. + */ + public Predicate getApprovedLicensePredicate() { + return lic -> !removedLicenseIds.contains(lic.getId()) && (approvedLicenseIds.contains(lic.getId()) || + isApprovedCategory(lic.getLicenseFamily())); + } + /** * Gets the License objects based on the filter. * @param filter the types of LicenseFamily objects to return. * @return a SortedSet of ILicense objects. */ public SortedSet getLicenses(final LicenseFilter filter) { - Predicate approved = l -> (isApprovedCategory(l.getLicenseFamily()) || - approvedLicenseIds.contains(l.getId())) && !removedLicenseIds.contains(l.getId()); - switch (filter) { case ALL: return Collections.unmodifiableSortedSet(licenses); case APPROVED: SortedSet result = new TreeSet<>(); - licenses.stream().filter(approved).forEach(result::add); + licenses.stream().filter(getApprovedLicensePredicate()).forEach(result::add); return result; case NONE: default: diff --git a/apache-rat-core/src/main/java/org/apache/rat/policy/DefaultPolicy.java b/apache-rat-core/src/main/java/org/apache/rat/policy/DefaultPolicy.java index c1155f71a..09e9c3b74 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/policy/DefaultPolicy.java +++ b/apache-rat-core/src/main/java/org/apache/rat/policy/DefaultPolicy.java @@ -18,14 +18,14 @@ */ package org.apache.rat.policy; -import java.util.Collection; + import java.util.Collections; import java.util.SortedSet; -import java.util.TreeSet; import org.apache.rat.api.Document; import org.apache.rat.document.IDocumentAnalyser; import org.apache.rat.license.ILicenseFamily; +import org.apache.rat.license.LicenseSetFactory; /** * A default Document Analyser that determines if the matched license is in the @@ -33,29 +33,20 @@ */ public class DefaultPolicy implements IDocumentAnalyser { /** The set of all approved license families */ - private final SortedSet approvedLicenseFamilies; + private final LicenseSetFactory licenseSetFactory; /** * Constructor with the list of approved license families. - * @param approvedLicenseFamilies the approved license families. - */ - public DefaultPolicy(final Collection approvedLicenseFamilies) { - this.approvedLicenseFamilies = new TreeSet<>(); - this.approvedLicenseFamilies.addAll(approvedLicenseFamilies); - } - - /** - * Adds an ILicenseFamily to the list of approved licenses. - * @param approvedLicense license to be approved. + * @param licenseSetFactory the licenseSetFactory to determine valie licenses */ - public void add(final ILicenseFamily approvedLicense) { - this.approvedLicenseFamilies.add(approvedLicense); + public DefaultPolicy(final LicenseSetFactory licenseSetFactory) { + this.licenseSetFactory = licenseSetFactory; } @Override public void analyse(final Document document) { if (document != null) { - document.getMetaData().setApprovedLicenses(getApprovedLicenseFamilies()); + document.getMetaData().setApprovalPredicate(licenseSetFactory.getApprovedLicensePredicate()); } } @@ -65,6 +56,6 @@ public void analyse(final Document document) { * @return sorted set of license family definitions. */ public SortedSet getApprovedLicenseFamilies() { - return Collections.unmodifiableSortedSet(approvedLicenseFamilies); + return Collections.unmodifiableSortedSet(licenseSetFactory.getLicenseFamilies(LicenseSetFactory.LicenseFilter.APPROVED)); } } diff --git a/apache-rat-core/src/main/java/org/apache/rat/report/xml/XmlReportFactory.java b/apache-rat-core/src/main/java/org/apache/rat/report/xml/XmlReportFactory.java index 0d1280dec..b2d44f6be 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/report/xml/XmlReportFactory.java +++ b/apache-rat-core/src/main/java/org/apache/rat/report/xml/XmlReportFactory.java @@ -74,7 +74,7 @@ public static RatReport createStandardReport(final IXmlWriter writer, final Clai reporters.add(new ClaimValidatorReport(writer, statistic, configuration)); final IDocumentAnalyser analyser = DefaultAnalyserFactory.createDefaultAnalyser(configuration); - final DefaultPolicy policy = new DefaultPolicy(configuration.getLicenseFamilies(LicenseFilter.APPROVED)); + final DefaultPolicy policy = new DefaultPolicy(configuration.getLicenseSetFactory()); final IDocumentAnalyser[] analysers = {analyser, policy}; DocumentAnalyserMultiplexer analysisMultiplexer = new DocumentAnalyserMultiplexer(analysers); diff --git a/apache-rat-core/src/test/java/org/apache/rat/policy/DefaultPolicyTest.java b/apache-rat-core/src/test/java/org/apache/rat/policy/DefaultPolicyTest.java index abceccb17..ae6b4d454 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/policy/DefaultPolicyTest.java +++ b/apache-rat-core/src/test/java/org/apache/rat/policy/DefaultPolicyTest.java @@ -21,7 +21,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import java.util.Collections; import java.util.SortedSet; import java.util.TreeSet; @@ -57,11 +56,13 @@ public class DefaultPolicyTest { private Document document; private DefaultPolicy policy; private Defaults defaults; + private LicenseSetFactory licenseSetFactory; @BeforeEach public void setUp() { defaults = Defaults.builder().build(); - policy = new DefaultPolicy(defaults.getLicenseSetFactory().getLicenseFamilies(LicenseFilter.APPROVED)); + licenseSetFactory = defaults.getLicenseSetFactory(); + policy = new DefaultPolicy(licenseSetFactory); document = new TestingDocument("subject"); } @@ -124,23 +125,23 @@ public void testAddNewApprovedLicenseAndDefaults() { policy.analyse(document); assertApproval(false); - policy.add(testingFamily); - assertNotNull(LicenseSetFactory.familySearch(testingFamily, policy.getApprovedLicenseFamilies()), - "Did not properly add ILicenseFamily"); + licenseSetFactory.approveLicenseCategory(testingFamily.getFamilyCategory()); policy.analyse(document); assertApproval(true); } @Test public void testAddNewApprovedLicenseNoDefaults() { - policy = new DefaultPolicy(Collections.emptySet()); + LicenseSetFactory licenseSetFactory = new LicenseSetFactory(); + policy = new DefaultPolicy(licenseSetFactory); assertEquals(0, policy.getApprovedLicenseFamilies().size()); ILicenseFamily testingFamily = makeFamily("test", "Testing License Family"); setMetadata(testingFamily); policy.analyse(document); assertApproval(false); - policy.add(testingFamily); + licenseSetFactory.addLicense(new TestingLicense(testingFamily.getFamilyCategory().trim(), new TestingMatcher(), testingFamily)); + licenseSetFactory.approveLicenseCategory(testingFamily.getFamilyCategory()); assertEquals(1, policy.getApprovedLicenseFamilies().size()); assertNotNull(LicenseSetFactory.familySearch(testingFamily, policy.getApprovedLicenseFamilies()), "Did not properly add ILicenseFamily"); diff --git a/apache-rat-plugin/src/main/java/org/apache/rat/mp/AbstractRatMojo.java b/apache-rat-plugin/src/main/java/org/apache/rat/mp/AbstractRatMojo.java index 1e2172f90..baf5ea20e 100644 --- a/apache-rat-plugin/src/main/java/org/apache/rat/mp/AbstractRatMojo.java +++ b/apache-rat-plugin/src/main/java/org/apache/rat/mp/AbstractRatMojo.java @@ -105,7 +105,7 @@ public void setAddDefaultLicenses(final boolean addDefaultLicenses) { /** * Whether to add the default list of license matchers. - * @deprecated Use <config>. + * @deprecated By default, license matchers are added. Use <configurationNoDefaults> to remove them. */ @Deprecated @Parameter(property = "rat.addDefaultLicenseMatchers") @@ -176,9 +176,8 @@ public void setAddDefaultLicenses(final boolean addDefaultLicenses) { *
  • configuration files for IDEA, see * useIdeaDefaultExcludes
  • * - * @deprecated When set to true specifies that the STANDARD_PATTERNS are excluded, as are - * the STANDARD_SCMS patterns. Use the various InputExclude and InputInclude elements to - * explicitly specify what to include or exclude. + * @deprecated use <inputExcludeStd><exclude>STANDARD_PATTERNS</exclude> + * <exclude>STANDARD_SCM</exclude></inputExcludeStd> */ @Parameter(property = "rat.useDefaultExcludes", defaultValue = "true") @Deprecated From 9735c092760379af5edd469dccf50a98be33f208 Mon Sep 17 00:00:00 2001 From: Claude Warren Date: Tue, 10 Dec 2024 23:16:48 +0000 Subject: [PATCH 02/22] reverted ubuntu to 22.04 --- .github/workflows/maven.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index 593c3afa0..f083a3210 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -31,7 +31,7 @@ jobs: strategy: matrix: - os: [ubuntu-latest, windows-latest] + os: [ubuntu-22.04, windows-latest] # RAT-296: disable JDK10 due to # Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target # From 8987032bb7216fcb1626b2170eecad0ca6b27a43 Mon Sep 17 00:00:00 2001 From: Claude Warren Date: Tue, 10 Dec 2024 23:32:19 +0000 Subject: [PATCH 03/22] reverted change to ubuntu version --- .github/workflows/maven.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml index f083a3210..593c3afa0 100644 --- a/.github/workflows/maven.yml +++ b/.github/workflows/maven.yml @@ -31,7 +31,7 @@ jobs: strategy: matrix: - os: [ubuntu-22.04, windows-latest] + os: [ubuntu-latest, windows-latest] # RAT-296: disable JDK10 due to # Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target # From 2713a154b5d97bd308c20df185367c063918b2dd Mon Sep 17 00:00:00 2001 From: Claude Warren Date: Tue, 10 Dec 2024 23:47:00 +0000 Subject: [PATCH 04/22] fixed tools spotbugs issue under java 21 --- apache-rat-tools/pom.xml | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/apache-rat-tools/pom.xml b/apache-rat-tools/pom.xml index 3caa42512..0053ab944 100644 --- a/apache-rat-tools/pom.xml +++ b/apache-rat-tools/pom.xml @@ -96,7 +96,14 @@ - + + + com.github.spotbugs + spotbugs-maven-plugin + + spotbugs-ignore.xml + + From d8b4ce7cf604b2482b603aff29a04c8e0f6c3927 Mon Sep 17 00:00:00 2001 From: "P. Ottlinger" Date: Wed, 11 Dec 2024 14:17:49 +0100 Subject: [PATCH 05/22] RAT-469: Add Itest which should work after the patch is applied --- .../src/it/RAT-469/invoker.properties | 16 ++++++ apache-rat-plugin/src/it/RAT-469/pom.xml | 49 +++++++++++++++++++ .../src/it/RAT-469/verify.groovy | 43 ++++++++++++++++ 3 files changed, 108 insertions(+) create mode 100644 apache-rat-plugin/src/it/RAT-469/invoker.properties create mode 100644 apache-rat-plugin/src/it/RAT-469/pom.xml create mode 100644 apache-rat-plugin/src/it/RAT-469/verify.groovy diff --git a/apache-rat-plugin/src/it/RAT-469/invoker.properties b/apache-rat-plugin/src/it/RAT-469/invoker.properties new file mode 100644 index 000000000..6486eb1de --- /dev/null +++ b/apache-rat-plugin/src/it/RAT-469/invoker.properties @@ -0,0 +1,16 @@ +# 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. + +invoker.goals = clean apache-rat:rat diff --git a/apache-rat-plugin/src/it/RAT-469/pom.xml b/apache-rat-plugin/src/it/RAT-469/pom.xml new file mode 100644 index 000000000..c588e020c --- /dev/null +++ b/apache-rat-plugin/src/it/RAT-469/pom.xml @@ -0,0 +1,49 @@ + + + + 4.0.0 + org.apache.rat.test + it-rat469 + 1.0 + + + + org.apache.rat + apache-rat-plugin + @pom.version@ + + GPL3 + true + false + + pom.xml + + + STANDARD_PATTERNS + STANDARD_SCMS + MAVEN + IDEA + + + + + + + + + org.apache.maven.plugins + maven-clean-plugin + 3.3.2 + + + + + diff --git a/apache-rat-plugin/src/it/RAT-469/verify.groovy b/apache-rat-plugin/src/it/RAT-469/verify.groovy new file mode 100644 index 000000000..8727cfe25 --- /dev/null +++ b/apache-rat-plugin/src/it/RAT-469/verify.groovy @@ -0,0 +1,43 @@ +/* + * 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. + */ +import org.apache.rat.testhelpers.TextUtils + +content = new File(basedir, 'build.log').text + +assert content.contains('BUILD SUCCESS') + +assert ! content.contains('[WARNING] No resources included') + +// Report is in apache-rat-plugin/target/invoker/RAT-469/target/site/rat-report.html +report = new File(basedir, 'target/site/rat-report.html').text +assert TextUtils.isMatching("^ /verify.groovy\\s+S ", report) + +// enable after patch is applied: +// assert TextUtils.isMatching("^ /pom.xml\\s+S ", report) + +/* should be GPL after patch is applied +! /pom.xml + S application/xml ISO-8859-1 + ????? ????? Unknown license (Unapproved) +*/ +assert report.contains('! Unapproved: 1') +// assert report.contains(' GPL GPL3 GNU General Public License V3.0') + +assert report.contains(' AL AL Apache License Version 2.0') +assert report.contains('Approved: 2') +assert report.contains('Standards: 3') +// TODO: RAT-469 apply and alter test after fix is applied \ No newline at end of file From 0b95cb67f36ac7749a2b70e8c7fda0318106d21d Mon Sep 17 00:00:00 2001 From: "P. Ottlinger" Date: Wed, 11 Dec 2024 14:19:18 +0100 Subject: [PATCH 06/22] RAT-469: Add changelog entry --- src/changes/changes.xml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 7a655a285..d7e43924b 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -72,6 +72,9 @@ The type attribute can be one of: --> + + Verify that projects that configure valid other licenses than the defaults, report correctly as well. + Add .externalToolBuilders to the default Eclipse exclusions during RAT runs. From c45e75d8551e2f11fb82bc99ce25284295516fd2 Mon Sep 17 00:00:00 2001 From: "P. Ottlinger" Date: Wed, 11 Dec 2024 15:05:49 +0100 Subject: [PATCH 07/22] LHF: fix typo --- .../org/apache/rat/config/exclusion/ExclusionProcessor.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apache-rat-core/src/main/java/org/apache/rat/config/exclusion/ExclusionProcessor.java b/apache-rat-core/src/main/java/org/apache/rat/config/exclusion/ExclusionProcessor.java index 8fe2b15cf..e21432e03 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/config/exclusion/ExclusionProcessor.java +++ b/apache-rat-core/src/main/java/org/apache/rat/config/exclusion/ExclusionProcessor.java @@ -136,7 +136,7 @@ public ExclusionProcessor addIncludedCollection(final StandardCollection collect /** * Add the patterns from collections of patterns as excluded patterns. - * @param patterns the strings to that define patterns to be ecxcuded from processing. + * @param patterns the strings to that define patterns to be excluded from processing. * @return this */ public ExclusionProcessor addExcludedPatterns(final Iterable patterns) { From 8f0ad6cadc9839db99d567a64124c64f6d601944 Mon Sep 17 00:00:00 2001 From: Claude Warren Date: Wed, 11 Dec 2024 16:35:28 +0000 Subject: [PATCH 08/22] removed RELEASE_NOTES from tools build --- apache-rat-tools/pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/apache-rat-tools/pom.xml b/apache-rat-tools/pom.xml index 0053ab944..f24f68191 100644 --- a/apache-rat-tools/pom.xml +++ b/apache-rat-tools/pom.xml @@ -91,7 +91,6 @@ apache-rat-core tests - META-INF/RELEASE_NOTES.txt ${project.build.outputDirectory} From 8645b121869efdda976050dfecbd6592812cbdd1 Mon Sep 17 00:00:00 2001 From: "P. Ottlinger" Date: Wed, 11 Dec 2024 17:37:24 +0100 Subject: [PATCH 09/22] RAT-469: Ensure inclusion/exclusion rules are logged properly --- apache-rat-plugin/src/it/RAT-469/verify.groovy | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apache-rat-plugin/src/it/RAT-469/verify.groovy b/apache-rat-plugin/src/it/RAT-469/verify.groovy index 8727cfe25..34a8c8271 100644 --- a/apache-rat-plugin/src/it/RAT-469/verify.groovy +++ b/apache-rat-plugin/src/it/RAT-469/verify.groovy @@ -19,7 +19,8 @@ import org.apache.rat.testhelpers.TextUtils content = new File(basedir, 'build.log').text assert content.contains('BUILD SUCCESS') - +assert content.contains('[INFO] Including patterns: pom.xml') // explicit inclusion worked +assert content.contains('[INFO] Processing exclude file from STANDARD_SCMS.') // default exclusions worked assert ! content.contains('[WARNING] No resources included') // Report is in apache-rat-plugin/target/invoker/RAT-469/target/site/rat-report.html From ef5c93bcc36d5f1e243179a283aad0fce13705e4 Mon Sep 17 00:00:00 2001 From: "P. Ottlinger" Date: Fri, 13 Dec 2024 14:25:35 +0100 Subject: [PATCH 10/22] RAT-469: Replace misleading default text as new configuration elements need to be put under the existing configuration instead a newly/non-existing config --- .../src/main/java/org/apache/rat/mp/AbstractRatMojo.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apache-rat-plugin/src/main/java/org/apache/rat/mp/AbstractRatMojo.java b/apache-rat-plugin/src/main/java/org/apache/rat/mp/AbstractRatMojo.java index baf5ea20e..8c336659f 100644 --- a/apache-rat-plugin/src/main/java/org/apache/rat/mp/AbstractRatMojo.java +++ b/apache-rat-plugin/src/main/java/org/apache/rat/mp/AbstractRatMojo.java @@ -78,7 +78,7 @@ public abstract class AbstractRatMojo extends BaseRatMojo { * Specifies the licenses to accept. By default, these are added to the default * licenses, unless you set <addDefaultLicenseMatchers> to false. Arguments should be * file name of <Configs> file structure. - * @deprecated Use <config>. + * @deprecated Use specific configuration under <configuration>. * @since 0.8 */ @Parameter @@ -87,7 +87,7 @@ public abstract class AbstractRatMojo extends BaseRatMojo { /** * Specifies the additional licenses file. - * @deprecated Use <config>. + * @deprecated Use specific configuration under <configuration>. */ @Parameter @Deprecated @@ -112,14 +112,14 @@ public void setAddDefaultLicenses(final boolean addDefaultLicenses) { private boolean addDefaultLicenseMatchers; /** The list of approved licenses - * @deprecated Use <config>. + * @deprecated Use specific configuration under <configuration>. */ @Deprecated @Parameter(required = false) private String[] approvedLicenses; /** The file of approved licenses - * @deprecated Use <config>. + * @deprecated Use specific configuration under <configuration>. */ @Deprecated @Parameter(property = "rat.approvedFile") From a4d720e4db6493c63461e6160f639ccac1a12500 Mon Sep 17 00:00:00 2001 From: Claude Warren Date: Sat, 14 Dec 2024 13:46:09 +0000 Subject: [PATCH 11/22] Cleaned up analysis and verified correctness of LicenseSetFactory --- .../org/apache/rat/ReportConfiguration.java | 2 +- ...lyserFactory.java => AnalyserFactory.java} | 44 +++- .../java/org/apache/rat/api/MetaData.java | 6 +- .../document/DocumentAnalyserMultiplexer.java | 50 ----- .../rat/document/IDocumentAnalyser.java | 1 + .../apache/rat/license/LicenseSetFactory.java | 2 +- .../org/apache/rat/policy/DefaultPolicy.java | 61 ------ .../org/apache/rat/policy/package-info.java | 23 --- .../rat/report/xml/XmlReportFactory.java | 12 +- ...toryTest.java => AnalyserFactoryTest.java} | 96 +++++---- .../rat/analysis/HeaderCheckWorkerTest.java | 2 + .../org/apache/rat/analysis/PolicyTest.java | 171 ++++++++++++++++ .../DocumentAnalyserMultiplexerTest.java | 60 ------ .../rat/license/LicenseSetFactoryTest.java | 193 ++++++++++++++++++ .../apache/rat/policy/DefaultPolicyTest.java | 170 --------------- 15 files changed, 471 insertions(+), 422 deletions(-) rename apache-rat-core/src/main/java/org/apache/rat/analysis/{DefaultAnalyserFactory.java => AnalyserFactory.java} (76%) delete mode 100644 apache-rat-core/src/main/java/org/apache/rat/document/DocumentAnalyserMultiplexer.java delete mode 100644 apache-rat-core/src/main/java/org/apache/rat/policy/DefaultPolicy.java delete mode 100644 apache-rat-core/src/main/java/org/apache/rat/policy/package-info.java rename apache-rat-core/src/test/java/org/apache/rat/analysis/{DefaultAnalyserFactoryTest.java => AnalyserFactoryTest.java} (70%) create mode 100644 apache-rat-core/src/test/java/org/apache/rat/analysis/PolicyTest.java delete mode 100644 apache-rat-core/src/test/java/org/apache/rat/document/DocumentAnalyserMultiplexerTest.java create mode 100644 apache-rat-core/src/test/java/org/apache/rat/license/LicenseSetFactoryTest.java delete mode 100644 apache-rat-core/src/test/java/org/apache/rat/policy/DefaultPolicyTest.java diff --git a/apache-rat-core/src/main/java/org/apache/rat/ReportConfiguration.java b/apache-rat-core/src/main/java/org/apache/rat/ReportConfiguration.java index e49799c8b..9af3e8dc2 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/ReportConfiguration.java +++ b/apache-rat-core/src/main/java/org/apache/rat/ReportConfiguration.java @@ -685,7 +685,7 @@ public void addApprovedLicenseId(final ILicense approvedLicense) { * @param licenseId the license id to add. */ public void addApprovedLicenseId(final String licenseId) { - licenseSetFactory.addLicenseId(licenseId); + licenseSetFactory.approveLicenseId(licenseId); } /** diff --git a/apache-rat-core/src/main/java/org/apache/rat/analysis/DefaultAnalyserFactory.java b/apache-rat-core/src/main/java/org/apache/rat/analysis/AnalyserFactory.java similarity index 76% rename from apache-rat-core/src/main/java/org/apache/rat/analysis/DefaultAnalyserFactory.java rename to apache-rat-core/src/main/java/org/apache/rat/analysis/AnalyserFactory.java index da2cc016e..300cf5022 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/analysis/DefaultAnalyserFactory.java +++ b/apache-rat-core/src/main/java/org/apache/rat/analysis/AnalyserFactory.java @@ -38,18 +38,49 @@ /** * Creates default analysers. */ -public final class DefaultAnalyserFactory { +public final class AnalyserFactory { - private DefaultAnalyserFactory() { + private AnalyserFactory() { // do not instantiate } + + /** + * Creates an anlayser that adds the approved license predicate to the document metadata. + *

    + * Note you probably do not want this as it is automatically added to {@link #createConfiguredAnalyser}. + *

    + * @param approvalPredicate the predicate to approve licenses. + * @return A document analyser that sets the approvalPredicate in document metadata. + */ + public static IDocumentAnalyser createPolicy(final Predicate approvalPredicate) { + return document -> { + if (document != null) { + document.getMetaData().setApprovalPredicate(approvalPredicate); + } + }; + } + + /** + * Creates an analyser that calls each of the provided analysers in order. + * @param analysers the array of analysers to call. + * @return an analyser that will call all the provided analysers. + */ + public static IDocumentAnalyser createMultiplexer(final IDocumentAnalyser... analysers) { + return document -> { + for (IDocumentAnalyser analyser : analysers) { + analyser.analyse(document); + } + }; + } + /** - * Creates a DocumentAnalyser from a collection of ILicenses. + * Creates a DocumentAnalyser from teh report configuraiton. * @param configuration the ReportConfiguration * @return A document analyser that uses the provides licenses. */ - public static IDocumentAnalyser createDefaultAnalyser(final ReportConfiguration configuration) { - Set licenses = configuration.getLicenses(LicenseSetFactory.LicenseFilter.ALL); + public static IDocumentAnalyser createConfiguredAnalyser(final ReportConfiguration configuration) { + LicenseSetFactory licenseSetFactory = configuration.getLicenseSetFactory(); + Set licenses = licenseSetFactory.getLicenses(LicenseSetFactory.LicenseFilter.ALL); if (licenses.isEmpty()) { throw new ConfigurationException("At least one license must be defined"); } @@ -57,7 +88,8 @@ public static IDocumentAnalyser createDefaultAnalyser(final ReportConfiguration DefaultLog.getInstance().debug("Currently active Licenses are:"); licenses.forEach(DefaultLog.getInstance()::debug); } - return new DefaultAnalyser(configuration, licenses); + return createMultiplexer(createPolicy(licenseSetFactory.getApprovedLicensePredicate()), + new DefaultAnalyser(configuration, licenses)); } /** diff --git a/apache-rat-core/src/main/java/org/apache/rat/api/MetaData.java b/apache-rat-core/src/main/java/org/apache/rat/api/MetaData.java index e821e6158..24d516f8d 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/api/MetaData.java +++ b/apache-rat-core/src/main/java/org/apache/rat/api/MetaData.java @@ -25,6 +25,7 @@ import java.util.stream.Stream; import org.apache.rat.license.ILicense; +import org.apache.rat.utils.DefaultLog; import org.apache.tika.mime.MediaType; /** @@ -50,7 +51,10 @@ public class MetaData { */ public MetaData() { this.matchedLicenses = new TreeSet<>(); - this.approvalPredicate = x -> true; + this.approvalPredicate = x -> { + DefaultLog.getInstance().error("Approved Predicate was not set."); + throw new IllegalStateException("Approved Predicate was not set."); + }; } /** diff --git a/apache-rat-core/src/main/java/org/apache/rat/document/DocumentAnalyserMultiplexer.java b/apache-rat-core/src/main/java/org/apache/rat/document/DocumentAnalyserMultiplexer.java deleted file mode 100644 index af75e5bd5..000000000 --- a/apache-rat-core/src/main/java/org/apache/rat/document/DocumentAnalyserMultiplexer.java +++ /dev/null @@ -1,50 +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.document; - -import org.apache.rat.api.Document; - -public class DocumentAnalyserMultiplexer implements IDocumentAnalyser { - - /** - * Array of Document analyzers that are to be executed. - */ - private final IDocumentAnalyser[] analysers; - - /** - * Constructor. - * @param analysers The Document analysers to execute. - */ - public DocumentAnalyserMultiplexer(final IDocumentAnalyser[] analysers) { - super(); - this.analysers = analysers; - } - - /** - * Execute the Document analysers on the provided document. - * @param document The document to analyse. - * @throws RatDocumentAnalysisException on error. - */ - public void analyse(final Document document) throws RatDocumentAnalysisException { - for (IDocumentAnalyser analyser : analysers) { - analyser.analyse(document); - } - } - -} diff --git a/apache-rat-core/src/main/java/org/apache/rat/document/IDocumentAnalyser.java b/apache-rat-core/src/main/java/org/apache/rat/document/IDocumentAnalyser.java index 4f2cb3025..a5f0cee10 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/document/IDocumentAnalyser.java +++ b/apache-rat-core/src/main/java/org/apache/rat/document/IDocumentAnalyser.java @@ -23,6 +23,7 @@ /** * Analyses Documents. */ +@FunctionalInterface public interface IDocumentAnalyser { /** * Analyse the specified document. diff --git a/apache-rat-core/src/main/java/org/apache/rat/license/LicenseSetFactory.java b/apache-rat-core/src/main/java/org/apache/rat/license/LicenseSetFactory.java index 7f08ec9f3..07301ee08 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/license/LicenseSetFactory.java +++ b/apache-rat-core/src/main/java/org/apache/rat/license/LicenseSetFactory.java @@ -254,7 +254,7 @@ public void removeLicenseCategory(final String familyCategory) { * Adds a license family category (id) to the list of approved licenses * @param licenseId the license ID to add. */ - public void addLicenseId(final String licenseId) { + public void approveLicenseId(final String licenseId) { approvedLicenseIds.add(licenseId); } diff --git a/apache-rat-core/src/main/java/org/apache/rat/policy/DefaultPolicy.java b/apache-rat-core/src/main/java/org/apache/rat/policy/DefaultPolicy.java deleted file mode 100644 index 09e9c3b74..000000000 --- a/apache-rat-core/src/main/java/org/apache/rat/policy/DefaultPolicy.java +++ /dev/null @@ -1,61 +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.policy; - - -import java.util.Collections; -import java.util.SortedSet; - -import org.apache.rat.api.Document; -import org.apache.rat.document.IDocumentAnalyser; -import org.apache.rat.license.ILicenseFamily; -import org.apache.rat.license.LicenseSetFactory; - -/** - * A default Document Analyser that determines if the matched license is in the - * set of approved licenses. - */ -public class DefaultPolicy implements IDocumentAnalyser { - /** The set of all approved license families */ - private final LicenseSetFactory licenseSetFactory; - - /** - * Constructor with the list of approved license families. - * @param licenseSetFactory the licenseSetFactory to determine valie licenses - */ - public DefaultPolicy(final LicenseSetFactory licenseSetFactory) { - this.licenseSetFactory = licenseSetFactory; - } - - @Override - public void analyse(final Document document) { - if (document != null) { - document.getMetaData().setApprovalPredicate(licenseSetFactory.getApprovedLicensePredicate()); - } - } - - /** - * Gets an unmodifiable reference to the SortedSet of approved licenses that - * this policy is holding. - * @return sorted set of license family definitions. - */ - public SortedSet getApprovedLicenseFamilies() { - return Collections.unmodifiableSortedSet(licenseSetFactory.getLicenseFamilies(LicenseSetFactory.LicenseFilter.APPROVED)); - } -} diff --git a/apache-rat-core/src/main/java/org/apache/rat/policy/package-info.java b/apache-rat-core/src/main/java/org/apache/rat/policy/package-info.java deleted file mode 100644 index c2eec94d1..000000000 --- a/apache-rat-core/src/main/java/org/apache/rat/policy/package-info.java +++ /dev/null @@ -1,23 +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. * - */ - -/** - * Policies for various methods of reporting. - */ -package org.apache.rat.policy; diff --git a/apache-rat-core/src/main/java/org/apache/rat/report/xml/XmlReportFactory.java b/apache-rat-core/src/main/java/org/apache/rat/report/xml/XmlReportFactory.java index b2d44f6be..2bdce3b0f 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/report/xml/XmlReportFactory.java +++ b/apache-rat-core/src/main/java/org/apache/rat/report/xml/XmlReportFactory.java @@ -22,11 +22,8 @@ import java.util.List; import org.apache.rat.ReportConfiguration; -import org.apache.rat.analysis.DefaultAnalyserFactory; -import org.apache.rat.document.DocumentAnalyserMultiplexer; -import org.apache.rat.document.IDocumentAnalyser; +import org.apache.rat.analysis.AnalyserFactory; import org.apache.rat.license.LicenseSetFactory.LicenseFilter; -import org.apache.rat.policy.DefaultPolicy; import org.apache.rat.report.ConfigurationReport; import org.apache.rat.report.RatReport; import org.apache.rat.report.claim.ClaimAggregator; @@ -73,11 +70,6 @@ public static RatReport createStandardReport(final IXmlWriter writer, final Clai reporters.add(new SimpleXmlClaimReporter(writer)); reporters.add(new ClaimValidatorReport(writer, statistic, configuration)); - final IDocumentAnalyser analyser = DefaultAnalyserFactory.createDefaultAnalyser(configuration); - final DefaultPolicy policy = new DefaultPolicy(configuration.getLicenseSetFactory()); - - final IDocumentAnalyser[] analysers = {analyser, policy}; - DocumentAnalyserMultiplexer analysisMultiplexer = new DocumentAnalyserMultiplexer(analysers); - return new ClaimReporterMultiplexer(writer, configuration.isDryRun(), analysisMultiplexer, reporters); + return new ClaimReporterMultiplexer(writer, configuration.isDryRun(), AnalyserFactory.createConfiguredAnalyser(configuration), reporters); } } diff --git a/apache-rat-core/src/test/java/org/apache/rat/analysis/DefaultAnalyserFactoryTest.java b/apache-rat-core/src/test/java/org/apache/rat/analysis/AnalyserFactoryTest.java similarity index 70% rename from apache-rat-core/src/test/java/org/apache/rat/analysis/DefaultAnalyserFactoryTest.java rename to apache-rat-core/src/test/java/org/apache/rat/analysis/AnalyserFactoryTest.java index bb2e515cc..d5b348335 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/analysis/DefaultAnalyserFactoryTest.java +++ b/apache-rat-core/src/test/java/org/apache/rat/analysis/AnalyserFactoryTest.java @@ -18,10 +18,8 @@ */ package org.apache.rat.analysis; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertFalse; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.io.File; import java.io.StringWriter; @@ -40,6 +38,8 @@ import org.apache.rat.report.claim.SimpleXmlClaimReporter; import org.apache.rat.report.xml.writer.XmlWriter; import org.apache.rat.test.utils.Resources; +import org.apache.rat.testhelpers.TestingDocument; +import org.apache.rat.testhelpers.TestingDocumentAnalyser; import org.apache.rat.testhelpers.TextUtils; import org.assertj.core.util.Files; import org.junit.jupiter.api.BeforeEach; @@ -48,7 +48,7 @@ import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; -public class DefaultAnalyserFactoryTest { +public class AnalyserFactoryTest { private final DocumentName basedir; @@ -56,7 +56,7 @@ public class DefaultAnalyserFactoryTest { private SimpleXmlClaimReporter reporter; private IDocumentAnalyser analyser; - DefaultAnalyserFactoryTest() { + AnalyserFactoryTest() { basedir = DocumentName.builder(new File(Files.currentFolder(), Resources.SRC_TEST_RESOURCES)).build(); } @@ -66,7 +66,7 @@ public void setUp() { reporter = new SimpleXmlClaimReporter(new XmlWriter(out)); ReportConfiguration config = new ReportConfiguration(); config.addLicense(UnknownLicense.INSTANCE); - analyser = DefaultAnalyserFactory.createDefaultAnalyser(config); + analyser = AnalyserFactory.createConfiguredAnalyser(config); } @Test @@ -74,10 +74,10 @@ public void standardTypeAnalyser() throws Exception { final Document document = new FileDocument(basedir, Resources.getExampleResource("exampleData/Text.txt"), DocumentNameMatcher.MATCHES_ALL); analyser.analyse(document); - assertEquals(Document.Type.STANDARD, document.getMetaData().getDocumentType()); - assertEquals("text/plain", document.getMetaData().getMediaType().toString()); - assertEquals(1, document.getMetaData().licenses().count()); - document.getMetaData().licenses().forEach(lic -> assertEquals(UnknownLicense.INSTANCE, lic)); + assertThat(document.getMetaData().getDocumentType()).isEqualTo(Document.Type.STANDARD); + assertThat(document.getMetaData().getMediaType().toString()).isEqualTo("text/plain"); + assertThat(document.getMetaData().licenses().count()).isEqualTo(1); + document.getMetaData().licenses().forEach(lic -> assertThat(lic).isEqualTo(UnknownLicense.INSTANCE)); } @Test @@ -85,8 +85,8 @@ public void noteTypeAnalyser() throws Exception { final Document document = new FileDocument(basedir, Resources.getExampleResource("exampleData/LICENSE"), DocumentNameMatcher.MATCHES_ALL); analyser.analyse(document); - assertEquals(Document.Type.NOTICE, document.getMetaData().getDocumentType()); - assertEquals("text/plain", document.getMetaData().getMediaType().toString()); + assertThat(document.getMetaData().getDocumentType()).isEqualTo(Document.Type.NOTICE); + assertThat(document.getMetaData().getMediaType().toString()).isEqualTo("text/plain"); } @Test @@ -94,8 +94,8 @@ public void binaryTypeAnalyser() throws Exception { final Document document = new FileDocument(basedir, Resources.getExampleResource("exampleData/Image.png"), DocumentNameMatcher.MATCHES_ALL); analyser.analyse(document); - assertEquals(Document.Type.BINARY, document.getMetaData().getDocumentType()); - assertEquals("image/png", document.getMetaData().getMediaType().toString()); + assertThat(document.getMetaData().getDocumentType()).isEqualTo(Document.Type.BINARY); + assertThat(document.getMetaData().getMediaType().toString()).isEqualTo("image/png"); } @Test @@ -105,10 +105,10 @@ public void archiveTypeAnalyserTest() throws Exception { Defaults defaults = Defaults.builder().build(); ReportConfiguration config = new ReportConfiguration(); config.setFrom(defaults); - analyser = DefaultAnalyserFactory.createDefaultAnalyser(config); + analyser = AnalyserFactory.createConfiguredAnalyser(config); analyser.analyse(document); - assertEquals(Document.Type.ARCHIVE, document.getMetaData().getDocumentType()); - assertEquals("application/java-archive", document.getMetaData().getMediaType().toString()); + assertThat(document.getMetaData().getDocumentType()).isEqualTo(Document.Type.ARCHIVE); + assertThat(document.getMetaData().getMediaType().toString()).isEqualTo("application/java-archive"); } private static Stream archiveProcessingTestData() { @@ -128,11 +128,11 @@ public void archiveProcessingTest(ReportConfiguration.Processing archiveProcessi ReportConfiguration config = new ReportConfiguration(); config.setFrom(defaults); config.setArchiveProcessing(archiveProcessing); - analyser = DefaultAnalyserFactory.createDefaultAnalyser(config); + analyser = AnalyserFactory.createConfiguredAnalyser(config); analyser.analyse(document); - assertEquals(Document.Type.ARCHIVE, document.getMetaData().getDocumentType()); - assertEquals("application/java-archive", document.getMetaData().getMediaType().toString()); - assertEquals(expectedLicenseCount, document.getMetaData().licenses().count()); + assertThat(document.getMetaData().getDocumentType()).isEqualTo(Document.Type.ARCHIVE); + assertThat(document.getMetaData().getMediaType().toString()).isEqualTo("application/java-archive"); + assertThat(document.getMetaData().licenses().count()).isEqualTo(expectedLicenseCount); } @Test @@ -142,8 +142,8 @@ public void missingFileTest() { Defaults defaults = Defaults.builder().build(); ReportConfiguration config = new ReportConfiguration(); config.setFrom(defaults); - analyser = DefaultAnalyserFactory.createDefaultAnalyser(config); - assertThrows(RatDocumentAnalysisException.class, () -> analyser.analyse(document)); + analyser = AnalyserFactory.createConfiguredAnalyser(config); + assertThatThrownBy(() -> analyser.analyse(document)).isInstanceOf(RatDocumentAnalysisException.class); } @@ -152,8 +152,8 @@ public void archiveTypeAnalyser() throws Exception { final Document document = new FileDocument(basedir, Resources.getExampleResource("exampleData/dummy.jar"), DocumentNameMatcher.MATCHES_ALL); analyser.analyse(document); - assertEquals(Document.Type.ARCHIVE, document.getMetaData().getDocumentType()); - assertEquals("application/java-archive", document.getMetaData().getMediaType().toString()); + assertThat(document.getMetaData().getDocumentType()).isEqualTo(Document.Type.ARCHIVE); + assertThat(document.getMetaData().getMediaType().toString()).isEqualTo("application/java-archive"); } @Test @@ -161,8 +161,8 @@ public void RAT211_bmp_Test() throws Exception { final Document document = new FileDocument(basedir, Resources.getResourceFile("/jira/RAT211/side_left.bmp"), DocumentNameMatcher.MATCHES_ALL); analyser.analyse(document); - assertEquals(Document.Type.BINARY, document.getMetaData().getDocumentType()); - assertEquals("image/bmp", document.getMetaData().getMediaType().toString()); + assertThat(document.getMetaData().getDocumentType()).isEqualTo(Document.Type.BINARY); + assertThat(document.getMetaData().getMediaType().toString()).isEqualTo("image/bmp"); } @Test @@ -170,8 +170,8 @@ public void RAT211_dia_Test() throws Exception { final Document document = new FileDocument(basedir, Resources.getResourceFile("/jira/RAT211/leader-election-message-arrives.dia"), DocumentNameMatcher.MATCHES_ALL); analyser.analyse(document); - assertEquals(Document.Type.ARCHIVE, document.getMetaData().getDocumentType()); - assertEquals("application/gzip", document.getMetaData().getMediaType().toString()); + assertThat(document.getMetaData().getDocumentType()).isEqualTo(Document.Type.ARCHIVE); + assertThat(document.getMetaData().getMediaType().toString()).isEqualTo("application/gzip"); } @Test @@ -205,17 +205,17 @@ public void standardNotificationTest() throws Exception { ReportConfiguration config = new ReportConfiguration(); config.setFrom(defaults); config.setStandardProcessing(ReportConfiguration.Processing.NOTIFICATION); - analyser = DefaultAnalyserFactory.createDefaultAnalyser(config); + analyser = AnalyserFactory.createConfiguredAnalyser(config); Document document = new FileDocument(basedir, Resources.getExampleResource("exampleData/Text.txt"), DocumentNameMatcher.MATCHES_ALL); analyser.analyse(document); - assertFalse(document.getMetaData().detectedLicense()); + assertThat(document.getMetaData().detectedLicense()).isFalse(); document = new FileDocument(basedir, Resources.getExampleResource("exampleData/sub/Empty.txt"), DocumentNameMatcher.MATCHES_ALL); analyser.analyse(document); - assertFalse(document.getMetaData().detectedLicense()); + assertThat(document.getMetaData().detectedLicense()).isFalse(); } @Test @@ -225,17 +225,17 @@ public void standardAbsenceTest() throws Exception { ReportConfiguration config = new ReportConfiguration(); config.setFrom(defaults); config.setStandardProcessing(ReportConfiguration.Processing.ABSENCE); - analyser = DefaultAnalyserFactory.createDefaultAnalyser(config); + analyser = AnalyserFactory.createConfiguredAnalyser(config); Document document = new FileDocument(basedir, Resources.getExampleResource("exampleData/Text.txt"), DocumentNameMatcher.MATCHES_ALL); analyser.analyse(document); - assertTrue(document.getMetaData().detectedLicense()); + assertThat(document.getMetaData().detectedLicense()).isTrue(); document = new FileDocument(basedir, Resources.getExampleResource("exampleData/sub/Empty.txt"), DocumentNameMatcher.MATCHES_ALL); analyser.analyse(document); - assertTrue(document.getMetaData().detectedLicense()); + assertThat(document.getMetaData().detectedLicense()).isTrue(); } @Test @@ -244,16 +244,34 @@ public void standardPresenceTest() throws Exception { ReportConfiguration config = new ReportConfiguration(); config.setFrom(defaults); config.setStandardProcessing(ReportConfiguration.Processing.PRESENCE); - analyser = DefaultAnalyserFactory.createDefaultAnalyser(config); + analyser = AnalyserFactory.createConfiguredAnalyser(config); Document document = new FileDocument(basedir, Resources.getExampleResource("exampleData/Text.txt"), DocumentNameMatcher.MATCHES_ALL); analyser.analyse(document); - assertTrue(document.getMetaData().detectedLicense()); + assertThat(document.getMetaData().detectedLicense()).isTrue(); document = new FileDocument(basedir, Resources.getExampleResource("exampleData/sub/Empty.txt"), DocumentNameMatcher.MATCHES_ALL); analyser.analyse(document); - assertFalse(document.getMetaData().detectedLicense()); + assertThat(document.getMetaData().detectedLicense()).isFalse(); + } + + + @Test + public void testMultiplexer() throws Exception { + TestingDocumentAnalyser[] analysers = { + new TestingDocumentAnalyser(), + new TestingDocumentAnalyser(), + new TestingDocumentAnalyser() + }; + IDocumentAnalyser multiplexer = AnalyserFactory.createMultiplexer(analysers); + TestingDocument document = new TestingDocument(); + + multiplexer.analyse(document); + for (int i=0; i < analysers.length; i++) { + assertThat(analysers[i].matches.size()).as("Matcher "+i).isEqualTo(1); + assertThat(analysers[i].matches.get(0)).as("Matcher "+i).isEqualTo(document); + } } } diff --git a/apache-rat-core/src/test/java/org/apache/rat/analysis/HeaderCheckWorkerTest.java b/apache-rat-core/src/test/java/org/apache/rat/analysis/HeaderCheckWorkerTest.java index 8d9eb3fe3..6e424032f 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/analysis/HeaderCheckWorkerTest.java +++ b/apache-rat-core/src/test/java/org/apache/rat/analysis/HeaderCheckWorkerTest.java @@ -23,6 +23,7 @@ import java.util.Collections; import java.util.stream.Collectors; +import org.apache.rat.Defaults; import org.apache.rat.api.Document; import org.apache.rat.configuration.builders.AnyBuilder; import org.apache.rat.license.ILicenseFamily; @@ -40,6 +41,7 @@ public class HeaderCheckWorkerTest { @Test public void emptyInputIsUnknownTest() throws Exception { final Document subject = new TestingDocument("subject"); + subject.getMetaData().setApprovalPredicate(Defaults.builder().build().getLicenseSetFactory().getApprovedLicensePredicate()); ILicense matcher = new TestingLicense("test", "test"); HeaderCheckWorker worker = new HeaderCheckWorker(new TestingMatcher(), new StringReader(""), Lists.list(matcher), subject); worker.read(); diff --git a/apache-rat-core/src/test/java/org/apache/rat/analysis/PolicyTest.java b/apache-rat-core/src/test/java/org/apache/rat/analysis/PolicyTest.java new file mode 100644 index 000000000..69f165b52 --- /dev/null +++ b/apache-rat-core/src/test/java/org/apache/rat/analysis/PolicyTest.java @@ -0,0 +1,171 @@ +///* +// * 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.analysis; +// +//import static org.junit.jupiter.api.Assertions.assertEquals; +//import static org.junit.jupiter.api.Assertions.assertNotNull; +// +//import java.util.SortedSet; +//import java.util.TreeSet; +// +//import org.apache.rat.Defaults; +//import org.apache.rat.api.Document; +//import org.apache.rat.document.IDocumentAnalyser; +//import org.apache.rat.license.ILicenseFamily; +//import org.apache.rat.license.LicenseSetFactory; +//import org.apache.rat.license.LicenseSetFactory.LicenseFilter; +//import org.apache.rat.testhelpers.TestingLicense; +//import org.apache.rat.testhelpers.TestingDocument; +//import org.apache.rat.testhelpers.TestingMatcher; +//import org.junit.jupiter.api.BeforeEach; +//import org.junit.jupiter.api.Test; +// +///** +// * Tests the Default policy implementation. +// */ +//public class PolicyTest { +// /** +// * This is the number of accepted licenses in the default license file : +// * /org/apache/rat/default.xml +// */ +// private static final int NUMBER_OF_DEFAULT_ACCEPTED_LICENSES = 7; +// +// private static final ILicenseFamily[] APPROVED_FAMILIES = { // +// makeFamily("AL", "Apache License Version 2.0"), +// makeFamily("BSD-3", "BSD 3 clause"), +// makeFamily("CDDL1", "COMMON DEVELOPMENT AND DISTRIBUTION LICENSE Version 1.0"), +// makeFamily("MIT", "The MIT License"), +// makeFamily("OASIS", "OASIS Open License"), makeFamily("W3CD", "W3C Document Copyright"), +// makeFamily("W3C", "W3C Software Copyright"), }; +// +// private Document document; +// private IDocumentAnalyser policy; +// private Defaults defaults; +// private LicenseSetFactory licenseSetFactory; +// +// @BeforeEach +// public void setUp() { +// defaults = Defaults.builder().build(); +// licenseSetFactory = defaults.getLicenseSetFactory(); +// policy = AnalyserFactory.createPolicy(licenseSetFactory); +// document = new TestingDocument("subject"); +// } +// +// private void assertApproval(boolean pApproved) { +// boolean state = document.getMetaData().approvedLicenses().findAny().isPresent(); +// assertEquals(pApproved, state); +// } +// +// private void setMetadata(ILicenseFamily family) { +// document.getMetaData().reportOnLicense(new TestingLicense(family.getFamilyCategory().trim(), new TestingMatcher(), family)); +// } +// +// private static ILicenseFamily makeFamily(String category, String name) { +// return ILicenseFamily.builder().setLicenseFamilyCategory(category).setLicenseFamilyName(name).build(); +// } +// +// @Test +// public void testCount() { +// assertEquals(NUMBER_OF_DEFAULT_ACCEPTED_LICENSES, policy.getApprovedLicenseFamilies().size()); +// } +// +// @Test +// public void testApprovedLicenses() { +// +// assertEquals(NUMBER_OF_DEFAULT_ACCEPTED_LICENSES, APPROVED_FAMILIES.length, "Approved license count mismatch"); +// for (ILicenseFamily family : APPROVED_FAMILIES) { +// setMetadata(family); +// policy.analyse(document); +// assertApproval(true); +// } +// } +// +// @Test +// public void testUnApprovedLicenses() { +// SortedSet all = defaults.getLicenseSetFactory().getLicenseFamilies(LicenseFilter.ALL); +// SortedSet unapproved = new TreeSet<>(); +// unapproved.addAll(all); +// unapproved.removeAll(defaults.getLicenseSetFactory().getLicenseFamilies(LicenseFilter.APPROVED)); +// +// assertEquals(all.size() - NUMBER_OF_DEFAULT_ACCEPTED_LICENSES, unapproved.size(), +// "Unapproved license count mismatch"); +// for (ILicenseFamily family : unapproved) { +// setMetadata(family); +// policy.analyse(document); +// assertApproval(false); +// } +// } +// +// @Test +// public void testUnknownFamily() { +// setMetadata(makeFamily("?????", "Unknown document")); +// policy.analyse(document); +// assertApproval(false); +// } +// +// @Test +// public void testAddNewApprovedLicenseAndDefaults() { +// ILicenseFamily testingFamily = makeFamily("test", "Testing License Family"); +// setMetadata(testingFamily); +// policy.analyse(document); +// assertApproval(false); +// +// licenseSetFactory.approveLicenseCategory(testingFamily.getFamilyCategory()); +// policy.analyse(document); +// assertApproval(true); +// } +// +// @Test +// public void testAddNewApprovedLicenseNoDefaults() { +// LicenseSetFactory licenseSetFactory = new LicenseSetFactory(); +// policy = new DefaultPolicy(licenseSetFactory); +// assertEquals(0, policy.getApprovedLicenseFamilies().size()); +// ILicenseFamily testingFamily = makeFamily("test", "Testing License Family"); +// setMetadata(testingFamily); +// policy.analyse(document); +// assertApproval(false); +// +// licenseSetFactory.addLicense(new TestingLicense(testingFamily.getFamilyCategory().trim(), new TestingMatcher(), testingFamily)); +// licenseSetFactory.approveLicenseCategory(testingFamily.getFamilyCategory()); +// assertEquals(1, policy.getApprovedLicenseFamilies().size()); +// assertNotNull(LicenseSetFactory.familySearch(testingFamily, policy.getApprovedLicenseFamilies()), +// "Did not properly add ILicenseFamily"); +// policy.analyse(document); +// assertApproval(true); +// } +// +// @Test +// public void testNonStandardDocumentsDoNotFailLicenseTests() { +// Document.Type[] nonStandardDocuments = { Document.Type.NOTICE, Document.Type.ARCHIVE, Document.Type.BINARY }; +// +// for (Document.Type d : nonStandardDocuments) { +// document = new TestingDocument("subject"); +// document.getMetaData().setDocumentType(d); +// policy.analyse(document); +// assertEquals(0, document.getMetaData().licenses().count(), "failed on " + d); +// } +// } +// +// @Test +// public void testUnclassifiedDocumentsDoNotFailLicenseTests() { +// document.getMetaData().setDocumentType(Document.Type.STANDARD); +// policy.analyse(document); +// assertApproval(false); +// } +//} diff --git a/apache-rat-core/src/test/java/org/apache/rat/document/DocumentAnalyserMultiplexerTest.java b/apache-rat-core/src/test/java/org/apache/rat/document/DocumentAnalyserMultiplexerTest.java deleted file mode 100644 index fb3e99409..000000000 --- a/apache-rat-core/src/test/java/org/apache/rat/document/DocumentAnalyserMultiplexerTest.java +++ /dev/null @@ -1,60 +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.document; - -import org.apache.rat.testhelpers.TestingDocument; -import org.apache.rat.testhelpers.TestingDocumentAnalyser; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -public class DocumentAnalyserMultiplexerTest { - - private DocumentAnalyserMultiplexer multiplexer; - private IDocumentAnalyser[] analysers; - private TestingDocument document; - - @BeforeEach - public void setUp() { - IDocumentAnalyser[] analysers = { - new TestingDocumentAnalyser(), - new TestingDocumentAnalyser(), - new TestingDocumentAnalyser() - }; - this.analysers = analysers; - document = new TestingDocument(); - multiplexer = new DocumentAnalyserMultiplexer(analysers); - } - - @Test - public void testAnalyse() throws Exception { - multiplexer.analyse(document); - TestingDocumentAnalyser analyser = (TestingDocumentAnalyser) (analysers[0]); - assertEquals(1, analyser.matches.size(),"Call made to analyser"); - assertEquals( document, analyser.matches.get(0), "Call made to analyser"); - analyser = (TestingDocumentAnalyser) (analysers[1]); - assertEquals(1, analyser.matches.size(), "Call made to analyser"); - assertEquals(document, analyser.matches.get(0), "Call made to analyser"); - analyser = (TestingDocumentAnalyser) (analysers[2]); - assertEquals( 1, analyser.matches.size()); - assertEquals( document, analyser.matches.get(0),"Call made to analyser"); - } - -} diff --git a/apache-rat-core/src/test/java/org/apache/rat/license/LicenseSetFactoryTest.java b/apache-rat-core/src/test/java/org/apache/rat/license/LicenseSetFactoryTest.java new file mode 100644 index 000000000..f847da87d --- /dev/null +++ b/apache-rat-core/src/test/java/org/apache/rat/license/LicenseSetFactoryTest.java @@ -0,0 +1,193 @@ +/* + * 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.license; + +import java.util.SortedSet; +import org.apache.rat.Defaults; +import org.apache.rat.analysis.UnknownLicense; +import org.apache.rat.testhelpers.TestingLicense; +import org.apache.rat.testhelpers.TestingMatcher; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class LicenseSetFactoryTest { + + /** + * This is the number of accepted licenses in the default license file : + * /org/apache/rat/default.xml + */ + private static final int NUMBER_OF_DEFAULT_ACCEPTED_LICENSES = 7; + + private static final ILicenseFamily[] APPROVED_FAMILIES = { // + makeFamily("AL", "Apache License Version 2.0"), + makeFamily("BSD-3", "BSD 3 clause"), + makeFamily("CDDL1", "COMMON DEVELOPMENT AND DISTRIBUTION LICENSE Version 1.0"), + makeFamily("MIT", "The MIT License"), + makeFamily("OASIS", "OASIS Open License"), + makeFamily("W3CD", "W3C Document Copyright"), + makeFamily("W3C", "W3C Software Copyright"),}; + + private Defaults defaults; + private LicenseSetFactory licenseSetFactory; + + @BeforeEach + public void setUp() { + defaults = Defaults.builder().build(); + licenseSetFactory = defaults.getLicenseSetFactory(); + assertThat(licenseSetFactory.getLicenseFamilies(LicenseSetFactory.LicenseFilter.APPROVED).size()).isEqualTo(NUMBER_OF_DEFAULT_ACCEPTED_LICENSES); + } + + private static ILicenseFamily makeFamily(String category, String name) { + return ILicenseFamily.builder().setLicenseFamilyCategory(category).setLicenseFamilyName(name).build(); + } + + private SortedSet getUnapprovedLicenseFamilies() { + SortedSet unapproved = defaults.getLicenseSetFactory().getLicenseFamilies(LicenseSetFactory.LicenseFilter.ALL); + for (ILicenseFamily family : APPROVED_FAMILIES) { + unapproved.remove(family); + } + return unapproved; + } + + @Test + void testDefaultApprovedLicenses() { + assertThat(APPROVED_FAMILIES.length).as("Approved license count mismatch").isEqualTo(NUMBER_OF_DEFAULT_ACCEPTED_LICENSES); + for (ILicenseFamily family : APPROVED_FAMILIES) { + TestingLicense license = new TestingLicense(family.getFamilyCategory().trim(), new TestingMatcher(), family); + assertThat(licenseSetFactory.getApprovedLicensePredicate().test(license)) + .as("Did not approve family " + family) + .isTrue(); + } + } + + @Test + void testDefaultUnApprovedLicenses() { + SortedSet unapproved = getUnapprovedLicenseFamilies(); + + for (ILicenseFamily family : unapproved) { + TestingLicense license = new TestingLicense(family.getFamilyCategory().trim(), new TestingMatcher(), family); + assertThat(licenseSetFactory.getApprovedLicensePredicate().test(license)) + .as("Did not find unapproved family " + family) + .isFalse(); + } + } + + @Test + public void testUnknownFamily() { + ILicenseFamily family = makeFamily("?????", "Unknown document"); + TestingLicense license = new TestingLicense(family.getFamilyCategory().trim(), new TestingMatcher(), family); + + assertThat(licenseSetFactory.getApprovedLicensePredicate().test(license)) + .as("Did not find unapproved family " + family) + .isFalse(); + + + assertThat(licenseSetFactory.getApprovedLicensePredicate().test(UnknownLicense.INSTANCE)) + .as("Did not find UnknownLicense.INSTANCE") + .isFalse(); + } + + @Test + void testLicenseCategoryManipulation() { + ILicenseFamily family = makeFamily("test", "Testing License Family"); + TestingLicense license = new TestingLicense(family.getFamilyCategory().trim(), new TestingMatcher(), family); + + assertThat(licenseSetFactory.getApprovedLicensePredicate().test(license)) + .as("Found unapproved family " + family) + .isFalse(); + + licenseSetFactory.approveLicenseCategory(family.getFamilyCategory()); + assertThat(licenseSetFactory.getApprovedLicensePredicate().test(license)) + .as("Did not find approved family " + family) + .isTrue(); + + licenseSetFactory.removeLicenseCategory(family.getFamilyCategory()); + assertThat(licenseSetFactory.getApprovedLicensePredicate().test(license)) + .as("Found unapproved family " + family) + .isFalse(); + + } + + @Test + void testAddNewApprovedLicenseNoDefaults() { + licenseSetFactory = new LicenseSetFactory(); + assertThat(licenseSetFactory.getLicenseFamilies(LicenseSetFactory.LicenseFilter.APPROVED).size()) + .isEqualTo(0); + + ILicenseFamily family = makeFamily("test", "Testing License Family"); + TestingLicense license = new TestingLicense(family.getFamilyCategory().trim(), new TestingMatcher(), family); + assertThat(licenseSetFactory.getApprovedLicensePredicate().test(license)) + .as("Did found unapproved family " + family) + .isFalse(); + + licenseSetFactory.addLicense(license); + licenseSetFactory.approveLicenseCategory(family.getFamilyCategory()); + assertThat(licenseSetFactory.getLicenseFamilies(LicenseSetFactory.LicenseFilter.APPROVED).size()) + .isEqualTo(1); + assertThat(licenseSetFactory.getApprovedLicensePredicate().test(license)) + .as("Did not find approved family " + family) + .isTrue(); + } + + @Test + void testLicenseIDManipulation() { + String licenseId = "customId"; + ILicenseFamily family = getUnapprovedLicenseFamilies().first(); + TestingLicense license = new TestingLicense(licenseId, new TestingMatcher(), family); + + assertThat(licenseSetFactory.getApprovedLicensePredicate().test(license)) + .as("Found unapproved id " + licenseId) + .isFalse(); + + licenseSetFactory.approveLicenseId(licenseId); + assertThat(licenseSetFactory.getApprovedLicensePredicate().test(license)) + .as("Did not find approved id " + licenseId) + .isTrue(); + + licenseSetFactory.removeLicenseId(licenseId); + assertThat(licenseSetFactory.getApprovedLicensePredicate().test(license)) + .as("Found unapproved id " + licenseId) + .isFalse(); + + } + +// @Test +// public void testNonStandardDocumentsDoNotFailLicenseTests() { +// Document.Type[] nonStandardDocuments = { Document.Type.NOTICE, Document.Type.ARCHIVE, Document.Type.BINARY }; +// for (Document.Type d : nonStandardDocuments) { +// document = new TestingDocument("subject"); +// document.getMetaData().setDocumentType(d); +// document.getMetaData().setApprovalPredicate(licenseSetFactory.getApprovedLicensePredicate()); +// for (ILicenseFamily family : APPROVED_FAMILIES) { +// TestingLicense license = new TestingLicense(family.getFamilyCategory().trim(), new TestingMatcher(), family); +// assertThat(document.getMetaData().isApproved(license)).isFalse(); +// } +// } +// } +// +// @Test +// public void testUnclassifiedDocumentsDoNotFailLicenseTests() { +// document.getMetaData().setDocumentType(Document.Type.STANDARD); +// policy.analyse(document); +// assertApproval(false); +// } + +} diff --git a/apache-rat-core/src/test/java/org/apache/rat/policy/DefaultPolicyTest.java b/apache-rat-core/src/test/java/org/apache/rat/policy/DefaultPolicyTest.java deleted file mode 100644 index 6e44cfc4c..000000000 --- a/apache-rat-core/src/test/java/org/apache/rat/policy/DefaultPolicyTest.java +++ /dev/null @@ -1,170 +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.policy; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; - -import java.util.SortedSet; -import java.util.TreeSet; - -import org.apache.rat.Defaults; -import org.apache.rat.api.Document; -import org.apache.rat.license.ILicenseFamily; -import org.apache.rat.license.LicenseSetFactory; -import org.apache.rat.license.LicenseSetFactory.LicenseFilter; -import org.apache.rat.testhelpers.TestingLicense; -import org.apache.rat.testhelpers.TestingDocument; -import org.apache.rat.testhelpers.TestingMatcher; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; - -/** - * Tests the Default policy implementation. - */ -public class DefaultPolicyTest { - /** - * This is the number of accepted licenses in the default license file : - * /org/apache/rat/default.xml - */ - private static final int NUMBER_OF_DEFAULT_ACCEPTED_LICENSES = 7; - - private static final ILicenseFamily[] APPROVED_FAMILIES = { // - makeFamily("AL", "Apache License Version 2.0"), - makeFamily("BSD-3", "BSD 3 clause"), - makeFamily("CDDL1", "COMMON DEVELOPMENT AND DISTRIBUTION LICENSE Version 1.0"), - makeFamily("MIT", "The MIT License"), - makeFamily("OASIS", "OASIS Open License"), makeFamily("W3CD", "W3C Document Copyright"), - makeFamily("W3C", "W3C Software Copyright"), }; - - private Document document; - private DefaultPolicy policy; - private Defaults defaults; - private LicenseSetFactory licenseSetFactory; - - @BeforeEach - public void setUp() { - defaults = Defaults.builder().build(); - licenseSetFactory = defaults.getLicenseSetFactory(); - policy = new DefaultPolicy(licenseSetFactory); - document = new TestingDocument("subject"); - } - - private void assertApproval(boolean pApproved) { - boolean state = document.getMetaData().approvedLicenses().findAny().isPresent(); - assertEquals(pApproved, state); - } - - private void setMetadata(ILicenseFamily family) { - document.getMetaData().reportOnLicense(new TestingLicense(family.getFamilyCategory().trim(), new TestingMatcher(), family)); - } - - private static ILicenseFamily makeFamily(String category, String name) { - return ILicenseFamily.builder().setLicenseFamilyCategory(category).setLicenseFamilyName(name).build(); - } - - @Test - public void testCount() { - assertEquals(NUMBER_OF_DEFAULT_ACCEPTED_LICENSES, policy.getApprovedLicenseFamilies().size()); - } - - @Test - public void testApprovedLicenses() { - - assertEquals(NUMBER_OF_DEFAULT_ACCEPTED_LICENSES, APPROVED_FAMILIES.length, "Approved license count mismatch"); - for (ILicenseFamily family : APPROVED_FAMILIES) { - setMetadata(family); - policy.analyse(document); - assertApproval(true); - } - } - - @Test - public void testUnApprovedLicenses() { - SortedSet all = defaults.getLicenseSetFactory().getLicenseFamilies(LicenseFilter.ALL); - SortedSet unapproved = new TreeSet<>(); - unapproved.addAll(all); - unapproved.removeAll(defaults.getLicenseSetFactory().getLicenseFamilies(LicenseFilter.APPROVED)); - - assertEquals(all.size() - NUMBER_OF_DEFAULT_ACCEPTED_LICENSES, unapproved.size(), - "Unapproved license count mismatch"); - for (ILicenseFamily family : unapproved) { - setMetadata(family); - policy.analyse(document); - assertApproval(false); - } - } - - @Test - public void testUnknownFamily() { - setMetadata(makeFamily("?????", "Unknown document")); - policy.analyse(document); - assertApproval(false); - } - - @Test - public void testAddNewApprovedLicenseAndDefaults() { - ILicenseFamily testingFamily = makeFamily("test", "Testing License Family"); - setMetadata(testingFamily); - policy.analyse(document); - assertApproval(false); - - licenseSetFactory.approveLicenseCategory(testingFamily.getFamilyCategory()); - policy.analyse(document); - assertApproval(true); - } - - @Test - public void testAddNewApprovedLicenseNoDefaults() { - LicenseSetFactory licenseSetFactory = new LicenseSetFactory(); - policy = new DefaultPolicy(licenseSetFactory); - assertEquals(0, policy.getApprovedLicenseFamilies().size()); - ILicenseFamily testingFamily = makeFamily("test", "Testing License Family"); - setMetadata(testingFamily); - policy.analyse(document); - assertApproval(false); - - licenseSetFactory.addLicense(new TestingLicense(testingFamily.getFamilyCategory().trim(), new TestingMatcher(), testingFamily)); - licenseSetFactory.approveLicenseCategory(testingFamily.getFamilyCategory()); - assertEquals(1, policy.getApprovedLicenseFamilies().size()); - assertNotNull(LicenseSetFactory.familySearch(testingFamily, policy.getApprovedLicenseFamilies()), - "Did not properly add ILicenseFamily"); - policy.analyse(document); - assertApproval(true); - } - - @Test - public void testNonStandardDocumentsDoNotFailLicenseTests() { - Document.Type[] nonStandardDocuments = { Document.Type.NOTICE, Document.Type.ARCHIVE, Document.Type.BINARY }; - - for (Document.Type d : nonStandardDocuments) { - document = new TestingDocument("subject"); - document.getMetaData().setDocumentType(d); - policy.analyse(document); - assertEquals(0, document.getMetaData().licenses().count(), "failed on " + d); - } - } - - @Test - public void testUnclassifiedDocumentsDoNotFailLicenseTests() { - document.getMetaData().setDocumentType(Document.Type.STANDARD); - policy.analyse(document); - assertApproval(false); - } -} From 6a5b17caa60f9e959831170cf9cede676e092606 Mon Sep 17 00:00:00 2001 From: "P. Ottlinger" Date: Sun, 15 Dec 2024 19:27:13 +0100 Subject: [PATCH 12/22] RAT-469: Minor fixes --- .../java/org/apache/rat/analysis/AnalyserFactory.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apache-rat-core/src/main/java/org/apache/rat/analysis/AnalyserFactory.java b/apache-rat-core/src/main/java/org/apache/rat/analysis/AnalyserFactory.java index 300cf5022..5553433be 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/analysis/AnalyserFactory.java +++ b/apache-rat-core/src/main/java/org/apache/rat/analysis/AnalyserFactory.java @@ -45,7 +45,7 @@ private AnalyserFactory() { } /** - * Creates an anlayser that adds the approved license predicate to the document metadata. + * Creates an analyser that adds the approved license predicate to the document metadata. *

    * Note you probably do not want this as it is automatically added to {@link #createConfiguredAnalyser}. *

    @@ -74,9 +74,9 @@ public static IDocumentAnalyser createMultiplexer(final IDocumentAnalyser... ana } /** - * Creates a DocumentAnalyser from teh report configuraiton. + * Creates a DocumentAnalyser from the report configuration. * @param configuration the ReportConfiguration - * @return A document analyser that uses the provides licenses. + * @return A document analyser that uses the provided licenses. */ public static IDocumentAnalyser createConfiguredAnalyser(final ReportConfiguration configuration) { LicenseSetFactory licenseSetFactory = configuration.getLicenseSetFactory(); @@ -85,7 +85,7 @@ public static IDocumentAnalyser createConfiguredAnalyser(final ReportConfigurati throw new ConfigurationException("At least one license must be defined"); } if (DefaultLog.getInstance().isEnabled(Log.Level.DEBUG)) { - DefaultLog.getInstance().debug("Currently active Licenses are:"); + DefaultLog.getInstance().debug("Currently active licenses are:"); licenses.forEach(DefaultLog.getInstance()::debug); } return createMultiplexer(createPolicy(licenseSetFactory.getApprovedLicensePredicate()), From b35df25bb6b11c0426301328650b8ff4aa4f8314 Mon Sep 17 00:00:00 2001 From: "P. Ottlinger" Date: Sun, 15 Dec 2024 19:30:05 +0100 Subject: [PATCH 13/22] RAT-469: Remove hungarian notation --- .../org/apache/rat/analysis/AnalyserFactory.java | 12 ++++++------ .../apache/rat/analysis/DocumentHeaderAnalyser.java | 4 ++-- ...{IDocumentAnalyser.java => DocumentAnalyser.java} | 2 +- .../rat/report/claim/ClaimReporterMultiplexer.java | 6 +++--- .../org/apache/rat/analysis/AnalyserFactoryTest.java | 12 ++++-------- .../rat/testhelpers/TestingDocumentAnalyser.java | 4 ++-- 6 files changed, 18 insertions(+), 22 deletions(-) rename apache-rat-core/src/main/java/org/apache/rat/document/{IDocumentAnalyser.java => DocumentAnalyser.java} (97%) diff --git a/apache-rat-core/src/main/java/org/apache/rat/analysis/AnalyserFactory.java b/apache-rat-core/src/main/java/org/apache/rat/analysis/AnalyserFactory.java index 5553433be..681adb47d 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/analysis/AnalyserFactory.java +++ b/apache-rat-core/src/main/java/org/apache/rat/analysis/AnalyserFactory.java @@ -27,7 +27,7 @@ import org.apache.rat.ReportConfiguration; import org.apache.rat.api.Document; import org.apache.rat.api.RatException; -import org.apache.rat.document.IDocumentAnalyser; +import org.apache.rat.document.DocumentAnalyser; import org.apache.rat.document.RatDocumentAnalysisException; import org.apache.rat.license.ILicense; import org.apache.rat.license.LicenseSetFactory; @@ -52,7 +52,7 @@ private AnalyserFactory() { * @param approvalPredicate the predicate to approve licenses. * @return A document analyser that sets the approvalPredicate in document metadata. */ - public static IDocumentAnalyser createPolicy(final Predicate approvalPredicate) { + public static DocumentAnalyser createPolicy(final Predicate approvalPredicate) { return document -> { if (document != null) { document.getMetaData().setApprovalPredicate(approvalPredicate); @@ -65,9 +65,9 @@ public static IDocumentAnalyser createPolicy(final Predicate approvalP * @param analysers the array of analysers to call. * @return an analyser that will call all the provided analysers. */ - public static IDocumentAnalyser createMultiplexer(final IDocumentAnalyser... analysers) { + public static DocumentAnalyser createMultiplexer(final DocumentAnalyser... analysers) { return document -> { - for (IDocumentAnalyser analyser : analysers) { + for (DocumentAnalyser analyser : analysers) { analyser.analyse(document); } }; @@ -78,7 +78,7 @@ public static IDocumentAnalyser createMultiplexer(final IDocumentAnalyser... ana * @param configuration the ReportConfiguration * @return A document analyser that uses the provided licenses. */ - public static IDocumentAnalyser createConfiguredAnalyser(final ReportConfiguration configuration) { + public static DocumentAnalyser createConfiguredAnalyser(final ReportConfiguration configuration) { LicenseSetFactory licenseSetFactory = configuration.getLicenseSetFactory(); Set licenses = licenseSetFactory.getLicenses(LicenseSetFactory.LicenseFilter.ALL); if (licenses.isEmpty()) { @@ -95,7 +95,7 @@ public static IDocumentAnalyser createConfiguredAnalyser(final ReportConfigurati /** * A DocumentAnalyser a collection of licenses. */ - private static final class DefaultAnalyser implements IDocumentAnalyser { + private static final class DefaultAnalyser implements DocumentAnalyser { /** The licenses to analyze */ private final Collection licenses; diff --git a/apache-rat-core/src/main/java/org/apache/rat/analysis/DocumentHeaderAnalyser.java b/apache-rat-core/src/main/java/org/apache/rat/analysis/DocumentHeaderAnalyser.java index f4b0812a2..cec174f6f 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/analysis/DocumentHeaderAnalyser.java +++ b/apache-rat-core/src/main/java/org/apache/rat/analysis/DocumentHeaderAnalyser.java @@ -23,7 +23,7 @@ import java.util.Collection; import org.apache.rat.api.Document; -import org.apache.rat.document.IDocumentAnalyser; +import org.apache.rat.document.DocumentAnalyser; import org.apache.rat.license.ILicense; import org.apache.rat.utils.DefaultLog; @@ -32,7 +32,7 @@ /** * A Document analyzer that analyses document headers for a license. */ -class DocumentHeaderAnalyser implements IDocumentAnalyser { +class DocumentHeaderAnalyser implements DocumentAnalyser { /** The license to analyse */ private final Collection licenses; diff --git a/apache-rat-core/src/main/java/org/apache/rat/document/IDocumentAnalyser.java b/apache-rat-core/src/main/java/org/apache/rat/document/DocumentAnalyser.java similarity index 97% rename from apache-rat-core/src/main/java/org/apache/rat/document/IDocumentAnalyser.java rename to apache-rat-core/src/main/java/org/apache/rat/document/DocumentAnalyser.java index a5f0cee10..69d4ed3c2 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/document/IDocumentAnalyser.java +++ b/apache-rat-core/src/main/java/org/apache/rat/document/DocumentAnalyser.java @@ -24,7 +24,7 @@ * Analyses Documents. */ @FunctionalInterface -public interface IDocumentAnalyser { +public interface DocumentAnalyser { /** * Analyse the specified document. * @param document the document to analyze diff --git a/apache-rat-core/src/main/java/org/apache/rat/report/claim/ClaimReporterMultiplexer.java b/apache-rat-core/src/main/java/org/apache/rat/report/claim/ClaimReporterMultiplexer.java index 2b73cf4b5..5f60284ab 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/report/claim/ClaimReporterMultiplexer.java +++ b/apache-rat-core/src/main/java/org/apache/rat/report/claim/ClaimReporterMultiplexer.java @@ -23,7 +23,7 @@ import org.apache.rat.api.Document; import org.apache.rat.api.RatException; -import org.apache.rat.document.IDocumentAnalyser; +import org.apache.rat.document.DocumentAnalyser; import org.apache.rat.document.RatDocumentAnalysisException; import org.apache.rat.report.RatReport; import org.apache.rat.report.xml.XmlElements; @@ -34,7 +34,7 @@ */ public class ClaimReporterMultiplexer implements RatReport { /** The document analyser to use */ - private final IDocumentAnalyser analyser; + private final DocumentAnalyser analyser; /** A list of reports that are being updated */ private final List reporters; /** If {@code true} this is a dry run do not generate report */ @@ -48,7 +48,7 @@ public class ClaimReporterMultiplexer implements RatReport { * @param analyser the analyser to use. * @param reporters the reports to execute. */ - public ClaimReporterMultiplexer(final IXmlWriter writer, final boolean dryRun, final IDocumentAnalyser analyser, + public ClaimReporterMultiplexer(final IXmlWriter writer, final boolean dryRun, final DocumentAnalyser analyser, final List reporters) { this.analyser = analyser; this.reporters = reporters; diff --git a/apache-rat-core/src/test/java/org/apache/rat/analysis/AnalyserFactoryTest.java b/apache-rat-core/src/test/java/org/apache/rat/analysis/AnalyserFactoryTest.java index d5b348335..f848d4e4a 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/analysis/AnalyserFactoryTest.java +++ b/apache-rat-core/src/test/java/org/apache/rat/analysis/AnalyserFactoryTest.java @@ -31,7 +31,7 @@ import org.apache.rat.ReportConfiguration; import org.apache.rat.api.Document; import org.apache.rat.document.DocumentNameMatcher; -import org.apache.rat.document.IDocumentAnalyser; +import org.apache.rat.document.DocumentAnalyser; import org.apache.rat.document.DocumentName; import org.apache.rat.document.FileDocument; import org.apache.rat.document.RatDocumentAnalysisException; @@ -54,7 +54,7 @@ public class AnalyserFactoryTest { private StringWriter out; private SimpleXmlClaimReporter reporter; - private IDocumentAnalyser analyser; + private DocumentAnalyser analyser; AnalyserFactoryTest() { basedir = DocumentName.builder(new File(Files.currentFolder(), Resources.SRC_TEST_RESOURCES)).build(); @@ -146,7 +146,6 @@ public void missingFileTest() { assertThatThrownBy(() -> analyser.analyse(document)).isInstanceOf(RatDocumentAnalysisException.class); } - @Test public void archiveTypeAnalyser() throws Exception { final Document document = new FileDocument(basedir, @@ -200,7 +199,6 @@ public void RAT147_windows_Test() throws Exception { @Test public void standardNotificationTest() throws Exception { - Defaults defaults = Defaults.builder().build(); ReportConfiguration config = new ReportConfiguration(); config.setFrom(defaults); @@ -220,7 +218,6 @@ public void standardNotificationTest() throws Exception { @Test public void standardAbsenceTest() throws Exception { - Defaults defaults = Defaults.builder().build(); ReportConfiguration config = new ReportConfiguration(); config.setFrom(defaults); @@ -257,7 +254,6 @@ public void standardPresenceTest() throws Exception { assertThat(document.getMetaData().detectedLicense()).isFalse(); } - @Test public void testMultiplexer() throws Exception { TestingDocumentAnalyser[] analysers = { @@ -265,11 +261,11 @@ public void testMultiplexer() throws Exception { new TestingDocumentAnalyser(), new TestingDocumentAnalyser() }; - IDocumentAnalyser multiplexer = AnalyserFactory.createMultiplexer(analysers); + DocumentAnalyser multiplexer = AnalyserFactory.createMultiplexer(analysers); TestingDocument document = new TestingDocument(); multiplexer.analyse(document); - for (int i=0; i < analysers.length; i++) { + for (int i = 0; i < analysers.length; i++) { assertThat(analysers[i].matches.size()).as("Matcher "+i).isEqualTo(1); assertThat(analysers[i].matches.get(0)).as("Matcher "+i).isEqualTo(document); } diff --git a/apache-rat-core/src/test/java/org/apache/rat/testhelpers/TestingDocumentAnalyser.java b/apache-rat-core/src/test/java/org/apache/rat/testhelpers/TestingDocumentAnalyser.java index f8c78d492..a4daaba08 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/testhelpers/TestingDocumentAnalyser.java +++ b/apache-rat-core/src/test/java/org/apache/rat/testhelpers/TestingDocumentAnalyser.java @@ -22,9 +22,9 @@ import java.util.List; import org.apache.rat.api.Document; -import org.apache.rat.document.IDocumentAnalyser; +import org.apache.rat.document.DocumentAnalyser; -public class TestingDocumentAnalyser implements IDocumentAnalyser { +public class TestingDocumentAnalyser implements DocumentAnalyser { public final List matches = new ArrayList<>(); From 83ac502f3e4b485198773e6a81be97c48f9edd10 Mon Sep 17 00:00:00 2001 From: "P. Ottlinger" Date: Tue, 17 Dec 2024 17:01:21 +0100 Subject: [PATCH 14/22] RAT-469: Minor fixes --- .../java/org/apache/rat/license/LicenseSetFactoryTest.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/apache-rat-core/src/test/java/org/apache/rat/license/LicenseSetFactoryTest.java b/apache-rat-core/src/test/java/org/apache/rat/license/LicenseSetFactoryTest.java index f847da87d..21f564be2 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/license/LicenseSetFactoryTest.java +++ b/apache-rat-core/src/test/java/org/apache/rat/license/LicenseSetFactoryTest.java @@ -123,7 +123,6 @@ void testLicenseCategoryManipulation() { assertThat(licenseSetFactory.getApprovedLicensePredicate().test(license)) .as("Found unapproved family " + family) .isFalse(); - } @Test @@ -135,7 +134,7 @@ void testAddNewApprovedLicenseNoDefaults() { ILicenseFamily family = makeFamily("test", "Testing License Family"); TestingLicense license = new TestingLicense(family.getFamilyCategory().trim(), new TestingMatcher(), family); assertThat(licenseSetFactory.getApprovedLicensePredicate().test(license)) - .as("Did found unapproved family " + family) + .as("Did find unapproved family " + family) .isFalse(); licenseSetFactory.addLicense(license); From aee404265b9c004dce9d4978ad12b5f82a3becbf Mon Sep 17 00:00:00 2001 From: Claude Warren Date: Sun, 12 Jan 2025 14:24:54 +0000 Subject: [PATCH 15/22] fixed tests --- .../apache/rat/license/ILicenseFamily.java | 16 + .../apache/rat/license/LicenseSetFactory.java | 2 +- .../rat/analysis/DocumentAnalyserTest.java | 331 ++++++++++++++++++ .../org/apache/rat/analysis/PolicyTest.java | 171 --------- .../rat/testhelpers/TestingDocument.java | 26 +- .../rat/testhelpers/TestingLicense.java | 6 + apache-rat-plugin/.gitignore | 1 + 7 files changed, 376 insertions(+), 177 deletions(-) create mode 100644 apache-rat-core/src/test/java/org/apache/rat/analysis/DocumentAnalyserTest.java delete mode 100644 apache-rat-core/src/test/java/org/apache/rat/analysis/PolicyTest.java diff --git a/apache-rat-core/src/main/java/org/apache/rat/license/ILicenseFamily.java b/apache-rat-core/src/main/java/org/apache/rat/license/ILicenseFamily.java index 0681c1a32..56ba494c3 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/license/ILicenseFamily.java +++ b/apache-rat-core/src/main/java/org/apache/rat/license/ILicenseFamily.java @@ -136,6 +136,22 @@ public String getFamilyName() { public String getFamilyCategory() { return cat; } + + @Override + public boolean equals(final Object other) { + if (other == this) { + return true; + } + if (other instanceof ILicenseFamily) { + return this.getFamilyCategory().equals(((ILicenseFamily) other).getFamilyCategory()); + } + return false; + } + + @Override + public int hashCode() { + return getFamilyCategory().hashCode(); + } }; } } diff --git a/apache-rat-core/src/main/java/org/apache/rat/license/LicenseSetFactory.java b/apache-rat-core/src/main/java/org/apache/rat/license/LicenseSetFactory.java index 07301ee08..7556c57b8 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/license/LicenseSetFactory.java +++ b/apache-rat-core/src/main/java/org/apache/rat/license/LicenseSetFactory.java @@ -344,7 +344,7 @@ public SortedSet getLicenseCategories(final LicenseFilter filter) { return result; case APPROVED: approvedLicenseCategories.stream().filter(s -> !removedLicenseCategories.contains(s)).forEach(result::add); - licenses.stream().filter(approved).forEach(l -> result.add(l.getLicenseFamily().getFamilyCategory())); + //licenses.stream().filter(approved).forEach(l -> result.add(l.getLicenseFamily().getFamilyCategory())); families.stream().filter(this::isApprovedCategory).forEach(f -> result.add(f.getFamilyCategory())); return result; case NONE: diff --git a/apache-rat-core/src/test/java/org/apache/rat/analysis/DocumentAnalyserTest.java b/apache-rat-core/src/test/java/org/apache/rat/analysis/DocumentAnalyserTest.java new file mode 100644 index 000000000..f339a75c4 --- /dev/null +++ b/apache-rat-core/src/test/java/org/apache/rat/analysis/DocumentAnalyserTest.java @@ -0,0 +1,331 @@ +/* + * 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.analysis; + +import static org.assertj.core.api.Assertions.assertThat; + +import java.io.ByteArrayInputStream; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import java.util.stream.Collectors; +import java.util.stream.Stream; +import org.apache.rat.Defaults; +import org.apache.rat.ReportConfiguration; +import org.apache.rat.api.Document; +import org.apache.rat.document.DocumentAnalyser; +import org.apache.rat.document.RatDocumentAnalysisException; +import org.apache.rat.license.ILicense; +import org.apache.rat.license.ILicenseFamily; +import org.apache.rat.license.LicenseSetFactory; +import org.apache.rat.license.LicenseSetFactory.LicenseFilter; +import org.apache.rat.testhelpers.TestingLicense; +import org.apache.rat.testhelpers.TestingDocument; +import org.apache.rat.testhelpers.TestingMatcher; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; + +/** + * Tests the Default policy implementation. + */ +public class DocumentAnalyserTest { + + private Document document; + + private void assertApproval(boolean state) { + if (state) { + assertThat(document.getMetaData().approvedLicenses().findAny()).isPresent(); + } else { + assertThat(document.getMetaData().approvedLicenses().findAny()).isNotPresent(); + } + } + + private void setMetadata(Document document, ILicenseFamily family) { + document.getMetaData().reportOnLicense(new TestingLicense(family.getFamilyCategory().trim(), new TestingMatcher(), family)); + } + + private static ILicenseFamily makeFamily(String category, String name) { + return ILicenseFamily.builder().setLicenseFamilyCategory(category).setLicenseFamilyName(name).build(); + } + + private Collection asCategories(Collection families) { + return families.stream().map(ILicenseFamily::getFamilyCategory).collect(Collectors.toList()); + } + + @ParameterizedTest(name = "{index} {0}") + @MethodSource("defaultAnalyserTestData") + void licenseSetFactoryTest(String name, ReportConfiguration configuration, + Map> licenseFamilies, + Map> licenses + ) { + LicenseSetFactory factory = configuration.getLicenseSetFactory(); + + Collection categories = asCategories(licenseFamilies.get(LicenseFilter.APPROVED)); + assertThat(factory.getLicenseCategories(LicenseFilter.APPROVED)).containsExactlyInAnyOrderElementsOf(categories); + categories = asCategories(licenseFamilies.get(LicenseFilter.ALL)); + assertThat(factory.getLicenseCategories(LicenseFilter.ALL)).containsExactlyInAnyOrderElementsOf(categories); + assertThat(factory.getLicenseCategories(LicenseFilter.NONE)).isEmpty(); + + assertThat(factory.getLicenseFamilies(LicenseFilter.APPROVED)).containsExactlyInAnyOrderElementsOf(licenseFamilies.get(LicenseFilter.APPROVED)); + assertThat(factory.getLicenseFamilies(LicenseFilter.ALL)).containsExactlyInAnyOrderElementsOf(licenseFamilies.get(LicenseFilter.ALL)); + assertThat(factory.getLicenseFamilies(LicenseFilter.NONE)).isEmpty(); + + assertThat(factory.getLicenses(LicenseFilter.APPROVED)).containsExactlyInAnyOrderElementsOf(licenses.get(LicenseFilter.APPROVED)); + assertThat(factory.getLicenses(LicenseFilter.ALL)).containsExactlyInAnyOrderElementsOf(licenses.get(LicenseFilter.ALL)); + assertThat(factory.getLicenses(LicenseFilter.NONE)).isEmpty(); + } + + @ParameterizedTest(name = "{index} {0}") + @MethodSource("defaultAnalyserTestData") + void analyserTest(String name, ReportConfiguration configuration, + Map> licenseFamilies, + Map> licenses + ) throws RatDocumentAnalysisException { + DocumentAnalyser analyser = AnalyserFactory.createConfiguredAnalyser(configuration); + + // verify approved license families report approved. + for (ILicenseFamily family : licenseFamilies.get(LicenseFilter.APPROVED)) { + document = new TestingDocument(() -> new ByteArrayInputStream("Hello World".getBytes(StandardCharsets.UTF_8)), "subject"); + setMetadata(document, family); + analyser.analyse(document); + assertApproval(true); + } + + // verify licenses report approved as per the licenses argument + for (ILicense license : licenses.get(LicenseFilter.ALL)) { + document = new TestingDocument(() -> new ByteArrayInputStream("Hello World".getBytes(StandardCharsets.UTF_8)), "subject"); + document.getMetaData().reportOnLicense(license); + analyser.analyse(document); + assertApproval(licenses.get(LicenseFilter.APPROVED).contains(license)); + } + + // verify that the unknown license is not approved. + document = new TestingDocument(() -> new ByteArrayInputStream("Hello World".getBytes(StandardCharsets.UTF_8)), "subject"); + setMetadata(document, makeFamily("?????", "Unknown document")); + analyser.analyse(document); + assertApproval(false); + + // verify that the standard document without a license detected is not approved. + document = new TestingDocument(() -> new ByteArrayInputStream("Hello World".getBytes(StandardCharsets.UTF_8)), "subject"); + document.getMetaData().setDocumentType(Document.Type.STANDARD); + analyser.analyse(document); + assertApproval(false); + } + + private static List defaultApprovedLicenseFamilies() { + return Arrays.asList( + ILicenseFamily.builder().setLicenseFamilyCategory("AL").setLicenseFamilyName("Apache License Version 2.0").build(), + ILicenseFamily.builder().setLicenseFamilyCategory("CDDL1").setLicenseFamilyName("COMMON DEVELOPMENT AND DISTRIBUTION LICENSE Version 1.0").build(), + ILicenseFamily.builder().setLicenseFamilyCategory("MIT").setLicenseFamilyName("The MIT License").build(), + ILicenseFamily.builder().setLicenseFamilyCategory("OASIS").setLicenseFamilyName("OASIS Open License").build(), + ILicenseFamily.builder().setLicenseFamilyCategory("W3C").setLicenseFamilyName("W3C Software Copyright").build(), + ILicenseFamily.builder().setLicenseFamilyCategory("W3CD").setLicenseFamilyName("W3C Document Copyright").build(), + ILicenseFamily.builder().setLicenseFamilyCategory("BSD-3").setLicenseFamilyName("BSD 3 clause").build() + ); + } + + private static List defaultAllLicenseFamilies() { + List result = new ArrayList<>(defaultApprovedLicenseFamilies()); + result.add(ILicenseFamily.builder().setLicenseFamilyCategory("GPL").setLicenseFamilyName("GNU General Public License family").build()); + return result; + } + + private static List defaultApprovedLicenses() { + return Arrays.asList( + new TestingLicense("AL", "AL"), + new TestingLicense("AL", "ASL"), + new TestingLicense("BSD-3", "BSD-3"), + new TestingLicense("BSD-3", "DOJO"), + new TestingLicense("BSD-3", "TMF"), + new TestingLicense("CDDL1", "CDDL1"), + new TestingLicense("CDDL1", "ILLUMOS"), + new TestingLicense("MIT", "MIT"), + new TestingLicense("OASIS", "OASIS"), + new TestingLicense("W3C", "W3C"), + new TestingLicense("W3CD", "W3CD") + ); + } + + private static List defaultAllLicenses() { + List result = new ArrayList<>(defaultApprovedLicenses()); + result.addAll(Arrays.asList(new TestingLicense("GPL", "GPL1"), + new TestingLicense("GPL", "GPL2"), + new TestingLicense("GPL", "GPL3"))); + return result; + } + + private static Stream defaultAnalyserTestData() { + List lst = new ArrayList<>(); + Defaults defaults = Defaults.builder().build(); + + ReportConfiguration config = new ReportConfiguration(); + config.setFrom(defaults); + + // default setup + + Map> licenseFamilies = new HashMap<>(); + licenseFamilies.put(LicenseFilter.APPROVED, defaultApprovedLicenseFamilies()); + licenseFamilies.put(LicenseFilter.ALL, defaultAllLicenseFamilies()); + + Map> licenses = new HashMap<>(); + licenses.put(LicenseFilter.APPROVED, defaultApprovedLicenses()); + licenses.put(LicenseFilter.ALL, defaultAllLicenses()); + + lst.add(Arguments.of("default", config, licenseFamilies, licenses)); + + // GPL approved license id + config = new ReportConfiguration(); + config.setFrom(defaults); + config.addApprovedLicenseId("GPL1"); + + licenseFamilies = new HashMap<>(); + licenseFamilies.put(LicenseFilter.APPROVED, defaultApprovedLicenseFamilies()); + licenseFamilies.put(LicenseFilter.ALL, defaultAllLicenseFamilies()); + + licenses = new HashMap<>(); + List approvedLicenses = new ArrayList<>(defaultApprovedLicenses()); + approvedLicenses.add(new TestingLicense("GPL", "GPL1")); + licenses.put(LicenseFilter.APPROVED, approvedLicenses); + licenses.put(LicenseFilter.ALL, defaultAllLicenses()); + + + lst.add(Arguments.of("GPL1 id added ", config, licenseFamilies, licenses)); + + // GPL family added + config = new ReportConfiguration(); + config.setFrom(defaults); + config.addApprovedLicenseCategory(ILicenseFamily.makeCategory("GPL")); + + licenseFamilies = new HashMap<>(); + licenseFamilies.put(LicenseFilter.APPROVED, defaultAllLicenseFamilies()); + licenseFamilies.put(LicenseFilter.ALL, defaultAllLicenseFamilies()); + + licenses = new HashMap<>(); + approvedLicenses = new ArrayList<>(defaultApprovedLicenses()); + approvedLicenses.addAll(Arrays.asList(new TestingLicense("GPL", "GPL1"), + new TestingLicense("GPL", "GPL2"), + new TestingLicense("GPL", "GPL3"))); + licenses.put(LicenseFilter.APPROVED, approvedLicenses); + licenses.put(LicenseFilter.ALL, defaultAllLicenses()); + + lst.add(Arguments.of("GPL family added", config, licenseFamilies, licenses)); + + // Add new license + config = new ReportConfiguration(); + config.setFrom(defaults); + ILicense newLicense = new TestingLicense("FAM", "Testing"); + config.addLicense(newLicense); + + licenseFamilies = new HashMap<>(); + licenseFamilies.put(LicenseFilter.APPROVED, defaultApprovedLicenseFamilies()); + ArrayList allFamilies = new ArrayList<>(defaultAllLicenseFamilies()); + allFamilies.add(newLicense.getLicenseFamily()); + licenseFamilies.put(LicenseFilter.ALL, allFamilies); + + licenses = new HashMap<>(); + licenses.put(LicenseFilter.APPROVED, defaultApprovedLicenses()); + ArrayList allLicenses = new ArrayList<>(defaultAllLicenses()); + allLicenses.add(newLicense); + licenses.put(LicenseFilter.ALL, allLicenses); + + lst.add(Arguments.of("Testing license added", config, licenseFamilies, licenses)); + + // Add new license approved by id + config = new ReportConfiguration(); + config.setFrom(defaults); + newLicense = new TestingLicense("FAM", "Testing"); + config.addLicense(newLicense); + config.addApprovedLicenseId(newLicense.getId()); + + licenseFamilies = new HashMap<>(); + licenseFamilies.put(LicenseFilter.APPROVED, defaultApprovedLicenseFamilies()); + allFamilies = new ArrayList<>(defaultAllLicenseFamilies()); + allFamilies.add(newLicense.getLicenseFamily()); + licenseFamilies.put(LicenseFilter.ALL, allFamilies); + + licenses = new HashMap<>(); + + approvedLicenses = new ArrayList<>(defaultApprovedLicenses()); + approvedLicenses.add(newLicense); + licenses.put(LicenseFilter.APPROVED, approvedLicenses); + allLicenses = new ArrayList<>(defaultAllLicenses()); + allLicenses.add(newLicense); + licenses.put(LicenseFilter.ALL, allLicenses); + + lst.add(Arguments.of("Testing license id approved", config, licenseFamilies, licenses)); + + // Add new license approved by family + config = new ReportConfiguration(); + config.setFrom(defaults); + newLicense = new TestingLicense("FAM", "Testing"); + config.addLicense(newLicense); + config.addApprovedLicenseCategory(newLicense.getLicenseFamily()); + + licenseFamilies = new HashMap<>(); + ArrayList approvedFamilies = new ArrayList<>(defaultApprovedLicenseFamilies()); + approvedFamilies.add(newLicense.getLicenseFamily()); + licenseFamilies.put(LicenseFilter.APPROVED, approvedFamilies); + allFamilies = new ArrayList<>(defaultAllLicenseFamilies()); + allFamilies.add(newLicense.getLicenseFamily()); + licenseFamilies.put(LicenseFilter.ALL, allFamilies); + + licenses = new HashMap<>(); + approvedLicenses = new ArrayList<>(defaultApprovedLicenses()); + approvedLicenses.add(newLicense); + licenses.put(LicenseFilter.APPROVED, approvedLicenses); + allLicenses = new ArrayList<>(defaultAllLicenses()); + allLicenses.add(newLicense); + licenses.put(LicenseFilter.ALL, allLicenses); + + lst.add(Arguments.of("Testing license family approved", config, licenseFamilies, licenses)); + + return lst.stream(); + } + + @ParameterizedTest(name = "{index} {0}") + @MethodSource("nonStandardDocumentData") + void testNonStandardDocumentsDoNotFailLicenseTests(Document.Type expected, Document document) throws RatDocumentAnalysisException { + Defaults defaults = Defaults.builder().build(); + ReportConfiguration config = new ReportConfiguration(); + config.setFrom(defaults); + + DocumentAnalyser analyser = AnalyserFactory.createConfiguredAnalyser(config); + analyser.analyse(document); + assertThat(document.getMetaData().getDocumentType()).isEqualTo(expected); + assertThat(document.getMetaData().licenses()).hasSize(0); + } + + private static Stream nonStandardDocumentData() { + List lst = new ArrayList<>(); + + lst.add(Arguments.of(Document.Type.NOTICE, new TestingDocument(() -> new ByteArrayInputStream("Hello World".getBytes(StandardCharsets.UTF_8)), "NOTICE"))); + byte[] zipMagic = new byte[]{0x50, 0x4B, 0x03, 0x06}; + lst.add(Arguments.of(Document.Type.ARCHIVE, new TestingDocument(() -> new ByteArrayInputStream(zipMagic), "example.zip"))); + byte[] gifMagic = new byte[]{0x47, 0x49, 0x46, 0x38, 0x37, 0x61}; + lst.add(Arguments.of(Document.Type.BINARY, new TestingDocument(() -> new ByteArrayInputStream(gifMagic), "example.gif"))); + lst.add(Arguments.of(Document.Type.IGNORED, new TestingDocument(() -> new ByteArrayInputStream("THIS FILE IS AUTOMATICALLY GENERATED".getBytes(StandardCharsets.UTF_8)), "example.ignored"))); + return lst.stream(); + } +} diff --git a/apache-rat-core/src/test/java/org/apache/rat/analysis/PolicyTest.java b/apache-rat-core/src/test/java/org/apache/rat/analysis/PolicyTest.java deleted file mode 100644 index 69f165b52..000000000 --- a/apache-rat-core/src/test/java/org/apache/rat/analysis/PolicyTest.java +++ /dev/null @@ -1,171 +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.analysis; -// -//import static org.junit.jupiter.api.Assertions.assertEquals; -//import static org.junit.jupiter.api.Assertions.assertNotNull; -// -//import java.util.SortedSet; -//import java.util.TreeSet; -// -//import org.apache.rat.Defaults; -//import org.apache.rat.api.Document; -//import org.apache.rat.document.IDocumentAnalyser; -//import org.apache.rat.license.ILicenseFamily; -//import org.apache.rat.license.LicenseSetFactory; -//import org.apache.rat.license.LicenseSetFactory.LicenseFilter; -//import org.apache.rat.testhelpers.TestingLicense; -//import org.apache.rat.testhelpers.TestingDocument; -//import org.apache.rat.testhelpers.TestingMatcher; -//import org.junit.jupiter.api.BeforeEach; -//import org.junit.jupiter.api.Test; -// -///** -// * Tests the Default policy implementation. -// */ -//public class PolicyTest { -// /** -// * This is the number of accepted licenses in the default license file : -// * /org/apache/rat/default.xml -// */ -// private static final int NUMBER_OF_DEFAULT_ACCEPTED_LICENSES = 7; -// -// private static final ILicenseFamily[] APPROVED_FAMILIES = { // -// makeFamily("AL", "Apache License Version 2.0"), -// makeFamily("BSD-3", "BSD 3 clause"), -// makeFamily("CDDL1", "COMMON DEVELOPMENT AND DISTRIBUTION LICENSE Version 1.0"), -// makeFamily("MIT", "The MIT License"), -// makeFamily("OASIS", "OASIS Open License"), makeFamily("W3CD", "W3C Document Copyright"), -// makeFamily("W3C", "W3C Software Copyright"), }; -// -// private Document document; -// private IDocumentAnalyser policy; -// private Defaults defaults; -// private LicenseSetFactory licenseSetFactory; -// -// @BeforeEach -// public void setUp() { -// defaults = Defaults.builder().build(); -// licenseSetFactory = defaults.getLicenseSetFactory(); -// policy = AnalyserFactory.createPolicy(licenseSetFactory); -// document = new TestingDocument("subject"); -// } -// -// private void assertApproval(boolean pApproved) { -// boolean state = document.getMetaData().approvedLicenses().findAny().isPresent(); -// assertEquals(pApproved, state); -// } -// -// private void setMetadata(ILicenseFamily family) { -// document.getMetaData().reportOnLicense(new TestingLicense(family.getFamilyCategory().trim(), new TestingMatcher(), family)); -// } -// -// private static ILicenseFamily makeFamily(String category, String name) { -// return ILicenseFamily.builder().setLicenseFamilyCategory(category).setLicenseFamilyName(name).build(); -// } -// -// @Test -// public void testCount() { -// assertEquals(NUMBER_OF_DEFAULT_ACCEPTED_LICENSES, policy.getApprovedLicenseFamilies().size()); -// } -// -// @Test -// public void testApprovedLicenses() { -// -// assertEquals(NUMBER_OF_DEFAULT_ACCEPTED_LICENSES, APPROVED_FAMILIES.length, "Approved license count mismatch"); -// for (ILicenseFamily family : APPROVED_FAMILIES) { -// setMetadata(family); -// policy.analyse(document); -// assertApproval(true); -// } -// } -// -// @Test -// public void testUnApprovedLicenses() { -// SortedSet all = defaults.getLicenseSetFactory().getLicenseFamilies(LicenseFilter.ALL); -// SortedSet unapproved = new TreeSet<>(); -// unapproved.addAll(all); -// unapproved.removeAll(defaults.getLicenseSetFactory().getLicenseFamilies(LicenseFilter.APPROVED)); -// -// assertEquals(all.size() - NUMBER_OF_DEFAULT_ACCEPTED_LICENSES, unapproved.size(), -// "Unapproved license count mismatch"); -// for (ILicenseFamily family : unapproved) { -// setMetadata(family); -// policy.analyse(document); -// assertApproval(false); -// } -// } -// -// @Test -// public void testUnknownFamily() { -// setMetadata(makeFamily("?????", "Unknown document")); -// policy.analyse(document); -// assertApproval(false); -// } -// -// @Test -// public void testAddNewApprovedLicenseAndDefaults() { -// ILicenseFamily testingFamily = makeFamily("test", "Testing License Family"); -// setMetadata(testingFamily); -// policy.analyse(document); -// assertApproval(false); -// -// licenseSetFactory.approveLicenseCategory(testingFamily.getFamilyCategory()); -// policy.analyse(document); -// assertApproval(true); -// } -// -// @Test -// public void testAddNewApprovedLicenseNoDefaults() { -// LicenseSetFactory licenseSetFactory = new LicenseSetFactory(); -// policy = new DefaultPolicy(licenseSetFactory); -// assertEquals(0, policy.getApprovedLicenseFamilies().size()); -// ILicenseFamily testingFamily = makeFamily("test", "Testing License Family"); -// setMetadata(testingFamily); -// policy.analyse(document); -// assertApproval(false); -// -// licenseSetFactory.addLicense(new TestingLicense(testingFamily.getFamilyCategory().trim(), new TestingMatcher(), testingFamily)); -// licenseSetFactory.approveLicenseCategory(testingFamily.getFamilyCategory()); -// assertEquals(1, policy.getApprovedLicenseFamilies().size()); -// assertNotNull(LicenseSetFactory.familySearch(testingFamily, policy.getApprovedLicenseFamilies()), -// "Did not properly add ILicenseFamily"); -// policy.analyse(document); -// assertApproval(true); -// } -// -// @Test -// public void testNonStandardDocumentsDoNotFailLicenseTests() { -// Document.Type[] nonStandardDocuments = { Document.Type.NOTICE, Document.Type.ARCHIVE, Document.Type.BINARY }; -// -// for (Document.Type d : nonStandardDocuments) { -// document = new TestingDocument("subject"); -// document.getMetaData().setDocumentType(d); -// policy.analyse(document); -// assertEquals(0, document.getMetaData().licenses().count(), "failed on " + d); -// } -// } -// -// @Test -// public void testUnclassifiedDocumentsDoNotFailLicenseTests() { -// document.getMetaData().setDocumentType(Document.Type.STANDARD); -// policy.analyse(document); -// assertApproval(false); -// } -//} diff --git a/apache-rat-core/src/test/java/org/apache/rat/testhelpers/TestingDocument.java b/apache-rat-core/src/test/java/org/apache/rat/testhelpers/TestingDocument.java index 7ab034a3e..c1e756687 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/testhelpers/TestingDocument.java +++ b/apache-rat-core/src/test/java/org/apache/rat/testhelpers/TestingDocument.java @@ -18,11 +18,14 @@ */ package org.apache.rat.testhelpers; +import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.io.Reader; import java.util.Collections; import java.util.SortedSet; +import org.apache.commons.io.function.IOSupplier; import org.apache.rat.api.Document; import org.apache.rat.document.DocumentNameMatcher; import org.apache.rat.document.DocumentName; @@ -30,33 +33,43 @@ public class TestingDocument extends Document { private final Reader reader; + private final IOSupplier input; public TestingDocument() { - this(null, "name"); + this("name"); } public TestingDocument(String name) { - this(null, name); + this(name, null); } public TestingDocument(DocumentName documentName) { super(documentName, DocumentNameMatcher.MATCHES_ALL); this.reader = null; + this.input = null; } public TestingDocument(String name, DocumentNameMatcher matcher) { super(DocumentName.builder().setName(name).setBaseName("").setDirSeparator("/").setCaseSensitive(true).build(), matcher); this.reader = null; + this.input = null; } public TestingDocument(Reader reader, String name) { super(DocumentName.builder().setName(name).setBaseName("").setDirSeparator("/").setCaseSensitive(true).build(), DocumentNameMatcher.MATCHES_ALL); this.reader = reader; + this.input = null; + } + + public TestingDocument(IOSupplier inputStream, String name) { + super(DocumentName.builder().setName(name).setBaseName("").setDirSeparator("/").setCaseSensitive(true).build(), DocumentNameMatcher.MATCHES_ALL); + this.input = inputStream; + this.reader = null; } @Override - public Reader reader() { - return reader; + public Reader reader() throws IOException { + return reader == null ? new InputStreamReader(input.get()) : reader; } @Override @@ -70,7 +83,10 @@ public SortedSet listChildren() { } @Override - public InputStream inputStream() { + public InputStream inputStream() throws IOException { + if (input != null) { + return input.get(); + } throw new UnsupportedOperationException(); } } diff --git a/apache-rat-core/src/test/java/org/apache/rat/testhelpers/TestingLicense.java b/apache-rat-core/src/test/java/org/apache/rat/testhelpers/TestingLicense.java index 10aee0d9f..efc651be0 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/testhelpers/TestingLicense.java +++ b/apache-rat-core/src/test/java/org/apache/rat/testhelpers/TestingLicense.java @@ -75,6 +75,7 @@ public TestingLicense(String id, IHeaderMatcher matcher, ILicenseFamily family) this.matcher = matcher; this.note = null; this.id = id; + this.name = id; } /** @@ -141,4 +142,9 @@ public boolean equals(Object o) { public int hashCode() { return ILicense.hash(this); } + + @Override + public String toString() { + return String.format("%s[id='%s', family='%s']", name, id, family); + } } diff --git a/apache-rat-plugin/.gitignore b/apache-rat-plugin/.gitignore index 659f5f33c..d302b51b5 100644 --- a/apache-rat-plugin/.gitignore +++ b/apache-rat-plugin/.gitignore @@ -1,2 +1,3 @@ /target/ /src/site/apt/argument_types.txt +/invoker_target/ From 1e4c7b1056b656b9adc799995c4519cff24bbb4d Mon Sep 17 00:00:00 2001 From: Claude Warren Date: Sun, 12 Jan 2025 14:27:24 +0000 Subject: [PATCH 16/22] removed dead code --- .../rat/license/LicenseSetFactoryTest.java | 22 ------------------- 1 file changed, 22 deletions(-) diff --git a/apache-rat-core/src/test/java/org/apache/rat/license/LicenseSetFactoryTest.java b/apache-rat-core/src/test/java/org/apache/rat/license/LicenseSetFactoryTest.java index 21f564be2..88b04a5db 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/license/LicenseSetFactoryTest.java +++ b/apache-rat-core/src/test/java/org/apache/rat/license/LicenseSetFactoryTest.java @@ -167,26 +167,4 @@ void testLicenseIDManipulation() { .isFalse(); } - -// @Test -// public void testNonStandardDocumentsDoNotFailLicenseTests() { -// Document.Type[] nonStandardDocuments = { Document.Type.NOTICE, Document.Type.ARCHIVE, Document.Type.BINARY }; -// for (Document.Type d : nonStandardDocuments) { -// document = new TestingDocument("subject"); -// document.getMetaData().setDocumentType(d); -// document.getMetaData().setApprovalPredicate(licenseSetFactory.getApprovedLicensePredicate()); -// for (ILicenseFamily family : APPROVED_FAMILIES) { -// TestingLicense license = new TestingLicense(family.getFamilyCategory().trim(), new TestingMatcher(), family); -// assertThat(document.getMetaData().isApproved(license)).isFalse(); -// } -// } -// } -// -// @Test -// public void testUnclassifiedDocumentsDoNotFailLicenseTests() { -// document.getMetaData().setDocumentType(Document.Type.STANDARD); -// policy.analyse(document); -// assertApproval(false); -// } - } From fdac590b0cedf6b1fe6b8bda1b4110b764469995 Mon Sep 17 00:00:00 2001 From: "P. Ottlinger" Date: Sun, 12 Jan 2025 16:48:56 +0100 Subject: [PATCH 17/22] Minor cleanups --- .../java/org/apache/rat/analysis/AnalyserFactoryTest.java | 4 ++-- .../java/org/apache/rat/analysis/DocumentAnalyserTest.java | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/apache-rat-core/src/test/java/org/apache/rat/analysis/AnalyserFactoryTest.java b/apache-rat-core/src/test/java/org/apache/rat/analysis/AnalyserFactoryTest.java index f848d4e4a..4e2461ce6 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/analysis/AnalyserFactoryTest.java +++ b/apache-rat-core/src/test/java/org/apache/rat/analysis/AnalyserFactoryTest.java @@ -266,8 +266,8 @@ public void testMultiplexer() throws Exception { multiplexer.analyse(document); for (int i = 0; i < analysers.length; i++) { - assertThat(analysers[i].matches.size()).as("Matcher "+i).isEqualTo(1); - assertThat(analysers[i].matches.get(0)).as("Matcher "+i).isEqualTo(document); + assertThat(analysers[i].matches.size()).as("Matcher " + i).isEqualTo(1); + assertThat(analysers[i].matches.get(0)).as("Matcher " + i).isEqualTo(document); } } } diff --git a/apache-rat-core/src/test/java/org/apache/rat/analysis/DocumentAnalyserTest.java b/apache-rat-core/src/test/java/org/apache/rat/analysis/DocumentAnalyserTest.java index f339a75c4..0c872b6b9 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/analysis/DocumentAnalyserTest.java +++ b/apache-rat-core/src/test/java/org/apache/rat/analysis/DocumentAnalyserTest.java @@ -43,6 +43,8 @@ import org.apache.rat.testhelpers.TestingLicense; import org.apache.rat.testhelpers.TestingDocument; import org.apache.rat.testhelpers.TestingMatcher; +import org.apache.rat.utils.DefaultLog; +import org.apache.rat.utils.Log; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; import org.junit.jupiter.params.provider.MethodSource; @@ -80,6 +82,7 @@ void licenseSetFactoryTest(String name, ReportConfiguration configuration, Map> licenseFamilies, Map> licenses ) { + DefaultLog.getInstance().log(Log.Level.DEBUG, "Running " + name); LicenseSetFactory factory = configuration.getLicenseSetFactory(); Collection categories = asCategories(licenseFamilies.get(LicenseFilter.APPROVED)); @@ -103,6 +106,7 @@ void analyserTest(String name, ReportConfiguration configuration, Map> licenseFamilies, Map> licenses ) throws RatDocumentAnalysisException { + DefaultLog.getInstance().log(Log.Level.DEBUG, "Running " + name); DocumentAnalyser analyser = AnalyserFactory.createConfiguredAnalyser(configuration); // verify approved license families report approved. @@ -184,7 +188,6 @@ private static Stream defaultAnalyserTestData() { config.setFrom(defaults); // default setup - Map> licenseFamilies = new HashMap<>(); licenseFamilies.put(LicenseFilter.APPROVED, defaultApprovedLicenseFamilies()); licenseFamilies.put(LicenseFilter.ALL, defaultAllLicenseFamilies()); @@ -210,7 +213,6 @@ private static Stream defaultAnalyserTestData() { licenses.put(LicenseFilter.APPROVED, approvedLicenses); licenses.put(LicenseFilter.ALL, defaultAllLicenses()); - lst.add(Arguments.of("GPL1 id added ", config, licenseFamilies, licenses)); // GPL family added From a886a0743481b81df5d2378ae4a1b47531c5605d Mon Sep 17 00:00:00 2001 From: "P. Ottlinger" Date: Sun, 12 Jan 2025 17:32:44 +0100 Subject: [PATCH 18/22] Minor cleanups --- .../java/org/apache/rat/license/LicenseSetFactoryTest.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/apache-rat-core/src/test/java/org/apache/rat/license/LicenseSetFactoryTest.java b/apache-rat-core/src/test/java/org/apache/rat/license/LicenseSetFactoryTest.java index 88b04a5db..92906c966 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/license/LicenseSetFactoryTest.java +++ b/apache-rat-core/src/test/java/org/apache/rat/license/LicenseSetFactoryTest.java @@ -31,8 +31,8 @@ public class LicenseSetFactoryTest { /** - * This is the number of accepted licenses in the default license file : - * /org/apache/rat/default.xml + * This is the number of accepted licenses in the default license file: + * {@code /org/apache/rat/default.xml} */ private static final int NUMBER_OF_DEFAULT_ACCEPTED_LICENSES = 7; @@ -99,7 +99,6 @@ public void testUnknownFamily() { .as("Did not find unapproved family " + family) .isFalse(); - assertThat(licenseSetFactory.getApprovedLicensePredicate().test(UnknownLicense.INSTANCE)) .as("Did not find UnknownLicense.INSTANCE") .isFalse(); @@ -165,6 +164,5 @@ void testLicenseIDManipulation() { assertThat(licenseSetFactory.getApprovedLicensePredicate().test(license)) .as("Found unapproved id " + licenseId) .isFalse(); - } } From 16a2319181f4a60f414f04e42ab1932ec94207f6 Mon Sep 17 00:00:00 2001 From: "P. Ottlinger" Date: Sun, 12 Jan 2025 17:47:18 +0100 Subject: [PATCH 19/22] RAT-469: Minor cleanup --- .../java/org/apache/rat/license/LicenseSetFactory.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/apache-rat-core/src/main/java/org/apache/rat/license/LicenseSetFactory.java b/apache-rat-core/src/main/java/org/apache/rat/license/LicenseSetFactory.java index 7556c57b8..6fe00db5a 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/license/LicenseSetFactory.java +++ b/apache-rat-core/src/main/java/org/apache/rat/license/LicenseSetFactory.java @@ -328,12 +328,10 @@ public SortedSet getLicenseFamilies(final LicenseFilter filter) /** * Gets the License ids based on the filter. * - * @param filter the types of License IDs to return. + * @param filter the types of License Ids to return. * @return The list of all licenses in the category regardless of whether or not it is used by an ILicense implementation. */ public SortedSet getLicenseCategories(final LicenseFilter filter) { - Predicate approved = l -> (isApprovedCategory(l.getLicenseFamily()) || - approvedLicenseIds.contains(l.getId())) && !removedLicenseIds.contains(l.getId()); SortedSet result = new TreeSet<>(); switch (filter) { case ALL: @@ -344,7 +342,6 @@ public SortedSet getLicenseCategories(final LicenseFilter filter) { return result; case APPROVED: approvedLicenseCategories.stream().filter(s -> !removedLicenseCategories.contains(s)).forEach(result::add); - //licenses.stream().filter(approved).forEach(l -> result.add(l.getLicenseFamily().getFamilyCategory())); families.stream().filter(this::isApprovedCategory).forEach(f -> result.add(f.getFamilyCategory())); return result; case NONE: @@ -356,7 +353,7 @@ public SortedSet getLicenseCategories(final LicenseFilter filter) { /** * Gets the License ids based on the filter. * - * @param filter the types of License IDs to return. + * @param filter the types of License Ids to return. * @return The list of all licenses in the category regardless of whether or not it is used by an ILicense implementation. */ public SortedSet getLicenseIds(final LicenseFilter filter) { @@ -447,7 +444,7 @@ public IHeaderMatcher getMatcher() { * Search a SortedSet of licenses for the matching license. * License must match both family code, and license id. * - * @param target the license to search for. Must not be null. + * @param target the license to search for. Must not be {@code null}. * @param licenses the SortedSet of licenses to search. * @return the matching license or {@code null} if not found. */ From 7cba21af98a08820a42ecdf83810b5aaedf54cdc Mon Sep 17 00:00:00 2001 From: "P. Ottlinger" Date: Sun, 12 Jan 2025 17:49:36 +0100 Subject: [PATCH 20/22] RAT-469: Build failure on GHA-windows --- .../config/exclusion/fileProcessors/HgIgnoreProcessorTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/apache-rat-core/src/test/java/org/apache/rat/config/exclusion/fileProcessors/HgIgnoreProcessorTest.java b/apache-rat-core/src/test/java/org/apache/rat/config/exclusion/fileProcessors/HgIgnoreProcessorTest.java index 1eca257c0..3085792c5 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/config/exclusion/fileProcessors/HgIgnoreProcessorTest.java +++ b/apache-rat-core/src/test/java/org/apache/rat/config/exclusion/fileProcessors/HgIgnoreProcessorTest.java @@ -47,7 +47,6 @@ public void processExampleFileTest() throws IOException { HgIgnoreProcessor processor = new HgIgnoreProcessor(); List actual = processor.apply(baseName); assertEquals(expected, actual); - } @Test From e7883c013e0a3c72237f87853f6e8c848da9289c Mon Sep 17 00:00:00 2001 From: "P. Ottlinger" Date: Sun, 12 Jan 2025 17:58:36 +0100 Subject: [PATCH 21/22] RAT-469: Fix spotbugs issue/encoding --- .../src/main/java/org/apache/rat/tools/Naming.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/apache-rat-tools/src/main/java/org/apache/rat/tools/Naming.java b/apache-rat-tools/src/main/java/org/apache/rat/tools/Naming.java index b5bff5c24..808546254 100644 --- a/apache-rat-tools/src/main/java/org/apache/rat/tools/Naming.java +++ b/apache-rat-tools/src/main/java/org/apache/rat/tools/Naming.java @@ -24,6 +24,7 @@ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; +import java.nio.charset.Charset; import java.util.ArrayList; import java.util.Arrays; import java.util.Deque; @@ -150,7 +151,7 @@ public static void main(final String[] args) throws IOException, ParseException }; } - try (Writer underWriter = cl.getArgs().length != 0 ? new FileWriter(cl.getArgs()[0]) : new OutputStreamWriter(System.out)) { + try (Writer underWriter = cl.getArgs().length != 0 ? new FileWriter(cl.getArgs()[0]) : new OutputStreamWriter(System.out, Charset.forName("UTF-8"));) { if (cl.hasOption(CSV)) { printCSV(columns, filter, cl.hasOption(CLI), mavenFilter, antFilter, descriptionFunction, underWriter); } From cf9c2f5d47a84e429a2ed6ae63d18fa441c6e092 Mon Sep 17 00:00:00 2001 From: "P. Ottlinger" Date: Sun, 12 Jan 2025 18:01:17 +0100 Subject: [PATCH 22/22] RAT-469: Fix spotbugs issue/encoding --- .../src/main/java/org/apache/rat/tools/Naming.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apache-rat-tools/src/main/java/org/apache/rat/tools/Naming.java b/apache-rat-tools/src/main/java/org/apache/rat/tools/Naming.java index 808546254..1b3d8cb80 100644 --- a/apache-rat-tools/src/main/java/org/apache/rat/tools/Naming.java +++ b/apache-rat-tools/src/main/java/org/apache/rat/tools/Naming.java @@ -24,7 +24,7 @@ import java.io.OutputStreamWriter; import java.io.PrintWriter; import java.io.Writer; -import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.Deque; @@ -151,7 +151,7 @@ public static void main(final String[] args) throws IOException, ParseException }; } - try (Writer underWriter = cl.getArgs().length != 0 ? new FileWriter(cl.getArgs()[0]) : new OutputStreamWriter(System.out, Charset.forName("UTF-8"));) { + try (Writer underWriter = cl.getArgs().length != 0 ? new FileWriter(cl.getArgs()[0]) : new OutputStreamWriter(System.out, StandardCharsets.UTF_8)) { if (cl.hasOption(CSV)) { printCSV(columns, filter, cl.hasOption(CLI), mavenFilter, antFilter, descriptionFunction, underWriter); }