From 748e769335370fc815fe42e1a85a236f00554630 Mon Sep 17 00:00:00 2001 From: Gary O'Neall Date: Mon, 2 Sep 2024 15:24:56 -0700 Subject: [PATCH] More fixes for issues found in SonarCloud Signed-off-by: Gary O'Neall --- .../java/org/spdx/core/CoreModelObject.java | 6 +-- .../java/org/spdx/core/DefaultModelStore.java | 8 +-- .../java/org/spdx/core/ModelCollection.java | 16 +++--- .../java/org/spdx/core/ModelRegistry.java | 2 +- src/main/java/org/spdx/core/ModelSet.java | 4 +- .../org/spdx/core/RuntimeSpdxException.java | 54 +++++++++++++++++++ .../java/org/spdx/core/SimpleUriValue.java | 2 +- .../TestLicenseTemplateRule.java | 32 ----------- .../TestSpdxLicenseTemplateHelper.java | 31 ----------- .../java/org/spdx/storage/MockModelStore.java | 8 +-- 10 files changed, 77 insertions(+), 86 deletions(-) create mode 100644 src/main/java/org/spdx/core/RuntimeSpdxException.java diff --git a/src/main/java/org/spdx/core/CoreModelObject.java b/src/main/java/org/spdx/core/CoreModelObject.java index e36a0df..62d0bb9 100644 --- a/src/main/java/org/spdx/core/CoreModelObject.java +++ b/src/main/java/org/spdx/core/CoreModelObject.java @@ -78,7 +78,7 @@ public abstract class CoreModelObject { static final String PROPERTY_MSG = "Property "; - private static final String ATTEMPTING_EXTERNAL_MSG = "Attempting to set {0} for an external model object"; + private static final String ATTEMPTING_EXTERNAL_MSG = "Attempting to set {} for an external model object"; protected IModelStore modelStore; protected String objectUri; protected String specVersion; @@ -862,7 +862,7 @@ public CoreModelObject clone(IModelStore modelStore) { retval.copyFrom(this); return retval; } catch (InvalidSPDXAnalysisException e) { - throw new RuntimeException(e); + throw new RuntimeSpdxException(e); } } @@ -992,7 +992,7 @@ public boolean isStrict() { /** * @return the version of the SPDX specification this object complies with */ - public String getSpecVersion() throws InvalidSPDXAnalysisException { + public String getSpecVersion() { return this.specVersion; } diff --git a/src/main/java/org/spdx/core/DefaultModelStore.java b/src/main/java/org/spdx/core/DefaultModelStore.java index 257c7f7..b8821c1 100644 --- a/src/main/java/org/spdx/core/DefaultModelStore.java +++ b/src/main/java/org/spdx/core/DefaultModelStore.java @@ -33,7 +33,7 @@ */ public class DefaultModelStore { - static IModelStore defaultModelStore = null; + static IModelStore defaultStore = null; static String defaultDocumentUri = "http://www.spdx.org/documents/default_doc_uri_for_SPDX_tools"; static IModelCopyManager defaultCopyManager = null; static final String NOT_INITIALIZED_MSG = "Default model store has not been initialized"; @@ -50,10 +50,10 @@ private DefaultModelStore() { public static IModelStore getDefaultModelStore() throws DefaultStoreNotInitialized { lock.readLock().lock(); try { - if (Objects.isNull(defaultModelStore)) { + if (Objects.isNull(defaultStore)) { throw new DefaultStoreNotInitialized(NOT_INITIALIZED_MSG); } - return defaultModelStore; + return defaultStore; } finally { lock.readLock().unlock(); } @@ -88,7 +88,7 @@ public static final void initialize(IModelStore newModelStore, String newDefault Objects.requireNonNull(newDefaultCopyManager, "Copy manager can not be null"); lock.writeLock().lock(); try { - defaultModelStore = newModelStore; + defaultStore = newModelStore; defaultDocumentUri = newDefaultDocumentUri; defaultCopyManager = newDefaultCopyManager; } finally { diff --git a/src/main/java/org/spdx/core/ModelCollection.java b/src/main/java/org/spdx/core/ModelCollection.java index 66387a3..624f1e0 100644 --- a/src/main/java/org/spdx/core/ModelCollection.java +++ b/src/main/java/org/spdx/core/ModelCollection.java @@ -120,7 +120,7 @@ public int size() { try { return this.modelStore.collectionSize(objectUri, this.propertyDescriptor); } catch (InvalidSPDXAnalysisException e) { - throw new RuntimeException(e); + throw new RuntimeSpdxException(e); } } @@ -129,7 +129,7 @@ public boolean isEmpty() { try { return this.modelStore.collectionSize(objectUri, this.propertyDescriptor) == 0; } catch (InvalidSPDXAnalysisException e) { - throw new RuntimeException(e); + throw new RuntimeSpdxException(e); } } @@ -145,7 +145,7 @@ public boolean contains(Object o) { return this.modelStore.collectionContains( objectUri, this.propertyDescriptor, storedObject); } catch (InvalidSPDXAnalysisException e) { - throw new RuntimeException(e); + throw new RuntimeSpdxException(e); } } @@ -169,7 +169,7 @@ private Object checkConvertTypedValue(Object value) { } return retval; } catch (InvalidSPDXAnalysisException e) { - throw new RuntimeException(e); + throw new RuntimeSpdxException(e); } } @@ -195,7 +195,7 @@ public Iterator iterator() { return new ModelCollectionIterator( modelStore.listValues(objectUri, propertyDescriptor)); } catch (InvalidSPDXAnalysisException e) { - throw new RuntimeException(e); + throw new RuntimeSpdxException(e); } } @@ -216,7 +216,7 @@ public boolean add(Object element) { objectUri, propertyDescriptor, ModelObjectHelper.modelObjectToStoredObject(element, modelStore, copyManager, idPrefix)); } catch (InvalidSPDXAnalysisException e) { - throw new RuntimeException(e); + throw new RuntimeSpdxException(e); } } @@ -226,7 +226,7 @@ public boolean remove(Object element) { return modelStore.removeValueFromCollection(objectUri, propertyDescriptor, ModelObjectHelper.modelObjectToStoredObject(element, modelStore, null, null)); } catch (InvalidSPDXAnalysisException e) { - throw new RuntimeException(e); + throw new RuntimeSpdxException(e); } } @@ -276,7 +276,7 @@ public void clear() { try { modelStore.clearValueCollection(objectUri, propertyDescriptor); } catch (InvalidSPDXAnalysisException e) { - throw new RuntimeException(e); + throw new RuntimeSpdxException(e); } } diff --git a/src/main/java/org/spdx/core/ModelRegistry.java b/src/main/java/org/spdx/core/ModelRegistry.java index 7c0150e..3f30ad2 100644 --- a/src/main/java/org/spdx/core/ModelRegistry.java +++ b/src/main/java/org/spdx/core/ModelRegistry.java @@ -43,7 +43,7 @@ public class ModelRegistry { * Private constructor - singleton class */ private ModelRegistry() { - // Nothing really todo here + // Nothing really to be done here } public static ModelRegistry getModelRegistry() { diff --git a/src/main/java/org/spdx/core/ModelSet.java b/src/main/java/org/spdx/core/ModelSet.java index 37be927..1952925 100644 --- a/src/main/java/org/spdx/core/ModelSet.java +++ b/src/main/java/org/spdx/core/ModelSet.java @@ -62,7 +62,7 @@ public boolean add(Object element) { try { lock = this.getModelStore().enterCriticalSection(false); } catch (InvalidSPDXAnalysisException e) { - throw new RuntimeException(e); + throw new RuntimeSpdxException(e); } try { if (!super.contains(element)) { @@ -82,7 +82,7 @@ public boolean addAll(Collection c) { try { lock = this.getModelStore().enterCriticalSection(false); } catch (InvalidSPDXAnalysisException e) { - throw new RuntimeException(e); + throw new RuntimeSpdxException(e); } try { boolean retval = false; diff --git a/src/main/java/org/spdx/core/RuntimeSpdxException.java b/src/main/java/org/spdx/core/RuntimeSpdxException.java new file mode 100644 index 0000000..bd9ef56 --- /dev/null +++ b/src/main/java/org/spdx/core/RuntimeSpdxException.java @@ -0,0 +1,54 @@ +/** + * SPDX-License-Identifier: Apache-2.0 + * Copyright (c) 2024 Source Auditor Inc. + */ +package org.spdx.core; + +/** + * Runtime Exception wrapper for SPDX exceptions (cause field) + * + * @author Gary O'Neall + * + */ +public class RuntimeSpdxException extends RuntimeException { + + + /** + * + */ + private static final long serialVersionUID = 1L; + + /** + * @param message exception message + */ + public RuntimeSpdxException(String message) { + super(message); + } + + /** + * @param cause SPDX analysis cause + */ + public RuntimeSpdxException(InvalidSPDXAnalysisException cause) { + super(cause); + } + + /** + * @param message exception message + * @param cause SPDX analysis cause + */ + public RuntimeSpdxException(String message, InvalidSPDXAnalysisException cause) { + super(message, cause); + } + + /** + * @param message exception message + * @param cause SPDX analysis cause + * @param enableSuppression + * @param writableStackTrace + */ + public RuntimeSpdxException(String message, Throwable cause, + boolean enableSuppression, boolean writableStackTrace) { + super(message, cause, enableSuppression, writableStackTrace); + } + +} diff --git a/src/main/java/org/spdx/core/SimpleUriValue.java b/src/main/java/org/spdx/core/SimpleUriValue.java index 1bfbcf3..e4686ec 100644 --- a/src/main/java/org/spdx/core/SimpleUriValue.java +++ b/src/main/java/org/spdx/core/SimpleUriValue.java @@ -113,7 +113,7 @@ public Object toModelObject(IModelStore store, IModelCopyManager copyManager, } else { retval = ModelRegistry.getModelRegistry().getExternalElement(store, uri, copyManager, type, specVersion); if (Objects.isNull(retval)) { - logger.warn("{0} does not match an enum, individual, or external pattern", this.getIndividualURI()); + logger.warn("{} does not match an enum, individual, or external pattern", this.getIndividualURI()); retval = this; } } diff --git a/src/test/java/org/spdx/licenseTemplate/TestLicenseTemplateRule.java b/src/test/java/org/spdx/licenseTemplate/TestLicenseTemplateRule.java index a568f8d..918ceb8 100644 --- a/src/test/java/org/spdx/licenseTemplate/TestLicenseTemplateRule.java +++ b/src/test/java/org/spdx/licenseTemplate/TestLicenseTemplateRule.java @@ -16,13 +16,8 @@ */ package org.spdx.licenseTemplate; - import static org.junit.Assert.*; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.spdx.licenseTemplate.LicenseTemplateRule.RuleType; @@ -38,33 +33,6 @@ public class TestLicenseTemplateRule { static final String RULE_ORIGINAL = "Copyright (c) \nAll rights reserved."; static final String RULE_MATCH = "Copyright \\(c\\) .+All rights reserved."; static final String RULE_EXAMPLE = "Copyright (C) 2013 John Doe\nAll rights reserved."; - /** - * @throws java.lang.Exception - */ - @BeforeClass - public static void setUpBeforeClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @AfterClass - public static void tearDownAfterClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @Before - public void setUp() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @After - public void tearDown() throws Exception { - } @Test public void testparseLicenseTemplateRule() throws LicenseTemplateRuleException { diff --git a/src/test/java/org/spdx/licenseTemplate/TestSpdxLicenseTemplateHelper.java b/src/test/java/org/spdx/licenseTemplate/TestSpdxLicenseTemplateHelper.java index db14009..12be38b 100644 --- a/src/test/java/org/spdx/licenseTemplate/TestSpdxLicenseTemplateHelper.java +++ b/src/test/java/org/spdx/licenseTemplate/TestSpdxLicenseTemplateHelper.java @@ -18,10 +18,6 @@ import static org.junit.Assert.*; -import org.junit.After; -import org.junit.AfterClass; -import org.junit.Before; -import org.junit.BeforeClass; import org.junit.Test; import org.spdx.licenseTemplate.LicenseTemplateRule.RuleType; @@ -121,33 +117,6 @@ public void completeParsing() { ";original=original;match=.+this>>"+ "<>"+PARSE_OPTIONAL_TEXT+"<>"; - /** - * @throws java.lang.Exception - */ - @BeforeClass - public static void setUpBeforeClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @AfterClass - public static void tearDownAfterClass() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @Before - public void setUp() throws Exception { - } - - /** - * @throws java.lang.Exception - */ - @After - public void tearDown() throws Exception { - } /** * Test method for {@link org.spdx.licenseTemplate.SpdxLicenseTemplateHelper#templateTextToHtml(java.lang.String)}. diff --git a/src/test/java/org/spdx/storage/MockModelStore.java b/src/test/java/org/spdx/storage/MockModelStore.java index 99e1377..59df880 100644 --- a/src/test/java/org/spdx/storage/MockModelStore.java +++ b/src/test/java/org/spdx/storage/MockModelStore.java @@ -36,14 +36,14 @@ public class MockModelStore implements IModelStore { @Override public void unlock() { - + // ignore - nothing to do } }; @Override public void close() throws Exception { - + // ignore - nothing to do } @Override @@ -104,7 +104,7 @@ public IModelStoreLock enterCriticalSection(boolean readLockRequested) @Override public void leaveCriticalSection(IModelStoreLock lock) { - + // ignore - nothing to do } @Override @@ -207,7 +207,7 @@ public Optional getTypedValue(String objectUri) @Override public void delete(String objectUri) throws InvalidSPDXAnalysisException { - + // ignore - nothing to do } @Override