Skip to content

Commit

Permalink
spdxGH-238: Return null if ID does not exist
Browse files Browse the repository at this point in the history
`ListedLicenses::getListedLicenseById` and `ListedLicenses::getListedExceptionById` should return `null` if ID does not exist.

Resolves spdxGH-238

Signed-off-by: David Walluck <dwalluck@redhat.com>
  • Loading branch information
dwalluck committed May 16, 2024
1 parent d837b8c commit 7c78e11
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
17 changes: 13 additions & 4 deletions src/main/java/org/spdx/library/model/license/ListedLicenses.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.spdx.Configuration;
import org.spdx.library.InvalidSPDXAnalysisException;
import org.spdx.library.SpdxConstants;
import org.spdx.library.model.SpdxIdNotFoundException;
import org.spdx.library.model.SpdxModelFactory;
import org.spdx.storage.IModelStore;
import org.spdx.storage.listedlicense.IListedLicenseStore;
Expand Down Expand Up @@ -175,13 +176,21 @@ public boolean isSpdxListedExceptionId(String exceptionId) {
* @throws InvalidSPDXAnalysisException
*/
public SpdxListedLicense getListedLicenseById(String licenseId) throws InvalidSPDXAnalysisException {
return (SpdxListedLicense)SpdxModelFactory.createModelObject(this.licenseModelStore, SpdxConstants.LISTED_LICENSE_URL, licenseId, SpdxConstants.CLASS_SPDX_LISTED_LICENSE, null);
try {
return (SpdxListedLicense) SpdxModelFactory.getModelObject(this.licenseModelStore, SpdxConstants.LISTED_LICENSE_URL, licenseId, SpdxConstants.CLASS_SPDX_LISTED_LICENSE, null, false);
} catch (SpdxIdNotFoundException ex) {
return null;
}
}

public ListedLicenseException getListedExceptionById(String exceptionId) throws InvalidSPDXAnalysisException {
return (ListedLicenseException)SpdxModelFactory.createModelObject(this.licenseModelStore, SpdxConstants.LISTED_LICENSE_URL, exceptionId, SpdxConstants.CLASS_SPDX_LISTED_LICENSE_EXCEPTION, null);
try {
return (ListedLicenseException) SpdxModelFactory.getModelObject(this.licenseModelStore, SpdxConstants.LISTED_LICENSE_URL, exceptionId, SpdxConstants.CLASS_SPDX_LISTED_LICENSE_EXCEPTION, null, false);
} catch (SpdxIdNotFoundException ex) {
return null;
}
}

/**
* @return List of all SPDX listed license IDs
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ public void testGetListedLicenseById() throws InvalidSPDXAnalysisException {
assertEquals(id, result.getLicenseId());
}

public void testGetListedLicenseByIdReturnsNull() throws InvalidSPDXAnalysisException {
String id = "XXXX";
SpdxListedLicense result = ListedLicenses.getListedLicenses().getListedLicenseById(id);
assertNull(result);
}

public void testGetLicenseIbyIdLocal() throws InvalidSPDXAnalysisException {
System.setProperty("SPDXParser.OnlyUseLocalLicenses", "true");
ListedLicenses.resetListedLicenses();
Expand Down Expand Up @@ -108,7 +114,15 @@ public void testGetListedExceptionById() throws InvalidSPDXAnalysisException {
LicenseException result = ListedLicenses.getListedLicenses().getListedExceptionById(id);
assertEquals(id, result.getLicenseExceptionId());
}


public void testGetListedExceptionByIdReturnsNull() throws InvalidSPDXAnalysisException {
ListedLicenses.resetListedLicenses();
String id = "XXXX";
assertFalse(ListedLicenses.getListedLicenses().isSpdxListedExceptionId(id));
LicenseException result = ListedLicenses.getListedLicenses().getListedExceptionById(id);
assertNull(result);
}

public void testGetExceptionbyIdLocal() throws InvalidSPDXAnalysisException {
System.setProperty("SPDXParser.OnlyUseLocalLicenses", "true");
ListedLicenses.resetListedLicenses();
Expand Down

0 comments on commit 7c78e11

Please sign in to comment.