Skip to content

Commit 6388208

Browse files
dwalluckgoneall
authored andcommitted
GH-238: Return null if ID does not exist
`ListedLicenses::getListedLicenseById` and `ListedLicenses::getListedExceptionById` should return `null` if ID does not exist. Resolves GH-238 Signed-off-by: David Walluck <[email protected]>
1 parent d837b8c commit 6388208

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

src/main/java/org/spdx/library/model/license/ListedLicenses.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.spdx.Configuration;
2929
import org.spdx.library.InvalidSPDXAnalysisException;
3030
import org.spdx.library.SpdxConstants;
31+
import org.spdx.library.model.SpdxIdNotFoundException;
3132
import org.spdx.library.model.SpdxModelFactory;
3233
import org.spdx.storage.IModelStore;
3334
import org.spdx.storage.listedlicense.IListedLicenseStore;
@@ -175,11 +176,19 @@ public boolean isSpdxListedExceptionId(String exceptionId) {
175176
* @throws InvalidSPDXAnalysisException
176177
*/
177178
public SpdxListedLicense getListedLicenseById(String licenseId) throws InvalidSPDXAnalysisException {
178-
return (SpdxListedLicense)SpdxModelFactory.createModelObject(this.licenseModelStore, SpdxConstants.LISTED_LICENSE_URL, licenseId, SpdxConstants.CLASS_SPDX_LISTED_LICENSE, null);
179+
try {
180+
return (SpdxListedLicense) SpdxModelFactory.getModelObject(this.licenseModelStore, SpdxConstants.LISTED_LICENSE_URL, licenseId, SpdxConstants.CLASS_SPDX_LISTED_LICENSE, null, false);
181+
} catch (SpdxIdNotFoundException ex) {
182+
return null;
183+
}
179184
}
180185

181186
public ListedLicenseException getListedExceptionById(String exceptionId) throws InvalidSPDXAnalysisException {
182-
return (ListedLicenseException)SpdxModelFactory.createModelObject(this.licenseModelStore, SpdxConstants.LISTED_LICENSE_URL, exceptionId, SpdxConstants.CLASS_SPDX_LISTED_LICENSE_EXCEPTION, null);
187+
try {
188+
return (ListedLicenseException) SpdxModelFactory.getModelObject(this.licenseModelStore, SpdxConstants.LISTED_LICENSE_URL, exceptionId, SpdxConstants.CLASS_SPDX_LISTED_LICENSE_EXCEPTION, null, false);
189+
} catch (SpdxIdNotFoundException ex) {
190+
return null;
191+
}
183192
}
184193

185194
/**

src/test/java/org/spdx/library/model/license/ListedLicensesTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ public void testGetListedLicenseById() throws InvalidSPDXAnalysisException {
8080
assertEquals(id, result.getLicenseId());
8181
}
8282

83+
public void testGetListedLicenseByIdReturnsNull() throws InvalidSPDXAnalysisException {
84+
String id = "XXXX";
85+
SpdxListedLicense result = ListedLicenses.getListedLicenses().getListedLicenseById(id);
86+
assertNull(result);
87+
}
88+
8389
public void testGetLicenseIbyIdLocal() throws InvalidSPDXAnalysisException {
8490
System.setProperty("SPDXParser.OnlyUseLocalLicenses", "true");
8591
ListedLicenses.resetListedLicenses();
@@ -109,6 +115,14 @@ public void testGetListedExceptionById() throws InvalidSPDXAnalysisException {
109115
assertEquals(id, result.getLicenseExceptionId());
110116
}
111117

118+
public void testGetListedExceptionByIdReturnsNull() throws InvalidSPDXAnalysisException {
119+
ListedLicenses.resetListedLicenses();
120+
String id = "XXXX";
121+
assertFalse(ListedLicenses.getListedLicenses().isSpdxListedExceptionId(id));
122+
LicenseException result = ListedLicenses.getListedLicenses().getListedExceptionById(id);
123+
assertNull(result);
124+
}
125+
112126
public void testGetExceptionbyIdLocal() throws InvalidSPDXAnalysisException {
113127
System.setProperty("SPDXParser.OnlyUseLocalLicenses", "true");
114128
ListedLicenses.resetListedLicenses();

0 commit comments

Comments
 (0)