Skip to content

Commit f9885f0

Browse files
authored
Merge pull request #211 from loosebazooka/listed-licenses-configurable
Make ListedLicenses more user configurable
2 parents 4318088 + 67dc8c6 commit f9885f0

8 files changed

Lines changed: 130 additions & 15 deletions

File tree

pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,12 @@
189189
<exclude>**/*.java</exclude>
190190
</excludes>
191191
</testResource>
192+
<testResource>
193+
<directory>src/test/resources</directory>
194+
<excludes>
195+
<exclude>**/*.java</exclude>
196+
</excludes>
197+
</testResource>
192198
<testResource>
193199
<filtering>false</filtering>
194200
<directory>TestFiles</directory>

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ private ListedLicenses() {
6464
initializeLicenseModelStore();
6565
}
6666

67+
/**
68+
* This constructor should only be called by the initializeListedLicenses method,
69+
* to programmatically configure licenseModelStore from the application consuming this library
70+
*/
71+
private ListedLicenses(IListedLicenseStore licenseModelStore) {
72+
this.licenseModelStore = licenseModelStore;
73+
}
74+
6775
private void initializeLicenseModelStore() {
6876
listedLicenseModificationLock.writeLock().lock();
6977
try {
@@ -89,7 +97,6 @@ private void initializeLicenseModelStore() {
8997
}
9098

9199
public static ListedLicenses getListedLicenses() {
92-
93100
ListedLicenses retval = null;
94101
listedLicenseModificationLock.readLock().lock();
95102
try {
@@ -110,7 +117,25 @@ public static ListedLicenses getListedLicenses() {
110117
}
111118
return retval;
112119
}
113-
120+
121+
/**
122+
* Initializes the listed licenses singleton from a provided cache. This will
123+
* ignore all configuration around fetching remote licenses.
124+
*
125+
* @param licenseStore a preconfigured licenseStore, see {@link SpdxListedLicenseLocalStore} for
126+
* an example.
127+
* @return a singleton instance
128+
*/
129+
public static ListedLicenses initializeListedLicenses(IListedLicenseStore licenseStore) {
130+
listedLicenseModificationLock.writeLock().lock();
131+
try {
132+
listedLicenses = new ListedLicenses(licenseStore);
133+
return listedLicenses;
134+
} finally {
135+
listedLicenseModificationLock.writeLock().unlock();
136+
}
137+
}
138+
114139
/**
115140
* Resets all of the cached license information and reloads the license IDs
116141
* NOTE: This method should be used with caution, it will negatively impact

src/main/java/org/spdx/storage/listedlicense/SpdxListedLicenseLocalStore.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public SpdxListedLicenseLocalStore() throws InvalidSPDXAnalysisException {
3838
}
3939

4040
@Override
41-
InputStream getTocInputStream() throws IOException {
41+
public InputStream getTocInputStream() throws IOException {
4242
String fileName = LISTED_LICENSE_JSON_LOCAL_DIR + "/" + LICENSE_TOC_FILENAME;
4343
InputStream retval = SpdxListedLicenseLocalStore.class.getResourceAsStream("/" + fileName);
4444
if (retval == null) {
@@ -48,7 +48,7 @@ InputStream getTocInputStream() throws IOException {
4848
}
4949

5050
@Override
51-
InputStream getLicenseInputStream(String licenseId) throws IOException {
51+
public InputStream getLicenseInputStream(String licenseId) throws IOException {
5252

5353
String fileName = LISTED_LICENSE_JSON_LOCAL_DIR + "/" + licenseId + JSON_SUFFIX;
5454
InputStream retval = SpdxListedLicenseLocalStore.class.getResourceAsStream("/" + fileName);
@@ -59,7 +59,7 @@ InputStream getLicenseInputStream(String licenseId) throws IOException {
5959
}
6060

6161
@Override
62-
InputStream getExceptionTocInputStream() throws IOException {
62+
public InputStream getExceptionTocInputStream() throws IOException {
6363
String fileName = LISTED_LICENSE_JSON_LOCAL_DIR + "/" + EXCEPTION_TOC_FILENAME;
6464
InputStream retval = SpdxListedLicenseLocalStore.class.getResourceAsStream("/" + fileName);
6565
if (retval == null) {
@@ -69,7 +69,7 @@ InputStream getExceptionTocInputStream() throws IOException {
6969
}
7070

7171
@Override
72-
InputStream getExceptionInputStream(String exceptionId) throws IOException {
72+
public InputStream getExceptionInputStream(String exceptionId) throws IOException {
7373
return getLicenseInputStream(exceptionId);
7474
}
7575

src/main/java/org/spdx/storage/listedlicense/SpdxListedLicenseModelStore.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -114,25 +114,25 @@ public SpdxListedLicenseModelStore() throws InvalidSPDXAnalysisException {
114114
* @return InputStream for the Table of Contents of the licenses formated in JSON SPDX
115115
* @throws IOException
116116
*/
117-
abstract InputStream getTocInputStream() throws IOException;
117+
public abstract InputStream getTocInputStream() throws IOException;
118118

119119
/**
120120
* @return InputStream for the Table of Contents of the exceptions formated in JSON SPDX
121121
* @throws IOException
122122
*/
123-
abstract InputStream getExceptionTocInputStream() throws IOException;
123+
public abstract InputStream getExceptionTocInputStream() throws IOException;
124124

125125
/**
126126
* @return InputStream for a license formated in SPDX JSON
127127
* @throws IOException
128128
*/
129-
abstract InputStream getLicenseInputStream(String licenseId) throws IOException;
129+
public abstract InputStream getLicenseInputStream(String licenseId) throws IOException;
130130

131131
/**
132132
* @return InputStream for an exception formated in SPDX JSON
133133
* @throws IOException
134134
*/
135-
abstract InputStream getExceptionInputStream(String exceptionId) throws IOException;
135+
public abstract InputStream getExceptionInputStream(String exceptionId) throws IOException;
136136

137137
/**
138138
* Loads all license and exception ID's from the appropriate JSON files

src/main/java/org/spdx/storage/listedlicense/SpdxListedLicenseWebStore.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,22 @@ private InputStream getUrlInputStream(final URL url) throws IOException {
4444
}
4545

4646
@Override
47-
InputStream getTocInputStream() throws IOException {
47+
public InputStream getTocInputStream() throws IOException {
4848
return getUrlInputStream(new URL(SpdxConstants.LISTED_LICENSE_URL + LICENSE_TOC_FILENAME));
4949
}
5050

5151
@Override
52-
InputStream getLicenseInputStream(String licenseId) throws IOException {
52+
public InputStream getLicenseInputStream(String licenseId) throws IOException {
5353
return getUrlInputStream(new URL(SpdxConstants.LISTED_LICENSE_URL + licenseId + JSON_SUFFIX));
5454
}
5555

5656
@Override
57-
InputStream getExceptionTocInputStream() throws IOException {
57+
public InputStream getExceptionTocInputStream() throws IOException {
5858
return getUrlInputStream(new URL(SpdxConstants.LISTED_LICENSE_URL + EXCEPTION_TOC_FILENAME));
5959
}
6060

6161
@Override
62-
InputStream getExceptionInputStream(String exceptionId) throws IOException {
62+
public InputStream getExceptionInputStream(String exceptionId) throws IOException {
6363
return getLicenseInputStream(exceptionId); // Same URL using exception ID rather than license ID
6464
}
6565

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

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
package org.spdx.library.model.license;
2+
import java.io.ByteArrayInputStream;
3+
import java.io.IOException;
4+
import java.io.InputStream;
25
import java.util.List;
36
import java.util.Optional;
47

@@ -7,6 +10,7 @@
710
import org.spdx.library.SpdxConstants;
811

912
import junit.framework.TestCase;
13+
import org.spdx.storage.listedlicense.SpdxListedLicenseModelStore;
1014

1115
/**
1216
* Copyright (c) 2019 Source Auditor Inc.
@@ -120,7 +124,9 @@ public void testGetExceptionbyIdLocal() throws InvalidSPDXAnalysisException {
120124
}
121125

122126
public void testGetExceptionIds() throws InvalidSPDXAnalysisException {
123-
assertTrue(ListedLicenses.getListedLicenses().getSpdxListedExceptionIds().size() >= NUM_3_7_EXCEPTION);
127+
List<String> result = ListedLicenses.getListedLicenses().getSpdxListedExceptionIds();
128+
assertTrue(result.size() >= NUM_3_7_EXCEPTION);
129+
assertTrue(result.contains("389-exception"));
124130
}
125131

126132
public void testListedLicenseIdCaseSensitive() {
@@ -154,4 +160,47 @@ public void testGetExceptionIdProperty() throws InvalidSPDXAnalysisException {
154160
assertTrue(idProp.get() instanceof String);
155161
assertEquals(id, idProp.get());
156162
}
163+
164+
public void testLicenseListInitializeListedLicenses() throws InvalidSPDXAnalysisException {
165+
try {
166+
ListedLicenses.initializeListedLicenses(new SpdxListedLicenseModelStore() {
167+
@Override
168+
public InputStream getTocInputStream() throws IOException {
169+
return ListedLicensesTest.class.getResourceAsStream("licenses.json");
170+
}
171+
172+
@Override
173+
public InputStream getExceptionTocInputStream() throws IOException {
174+
return ListedLicensesTest.class.getResourceAsStream("exceptions.json");
175+
}
176+
177+
@Override
178+
public InputStream getLicenseInputStream(String licenseId) throws IOException {
179+
throw new UnsupportedOperationException("this shouldn't be used in tests");
180+
}
181+
182+
@Override
183+
public InputStream getExceptionInputStream(String exceptionId) throws IOException {
184+
throw new UnsupportedOperationException("this shouldn't be used in tests");
185+
}
186+
187+
@Override
188+
public void close() throws Exception {
189+
// Nothing to do for the either the in-memory or the web store
190+
}
191+
});
192+
List<String> licenseIds = ListedLicenses.getListedLicenses().getSpdxListedLicenseIds();
193+
assertEquals(1, licenseIds.size());
194+
assertFalse(licenseIds.contains("Apache-2.0"));
195+
assertTrue(licenseIds.contains("TEST"));
196+
197+
List<String> exceptionIds = ListedLicenses.getListedLicenses().getSpdxListedExceptionIds();
198+
assertEquals(1, licenseIds.size());
199+
assertFalse(exceptionIds.contains("389-exception"));
200+
assertTrue(exceptionIds.contains("TEST-exception"));
201+
} finally {
202+
// since ListedLicenses in a singleton, reset it after running this test
203+
ListedLicenses.resetListedLicenses();
204+
}
205+
}
157206
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"licenseListVersion": "1.1-test",
3+
"exceptions": [
4+
{
5+
"reference": "./TEST-exception.json",
6+
"isDeprecatedLicenseId": false,
7+
"detailsUrl": "./TEST-exception.html",
8+
"referenceNumber": 1,
9+
"name": "TEST Directory Server Exception",
10+
"licenseExceptionId": "TEST-exception",
11+
"seeAlso": [
12+
"http://example.com/TEST_Exception_License_Text"
13+
]
14+
}
15+
],
16+
"releaseDate": "1999-01-01"
17+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"licenseListVersion": "1.1-test",
3+
"licenses": [
4+
{
5+
"reference": "https://spdx.org/licenses/test.html",
6+
"isDeprecatedLicenseId": false,
7+
"detailsUrl": "https://spdx.org/licenses/test.json",
8+
"referenceNumber": 1,
9+
"name": "Some fake licenses for tests",
10+
"licenseId": "TEST",
11+
"seeAlso": [
12+
"https://opensource.org/licenses/TEST"
13+
],
14+
"isOsiApproved": true
15+
}
16+
],
17+
"releaseDate": "1999-01-01"
18+
}

0 commit comments

Comments
 (0)