Skip to content

Commit 1a511f9

Browse files
committed
Fixes to support changes in the v2 model
Signed-off-by: Gary O'Neall <gary@sourceauditor.com>
1 parent b43ec07 commit 1a511f9

4 files changed

Lines changed: 160 additions & 2 deletions

File tree

src/main/java/org/spdx/library/ModelCopyManager.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ public TypedValue copy(IModelStore toStore, IModelStore fromStore,
259259
String toObjectUri = getCopiedObjectUri(fromStore, sourceUri, toStore);
260260
if (Objects.isNull(toObjectUri)) {
261261
if (toStore.exists(sourceUri) || IdType.Anonymous.equals(fromStore.getIdType(sourceUri))) {
262+
// TODO - the toNamspace will never be used - we need to add a from namespace
262263
if (Objects.nonNull(toNamespace)) {
263264
if (SpdxConstantsCompatV2.CLASS_EXTERNAL_DOC_REF.equals(type)) {
264265
toObjectUri = toNamespace + toStore.getNextId(IdType.DocumentRef);

src/main/java/org/spdx/utility/license/LicenseExpressionParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ private static AnyLicenseInfo parseSimpleLicenseTokenCompatV2(String token, IMod
553553
Objects.requireNonNull(documentUri, "URI prefix can not be null");
554554
if (token.contains(":")) {
555555
// External License Ref
556-
return new ExternalExtractedLicenseInfo(store, documentUri, token, copyManager, true);
556+
return new ExternalExtractedLicenseInfo(store, documentUri, token, copyManager);
557557
}
558558
Optional<String> licenseId = Optional.empty();
559559
if (LicenseInfoFactory.isSpdxListedLicenseId(token)) {
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package org.spdx.library;
2+
import java.util.List;
3+
import java.util.Optional;
4+
5+
import org.spdx.core.InvalidSPDXAnalysisException;
6+
import org.spdx.library.model.v2.SpdxConstantsCompatV2;
7+
import org.spdx.library.model.v2.license.LicenseException;
8+
import org.spdx.library.model.v2.license.ListedLicenseException;
9+
import org.spdx.library.model.v2.license.SpdxListedLicense;
10+
import org.spdx.storage.CompatibleModelStoreWrapper;
11+
12+
import junit.framework.TestCase;
13+
14+
/**
15+
* Copyright (c) 2019 Source Auditor Inc.
16+
*
17+
* SPDX-License-Identifier: Apache-2.0
18+
*
19+
* Licensed under the Apache License, Version 2.0 (the "License");
20+
* you may not use this file except in compliance with the License.
21+
* You may obtain a copy of the License at
22+
*
23+
* http://www.apache.org/licenses/LICENSE-2.0
24+
*
25+
* Unless required by applicable law or agreed to in writing, software
26+
* distributed under the License is distributed on an "AS IS" BASIS,
27+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
28+
* See the License for the specific language governing permissions and
29+
* limitations under the License.
30+
*/
31+
32+
/**
33+
* @author yevster@gmail.com, Black Duck Software
34+
* SPDX-License-Identifier: Apache-2.0
35+
*/
36+
public class ListedLicensesTest extends TestCase {
37+
38+
@SuppressWarnings("unused")
39+
private static final String LICENSE_LIST_VERSION = "3.7";
40+
private static final int NUM_3_7_LICENSES = 373;
41+
private static final int NUM_3_7_EXCEPTION = 36;
42+
43+
/* (non-Javadoc)
44+
* @see junit.framework.TestCase#setUp()
45+
*/
46+
protected void setUp() throws Exception {
47+
super.setUp();
48+
SpdxModelFactory.init();
49+
}
50+
51+
/* (non-Javadoc)
52+
* @see junit.framework.TestCase#tearDown()
53+
*/
54+
protected void tearDown() throws Exception {
55+
super.tearDown();
56+
}
57+
58+
/**
59+
* Test method for {@link org.spdx.library.model.compat.v2.compat.v2.license.ListedLicenses#isSpdxListedLicenseId(java.lang.String)}.
60+
*/
61+
public void testIsSpdxListedLicenseID() {
62+
assertTrue(ListedLicenses.getListedLicenses().isSpdxListedLicenseId("Apache-2.0"));
63+
}
64+
65+
/**
66+
* Test method for {@link org.spdx.library.model.compat.v2.compat.v2.license.ListedLicenses#getListedLicenseById(java.lang.String)}.
67+
* @throws InvalidSPDXAnalysisException
68+
*/
69+
public void testGetListedLicenseById() throws InvalidSPDXAnalysisException {
70+
String id = "Apache-2.0";
71+
SpdxListedLicense result = ListedLicenses.getListedLicenses().getListedLicenseByIdCompatV2(id);
72+
assertEquals(id, result.getLicenseId());
73+
}
74+
75+
public void testGetLicenseIbyIdLocal() throws InvalidSPDXAnalysisException {
76+
System.setProperty("SPDXParser.OnlyUseLocalLicenses", "true");
77+
ListedLicenses.resetListedLicenses();
78+
try {
79+
String id = "Apache-2.0";
80+
SpdxListedLicense result = ListedLicenses.getListedLicenses().getListedLicenseByIdCompatV2(id);
81+
assertEquals(id, result.getLicenseId());
82+
} finally {
83+
System.setProperty("SPDXParser.OnlyUseLocalLicenses", "false");
84+
ListedLicenses.resetListedLicenses();
85+
}
86+
}
87+
/**
88+
* Test method for {@link org.spdx.library.model.compat.v2.compat.v2.license.ListedLicenses#getSpdxListedLicenseIds()}.
89+
*/
90+
public void testGetSpdxListedLicenseIds() {
91+
List<String> result = ListedLicenses.getListedLicenses().getSpdxListedLicenseIds();
92+
assertTrue(result.size() >= NUM_3_7_LICENSES);
93+
assertTrue(result.contains("Apache-2.0"));
94+
}
95+
96+
public void testGetListedExceptionById() throws InvalidSPDXAnalysisException {
97+
ListedLicenses.resetListedLicenses();
98+
String id = "Classpath-exception-2.0";
99+
assertTrue(ListedLicenses.getListedLicenses().isSpdxListedExceptionId(id));
100+
LicenseException result = ListedLicenses.getListedLicenses().getListedExceptionByIdCompatV2(id);
101+
assertEquals(id, result.getLicenseExceptionId());
102+
}
103+
104+
public void testGetExceptionbyIdLocal() throws InvalidSPDXAnalysisException {
105+
System.setProperty("SPDXParser.OnlyUseLocalLicenses", "true");
106+
ListedLicenses.resetListedLicenses();
107+
try {
108+
String id = "Classpath-exception-2.0";
109+
assertTrue(ListedLicenses.getListedLicenses().isSpdxListedExceptionId(id));
110+
LicenseException result = ListedLicenses.getListedLicenses().getListedExceptionByIdCompatV2(id);
111+
assertEquals(id, result.getLicenseExceptionId());
112+
} finally {
113+
System.setProperty("SPDXParser.OnlyUseLocalLicenses", "false");
114+
ListedLicenses.resetListedLicenses();
115+
}
116+
}
117+
118+
public void testGetExceptionIds() throws InvalidSPDXAnalysisException {
119+
assertTrue(ListedLicenses.getListedLicenses().getSpdxListedExceptionIds().size() >= NUM_3_7_EXCEPTION);
120+
}
121+
122+
public void testListedLicenseIdCaseSensitive() {
123+
String expected = "Apache-2.0";
124+
String lower = expected.toLowerCase();
125+
assertEquals(expected, ListedLicenses.getListedLicenses().listedLicenseIdCaseSensitive(lower).get());
126+
assertFalse(ListedLicenses.getListedLicenses().listedLicenseIdCaseSensitive("NotaLicenseId").isPresent());
127+
}
128+
129+
public void testListedExceptionIdCaseSensitive() {
130+
String expected = "Classpath-exception-2.0";
131+
String lower = expected.toLowerCase();
132+
assertEquals(expected, ListedLicenses.getListedLicenses().listedExceptionIdCaseSensitive(lower).get());
133+
assertFalse(ListedLicenses.getListedLicenses().listedExceptionIdCaseSensitive("NotAnExceptionId").isPresent());
134+
}
135+
136+
public void testGetLicenseIdProperty() throws InvalidSPDXAnalysisException {
137+
String id = "Apache-2.0";
138+
SpdxListedLicense lic = ListedLicenses.getListedLicenses().getListedLicenseByIdCompatV2(id);
139+
Optional<Object> idProp = lic.getModelStore().getValue(
140+
CompatibleModelStoreWrapper.documentUriIdToUri(lic.getDocumentUri(), id, false), SpdxConstantsCompatV2.PROP_LICENSE_ID);
141+
assertTrue(idProp.isPresent());
142+
assertTrue(idProp.get() instanceof String);
143+
assertEquals(id, idProp.get());
144+
}
145+
146+
public void testGetExceptionIdProperty() throws InvalidSPDXAnalysisException {
147+
String id = "Classpath-exception-2.0";
148+
ListedLicenseException ex = ListedLicenses.getListedLicenses().getListedExceptionByIdCompatV2(id);
149+
Optional<Object> idProp = ex.getModelStore().getValue(
150+
CompatibleModelStoreWrapper.documentUriIdToUri(ex.getDocumentUri(), id, false), SpdxConstantsCompatV2.PROP_LICENSE_EXCEPTION_ID);
151+
assertTrue(idProp.isPresent());
152+
assertTrue(idProp.get() instanceof String);
153+
assertEquals(id, idProp.get());
154+
}
155+
}

src/test/java/org/spdx/library/ModelCopyManagerTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,15 @@
3232
import org.spdx.library.model.v2.SpdxConstantsCompatV2;
3333
import org.spdx.library.model.v3.core.Agent;
3434
import org.spdx.library.model.v3.core.CreationInfo;
35+
import org.spdx.library.model.v3.core.Element;
3536
import org.spdx.library.model.v3.core.Hash;
3637
import org.spdx.library.model.v3.core.HashAlgorithm;
3738
import org.spdx.library.model.v3.core.Relationship;
3839
import org.spdx.library.model.v3.core.RelationshipType;
3940
import org.spdx.library.model.v3.core.SpdxDocument;
4041
import org.spdx.library.model.v3.software.SoftwareSpdxFile;
4142
import org.spdx.library.model.v3.software.SoftwareSpdxPackage;
43+
import org.spdx.storage.IModelStore;
4244
import org.spdx.storage.IModelStore.IdType;
4345
import org.spdx.storage.simple.InMemSpdxStore;
4446

@@ -59,6 +61,7 @@ public class ModelCopyManagerTest {
5961
private InMemSpdxStore toStore;
6062
private Hash hash;
6163
String date;
64+
private Object SpdxConstantsV2Compat;
6265

6366
/**
6467
* @throws java.lang.Exception
@@ -193,5 +196,4 @@ public void testCopyToExistingUri() throws InvalidSPDXAnalysisException {
193196
Hash differentCopiedHash = (Hash)SpdxModelFactory.inflateModelObject(toStore, result.getObjectUri(), HASH_TYPE, modelCopyManager, "3.0.0", true);
194197
assertTrue(differentHash.equivalent(differentCopiedHash));
195198
}
196-
197199
}

0 commit comments

Comments
 (0)