Skip to content

Commit

Permalink
Add unit tests that exercise issue spdx#234
Browse files Browse the repository at this point in the history
  • Loading branch information
pmonks committed Apr 23, 2024
1 parent d837b8c commit 8efe8d8
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.spdx.utility.compare;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.spdx.library.InvalidSPDXAnalysisException;
import org.spdx.library.model.license.LicenseInfoFactory;
import org.spdx.library.model.license.SpdxListedLicense;

public class CompareConsistencyHelper {

/**
* Tests for consistency across the various comparison methods in LicenseCompareHelper, and returns either null
* (no inconsistencies found), or a String describing all of the detected inconsistencies.
*
* Note: assumes that `text` contains just a single license text, with no extraneous prefix or suffix text.
*
* @param licenseId The SPDX license identifier that's expected to be detected within text.
* @param text The license text being tested.
* @return Whether consistency across the APIs under test was found or not.
*/
public static String explainCompareInconsistencies(final String licenseId, final String text) throws InvalidSPDXAnalysisException, SpdxCompareException {
StringBuilder result = new StringBuilder();

// PRECONDITIONS
if (licenseId == null || licenseId.trim().length() == 0) {
throw new IllegalArgumentException("licenseId was null or blank");
}

if (text == null || text.trim().length() == 0) {
throw new IllegalArgumentException("text was null or blank");
}

// Body
final SpdxListedLicense listedLicense = LicenseInfoFactory.getListedLicenseById(licenseId);

if (listedLicense == null) {
throw new IllegalArgumentException("could not find listed license for identifier" + licenseId);
}

final boolean differenceFound = LicenseCompareHelper.isTextStandardLicense(listedLicense, text).isDifferenceFound();
final boolean standardLicenseWithinText = LicenseCompareHelper.isStandardLicenseWithinText(text, listedLicense);
final List<String> standardLicensesWithinText = Arrays.asList(LicenseCompareHelper.matchingStandardLicenseIds(text));
final List<String> matchingStandardLicensesWithinText = LicenseCompareHelper.matchingStandardLicenseIdsWithinText(text);

// Note: we sort the elements because we don't care about different orderings within each list - just that they contain the same elements (in any order)
Collections.sort(standardLicensesWithinText);
Collections.sort(matchingStandardLicensesWithinText);

if (differenceFound == standardLicenseWithinText) { // Note: this condition seems 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)
result.append(" * LicenseCompareHelper.isTextStandardLicense() and LicenseCompareHelper.isStandardLicenseWithinText()\n");
}

if (standardLicenseWithinText && (standardLicensesWithinText == null || standardLicensesWithinText.isEmpty())) {
result.append(" * LicenseCompareHelper.isStandardLicenseWithinText() and LicenseCompareHelper.matchingStandardLicenseIds()\n");
}

if (standardLicenseWithinText && (matchingStandardLicensesWithinText == null || matchingStandardLicensesWithinText.isEmpty())) {
result.append(" * LicenseCompareHelper.isStandardLicenseWithinText() and LicenseCompareHelper.matchingStandardLicensesWithinText()\n");
}

if (!Objects.equals(standardLicensesWithinText, matchingStandardLicensesWithinText)) {
result.append(" * LicenseCompareHelper.matchingStandardLicenseIds() and LicenseCompareHelper.matchingStandardLicenseIdsWithinText()");
}

if (result.toString().trim().isEmpty()) {
return null;
} else {
return(("Inconsistencies found between:\n" + result.toString()).trim());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -1080,5 +1080,20 @@ public void testImageMagikTextWithin() throws InvalidSPDXAnalysisException, Spdx
.setTemplate(templateText));
assertFalse(LicenseCompareHelper.isStandardLicenseWithinText(licText, lic));
}


private void consistencyTest(String licenseId, String text) throws InvalidSPDXAnalysisException, SpdxCompareException {
final String inconsistencies = CompareConsistencyHelper.explainCompareInconsistencies(licenseId, text);
assertNull(inconsistencies, inconsistencies);
}

public void testAPIConsistency() throws InvalidSPDXAnalysisException, SpdxCompareException, IOException {
consistencyTest("MIT", UnitTestHelper.fileToText(MIT_2_SPACES));
consistencyTest("BSD-2-Clause", UnitTestHelper.fileToText(BSD_2_CLAUSE_NL));

if (UnitTestHelper.runSlowTests()) {
consistencyTest("GPL-2.0-only", UnitTestHelper.fileToText(GPL_2_TEXT));
consistencyTest("MPL-2.0", UnitTestHelper.fileToText(MPL_2_FROM_MOZILLA_FILE));
}
}

}

0 comments on commit 8efe8d8

Please sign in to comment.