|
| 1 | +package org.spdx.utility.compare; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.Collections; |
| 5 | +import java.util.List; |
| 6 | +import java.util.Objects; |
| 7 | + |
| 8 | +import org.spdx.core.InvalidSPDXAnalysisException; |
| 9 | +import org.spdx.library.LicenseInfoFactory; |
| 10 | +import org.spdx.library.model.v3_0_1.expandedlicensing.ListedLicense; |
| 11 | + |
| 12 | + |
| 13 | +public class CompareConsistencyHelper { |
| 14 | + |
| 15 | + /** |
| 16 | + * Tests for consistency across the various comparison methods in LicenseCompareHelper. |
| 17 | + * |
| 18 | + * Note: assumes that `text` contains just a single license text, with no extraneous prefix or suffix text. |
| 19 | + * |
| 20 | + * @param licenseId The SPDX license identifier that's expected to be detected within `text`. |
| 21 | + * @param text The license text being used to test API consistency. |
| 22 | + * @return null if no inconsistencies were found, or a String describing the detected inconsistencies. |
| 23 | + */ |
| 24 | + public static String explainCompareInconsistencies(final String licenseId, final String text) throws InvalidSPDXAnalysisException, SpdxCompareException { |
| 25 | + StringBuilder result = new StringBuilder(); |
| 26 | + |
| 27 | + // PRECONDITIONS |
| 28 | + if (licenseId == null || licenseId.trim().length() == 0) { |
| 29 | + throw new IllegalArgumentException("licenseId was null or blank"); |
| 30 | + } |
| 31 | + |
| 32 | + if (text == null || text.trim().length() == 0) { |
| 33 | + throw new IllegalArgumentException("text was null or blank"); |
| 34 | + } |
| 35 | + |
| 36 | + // Body |
| 37 | + final ListedLicense listedLicense = LicenseInfoFactory.getListedLicenseById(licenseId); |
| 38 | + |
| 39 | + if (listedLicense == null) { |
| 40 | + throw new IllegalArgumentException("Could not find listed license for identifier " + licenseId); |
| 41 | + } |
| 42 | + |
| 43 | + final boolean isDifferenceFound = LicenseCompareHelper.isTextStandardLicense(listedLicense, text).isDifferenceFound(); |
| 44 | + final boolean isStandardLicenseWithinText = LicenseCompareHelper.isStandardLicenseWithinText(text, listedLicense); |
| 45 | + final List<String> matchingStandardLicenseIds = Arrays.asList(LicenseCompareHelper.matchingStandardLicenseIds(text)); |
| 46 | + final List<String> matchingStandardLicenseIdsWithinText = LicenseCompareHelper.matchingStandardLicenseIdsWithinText(text); |
| 47 | + |
| 48 | + // Note: we sort these lists because we don't care about different orderings within them - just that they contain the same elements (in any order) |
| 49 | + Collections.sort(matchingStandardLicenseIds); |
| 50 | + Collections.sort(matchingStandardLicenseIdsWithinText); |
| 51 | + |
| 52 | + if (isDifferenceFound == isStandardLicenseWithinText) { // Note: this condition may seem backwards, but only because one variable indicates whether there was a difference, while the other indicates whether the license was found (they're logically opposite) |
| 53 | + result.append(" * .isTextStandardLicense() and .isStandardLicenseWithinText()\n"); |
| 54 | + } |
| 55 | + |
| 56 | + if (!isDifferenceFound && !matchingStandardLicenseIds.contains(licenseId)) { |
| 57 | + result.append(" * .isTextStandardLicense() and .matchingStandardLicenseIds()\n"); |
| 58 | + } |
| 59 | + |
| 60 | + if (!isDifferenceFound && !matchingStandardLicenseIdsWithinText.contains(licenseId)) { |
| 61 | + result.append(" * .isTextStandardLicense() and .matchingStandardLicensesWithinText()\n"); |
| 62 | + } |
| 63 | + |
| 64 | + if (isStandardLicenseWithinText && !matchingStandardLicenseIds.contains(licenseId)) { |
| 65 | + result.append(" * .isStandardLicenseWithinText() and .matchingStandardLicenseIds()\n"); |
| 66 | + } |
| 67 | + |
| 68 | + if (isStandardLicenseWithinText && !matchingStandardLicenseIdsWithinText.contains(licenseId)) { |
| 69 | + result.append(" * .isStandardLicenseWithinText() and .matchingStandardLicensesWithinText()\n"); |
| 70 | + } |
| 71 | + |
| 72 | + if (!Objects.equals(matchingStandardLicenseIds, matchingStandardLicenseIdsWithinText)) { |
| 73 | + result.append(" * .matchingStandardLicenseIds() and .matchingStandardLicenseIdsWithinText()\n"); |
| 74 | + } |
| 75 | + |
| 76 | + if (result.toString().trim().isEmpty()) { |
| 77 | + return null; |
| 78 | + } else { |
| 79 | + return(("While testing API consistency with a " + licenseId + " text, inconsistencies were found between LicenseCompareHelper APIs:\n" + result.toString()).trim()); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | +} |
0 commit comments