Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-238: Return null if ID does not exist #239

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 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,11 +176,19 @@ 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;
}
}

/**
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 @@ -109,6 +115,14 @@ public void testGetListedExceptionById() throws InvalidSPDXAnalysisException {
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
Loading