From 6034936f7a63e8a8d98558ccf654e0a338d37cd4 Mon Sep 17 00:00:00 2001 From: Claude Warren Date: Sun, 22 Oct 2023 13:53:12 +0200 Subject: [PATCH] Fix for bad reporting of license status for non standard documents (e.g. binary). Collected license filtering into new LicenseSetFactory class and applied it where expected. Added more tests to DefaultsTests Added more tests to ReportConfigurtionTest --- .../main/java/org/apache/rat/Defaults.java | 61 ++--- .../src/main/java/org/apache/rat/Report.java | 2 +- .../org/apache/rat/ReportConfiguration.java | 33 +-- .../rat/analysis/HeaderCheckWorker.java | 8 +- .../apache/rat/analysis/UnknownLicense.java | 92 +++++++ .../java/org/apache/rat/api/MetaData.java | 8 +- .../java/org/apache/rat/license/ILicense.java | 74 ------ .../apache/rat/license/ILicenseFamily.java | 14 -- .../apache/rat/license/LicenseSetFactory.java | 228 ++++++++++++++++++ .../org/apache/rat/policy/DefaultPolicy.java | 12 +- .../rat/report/xml/XmlReportFactory.java | 2 +- .../main/resources/org/apache/rat/default.xml | 6 +- .../java/org/apache/rat/DefaultsTest.java | 3 +- .../apache/rat/ReportConfigurationTest.java | 46 +++- .../java/org/apache/rat/ReporterTest.java | 30 +-- .../analysis/license/AbstractLicenseTest.java | 21 +- .../AppliedApacheSoftwareLicenseTest.java | 2 +- .../apache/rat/policy/DefaultPolicyTest.java | 169 ++++++++----- .../rat/testhelpers/TestingLicense.java | 64 ++++- .../rat/testhelpers/TestingMatcher.java | 17 ++ .../java/org/apache/rat/mp/RatCheckMojo.java | 4 +- .../java/org/apache/rat/mp/RatReportMojo.java | 22 +- .../java/org/apache/rat/anttasks/Report.java | 9 +- 23 files changed, 619 insertions(+), 308 deletions(-) create mode 100644 apache-rat-core/src/main/java/org/apache/rat/analysis/UnknownLicense.java create mode 100644 apache-rat-core/src/main/java/org/apache/rat/license/LicenseSetFactory.java 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 c5ead2beb..84885c366 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 @@ -23,7 +23,6 @@ import java.net.MalformedURLException; import java.net.URL; import java.util.Collection; -import java.util.Collections; import java.util.Comparator; import java.util.Set; import java.util.SortedSet; @@ -35,6 +34,8 @@ import org.apache.rat.configuration.MatcherReader; 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; /** * A class that holds the list of licenses and approved licences from one or more Configuration file. @@ -54,15 +55,8 @@ public class Defaults { */ public static final String UNAPPROVED_LICENSES_STYLESHEET = "org/apache/rat/unapproved-licenses.xsl"; - /** - * The set of defined licenses. - */ - private final SortedSet licenses; - /** - * The set of approved license ids. - */ - private final SortedSet approvedLicenseIds; - + private LicenseSetFactory setFactory; + /** * Initialize the system configuration reader.. */ @@ -76,9 +70,8 @@ public static void init() { /** * Builder constructs instances. */ - private Defaults() { - licenses = new TreeSet<>(ILicense.getComparator()); - approvedLicenseIds = new TreeSet<>(); + private Defaults(Set urls) { + this.setFactory = Defaults.readConfigFiles(urls); } /** @@ -89,19 +82,16 @@ public static Builder builder() { return new Builder(); } - /** - * Clears all licenses and approved licenses. - */ - public void clear() { - licenses.clear(); - approvedLicenseIds.clear(); - } - /** * Reads the configuration files. * @param urls the URLS to read. */ - private void readConfigFiles(Collection urls) { + private static LicenseSetFactory readConfigFiles(Collection urls) { + + SortedSet licenses = LicenseSetFactory.emptyLicenseSet(); + + SortedSet approvedLicenseIds = new TreeSet(); + for (URL url : urls) { Format fmt = Format.fromURL(url); MatcherReader mReader = fmt.matcherReader(); @@ -117,6 +107,7 @@ private void readConfigFiles(Collection urls) { lReader.approvedLicenseId().stream().map(ILicenseFamily::makeCategory).forEach(approvedLicenseIds::add); } } + return new LicenseSetFactory(licenses, approvedLicenseIds); } /** @@ -135,11 +126,12 @@ public static IOSupplier getUnapprovedLicensesStyleSheet() { return () -> Defaults.class.getClassLoader().getResourceAsStream(Defaults.UNAPPROVED_LICENSES_STYLESHEET); } - /** - * @return the sorted set of defined licenses. - */ - public SortedSet getLicenses() { - return Collections.unmodifiableSortedSet(licenses); + public SortedSet getLicenses(LicenseFilter filter) { + return setFactory.getLicenses(filter); + } + + public SortedSet getLicenseFamilies(LicenseFilter filter) { + return setFactory.getLicenseFamilies(filter); } /** @@ -147,15 +139,10 @@ public SortedSet getLicenses() { * If not licenses have been explicitly listed as approved all licenses are assumed to be approved. * @return The sorted set of approved licenseIds. */ - public SortedSet getLicenseIds() { - if (approvedLicenseIds.isEmpty()) { - SortedSet result = new TreeSet<>(); - licenses.stream().map(x -> x.getLicenseFamily().getFamilyCategory()).forEach(result::add); - return result; - } - return Collections.unmodifiableSortedSet(approvedLicenseIds); + public SortedSet getLicenseIds(LicenseFilter filter) { + return setFactory.getLicenseFamilyIds(filter); } - + /** * The Defaults builder. */ @@ -247,9 +234,7 @@ public Builder noDefault() { * Builds the defaults object. */ public Defaults build() { - Defaults result = new Defaults(); - result.readConfigFiles(fileNames); - return result; + return new Defaults(fileNames); } } } diff --git a/apache-rat-core/src/main/java/org/apache/rat/Report.java b/apache-rat-core/src/main/java/org/apache/rat/Report.java index a2fe778a3..2ce2be02c 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/Report.java +++ b/apache-rat-core/src/main/java/org/apache/rat/Report.java @@ -45,10 +45,10 @@ import org.apache.commons.io.filefilter.RegexFileFilter; import org.apache.commons.io.filefilter.WildcardFileFilter; import org.apache.commons.lang3.StringUtils; -import org.apache.rat.ReportConfiguration.LicenseFilter; import org.apache.rat.config.AddLicenseHeaders; import org.apache.rat.license.ILicense; import org.apache.rat.license.ILicenseFamily; +import org.apache.rat.license.LicenseSetFactory.LicenseFilter; import org.apache.rat.report.IReportable; import org.apache.rat.walker.ArchiveWalker; import org.apache.rat.walker.DirectoryWalker; 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 394903530..c3dbff3c0 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 @@ -38,6 +38,8 @@ import org.apache.rat.config.AddLicenseHeaders; 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.report.IReportable; /** @@ -46,19 +48,7 @@ * configuration and invoke the {@link Reporter}. */ public class ReportConfiguration { - /** - * An enum that defines the types of License filters. - */ - public enum LicenseFilter { - /** All defined licenses are returned */ - all, - /** Only approved licenses are returned */ - approved, - /** No licenses are returned */ - none - } - - private SortedSet licenses = new TreeSet<>(ILicense.getComparator()); + private SortedSet licenses = LicenseSetFactory.emptyLicenseSet(); private SortedSet approvedLicenseCategories = new TreeSet<>(); private SortedSet removedLicenseCategories = new TreeSet<>(); private boolean addingLicenses; @@ -69,7 +59,6 @@ public enum LicenseFilter { private IOSupplier styleSheet = null; private IReportable reportable = null; private FilenameFilter inputFileFilter = null; - // private LicenseFilter approvalFilter = LicenseFilter.approved; /** * @return The filename filter for the potential input files. @@ -121,8 +110,8 @@ public void setStyleSheet(IOSupplier styleSheet) { * @param defaults The defaults to set. */ public void setFrom(Defaults defaults) { - addLicenses(defaults.getLicenses()); - addApprovedLicenseCategories(defaults.getLicenseIds()); + addLicenses(defaults.getLicenses(LicenseFilter.all)); + addApprovedLicenseCategories(defaults.getLicenseIds(LicenseFilter.approved)); if (isStyleReport() && getStyleSheet() == null) { setStyleSheet(Defaults.getPlainStyleSheet()); } @@ -354,14 +343,10 @@ public SortedSet getLicenses(LicenseFilter filter) { case all: return Collections.unmodifiableSortedSet(licenses); case approved: - SortedSet approvedLicenses = getApprovedLicenseCategories(); - SortedSet result = new TreeSet<>(ILicense.getComparator()); - licenses.stream().filter(x -> approvedLicenses.contains(x.getLicenseFamily().getFamilyCategory())) - .forEach(result::add); - return result; + return new LicenseSetFactory(licenses, getApprovedLicenseCategories()).getLicenses(filter); case none: default: - return Collections.emptySortedSet(); + return LicenseSetFactory.emptyLicenseSet(); } } @@ -378,9 +363,7 @@ public SortedSet getLicenses(LicenseFilter filter) { * @return The set of defined licenses. */ public SortedSet getLicenseFamilies(LicenseFilter filter) { - SortedSet result = new TreeSet<>(); - getLicenses(filter).stream().map(ILicense::getLicenseFamily).forEach(result::add); - return result; + return new LicenseSetFactory(licenses, getApprovedLicenseCategories()).getLicenseFamilies(filter); } /** diff --git a/apache-rat-core/src/main/java/org/apache/rat/analysis/HeaderCheckWorker.java b/apache-rat-core/src/main/java/org/apache/rat/analysis/HeaderCheckWorker.java index f4ec3832c..01ef13b71 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/analysis/HeaderCheckWorker.java +++ b/apache-rat-core/src/main/java/org/apache/rat/analysis/HeaderCheckWorker.java @@ -110,12 +110,8 @@ public void read() throws RatHeaderAnalysisException { if (license.finalizeState().asBoolean()) { document.getMetaData().reportOnLicense(license); } else { - final String notes = headers.toString(); - final MetaData metaData = document.getMetaData(); - metaData.set(new MetaData.Datum(MetaData.RAT_URL_HEADER_SAMPLE, notes)); - metaData.set(new MetaData.Datum(MetaData.RAT_URL_HEADER_CATEGORY, - MetaData.RAT_LICENSE_FAMILY_CATEGORY_VALUE_UNKNOWN)); - metaData.set(MetaData.RAT_LICENSE_FAMILY_NAME_DATUM_UNKNOWN); + document.getMetaData().reportOnLicense(UnknownLicense.INSTANCE); + document.getMetaData().set(new MetaData.Datum(MetaData.RAT_URL_HEADER_SAMPLE, headers.toString())); } } catch (IOException e) { throw new RatHeaderAnalysisException("Cannot read header for " + document, e); diff --git a/apache-rat-core/src/main/java/org/apache/rat/analysis/UnknownLicense.java b/apache-rat-core/src/main/java/org/apache/rat/analysis/UnknownLicense.java new file mode 100644 index 000000000..259a5ea9c --- /dev/null +++ b/apache-rat-core/src/main/java/org/apache/rat/analysis/UnknownLicense.java @@ -0,0 +1,92 @@ +/* + * 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 org.apache.rat.license.ILicense; +import org.apache.rat.license.ILicenseFamily; +import org.apache.rat.license.ILicenseFamilyBuilder; + +/** + * An ILicense implementation that represents an unknown license. + *

+ * The UnknownLicense is used during processing to report that a document license can not be determined. + *

+ */ +public class UnknownLicense implements ILicense { + + /** + * The single instance of this class. + */ + static final UnknownLicense INSTANCE = new UnknownLicense(); + + private final ILicenseFamily family ; + + /** + * Do not allow other constructions. + */ + private UnknownLicense() { + family = new ILicenseFamilyBuilder().setLicenseFamilyCategory("?????") + .setLicenseFamilyName("?????").build(); + } + + @Override + public String getId() { + return "?????"; + } + + @Override + public void reset() { + // do nothing + } + + @Override + public State matches(String line) { + return State.f; + } + + @Override + public State finalizeState() { + return State.f; + } + + @Override + public State currentState() { + return State.f; + } + + @Override + public int compareTo(ILicense arg0) { + return getLicenseFamily().compareTo(arg0.getLicenseFamily()); + } + + @Override + public ILicenseFamily getLicenseFamily() { + return family; + } + + @Override + public String getNotes() { + return null; + } + + @Override + public String derivedFrom() { + return null; + } +} 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 d3dd7bfb8..830722b6a 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 @@ -180,8 +180,12 @@ public class MetaData { public static final String RAT_URL_APPROVED_LICENSE = RAT_BASE_URL + "#ApprovedLicense"; public static final String RAT_APPROVED_LICENSE_VALUE_TRUE = Boolean.TRUE.toString(); public static final String RAT_APPROVED_LICENSE_VALUE_FALSE = Boolean.FALSE.toString(); - public static final Datum RAT_APPROVED_LICENSE_DATIM_TRUE = new Datum(RAT_URL_APPROVED_LICENSE, RAT_APPROVED_LICENSE_VALUE_TRUE); - public static final Datum RAT_APPROVED_LICENSE_DATIM_FALSE = new Datum(RAT_URL_APPROVED_LICENSE, RAT_APPROVED_LICENSE_VALUE_FALSE); + public static final Datum RAT_APPROVED_LICENSE_DATUM_TRUE = new Datum(RAT_URL_APPROVED_LICENSE, RAT_APPROVED_LICENSE_VALUE_TRUE); + public static final Datum RAT_APPROVED_LICENSE_DATUM_FALSE = new Datum(RAT_URL_APPROVED_LICENSE, RAT_APPROVED_LICENSE_VALUE_FALSE); + @Deprecated + public static final Datum RAT_APPROVED_LICENSE_DATIM_TRUE = RAT_APPROVED_LICENSE_DATUM_TRUE; + @Deprecated + public static final Datum RAT_APPROVED_LICENSE_DATIM_FALSE = RAT_APPROVED_LICENSE_DATUM_FALSE; /** * Only likely to be a small quantity of data diff --git a/apache-rat-core/src/main/java/org/apache/rat/license/ILicense.java b/apache-rat-core/src/main/java/org/apache/rat/license/ILicense.java index ee8e872de..c0f3c55c5 100644 --- a/apache-rat-core/src/main/java/org/apache/rat/license/ILicense.java +++ b/apache-rat-core/src/main/java/org/apache/rat/license/ILicense.java @@ -19,7 +19,6 @@ package org.apache.rat.license; import java.util.Comparator; -import java.util.SortedSet; import org.apache.rat.analysis.IHeaderMatcher; @@ -49,79 +48,6 @@ public static ILicense.Builder builder() { return new ILicenseBuilder(); } - /** - * Search a SortedSet of licenses for the matching license id. - * - * @param licenseId the id to search for. - * @param licenses the SortedSet of licenses to search. - * @return the matching license or {@code null} if not found. - */ - static ILicense search(String licenseId, SortedSet licenses) { - ILicenseFamily searchFamily = ILicenseFamily.builder().setLicenseFamilyCategory(licenseId) - .setLicenseFamilyCategory("searching proxy").build(); - ILicense target = new ILicense() { - - @Override - public String getId() { - return licenseId; - } - - @Override - public void reset() { - // do nothing - } - - @Override - public State matches(String line) { - return State.f; - } - - @Override - public int compareTo(ILicense arg0) { - return searchFamily.compareTo(arg0.getLicenseFamily()); - } - - @Override - public ILicenseFamily getLicenseFamily() { - return searchFamily; - } - - @Override - public String getNotes() { - return null; - } - - @Override - public String derivedFrom() { - return null; - } - - @Override - public State finalizeState() { - return State.f; - } - - @Override - public State currentState() { - return State.f; - } - - }; - return search(target, licenses); - } - - /** - * Search a SortedSet of licenses for the matching license. - * - * @param target the license to search for. - * @param licenses the SortedSet of licenses to search. - * @return the matching license or {@code null} if not found. - */ - static ILicense search(ILicense target, SortedSet licenses) { - SortedSet part = licenses.tailSet(target); - return (!part.isEmpty() && part.first().compareTo(target) == 0) ? part.first() : null; - } - /** * @return The comparator for used to sort Licenses. */ 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 e1369752f..d331223bb 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 @@ -18,8 +18,6 @@ */ package org.apache.rat.license; -import java.util.SortedSet; - /** * The definition of the license family. */ @@ -56,18 +54,6 @@ default int compareTo(ILicenseFamily other) { return getFamilyCategory().compareTo(other.getFamilyCategory()); } - - /** - * Search a SortedSet of ILicenseFamily instances looking for a matching instance. - * @param target The instance to serch for. - * @param licenseFamilies the license families to search - * @return - */ - static ILicenseFamily search(ILicenseFamily target, SortedSet licenseFamilies) { - SortedSet part = licenseFamilies.tailSet(target); - return (!part.isEmpty() && part.first().compareTo(target) == 0) ? part.first() : null; - } - /** * The definition of an ILicenseFamily builder. */ 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 new file mode 100644 index 000000000..a097733a0 --- /dev/null +++ b/apache-rat-core/src/main/java/org/apache/rat/license/LicenseSetFactory.java @@ -0,0 +1,228 @@ +/* + * 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.Collection; +import java.util.Collections; +import java.util.SortedSet; +import java.util.TreeSet; + +/** + * Class to take a set of ILicenses and collecton of approved license categories and extract Subsets. + */ +public class LicenseSetFactory { + + /** + * An enum that defines the types of Licenses to extract. + */ + public enum LicenseFilter { + /** All defined licenses are returned */ + all, + /** Only approved licenses are returned */ + approved, + /** No licenses are returned */ + none + } + + private final SortedSet licenses; + private final Collection approvedLicenses; + + /** + * Constructs a factory with the specified set of Licenses and the approved license collection. + * @param licenses the set of defined licenses. + * @param approvedLicenses the list of approved licenses. + */ + public LicenseSetFactory(SortedSet licenses, Collection approvedLicenses) { + this.licenses = licenses; + this.approvedLicenses = approvedLicenses; + } + + /** + * Create an empty sorted Set with proper comparator. + * @return An empty sorted set of ILicense objects. + */ + public static SortedSet emptyLicenseSet() { + return new TreeSet<>(ILicense.getComparator()); + } + + /** + * Create an empty sorted Set with proper comparator. + * @return An empty sorted set of ILicenseFamily objects. + */ + public static SortedSet emptyLicenseFamilySet() { + return new TreeSet<>(); + } + + /** + * Create a sorted set of licenses families from the collection. + * @param licenses the collection of all licenses. + * @return a SortedSet of license families from the collection. + */ + private static SortedSet extractFamily(Collection licenses) { + SortedSet result = new TreeSet<>(); + licenses.stream().map( ILicense::getLicenseFamily ).forEach(result::add); + return result; + } + + /** + * 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(LicenseFilter filter) { + switch (filter) { + case all: + return Collections.unmodifiableSortedSet(licenses); + case approved: + SortedSet result = LicenseSetFactory.emptyLicenseSet(); + licenses.stream().filter(x -> approvedLicenses.contains(x.getLicenseFamily().getFamilyCategory())) + .forEach(result::add); + return result; + case none: + default: + return Collections.emptySortedSet(); + } + } + + /** + * Gets the LicenseFamily objects based on the filter. + * @param filter the types of LicenseFamily objects to return. + * @return a SortedSet of ILicenseFamily objects. + */ + public SortedSet getLicenseFamilies(LicenseFilter filter) { + switch (filter) { + case all: + return extractFamily(licenses); + case approved: + SortedSet result = LicenseSetFactory.emptyLicenseFamilySet(); + licenses.stream().map(ILicense::getLicenseFamily) + .filter(x -> approvedLicenses.contains(x.getFamilyCategory())) + .forEach(result::add); + return result; + case none: + default: + return Collections.emptySortedSet(); + } + } + + /** + * Gets the categories fof LicenseFamily objects based on the filter. + * @param filter the types of LicenseFamily objects to return. + * @return a SortedSet of ILicenseFamily categories. + */ + public SortedSet getLicenseFamilyIds(LicenseFilter filter) { + SortedSet result = new TreeSet<>(); + switch (filter) { + case all: + licenses.stream().map(x -> x.getLicenseFamily().getFamilyCategory()) + .forEach(result::add); + break; + case approved: + result.addAll(approvedLicenses); + break; + case none: + default: + // do nothing + } + return result; + } + + /** + * Search a SortedSet of licenses for the matching license id. + * + * @param licenseId the id to search for. + * @param licenses the SortedSet of licenses to search. + * @return the matching license or {@code null} if not found. + */ + public static ILicense search(String licenseId, SortedSet licenses) { + ILicenseFamily searchFamily = ILicenseFamily.builder().setLicenseFamilyCategory(licenseId) + .setLicenseFamilyCategory("searching proxy").build(); + ILicense target = new ILicense() { + + @Override + public String getId() { + return licenseId; + } + + @Override + public void reset() { + // do nothing + } + + @Override + public State matches(String line) { + return State.f; + } + + @Override + public int compareTo(ILicense arg0) { + return searchFamily.compareTo(arg0.getLicenseFamily()); + } + + @Override + public ILicenseFamily getLicenseFamily() { + return searchFamily; + } + + @Override + public String getNotes() { + return null; + } + + @Override + public String derivedFrom() { + return null; + } + + @Override + public State finalizeState() { + return State.f; + } + + @Override + public State currentState() { + return State.f; + } + + }; + return search(target, licenses); + } + + /** + * Search a SortedSet of licenses for the matching license. + * + * @param target the license to search for. + * @param licenses the SortedSet of licenses to search. + * @return the matching license or {@code null} if not found. + */ + public static ILicense search(ILicense target, SortedSet licenses) { + SortedSet part = licenses.tailSet(target); + return (!part.isEmpty() && part.first().compareTo(target) == 0) ? part.first() : null; + } + + /** + * Search a SortedSet of ILicenseFamily instances looking for a matching instance. + * @param target The instance to serch for. + * @param licenseFamilies the license families to search + * @return + */ + public static ILicenseFamily search(ILicenseFamily target, SortedSet licenseFamilies) { + SortedSet part = licenseFamilies.tailSet(target); + return (!part.isEmpty() && part.first().compareTo(target) == 0) ? part.first() : null; + } +} 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 33883edcc..af45a18d5 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 @@ -21,12 +21,12 @@ 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.api.MetaData; 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. @@ -39,7 +39,7 @@ public class DefaultPolicy implements IDocumentAnalyser { * @param approvedLicenseFamilies the approved license families. */ public DefaultPolicy(final Collection approvedLicenseFamilies) { - this.approvedLicenseFamilies = new TreeSet<>(); + this.approvedLicenseFamilies = LicenseSetFactory.emptyLicenseFamilySet(); this.approvedLicenseFamilies.addAll(approvedLicenseFamilies); } @@ -55,15 +55,15 @@ public void add(ILicenseFamily approvedLicense) { public void analyse(final Document document) { if (document != null) { boolean approval = false; - if (document.getMetaData().value(MetaData.RAT_URL_LICENSE_FAMILY_CATEGORY) != null) { + if (document.getMetaData().value(MetaData.RAT_URL_HEADER_CATEGORY) != null) { ILicenseFamily licenseFamily = ILicenseFamily.builder() .setLicenseFamilyCategory( - document.getMetaData().value(MetaData.RAT_URL_LICENSE_FAMILY_CATEGORY)) + document.getMetaData().value(MetaData.RAT_URL_HEADER_CATEGORY)) .setLicenseFamilyName(document.getMetaData().value(MetaData.RAT_URL_LICENSE_FAMILY_NAME)) .build(); approval = approvedLicenseFamilies.contains(licenseFamily); + reportLicenseApprovalClaim(document, approval); } - reportLicenseApprovalClaim(document, approval); } } @@ -74,7 +74,7 @@ public void analyse(final Document document) { */ public void reportLicenseApprovalClaim(final Document document, final boolean isAcceptable) { document.getMetaData().set( - isAcceptable ? MetaData.RAT_APPROVED_LICENSE_DATIM_TRUE : MetaData.RAT_APPROVED_LICENSE_DATIM_FALSE); + isAcceptable ? MetaData.RAT_APPROVED_LICENSE_DATUM_TRUE : MetaData.RAT_APPROVED_LICENSE_DATUM_FALSE); } /** 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 8e9fe8ee8..78bb9d171 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 @@ -19,10 +19,10 @@ package org.apache.rat.report.xml; import org.apache.rat.ReportConfiguration; -import org.apache.rat.ReportConfiguration.LicenseFilter; import org.apache.rat.analysis.DefaultAnalyserFactory; import org.apache.rat.document.IDocumentAnalyser; import org.apache.rat.document.impl.util.DocumentAnalyserMultiplexer; +import org.apache.rat.license.LicenseSetFactory.LicenseFilter; import org.apache.rat.policy.DefaultPolicy; import org.apache.rat.report.RatReport; import org.apache.rat.report.claim.ClaimStatistic; diff --git a/apache-rat-core/src/main/resources/org/apache/rat/default.xml b/apache-rat-core/src/main/resources/org/apache/rat/default.xml index 156a8ce6a..89fa786c2 100644 --- a/apache-rat-core/src/main/resources/org/apache/rat/default.xml +++ b/apache-rat-core/src/main/resources/org/apache/rat/default.xml @@ -25,7 +25,7 @@ - + @@ -229,15 +229,15 @@ - + + - diff --git a/apache-rat-core/src/test/java/org/apache/rat/DefaultsTest.java b/apache-rat-core/src/test/java/org/apache/rat/DefaultsTest.java index 7153fe89d..87cd6687c 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/DefaultsTest.java +++ b/apache-rat-core/src/test/java/org/apache/rat/DefaultsTest.java @@ -26,6 +26,7 @@ import java.util.TreeSet; import org.apache.rat.license.ILicense; +import org.apache.rat.license.LicenseSetFactory.LicenseFilter; import org.junit.Test; public class DefaultsTest { @@ -36,7 +37,7 @@ public class DefaultsTest { public void defaultConfigTest() { Defaults defaults = Defaults.builder().build(); - Set licenses = defaults.getLicenses(); + Set licenses = defaults.getLicenses(LicenseFilter.all); Set names = new TreeSet<>(); licenses.forEach(x -> names.add(x.getLicenseFamily().getFamilyCategory())); diff --git a/apache-rat-core/src/test/java/org/apache/rat/ReportConfigurationTest.java b/apache-rat-core/src/test/java/org/apache/rat/ReportConfigurationTest.java index 006698e9e..329de54a4 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/ReportConfigurationTest.java +++ b/apache-rat-core/src/test/java/org/apache/rat/ReportConfigurationTest.java @@ -43,11 +43,12 @@ import java.util.SortedSet; import org.apache.commons.io.function.IOSupplier; -import org.apache.rat.ReportConfiguration.LicenseFilter; import org.apache.rat.config.AddLicenseHeaders; import org.apache.rat.license.ILicense; import org.apache.rat.license.ILicenseFamily; +import org.apache.rat.license.LicenseSetFactory.LicenseFilter; import org.apache.rat.report.IReportable; +import org.apache.rat.testhelpers.TestingLicense; import org.junit.Before; import org.junit.Test; @@ -63,33 +64,56 @@ public void setup() { @Test public void testAddAndRemoveApproveLicenseCategories() { List expected = new ArrayList<>(); + underTest.addLicense( new TestingLicense("Unapproved")); + assertTrue(underTest.getApprovedLicenseCategories().isEmpty()); - underTest.addApprovedLicenseCategory( - ILicenseFamily.builder().setLicenseFamilyCategory("TheCat").setLicenseFamilyName("TheName").build()); + TestingLicense license = new TestingLicense("TheCat"); + underTest.addLicense(license); + underTest.addApprovedLicenseCategory(license.getFamily()); expected.add("TheCa"); SortedSet result = underTest.getApprovedLicenseCategories(); assertEquals(expected.size(), result.size()); assertTrue(result.containsAll(expected)); + SortedSet families = underTest.getLicenseFamilies(LicenseFilter.approved); + assertEquals(expected.size(), families.size()); + SortedSet licenses = underTest.getLicenses(LicenseFilter.approved); + assertEquals(expected.size(), licenses.size()); + underTest.addLicense(new TestingLicense("ACat")); underTest.addApprovedLicenseCategory("ACat"); expected.add("ACat "); result = underTest.getApprovedLicenseCategories(); assertEquals(expected.size(), result.size()); assertTrue(result.containsAll(expected)); + families = underTest.getLicenseFamilies(LicenseFilter.approved); + assertEquals(expected.size(), families.size()); + licenses = underTest.getLicenses(LicenseFilter.approved); + assertEquals(expected.size(), licenses.size()); String[] cats = { "Spot ", "Felix" }; + underTest.addLicense(new TestingLicense("Spot")); + underTest.addLicense(new TestingLicense("Felix")); underTest.addApprovedLicenseCategories(Arrays.asList(cats)); expected.addAll(Arrays.asList(cats)); result = underTest.getApprovedLicenseCategories(); assertEquals(expected.size(), result.size()); assertTrue(result.containsAll(expected)); - + families = underTest.getLicenseFamilies(LicenseFilter.approved); + assertEquals(expected.size(), families.size()); + licenses = underTest.getLicenses(LicenseFilter.approved); + assertEquals(expected.size(), licenses.size()); + underTest.removeApprovedLicenseCategory("Spot "); expected.remove("Spot "); result = underTest.getApprovedLicenseCategories(); assertEquals(expected.size(), result.size()); assertTrue(result.containsAll(expected)); + families = underTest.getLicenseFamilies(LicenseFilter.approved); + assertEquals(expected.size(), families.size()); + licenses = underTest.getLicenses(LicenseFilter.approved); + assertEquals(expected.size(), licenses.size()); + cats[0] = "TheCa"; underTest.removeApprovedLicenseCategories(Arrays.asList(cats)); @@ -97,17 +121,27 @@ public void testAddAndRemoveApproveLicenseCategories() { result = underTest.getApprovedLicenseCategories(); assertEquals(expected.size(), result.size()); assertTrue(result.containsAll(expected)); - } + families = underTest.getLicenseFamilies(LicenseFilter.approved); + assertEquals(expected.size(), families.size()); + licenses = underTest.getLicenses(LicenseFilter.approved); + assertEquals(expected.size(), licenses.size()); } @Test public void testRemoveBeforeAddApproveLicenseCategories() { + underTest.addLicense( new TestingLicense("TheCat")); assertTrue(underTest.getApprovedLicenseCategories().isEmpty()); - + assertTrue(underTest.getLicenseFamilies(LicenseFilter.approved).isEmpty()); + assertTrue(underTest.getLicenses(LicenseFilter.approved).isEmpty()); + underTest.removeApprovedLicenseCategory("TheCat"); assertTrue(underTest.getApprovedLicenseCategories().isEmpty()); + assertTrue(underTest.getLicenseFamilies(LicenseFilter.approved).isEmpty()); + assertTrue(underTest.getLicenses(LicenseFilter.approved).isEmpty()); underTest.addApprovedLicenseCategory("TheCat"); assertTrue(underTest.getApprovedLicenseCategories().isEmpty()); + assertTrue(underTest.getLicenseFamilies(LicenseFilter.approved).isEmpty()); + assertTrue(underTest.getLicenses(LicenseFilter.approved).isEmpty()); } private ILicense mockLicense(String category, String name) { diff --git a/apache-rat-core/src/test/java/org/apache/rat/ReporterTest.java b/apache-rat-core/src/test/java/org/apache/rat/ReporterTest.java index 993a8b6e1..dd15ad1e6 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/ReporterTest.java +++ b/apache-rat-core/src/test/java/org/apache/rat/ReporterTest.java @@ -37,6 +37,9 @@ import org.w3c.dom.Document; import org.w3c.dom.NodeList; +/** + * Tests the output of the Reporter. + */ public class ReporterTest { @Test @@ -57,16 +60,16 @@ public void xmlReportTest() throws Exception { XmlUtils.getNode(doc, xPath, "/rat-report[@timestamp]"); XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/ILoggerFactory.java", "MIT", "true", "standard"); - XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/Image.png", null, "false", "binary"); - XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/LICENSE", null, "false", "notice"); - XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/NOTICE", null, "false", "notice"); + XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/Image.png", null, null, "binary"); + XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/LICENSE", null, null, "notice"); + XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/NOTICE", null, null, "notice"); XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/Source.java", "?????", "false", "standard"); XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/Text.txt", "AL", "true", "standard"); XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/TextHttps.txt", "AL", "true", "standard"); XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/Xml.xml", "AL", "true", "standard"); XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/buildr.rb", "AL", "true", "standard"); - XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/dummy.jar", null, "false", "archive"); - XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/plain.json", null, "false", "binary"); + XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/dummy.jar", null, null, "archive"); + XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/plain.json", null, null, "binary"); XmlUtils.checkNode(doc, xPath, "src/test/resources/elements/sub/Empty.txt", "?????", "false", "standard"); NodeList nodeList = (NodeList) xPath.compile("/rat-report/resource").evaluate(doc, XPathConstants.NODESET); @@ -104,23 +107,22 @@ public void plainReportWithArchivesAndUnapprovedLicenses() throws Exception { find("^Apache Licensed: 4$", document); find("^Generated Documents: 0$", document); find("^2 Unknown Licenses$", document); - find("^Files with unapproved licenses:\\s+" + "src/test/resources/elements/Image.png\\s+" - + "src/test/resources/elements/LICENSE\\s+" + "src/test/resources/elements/NOTICE\\s+" - + "src/test/resources/elements/Source.java\\s+" + "src/test/resources/elements/dummy.jar\\s+" - + "src/test/resources/elements/plain.json\\s+" + "src/test/resources/elements/sub/Empty.txt\\s", + find("^Files with unapproved licenses:\\s+" + + "src/test/resources/elements/Source.java\\s+" + + "src/test/resources/elements/sub/Empty.txt\\s", document); find("^Archives:\\s+" + "\\+ src/test/resources/elements/dummy.jar\\s*", document); find("MIT\\s+src/test/resources/elements/ILoggerFactory.java", document); - find("!B\\s+src/test/resources/elements/Image.png", document); - find("!N\\s+src/test/resources/elements/LICENSE", document); - find("!N\\s+src/test/resources/elements/NOTICE", document); + find("B\\s+src/test/resources/elements/Image.png", document); + find("N\\s+src/test/resources/elements/LICENSE", document); + find("N\\s+src/test/resources/elements/NOTICE", document); find("!\\Q?????\\E\\s+src/test/resources/elements/Source.java", document); find("AL\\s+src/test/resources/elements/Text.txt", document); find("AL\\s+src/test/resources/elements/TextHttps.txt", document); find("AL\\s+src/test/resources/elements/Xml.xml", document); find("AL\\s+src/test/resources/elements/buildr.rb", document); - find("!A\\s+src/test/resources/elements/dummy.jar", document); - find("!B\\s+src/test/resources/elements/plain.json", document); + find("A\\s+src/test/resources/elements/dummy.jar", document); + find("B\\s+src/test/resources/elements/plain.json", document); find("!\\Q?????\\E\\s+src/test/resources/elements/sub/Empty.txt", document); find("== File: src/test/resources/elements/sub/Empty.txt", document); } diff --git a/apache-rat-core/src/test/java/org/apache/rat/analysis/license/AbstractLicenseTest.java b/apache-rat-core/src/test/java/org/apache/rat/analysis/license/AbstractLicenseTest.java index 1e21b72b4..ca608602d 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/analysis/license/AbstractLicenseTest.java +++ b/apache-rat-core/src/test/java/org/apache/rat/analysis/license/AbstractLicenseTest.java @@ -37,9 +37,8 @@ import org.apache.rat.api.MetaData; import org.apache.rat.license.ILicense; import org.apache.rat.license.ILicenseFamily; -import org.junit.AfterClass; +import org.apache.rat.license.LicenseSetFactory.LicenseFilter; import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; /** @@ -49,7 +48,8 @@ abstract public class AbstractLicenseTest { private static int NAME = 0; private static int TEXT = 1; - private static Defaults DEFAULTS; + + private Defaults defaults; protected MetaData data; private final String category; @@ -58,16 +58,6 @@ abstract public class AbstractLicenseTest { private final String[][] targets; - @BeforeClass - public static void init() { - DEFAULTS = Defaults.builder().build(); - } - - @AfterClass - public static void shutdown() { - DEFAULTS.clear(); - } - protected AbstractLicenseTest(String cat, String name, String notes, String[][] targets) { this.category = ILicenseFamily.makeCategory(cat); this.name = name; @@ -78,14 +68,15 @@ protected AbstractLicenseTest(String cat, String name, String notes, String[][] @Before public void setup() { data = new MetaData(); + defaults = Defaults.builder().build(); } - protected static ILicense extractCategory(String category) { + protected ILicense extractCategory(String category) { ILicenseFamily testingFamily = ILicenseFamily.builder() .setLicenseFamilyCategory(category) .setLicenseFamilyName("Testing category").build(); List matchers = new ArrayList<>(); - DEFAULTS.getLicenses().stream().filter(x -> x.getLicenseFamily().compareTo(testingFamily) == 0) + defaults.getLicenses(LicenseFilter.all).stream().filter(x -> x.getLicenseFamily().compareTo(testingFamily) == 0) .forEach(matchers::add); if (matchers.isEmpty()) { fail("No machers for category: " + category); diff --git a/apache-rat-core/src/test/java/org/apache/rat/analysis/license/AppliedApacheSoftwareLicenseTest.java b/apache-rat-core/src/test/java/org/apache/rat/analysis/license/AppliedApacheSoftwareLicenseTest.java index b87862878..abda9f67d 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/analysis/license/AppliedApacheSoftwareLicenseTest.java +++ b/apache-rat-core/src/test/java/org/apache/rat/analysis/license/AppliedApacheSoftwareLicenseTest.java @@ -21,7 +21,7 @@ public class AppliedApacheSoftwareLicenseTest extends AbstractLicenseTest { private static String CATEGORY = "ASL"; - private static String NAME = "Applied Apahce License Version 2.0"; + private static String NAME = "Applied Apache License Version 2.0"; private static String[][] targets = { { "simple", "/*\n" + " * Copyright 2012-2013 FooBar.\n" + " *\n" + " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" + " * you may not use this file except in compliance with the License.\n" + " *\n" 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 fdcda06e2..675d528a4 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 @@ -20,107 +20,110 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import java.util.Collections; -import java.util.stream.Collectors; +import java.util.SortedSet; import org.apache.rat.Defaults; import org.apache.rat.api.Document; import org.apache.rat.api.MetaData; import org.apache.rat.api.MetaData.Datum; -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.TestingLocation; import org.junit.Before; import org.junit.Test; +/** + * Tests the Default policy imlementatin. + */ public class DefaultPolicyTest { - private static final int NUMBER_OF_DEFAULT_ACCEPTED_LICENSES = 15; - - private Document subject; + /** + * 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 = 13; + + private static final ILicenseFamily[] APPROVED_FAMILIES = { + makeFamily("AL", "Apache License Version 2.0"), + makeFamily("ASL", "Applied Apache License Version 2.0"), + makeFamily("BSD-3", "BSD 3 clause"), + makeFamily("CDDL1", "COMMON DEVELOPMENT AND DISTRIBUTION LICENSE Version 1.0"), + makeFamily("DOJO", "DOJO License"), + makeFamily("GEN", "Generated Files"), + makeFamily("GPL1", "GNU General Public License, version 1"), + makeFamily("GPL2", "GNU General Public License, version 2"), + makeFamily("GPL3", "GNU General Public License, version 3"), + 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; + @Before public void setUp() throws Exception { - Defaults defaults = Defaults.builder().build(); - - policy = new DefaultPolicy( - defaults.getLicenses().stream().map(ILicense::getLicenseFamily).collect(Collectors.toList())); - subject = new TestingLocation("subject"); + defaults = Defaults.builder().build(); + policy = new DefaultPolicy(defaults.getLicenseFamilies(LicenseFilter.approved)); + document = new TestingLocation("subject"); } private void assertApproval(boolean pApproved) { assertEquals(pApproved, MetaData.RAT_APPROVED_LICENSE_VALUE_TRUE - .equals(subject.getMetaData().value(MetaData.RAT_URL_APPROVED_LICENSE))); + .equals(document.getMetaData().value(MetaData.RAT_URL_APPROVED_LICENSE))); } private void setMetadata(ILicenseFamily family) { - subject.getMetaData().add(new Datum(MetaData.RAT_URL_LICENSE_FAMILY_NAME, family.getFamilyName())); - subject.getMetaData().add(new Datum(MetaData.RAT_URL_LICENSE_FAMILY_CATEGORY, family.getFamilyCategory())); + document.getMetaData().reportOnLicense(new TestingLicense(family)); } - private ILicenseFamily makeFamily(String category, String name) { + 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.getApprovedLicenseNames().size()); } @Test - public void testALFamily() throws Exception { - setMetadata(makeFamily("AL", "Apache License Version 2.0")); - policy.analyse(subject); - assertApproval(true); - } - - @Test - public void testOASISFamily() throws Exception { - setMetadata(makeFamily("OASIS", "OASIS Open License")); - policy.analyse(subject); - assertApproval(true); - } - - @Test - public void testW3CFamily() throws Exception { - setMetadata(makeFamily("W3C", "W3C Software Copyright")); - policy.analyse(subject); - assertApproval(true); - } - - @Test - public void testW3CDocFamily() throws Exception { - setMetadata(makeFamily("W3CD", "W3C Document Copyright")); - policy.analyse(subject); - assertApproval(true); - } - - @Test - public void testModifiedBSDFamily() throws Exception { - setMetadata(makeFamily("TMF", "The Telemanagement Forum License")); - policy.analyse(subject); - assertApproval(true); + public void testApprovedLicenses() { + + assertEquals("Approved license count mismatch", NUMBER_OF_DEFAULT_ACCEPTED_LICENSES, APPROVED_FAMILIES.length); + for (ILicenseFamily family : APPROVED_FAMILIES) { + setMetadata(family); + policy.analyse(document); + assertApproval(true); + } } - + @Test - public void testMITFamily() throws Exception { - setMetadata(makeFamily("MIT", "The MIT License")); - policy.analyse(subject); - assertApproval(true); - } - - @Test - public void testCDDL1Family() throws Exception { - setMetadata(makeFamily("CDDL1", "COMMON DEVELOPMENT AND DISTRIBUTION LICENSE Version 1.0")); - policy.analyse(subject); - assertApproval(true); + public void testUnApprovedLicenses() { + SortedSet all = defaults.getLicenseFamilies(LicenseFilter.all); + SortedSet unapproved = LicenseSetFactory.emptyLicenseFamilySet(); + unapproved.addAll(all); + unapproved.removeAll(defaults.getLicenseFamilies(LicenseFilter.approved)); + + assertEquals("Unapproved license count mismatch", all.size()-NUMBER_OF_DEFAULT_ACCEPTED_LICENSES, unapproved.size()); + for (ILicenseFamily family : unapproved) { + setMetadata(family); + policy.analyse(document); + assertApproval(false); + } } + @Test public void testUnknownFamily() throws Exception { setMetadata(makeFamily("?????", "Unknown document")); - policy.analyse(subject); + policy.analyse(document); assertApproval(false); } @@ -128,13 +131,13 @@ public void testUnknownFamily() throws Exception { public void testAddNewApprovedLicenseAndDefaults() { ILicenseFamily testingFamily = makeFamily("test", "Testing License Family"); setMetadata(testingFamily); - policy.analyse(subject); + policy.analyse(document); assertApproval(false); policy.add(testingFamily); assertNotNull("Did not properly add ILicenseFamily", - ILicenseFamily.search(testingFamily, policy.getApprovedLicenseNames())); - policy.analyse(subject); + LicenseSetFactory.search(testingFamily, policy.getApprovedLicenseNames())); + policy.analyse(document); assertApproval(true); } @@ -144,14 +147,50 @@ public void testAddNewApprovedLicenseNoDefaults() { assertEquals(0, policy.getApprovedLicenseNames().size()); ILicenseFamily testingFamily = makeFamily("test", "Testing License Family"); setMetadata(testingFamily); - policy.analyse(subject); + policy.analyse(document); assertApproval(false); policy.add(testingFamily); assertEquals(1, policy.getApprovedLicenseNames().size()); assertNotNull("Did not properly add ILicenseFamily", - ILicenseFamily.search(testingFamily, policy.getApprovedLicenseNames())); - policy.analyse(subject); + LicenseSetFactory.search(testingFamily, policy.getApprovedLicenseNames())); + policy.analyse(document); assertApproval(true); } + + @Test + public void testNonStandardDocumentsDoNotFailLicenseTests() { + Datum[] nonStandardDocuments = { + MetaData.RAT_DOCUMENT_CATEGORY_DATUM_NOTICE, + MetaData.RAT_DOCUMENT_CATEGORY_DATUM_ARCHIVE, + MetaData.RAT_DOCUMENT_CATEGORY_DATUM_BINARY + }; + + for (Datum d : nonStandardDocuments) { + document = new TestingLocation("subject"); + document.getMetaData().set(d); + policy.analyse(document); + assertNull( "failed on "+d.getValue(), document.getMetaData().get(MetaData.RAT_URL_APPROVED_LICENSE) ); + } + } + + @Test + public void testUnclassifiedDocumentsDoNotFailLicenseTests() { + document.getMetaData().set(MetaData.RAT_DOCUMENT_CATEGORY_DATUM_STANDARD); + policy.analyse(document); + assertApproval( false ); + } + + @Test + public void testReportLicenseApprovalClaim() { + assertNull( document.getMetaData().get(MetaData.RAT_URL_APPROVED_LICENSE)); + + + policy.reportLicenseApprovalClaim(document, false); + assertEquals( MetaData.RAT_APPROVED_LICENSE_DATUM_FALSE, document.getMetaData().get(MetaData.RAT_URL_APPROVED_LICENSE)); + + policy.reportLicenseApprovalClaim(document, true); + assertEquals( MetaData.RAT_APPROVED_LICENSE_DATUM_TRUE, document.getMetaData().get(MetaData.RAT_URL_APPROVED_LICENSE)); + + } } 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 275fbc09d..980d6db8b 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 @@ -22,42 +22,90 @@ import org.apache.rat.license.ILicense; import org.apache.rat.license.ILicenseFamily; +/** + * A class to quickly build testing licenses. + */ public class TestingLicense implements ILicense { private final ILicenseFamily family; - IHeaderMatcher matcher; + private IHeaderMatcher matcher; private String derivedFrom; private String notes; + /** + * Creates a testing license named "DfltTst" with category of "DfltT" and a default + * TestingMatcher. + * @see TestingMatcher + */ public TestingLicense() { this("DfltTst", new TestingMatcher()); } - + + /** + * Creates a testing license with the specified id and a default TestingMatcher + * @param id The ID to use. + * @see TestingMatcher + */ + public TestingLicense(String id) { + this(id, new TestingMatcher()); + } + + /** + * Creates a testing license wit the specified id and matcher. + * @param id the ID to use + * @param matcher the matcher to execute. + */ public TestingLicense(String id, IHeaderMatcher matcher) { - this.family = ILicenseFamily.builder().setLicenseFamilyCategory(id) - .setLicenseFamilyName("TestingLicense: " + id).build(); + this(matcher, ILicenseFamily.builder().setLicenseFamilyCategory(id) + .setLicenseFamilyName("TestingLicense: " + id).build()); + } + + /** + * Creates a testing license with the specified matcher and family. + * @param matcher the matcher to use. + * @param family the family for this license. + */ + public TestingLicense(IHeaderMatcher matcher, ILicenseFamily family) { + this.family = family; this.matcher = matcher; this.derivedFrom = null; this.notes = null; } + + /** + * Create a testing license for the specified family using a default TestingMatcher + * @param family the family for the license. + * @see TestingMatcher + */ + public TestingLicense(ILicenseFamily family) { + this(new TestingMatcher(), family ); + } @Override public String toString() { return family.toString(); } + /** + * Gets the family from the license + * @return the license family. + */ public ILicenseFamily getFamily() { return family; } + /** + * Gets the matcher from the license + * @return the matcher. + */ public IHeaderMatcher getMatcher() { return matcher; } - public void setMatcher(IHeaderMatcher matcher) { - this.matcher = matcher; - } - + /** + * Sets the derived from value for this license. + * @param derivedFrom the license this license is derived from. + */ public void setDerivedFrom(String derivedFrom) { this.derivedFrom = derivedFrom; } diff --git a/apache-rat-core/src/test/java/org/apache/rat/testhelpers/TestingMatcher.java b/apache-rat-core/src/test/java/org/apache/rat/testhelpers/TestingMatcher.java index 2d6d0b26e..af3a5903d 100644 --- a/apache-rat-core/src/test/java/org/apache/rat/testhelpers/TestingMatcher.java +++ b/apache-rat-core/src/test/java/org/apache/rat/testhelpers/TestingMatcher.java @@ -23,20 +23,37 @@ import org.apache.rat.analysis.matchers.AbstractHeaderMatcher; +/** + * An Matcher for testing. + */ public class TestingMatcher extends AbstractHeaderMatcher { private State lastState; private final boolean[] initialResults; private Queue results; public State finalState = State.f; + /** + * Constructs a matcher with an ID of "dfltMtch" that does not match anyting. + */ public TestingMatcher() { this("dfltMtch", false); } + /** + * Constructs a matcher with the specified id and matching result. + * @param id the ID for this matcher + * @param result if {@code true} will match everything, otherwise it matches nothing. + */ public TestingMatcher(String id, boolean result) { this(id, new boolean[] { result }); } + /** + * Constructs a matcher with the specified ID that returns the matching values in order. + * Will throw NPE if more {@code matches()} are called than there are results. + * @param id the id of the matcher. + * @param results the result for each call to match. + */ public TestingMatcher(String id, boolean... results) { super(id); initialResults = results; diff --git a/apache-rat-plugin/src/main/java/org/apache/rat/mp/RatCheckMojo.java b/apache-rat-plugin/src/main/java/org/apache/rat/mp/RatCheckMojo.java index a8811056b..7a1945cee 100644 --- a/apache-rat-plugin/src/main/java/org/apache/rat/mp/RatCheckMojo.java +++ b/apache-rat-plugin/src/main/java/org/apache/rat/mp/RatCheckMojo.java @@ -21,8 +21,6 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; import org.apache.commons.lang3.StringUtils; import org.apache.maven.plugin.MojoExecutionException; @@ -32,9 +30,9 @@ import org.apache.maven.plugins.annotations.Parameter; import org.apache.rat.Defaults; import org.apache.rat.ReportConfiguration; -import org.apache.rat.ReportConfiguration.LicenseFilter; import org.apache.rat.Reporter; import org.apache.rat.config.AddLicenseHeaders; +import org.apache.rat.license.LicenseSetFactory.LicenseFilter; import org.apache.rat.report.claim.ClaimStatistic; /** diff --git a/apache-rat-plugin/src/main/java/org/apache/rat/mp/RatReportMojo.java b/apache-rat-plugin/src/main/java/org/apache/rat/mp/RatReportMojo.java index b6ee9ce18..11d328e1b 100644 --- a/apache-rat-plugin/src/main/java/org/apache/rat/mp/RatReportMojo.java +++ b/apache-rat-plugin/src/main/java/org/apache/rat/mp/RatReportMojo.java @@ -32,26 +32,6 @@ import java.util.Map; import java.util.ResourceBundle; import java.nio.charset.StandardCharsets; - -/* - * 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.maven.artifact.Artifact; import org.apache.maven.artifact.repository.ArtifactRepository; import org.apache.maven.doxia.sink.Sink; @@ -74,8 +54,8 @@ import org.apache.maven.reporting.MavenMultiPageReport; import org.apache.maven.reporting.MavenReportException; import org.apache.rat.ReportConfiguration; -import org.apache.rat.ReportConfiguration.LicenseFilter; import org.apache.rat.Reporter; +import org.apache.rat.license.LicenseSetFactory.LicenseFilter; import org.codehaus.plexus.util.ReaderFactory; /** diff --git a/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Report.java b/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Report.java index 55c9ad5d5..3606ec78e 100644 --- a/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Report.java +++ b/apache-rat-tasks/src/main/java/org/apache/rat/anttasks/Report.java @@ -37,6 +37,7 @@ import org.apache.rat.configuration.Format; import org.apache.rat.configuration.LicenseReader; import org.apache.rat.configuration.MatcherReader; +import org.apache.rat.license.LicenseSetFactory; import org.apache.rat.configuration.MatcherBuilderTracker; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; @@ -270,12 +271,12 @@ public ApprovalFilter(String s) { @Override public String[] getValues() { - return Arrays.stream(ReportConfiguration.LicenseFilter.values()).map(ReportConfiguration.LicenseFilter::name).collect(Collectors.toList()) - .toArray(new String[ReportConfiguration.LicenseFilter.values().length]); + return Arrays.stream(LicenseSetFactory.LicenseFilter.values()).map(LicenseSetFactory.LicenseFilter::name).collect(Collectors.toList()) + .toArray(new String[LicenseSetFactory.LicenseFilter.values().length]); } - public ReportConfiguration.LicenseFilter internalFilter() { - return ReportConfiguration.LicenseFilter.valueOf(getValue()); + public LicenseSetFactory.LicenseFilter internalFilter() { + return LicenseSetFactory.LicenseFilter.valueOf(getValue()); } } }