Skip to content

Commit d307f84

Browse files
committed
isStandardLicenseWithinText - match spaces after var rule
Fixes #241 Fixes #234 Include code provided by @pmonk in #236 modified for version 2.0+ Signed-off-by: Gary O'Neall <gary@sourceauditor.com>
1 parent bb41916 commit d307f84

File tree

3 files changed

+86
-1
lines changed

3 files changed

+86
-1
lines changed

src/main/java/org/spdx/utility/compare/TemplateRegexMatcher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public String getPattern() {
126126

127127
@Override
128128
public String toString() {
129-
return "(" + pattern + ")"; // We always treat the pattern as a group
129+
return "(" + pattern + ")" + "\\s*"; // We always treat the pattern as a group
130130
}
131131

132132
/**
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
}

src/test/java/org/spdx/utility/compare/LicenseCompareHelperTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -911,6 +911,8 @@ public void test2Spaces() throws InvalidSPDXAnalysisException, SpdxCompareExcept
911911
if (diff.isDifferenceFound()) {
912912
fail(diff.getDifferenceMessage());
913913
}
914+
boolean result = LicenseCompareHelper.isStandardLicenseWithinText(licText, lic);
915+
assertTrue(result);
914916
}
915917

916918
public void testBsdNewLine() throws InvalidSPDXAnalysisException, SpdxCompareException, IOException {

0 commit comments

Comments
 (0)