Skip to content

Commit f60be43

Browse files
authored
Merge pull request #249 from spdx/spaceafterregex
isStandardLicenseWithinText - match spaces after var rule
2 parents bb41916 + 64e44d2 commit f60be43

File tree

3 files changed

+100
-1
lines changed

3 files changed

+100
-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: 16 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 {
@@ -973,4 +975,18 @@ public void testImageMagikTextWithin() throws InvalidSPDXAnalysisException, Spdx
973975
assertFalse(LicenseCompareHelper.isStandardLicenseWithinText(licText, lic));
974976
}
975977

978+
private void assertAPIConsistency(final String licenseId, final String text) throws InvalidSPDXAnalysisException, SpdxCompareException {
979+
final String inconsistencies = CompareConsistencyHelper.explainCompareInconsistencies(licenseId, text);
980+
assertNull(inconsistencies, inconsistencies);
981+
}
982+
983+
public void testAPIConsistency() throws InvalidSPDXAnalysisException, SpdxCompareException, IOException {
984+
assertAPIConsistency("MIT", UnitTestHelper.fileToText(MIT_2_SPACES));
985+
assertAPIConsistency("BSD-2-Clause", UnitTestHelper.fileToText(BSD_2_CLAUSE_NL));
986+
987+
if (UnitTestHelper.runSlowTests()) {
988+
assertAPIConsistency("GPL-2.0-only", UnitTestHelper.fileToText(GPL_2_TEXT));
989+
assertAPIConsistency("MPL-2.0", UnitTestHelper.fileToText(MPL_2_FROM_MOZILLA_FILE));
990+
}
991+
}
976992
}

0 commit comments

Comments
 (0)