|
| 1 | +/** |
| 2 | + * Copyright (c) 2019 Source Auditor Inc. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.spdx.library; |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.List; |
| 22 | + |
| 23 | +import org.spdx.core.DefaultModelStore; |
| 24 | +import org.spdx.core.DefaultStoreNotInitialized; |
| 25 | +import org.spdx.core.InvalidSPDXAnalysisException; |
| 26 | +import org.spdx.library.model.v2.SpdxConstantsCompatV2; |
| 27 | +import org.spdx.library.model.v2.license.InvalidLicenseStringException; |
| 28 | +import org.spdx.library.model.v3.expandedlicensing.ExpandedLicensingConjunctiveLicenseSet; |
| 29 | +import org.spdx.library.model.v3.expandedlicensing.ExpandedLicensingCustomLicense; |
| 30 | +import org.spdx.library.model.v3.expandedlicensing.ExpandedLicensingDisjunctiveLicenseSet; |
| 31 | +import org.spdx.library.model.v3.expandedlicensing.ExpandedLicensingListedLicense; |
| 32 | +import org.spdx.library.model.v3.expandedlicensing.ExpandedLicensingNoAssertionLicense; |
| 33 | +import org.spdx.library.model.v3.expandedlicensing.ExpandedLicensingNoneLicense; |
| 34 | +import org.spdx.library.model.v3.simplelicensing.SimpleLicensingAnyLicenseInfo; |
| 35 | +import org.spdx.storage.IModelStore; |
| 36 | +import org.spdx.storage.IModelStore.IdType; |
| 37 | +import org.spdx.storage.simple.InMemSpdxStore; |
| 38 | + |
| 39 | +import junit.framework.TestCase; |
| 40 | + |
| 41 | +/** |
| 42 | + * @author gary |
| 43 | + * |
| 44 | + */ |
| 45 | +public class LicenseInfoFactoryTest extends TestCase { |
| 46 | + |
| 47 | + static final String[] NONSTD_IDS = new String[] {SpdxConstantsCompatV2.NON_STD_LICENSE_ID_PRENUM+"1", |
| 48 | + SpdxConstantsCompatV2.NON_STD_LICENSE_ID_PRENUM+"2", SpdxConstantsCompatV2.NON_STD_LICENSE_ID_PRENUM+"3", |
| 49 | + SpdxConstantsCompatV2.NON_STD_LICENSE_ID_PRENUM+"4"}; |
| 50 | + static final String[] NONSTD_TEXTS = new String[] {"text1", "text2", "text3", "text4"}; |
| 51 | + static final String[] STD_IDS = new String[] {"AFL-3.0", "CECILL-B", "EUPL-1.0"}; |
| 52 | + static final String[] STD_TEXTS = new String[] {"Academic Free License (", "CONTRAT DE LICENCE DE LOGICIEL LIBRE CeCILL-B", |
| 53 | + "European Union Public Licence"}; |
| 54 | + |
| 55 | + ExpandedLicensingCustomLicense[] NON_STD_LICENSES; |
| 56 | + ExpandedLicensingListedLicense[] STANDARD_LICENSES; |
| 57 | + ExpandedLicensingDisjunctiveLicenseSet[] DISJUNCTIVE_LICENSES; |
| 58 | + ExpandedLicensingConjunctiveLicenseSet[] CONJUNCTIVE_LICENSES; |
| 59 | + |
| 60 | + ExpandedLicensingConjunctiveLicenseSet COMPLEX_LICENSE; |
| 61 | + |
| 62 | + ModelCopyManager copyManager; |
| 63 | + IModelStore modelStore; |
| 64 | + static final String TEST_DOCUMENT_URI = "https://test.doc.uri"; |
| 65 | + |
| 66 | + /* (non-Javadoc) |
| 67 | + * @see junit.framework.TestCase#setUp() |
| 68 | + */ |
| 69 | + protected void setUp() throws Exception { |
| 70 | + super.setUp(); |
| 71 | + SpdxModelFactory.init(); |
| 72 | + modelStore = new InMemSpdxStore(); |
| 73 | + copyManager = new ModelCopyManager(); |
| 74 | + DefaultModelStore.initialize(new InMemSpdxStore(), TEST_DOCUMENT_URI, copyManager); |
| 75 | + NON_STD_LICENSES = new ExpandedLicensingCustomLicense[NONSTD_IDS.length]; |
| 76 | + for (int i = 0; i < NONSTD_IDS.length; i++) { |
| 77 | + NON_STD_LICENSES[i] = new ExpandedLicensingCustomLicense(modelStore, |
| 78 | + TEST_DOCUMENT_URI + "#" + NONSTD_IDS[i], copyManager, true); |
| 79 | + NON_STD_LICENSES[i].setSimpleLicensingLicenseText(NONSTD_TEXTS[i]); |
| 80 | + } |
| 81 | + |
| 82 | + STANDARD_LICENSES = new ExpandedLicensingListedLicense[STD_IDS.length]; |
| 83 | + for (int i = 0; i < STD_IDS.length; i++) { |
| 84 | + STANDARD_LICENSES[i] = new ExpandedLicensingListedLicense(modelStore, |
| 85 | + SpdxConstantsCompatV2.LISTED_LICENSE_NAMESPACE_PREFIX + STD_IDS[i], copyManager, true); |
| 86 | + STANDARD_LICENSES[i].setName("Name "+String.valueOf(i)); |
| 87 | + STANDARD_LICENSES[i].setSimpleLicensingLicenseText(STD_TEXTS[i]); |
| 88 | + STANDARD_LICENSES[i].getExpandedLicensingSeeAlsos().add("URL "+String.valueOf(i)); |
| 89 | + STANDARD_LICENSES[i].setComment("Notes "+String.valueOf(i)); |
| 90 | + STANDARD_LICENSES[i].setExpandedLicensingStandardLicenseHeader("LicHeader "+String.valueOf(i)); |
| 91 | + STANDARD_LICENSES[i].setExpandedLicensingStandardLicenseTemplate("Template "+String.valueOf(i)); |
| 92 | + } |
| 93 | + |
| 94 | + DISJUNCTIVE_LICENSES = new ExpandedLicensingDisjunctiveLicenseSet[3]; |
| 95 | + CONJUNCTIVE_LICENSES = new ExpandedLicensingConjunctiveLicenseSet[2]; |
| 96 | + |
| 97 | + DISJUNCTIVE_LICENSES[0] = new ExpandedLicensingDisjunctiveLicenseSet(modelStore, |
| 98 | + modelStore.getNextId(IdType.Anonymous), copyManager, true); |
| 99 | + DISJUNCTIVE_LICENSES[0].getExpandedLicensingMembers().addAll(new ArrayList<SimpleLicensingAnyLicenseInfo>(Arrays.asList(new SimpleLicensingAnyLicenseInfo[] { |
| 100 | + NON_STD_LICENSES[0], NON_STD_LICENSES[1], STANDARD_LICENSES[1] |
| 101 | + }))); |
| 102 | + CONJUNCTIVE_LICENSES[0] = new ExpandedLicensingConjunctiveLicenseSet(modelStore, |
| 103 | + modelStore.getNextId(IdType.Anonymous), copyManager, true); |
| 104 | + CONJUNCTIVE_LICENSES[0].getExpandedLicensingMembers().addAll(new ArrayList<SimpleLicensingAnyLicenseInfo>(Arrays.asList(new SimpleLicensingAnyLicenseInfo[] { |
| 105 | + STANDARD_LICENSES[0], NON_STD_LICENSES[0], STANDARD_LICENSES[1] |
| 106 | + }))); |
| 107 | + CONJUNCTIVE_LICENSES[1] = new ExpandedLicensingConjunctiveLicenseSet(modelStore, |
| 108 | + modelStore.getNextId(IdType.Anonymous), copyManager, true); |
| 109 | + CONJUNCTIVE_LICENSES[1].getExpandedLicensingMembers().addAll(new ArrayList<SimpleLicensingAnyLicenseInfo>(Arrays.asList(new SimpleLicensingAnyLicenseInfo[] { |
| 110 | + DISJUNCTIVE_LICENSES[0], NON_STD_LICENSES[2] |
| 111 | + }))); |
| 112 | + DISJUNCTIVE_LICENSES[1] = new ExpandedLicensingDisjunctiveLicenseSet(modelStore, |
| 113 | + modelStore.getNextId(IdType.Anonymous), copyManager, true); |
| 114 | + DISJUNCTIVE_LICENSES[1].getExpandedLicensingMembers().addAll(new ArrayList<SimpleLicensingAnyLicenseInfo>(Arrays.asList(new SimpleLicensingAnyLicenseInfo[] { |
| 115 | + CONJUNCTIVE_LICENSES[1], NON_STD_LICENSES[0], STANDARD_LICENSES[0] |
| 116 | + }))); |
| 117 | + DISJUNCTIVE_LICENSES[2] = new ExpandedLicensingDisjunctiveLicenseSet(modelStore, |
| 118 | + modelStore.getNextId(IdType.Anonymous), copyManager, true); |
| 119 | + DISJUNCTIVE_LICENSES[2].getExpandedLicensingMembers().addAll(new ArrayList<SimpleLicensingAnyLicenseInfo>(Arrays.asList(new SimpleLicensingAnyLicenseInfo[] { |
| 120 | + DISJUNCTIVE_LICENSES[1], CONJUNCTIVE_LICENSES[0], STANDARD_LICENSES[2] |
| 121 | + }))); |
| 122 | + COMPLEX_LICENSE = new ExpandedLicensingConjunctiveLicenseSet(modelStore, |
| 123 | + modelStore.getNextId(IdType.Anonymous), copyManager, true); |
| 124 | + COMPLEX_LICENSE.getExpandedLicensingMembers().addAll(new ArrayList<SimpleLicensingAnyLicenseInfo>(Arrays.asList(new SimpleLicensingAnyLicenseInfo[] { |
| 125 | + DISJUNCTIVE_LICENSES[2], NON_STD_LICENSES[2], CONJUNCTIVE_LICENSES[1] |
| 126 | + }))); |
| 127 | + } |
| 128 | + |
| 129 | + /* (non-Javadoc) |
| 130 | + * @see junit.framework.TestCase#tearDown() |
| 131 | + */ |
| 132 | + protected void tearDown() throws Exception { |
| 133 | + super.tearDown(); |
| 134 | + DefaultModelStore.initialize(new InMemSpdxStore(), "https://default/prefix", new ModelCopyManager()); |
| 135 | + } |
| 136 | + |
| 137 | + public void testParseSPDXLicenseString() throws InvalidLicenseStringException, DefaultStoreNotInitialized { |
| 138 | + String parseString = COMPLEX_LICENSE.toString(); |
| 139 | + SimpleLicensingAnyLicenseInfo li = LicenseInfoFactory.parseSPDXLicenseString(parseString); |
| 140 | + if (!li.equals(COMPLEX_LICENSE)) { |
| 141 | + fail("Parsed license does not equal"); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + |
| 146 | + public void testSpecialLicenses() throws InvalidLicenseStringException, InvalidSPDXAnalysisException { |
| 147 | + // NONE |
| 148 | + SimpleLicensingAnyLicenseInfo none = LicenseInfoFactory.parseSPDXLicenseString(LicenseInfoFactory.NONE_LICENSE_NAME); |
| 149 | + SimpleLicensingAnyLicenseInfo comp = new ExpandedLicensingNoneLicense(); |
| 150 | + assertEquals(none, comp); |
| 151 | + List<String> verify = comp.verify(); |
| 152 | + assertEquals(0, verify.size()); |
| 153 | + // NOASSERTION_NAME |
| 154 | + SimpleLicensingAnyLicenseInfo noAssertion = LicenseInfoFactory.parseSPDXLicenseString(LicenseInfoFactory.NOASSERTION_LICENSE_NAME); |
| 155 | + comp = new ExpandedLicensingNoAssertionLicense(); |
| 156 | + assertEquals(noAssertion, comp); |
| 157 | + verify = comp.verify(); |
| 158 | + assertEquals(0, verify.size()); |
| 159 | + } |
| 160 | + |
| 161 | + |
| 162 | + public void testDifferentLicenseOrder() throws InvalidSPDXAnalysisException { |
| 163 | + SimpleLicensingAnyLicenseInfo order1 = LicenseInfoFactory.parseSPDXLicenseString("(LicenseRef-14 AND LicenseRef-5 AND LicenseRef-6 AND LicenseRef-15 AND LicenseRef-3 AND LicenseRef-12 AND LicenseRef-4 AND LicenseRef-13 AND LicenseRef-10 AND LicenseRef-9 AND LicenseRef-11 AND LicenseRef-7 AND LicenseRef-8 AND LGPL-2.1+ AND LicenseRef-1 AND LicenseRef-2 AND LicenseRef-0 AND GPL-2.0+ AND GPL-2.0 AND LicenseRef-17 AND LicenseRef-16 AND BSD-3-Clause-Clear)"); |
| 164 | + SimpleLicensingAnyLicenseInfo order2 = LicenseInfoFactory.parseSPDXLicenseString("(LicenseRef-14 AND LicenseRef-5 AND LicenseRef-6 AND LicenseRef-15 AND LicenseRef-12 AND LicenseRef-3 AND LicenseRef-13 AND LicenseRef-4 AND LicenseRef-10 AND LicenseRef-9 AND LicenseRef-11 AND LicenseRef-7 AND LicenseRef-8 AND LGPL-2.1+ AND LicenseRef-1 AND LicenseRef-2 AND LicenseRef-0 AND GPL-2.0+ AND GPL-2.0 AND LicenseRef-17 AND BSD-3-Clause-Clear AND LicenseRef-16)"); |
| 165 | + assertTrue(order1.equals(order2)); |
| 166 | + assertTrue(order1.equivalent(order2)); |
| 167 | + } |
| 168 | + |
| 169 | + public void testParseSPDXLicenseStringMixedCase() throws InvalidSPDXAnalysisException { |
| 170 | + String parseString = COMPLEX_LICENSE.toString(); |
| 171 | + String lowerCaseCecil = parseString.replace("CECILL-B", "CECILL-B".toLowerCase()); |
| 172 | + SimpleLicensingAnyLicenseInfo result = LicenseInfoFactory.parseSPDXLicenseString(lowerCaseCecil); |
| 173 | + assertEquals(COMPLEX_LICENSE, result); |
| 174 | + } |
| 175 | +} |
0 commit comments