|
| 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 | +} |
0 commit comments