Skip to content

Commit

Permalink
Merge pull request #3 from spdx/morefixes
Browse files Browse the repository at this point in the history
Additional fixes for issues found in SonarCloud
  • Loading branch information
goneall authored Sep 2, 2024
2 parents 665b65d + fa67c38 commit c902569
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 102 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ Please refer to the [Spdx-Java-Library](https://github.com/spdx/spdx-java-Librar

Contributions are welcome. See [CONTRIBUTING.md](CONTRIBUTING.md).

# Code quality badges

| [![Bugs](https://sonarcloud.io/api/project_badges/measure?project=spdx-java-core&metric=bugs)](https://sonarcloud.io/dashboard?id=spdx-java-core) | [![Security Rating](https://sonarcloud.io/api/project_badges/measure?project=spdx-java-core&metric=security_rating)](https://sonarcloud.io/dashboard?id=spdx-java-core) | [![Maintainability Rating](https://sonarcloud.io/api/project_badges/measure?project=spdx-java-core&metric=sqale_rating)](https://sonarcloud.io/dashboard?id=spdx-java-core) | [![Technical Debt](https://sonarcloud.io/api/project_badges/measure?project=spdx-java-core&metric=sqale_index)](https://sonarcloud.io/dashboard?id=spdx-java-core) |

## Overall Architecture

The primary class in the core library is the CoreModelObject. All SPDX model classes inherit this class. It contains several useful functions including the ability to compare to other CoreModelObjects, add/remove properties, and manage collections.
Expand Down
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,9 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.build.outputTimestamp>1675930644</project.build.outputTimestamp>
<sonar.host.url>https://sonarcloud.io</sonar.host.url>
<sonar.organization>spdx</sonar.organization>
<sonar.organization>spdx-1</sonar.organization>
<sonar.projectKey>spdx-java-core</sonar.projectKey>
<dependency-check-maven.version>8.0.1</dependency-check-maven.version>
</properties>
<profiles>
<profile>
Expand Down Expand Up @@ -116,6 +117,11 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.owasp</groupId>
<artifactId>dependency-check-maven</artifactId>
<version>${dependency-check-maven.version}</version>
</plugin>
</plugins>
</build>
</profile>
Expand Down
26 changes: 11 additions & 15 deletions src/main/java/org/spdx/core/CoreModelObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -357,10 +357,9 @@ public void setPropertyValue(PropertyDescriptor propertyDescriptor, @Nullable Ob
* @return an update which can be applied by invoking the apply method
*/
public ModelUpdate updatePropertyValue(PropertyDescriptor propertyDescriptor, Object value) {
return () ->{
return () ->
ModelObjectHelper.setPropertyValue(this.modelStore, objectUri, propertyDescriptor, value,
copyManager, idPrefix);
};
}

/**
Expand Down Expand Up @@ -476,9 +475,8 @@ public void removeProperty(PropertyDescriptor propertyDescriptor) throws Invalid
* @return an update which can be applied by invoking the apply method
*/
public ModelUpdate updateRemoveProperty(PropertyDescriptor propertyDescriptor) {
return () -> {
return () ->
ModelObjectHelper.removeProperty(modelStore, objectUri, propertyDescriptor);
};
}

/**
Expand All @@ -499,9 +497,8 @@ public void clearValueCollection(PropertyDescriptor propertyDescriptor) throws I
* @return an update which can be applied by invoking the apply method
*/
public ModelUpdate updateClearValueCollection(PropertyDescriptor propertyDescriptor) {
return () ->{
return () ->
ModelObjectHelper.clearValueCollection(modelStore, objectUri, propertyDescriptor);
};
}

/**
Expand All @@ -528,10 +525,9 @@ public void addPropertyValueToCollection(PropertyDescriptor propertyDescriptor,
* @return an update which can be applied by invoking the apply method
*/
public ModelUpdate updateAddPropertyValueToCollection(PropertyDescriptor propertyDescriptor, Object value) {
return () ->{
return () ->
ModelObjectHelper.addValueToCollection(modelStore, objectUri, propertyDescriptor, value,
copyManager, idPrefix);
};
}

/**
Expand All @@ -555,17 +551,16 @@ public void removePropertyValueFromCollection(PropertyDescriptor propertyDescrip
* @return an update which can be applied by invoking the apply method
*/
public ModelUpdate updateRemovePropertyValueFromCollection(PropertyDescriptor propertyDescriptor, Object value) {
return () -> {
return () ->
ModelObjectHelper.removePropertyValueFromCollection(modelStore, objectUri, propertyDescriptor, value);
};
}

/**
* @param propertyDescriptor Descriptor for the property
* @return Set of values associated with a property
*/
public ModelSet<?> getObjectPropertyValueSet(PropertyDescriptor propertyDescriptor, Class<?> type) throws InvalidSPDXAnalysisException {
return new ModelSet<Object>(this.modelStore, this.objectUri, propertyDescriptor,
return new ModelSet<>(this.modelStore, this.objectUri, propertyDescriptor,
this.copyManager, type, specVersion, idPrefix);
}

Expand All @@ -574,7 +569,7 @@ public ModelSet<?> getObjectPropertyValueSet(PropertyDescriptor propertyDescript
* @return Collection of values associated with a property
*/
public ModelCollection<?> getObjectPropertyValueCollection(PropertyDescriptor propertyDescriptor, Class<?> type) throws InvalidSPDXAnalysisException {
return new ModelCollection<Object>(this.modelStore, this.objectUri, propertyDescriptor,
return new ModelCollection<>(this.modelStore, this.objectUri, propertyDescriptor,
this.copyManager, type, specVersion, idPrefix);
}

Expand Down Expand Up @@ -615,7 +610,7 @@ public boolean equivalent(CoreModelObject compare, boolean ignoreRelatedElements
return false;
}
List<PropertyDescriptor> propertyValueDescriptors = getPropertyValueDescriptors();
List<PropertyDescriptor> comparePropertyValueDescriptors = new ArrayList<PropertyDescriptor>(compare.getPropertyValueDescriptors()); // create a copy since we're going to modify it
List<PropertyDescriptor> comparePropertyValueDescriptors = new ArrayList<>(compare.getPropertyValueDescriptors()); // create a copy since we're going to modify it
for (PropertyDescriptor propertyDescriptor:propertyValueDescriptors) {
if (ignoreRelatedElements && isRelatedElement(propertyDescriptor)) {
continue;
Expand Down Expand Up @@ -862,7 +857,7 @@ public CoreModelObject clone(IModelStore modelStore) {
retval.copyFrom(this);
return retval;
} catch (InvalidSPDXAnalysisException e) {
throw new RuntimeException(e);
throw new RuntimeSpdxException(e);
}
}

Expand Down Expand Up @@ -991,6 +986,7 @@ public boolean isStrict() {

/**
* @return the version of the SPDX specification this object complies with
* @throws InvalidSPDXAnalysisException - this is here just for compatibility with overriden methods
*/
public String getSpecVersion() throws InvalidSPDXAnalysisException {
return this.specVersion;
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/spdx/core/DefaultModelStore.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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();
}
Expand Down Expand Up @@ -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 {
Expand Down
21 changes: 10 additions & 11 deletions src/main/java/org/spdx/core/ModelCollection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand All @@ -169,16 +169,15 @@ private Object checkConvertTypedValue(Object value) {
}
return retval;
} catch (InvalidSPDXAnalysisException e) {
throw new RuntimeException(e);
throw new RuntimeSpdxException(e);
}
}

/**
* Converts any typed or individual value objects to a ModelObject
*/
private UnaryOperator<Object> checkConvertTypedValue = value -> {
return checkConvertTypedValue(value);
};

private UnaryOperator<Object> checkConvertTypedValue = ModelCollection.this::checkConvertTypedValue;

/**
* @return a list of objects for the model collection
Expand All @@ -195,7 +194,7 @@ public Iterator<Object> iterator() {
return new ModelCollectionIterator(
modelStore.listValues(objectUri, propertyDescriptor));
} catch (InvalidSPDXAnalysisException e) {
throw new RuntimeException(e);
throw new RuntimeSpdxException(e);
}
}

Expand All @@ -216,7 +215,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);
}
}

Expand All @@ -226,7 +225,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);
}
}

Expand Down Expand Up @@ -276,7 +275,7 @@ public void clear() {
try {
modelStore.clearValueCollection(objectUri, propertyDescriptor);
} catch (InvalidSPDXAnalysisException e) {
throw new RuntimeException(e);
throw new RuntimeSpdxException(e);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/spdx/core/ModelRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/spdx/core/ModelSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -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;
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/org/spdx/core/RuntimeSpdxException.java
Original file line number Diff line number Diff line change
@@ -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);
}

}
2 changes: 1 addition & 1 deletion src/main/java/org/spdx/core/SimpleUriValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -38,33 +33,6 @@ public class TestLicenseTemplateRule {
static final String RULE_ORIGINAL = "Copyright (c) <year> <owner>\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 {
Expand Down
Loading

0 comments on commit c902569

Please sign in to comment.