diff --git a/core/src/main/java/io/wcm/testing/mock/aem/MockComponent.java b/core/src/main/java/io/wcm/testing/mock/aem/MockComponent.java index 6838cf45..92438902 100644 --- a/core/src/main/java/io/wcm/testing/mock/aem/MockComponent.java +++ b/core/src/main/java/io/wcm/testing/mock/aem/MockComponent.java @@ -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; @@ -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}. */ @@ -261,12 +261,12 @@ private static class RemoveKeyPrefixMap extends ValueMapDecorator { } @Override - public T get(String name, Class type) { + public T get(@NotNull String name, Class type) { return super.get(removeKeyPrefix(name), type); } @Override - public T get(String name, T defaultValue) { + public T get(@NotNull String name, T defaultValue) { return super.get(removeKeyPrefix(name), defaultValue); } diff --git a/core/src/main/java/io/wcm/testing/mock/aem/MockComponentContext.java b/core/src/main/java/io/wcm/testing/mock/aem/MockComponentContext.java index 557fc42a..333e372c 100644 --- a/core/src/main/java/io/wcm/testing/mock/aem/MockComponentContext.java +++ b/core/src/main/java/io/wcm/testing/mock/aem/MockComponentContext.java @@ -79,6 +79,7 @@ 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) { @@ -86,7 +87,7 @@ public Component getComponent() { } 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); } diff --git a/core/src/main/java/io/wcm/testing/mock/aem/MockLanguageManager.java b/core/src/main/java/io/wcm/testing/mock/aem/MockLanguageManager.java index acd263ce..abfb7898 100644 --- a/core/src/main/java/io/wcm/testing/mock/aem/MockLanguageManager.java +++ b/core/src/main/java/io/wcm/testing/mock/aem/MockLanguageManager.java @@ -61,7 +61,7 @@ public final class MockLanguageManager implements LanguageManager { @Override - @Deprecated + @Deprecated(forRemoval = true) public Map getAdjacentInfo(final ResourceResolver resourceResolver, final String path) { return Optional.ofNullable(getAdjacentLanguageInfo(resourceResolver, path)) .map(Map::entrySet) @@ -71,6 +71,7 @@ public Map getAdjacentInfo(final ResourceResolver resourceResolver } @Override + @SuppressWarnings("null") public Map getAdjacentLanguageInfo(final ResourceResolver resourceResolver, final String path) { return Optional.ofNullable(LanguageUtil.getLanguageRoot(path)) .map(root -> path.substring(root.length())) diff --git a/core/src/main/java/io/wcm/testing/mock/aem/MockTagManager.java b/core/src/main/java/io/wcm/testing/mock/aem/MockTagManager.java index 490d0b65..fa7785a1 100644 --- a/core/src/main/java/io/wcm/testing/mock/aem/MockTagManager.java +++ b/core/src/main/java/io/wcm/testing/mock/aem/MockTagManager.java @@ -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; @@ -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); @@ -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; @@ -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); @@ -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); } } @@ -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(); } @@ -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(); } diff --git a/core/src/main/java/io/wcm/testing/mock/aem/builder/ContentBuilder.java b/core/src/main/java/io/wcm/testing/mock/aem/builder/ContentBuilder.java index 91071e96..0a0858b2 100644 --- a/core/src/main/java/io/wcm/testing/mock/aem/builder/ContentBuilder.java +++ b/core/src/main/java/io/wcm/testing/mock/aem/builder/ContentBuilder.java @@ -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 diff --git a/core/src/main/java/io/wcm/testing/mock/aem/context/MockAemBindingsValuesProvider.java b/core/src/main/java/io/wcm/testing/mock/aem/context/MockAemBindingsValuesProvider.java index c77cc4b2..dfa1efe8 100644 --- a/core/src/main/java/io/wcm/testing/mock/aem/context/MockAemBindingsValuesProvider.java +++ b/core/src/main/java/io/wcm/testing/mock/aem/context/MockAemBindingsValuesProvider.java @@ -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 config) { diff --git a/core/src/test/java/io/wcm/testing/mock/aem/MockPageTest.java b/core/src/test/java/io/wcm/testing/mock/aem/MockPageTest.java index d8fe52d8..702c4098 100644 --- a/core/src/test/java/io/wcm/testing/mock/aem/MockPageTest.java +++ b/core/src/test/java/io/wcm/testing/mock/aem/MockPageTest.java @@ -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; @@ -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 @@ -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); } } diff --git a/core/src/test/java/io/wcm/testing/mock/aem/dam/MockAssetTest.java b/core/src/test/java/io/wcm/testing/mock/aem/dam/MockAssetTest.java index 918e5967..1b1802b0 100644 --- a/core/src/test/java/io/wcm/testing/mock/aem/dam/MockAssetTest.java +++ b/core/src/test/java/io/wcm/testing/mock/aem/dam/MockAssetTest.java @@ -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; @@ -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 @@ -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) { diff --git a/core/src/test/java/io/wcm/testing/mock/aem/dam/MockRenditionTest.java b/core/src/test/java/io/wcm/testing/mock/aem/dam/MockRenditionTest.java index b3e13f9d..ea04b7c4 100644 --- a/core/src/test/java/io/wcm/testing/mock/aem/dam/MockRenditionTest.java +++ b/core/src/test/java/io/wcm/testing/mock/aem/dam/MockRenditionTest.java @@ -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; @@ -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 @@ -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); } } diff --git a/junit5/src/main/java/io/wcm/testing/mock/aem/junit5/AemContextExtension.java b/junit5/src/main/java/io/wcm/testing/mock/aem/junit5/AemContextExtension.java index c5b3f9fd..82c25e18 100644 --- a/junit5/src/main/java/io/wcm/testing/mock/aem/junit5/AemContextExtension.java +++ b/junit5/src/main/java/io/wcm/testing/mock/aem/junit5/AemContextExtension.java @@ -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) + ); } }