Skip to content

Commit

Permalink
Eliminate SonarQube warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanseifert committed Jan 25, 2024
1 parent 3b37eef commit a3d80bb
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 40 deletions.
8 changes: 4 additions & 4 deletions core/src/main/java/io/wcm/testing/mock/aem/MockComponent.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
package io.wcm.testing.mock.aem;

import static org.apache.sling.api.resource.ResourceResolver.PROPERTY_RESOURCE_TYPE;

import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -41,8 +43,6 @@
import com.day.cq.wcm.api.components.ComponentEditConfig;
import com.day.cq.wcm.api.components.VirtualComponent;

import static org.apache.sling.api.resource.ResourceResolver.PROPERTY_RESOURCE_TYPE;

/**
* Mock implementation of {@link Component}.
*/
Expand Down Expand Up @@ -261,12 +261,12 @@ private static class RemoveKeyPrefixMap extends ValueMapDecorator {
}

@Override
public <T> T get(String name, Class<T> type) {
public <T> T get(@NotNull String name, Class<T> type) {
return super.get(removeKeyPrefix(name), type);
}

@Override
public <T> T get(String name, T defaultValue) {
public <T> T get(@NotNull String name, T defaultValue) {
return super.get(removeKeyPrefix(name), defaultValue);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ public Resource getResource() {

@Override
@SuppressFBWarnings("RCN_REDUNDANT_NULLCHECK_OF_NONNULL_VALUE")
@SuppressWarnings("java:S2583") // null check
public Component getComponent() {
Resource currentResource = getResource();
if (currentResource == null) {
return null;
}
ComponentManager componentManager = currentResource.getResourceResolver().adaptTo(ComponentManager.class);
if (componentManager == null) {
throw new RuntimeException("No component manager.");
throw new IllegalStateException("No component manager.");
}
return componentManager.getComponentOfResource(currentResource);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
public final class MockLanguageManager implements LanguageManager {

@Override
@Deprecated
@Deprecated(forRemoval = true)
public Map<Locale, Info> getAdjacentInfo(final ResourceResolver resourceResolver, final String path) {
return Optional.ofNullable(getAdjacentLanguageInfo(resourceResolver, path))
.map(Map::entrySet)
Expand All @@ -71,6 +71,7 @@ public Map<Locale, Info> getAdjacentInfo(final ResourceResolver resourceResolver
}

@Override
@SuppressWarnings("null")
public Map<Language, Info> getAdjacentLanguageInfo(final ResourceResolver resourceResolver, final String path) {
return Optional.ofNullable(LanguageUtil.getLanguageRoot(path))
.map(root -> path.substring(root.length()))
Expand Down
24 changes: 12 additions & 12 deletions core/src/main/java/io/wcm/testing/mock/aem/MockTagManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

import static com.day.cq.tagging.TagConstants.TAG_ROOT_PATH;

import java.security.AccessControlException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
Expand Down Expand Up @@ -149,13 +148,13 @@ public boolean canCreateTag(String tagID) throws InvalidTagFormatException {

@Override
public Tag createTag(String tagID, String title, String description)
throws AccessControlException, InvalidTagFormatException {
throws InvalidTagFormatException {
return createTag(tagID, title, description, true);
}

@Override
public Tag createTag(String tagID, String title, String description, boolean autoSave)
throws AccessControlException, InvalidTagFormatException {
throws InvalidTagFormatException {
String tagPath = getPathFromID(tagID);
if (!StringUtils.startsWith(tagPath, TAG_ROOT_PATH)) {
throw new InvalidTagFormatException("Tag path '" + tagPath + "' does not start with: " + TAG_ROOT_PATH);
Expand Down Expand Up @@ -196,17 +195,17 @@ public Tag createTag(String tagID, String title, String description, boolean aut
}

@Override
public Tag createTagByTitle(String titlePath) throws AccessControlException, InvalidTagFormatException {
public Tag createTagByTitle(String titlePath) throws InvalidTagFormatException {
return createTagByTitle(titlePath, true);
}

@Override
public void deleteTag(Tag tag) throws AccessControlException {
public void deleteTag(Tag tag) {
deleteTag(tag, true);
}

@Override
public void deleteTag(Tag tag, boolean autoSave) throws AccessControlException {
public void deleteTag(Tag tag, boolean autoSave) {
Resource tagResource = tag.adaptTo(Resource.class);
if (tagResource == null) {
return;
Expand Down Expand Up @@ -427,7 +426,7 @@ public void setTags(Resource resource, Tag[] tags) {
public void setTags(Resource resource, Tag[] tags, boolean autoSave) {
ModifiableValueMap props = resource.adaptTo(ModifiableValueMap.class);
if (props == null) {
throw new RuntimeException("Unable to get modifiable value map: " + resource.getPath());
throw new IllegalStateException("Unable to get modifiable value map: " + resource.getPath());
}
if (tags == null) {
props.remove(TagConstants.PN_TAGS);
Expand All @@ -444,7 +443,8 @@ public void setTags(Resource resource, Tag[] tags, boolean autoSave) {
if (autoSave) {
try {
resourceResolver.commit();
} catch (PersistenceException e) {
}
catch (PersistenceException e) {
log.error("failed to commit updates for setting tags", e);
}
}
Expand All @@ -470,12 +470,12 @@ public boolean canCreateTagByTitle(String tagTitlePath, Locale locale) throws In
}

@Override
public Tag createTagByTitle(String titlePath, boolean autoSave) throws AccessControlException, InvalidTagFormatException {
public Tag createTagByTitle(String titlePath, boolean autoSave) throws InvalidTagFormatException {
throw new UnsupportedOperationException();
}

@Override
public Tag createTagByTitle(String titlePath, Locale locale) throws AccessControlException, InvalidTagFormatException {
public Tag createTagByTitle(String titlePath, Locale locale) throws InvalidTagFormatException {
throw new UnsupportedOperationException();
}

Expand All @@ -490,12 +490,12 @@ public FindResults findByTitle(String title) {
}

@Override
public void mergeTag(Tag tag, Tag destination) throws AccessControlException, TagException {
public void mergeTag(Tag tag, Tag destination) throws TagException {
throw new UnsupportedOperationException();
}

@Override
public Tag moveTag(Tag tag, String destination) throws AccessControlException, InvalidTagFormatException, TagException {
public Tag moveTag(Tag tag, String destination) throws InvalidTagFormatException, TagException {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -563,13 +563,13 @@ private ContentFragment contentFragmentTextOrStructured(@NotNull String path,
resource(path + "/" + JcrConstants.JCR_CONTENT + "/" + DamConstants.METADATA_FOLDER, metadataProps);

// store text as original rendition
if (text != null) {
if (text != null && mimeType != null) {
try (InputStream is = new ByteArrayInputStream(text.getBytes(StandardCharsets.UTF_8))) {
ContentLoader contentLoader = new ContentLoader(resourceResolver);
contentLoader.binaryFile(is, renditionsPath + "/" + DamConstants.ORIGINAL_FILE, mimeType);
}
catch (IOException ex) {
throw new RuntimeException("Unable to create content fragment at " + path, ex);
throw new IllegalArgumentException("Unable to create content fragment at " + path, ex);
}

// create model/elements/main node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class MockAemBindingsValuesProvider implements BindingsValuesProvider {

static final String PROPERTY_CONTEXT = "context";

private volatile AemContextImpl context;
@SuppressWarnings("java:S1845") // naming
private AemContextImpl context;

@Activate
private void activate(Map<String, Object> config) {
Expand Down
7 changes: 4 additions & 3 deletions core/src/test/java/io/wcm/testing/mock/aem/MockPageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -75,7 +76,7 @@ public void testProperties() {
assertEquals(3, this.page.getDepth());
assertEquals(false, this.page.isHideInNav());
assertNull(this.page.getVanityUrl());
assertNotNull(this.page.hashCode());
assertNotEquals(0, this.page.hashCode());
}

@Test
Expand Down Expand Up @@ -293,8 +294,8 @@ public void testEquals() throws Exception {
Page page2 = context.pageManager().getPage("/content/sample/en");
Page page3 = context.pageManager().getPage("/content/sample/en/toolbar/profiles");

assertTrue(page1.equals(page2));
assertFalse(page1.equals(page3));
assertEquals(page1, page2);
assertNotEquals(page1, page3);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
Expand Down Expand Up @@ -79,7 +80,7 @@ public void testProperties() {
assertEquals(1368001317000L, asset.getLastModified());
assertEquals("admin", asset.getModifier());
assertEquals("image/jpeg", asset.getMimeType());
assertNotNull(asset.hashCode());
assertNotEquals(0, asset.hashCode());
}

@Test
Expand Down Expand Up @@ -113,7 +114,7 @@ public void testEquals() throws Exception {
Asset asset1 = this.context.resourceResolver().getResource("/content/dam/sample/portraits/scott_reynolds.jpg").adaptTo(Asset.class);
Asset asset2 = this.context.resourceResolver().getResource("/content/dam/sample/portraits/scott_reynolds.jpg").adaptTo(Asset.class);

assertTrue(asset1.equals(asset2));
assertEquals(asset1, asset2);
}

private void doTestAddRemoveRendition(final String renditionName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
package io.wcm.testing.mock.aem.dam;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.apache.sling.api.resource.Resource;
import org.junit.Before;
Expand Down Expand Up @@ -58,7 +57,7 @@ public void testProperties() {
assertEquals("/content/dam/sample/portraits/scott_reynolds.jpg/jcr:content/renditions/original", rendition.getPath());
assertEquals("image/jpeg", rendition.getMimeType());
assertEquals("admin", rendition.getProperties().get(JcrConstants.JCR_LAST_MODIFIED_BY, String.class));
assertNotNull(rendition.hashCode());
assertNotEquals(0, rendition.hashCode());
}

@Test
Expand All @@ -85,8 +84,8 @@ public void testEquals() throws Exception {
Rendition rendition3 = this.context.resourceResolver()
.getResource("/content/dam/sample/portraits/scott_reynolds.jpg/jcr:content/renditions/cq5dam.thumbnail.48.48.png").adaptTo(Rendition.class);

assertTrue(rendition1.equals(rendition2));
assertFalse(rendition1.equals(rendition3));
assertEquals(rendition1, rendition2);
assertNotEquals(rendition1, rendition3);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -112,32 +112,32 @@ public void beforeAll(ExtensionContext extensionContext) throws Exception {
if (aemContextField != null) {
setAemContextInStore(extensionContext, aemContextField, null);
}
applyAemContext(extensionContext, aemContext -> {
applyAemContext(extensionContext, aemContext ->
// call context plugins setup after @BeforeAll methods were called
/* please note: in JUnit5 there is no callback to be called after all @BeforeAll methods are called
* so we call it before @BeforeAll execution to make sure the plugin code is called at all */
aemContext.getContextPlugins().executeAfterSetUpCallback(aemContext);
});
aemContext.getContextPlugins().executeAfterSetUpCallback(aemContext)
);
}
}

@Override
public void beforeEach(ExtensionContext extensionContext) {
if (!isBeforeAllContext(extensionContext)) {
applyAemContext(extensionContext, aemContext -> {
applyAemContext(extensionContext, aemContext ->
// call context plugins setup after @BeforeEach methods were called
aemContext.getContextPlugins().executeAfterSetUpCallback(aemContext);
});
aemContext.getContextPlugins().executeAfterSetUpCallback(aemContext)
);
}
}

@Override
public void afterTestExecution(ExtensionContext extensionContext) {
if (!isBeforeAllContext(extensionContext)) {
applyAemContext(extensionContext, aemContext -> {
applyAemContext(extensionContext, aemContext ->
// call context plugins setup before @AfterEach methods are called
aemContext.getContextPlugins().executeBeforeTearDownCallback(aemContext);
});
aemContext.getContextPlugins().executeBeforeTearDownCallback(aemContext)
);
}
}

Expand Down

0 comments on commit a3d80bb

Please sign in to comment.