Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eliminate SonarQube warnings #37

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/io/wcm/handler/media/MediaRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public MediaRequest(@NotNull Resource resource, @Nullable MediaArgs mediaArgs) {
* @param mediaRef Reference to media item
* @param mediaArgs Additional arguments affection media resolving
*/
public MediaRequest(@NotNull String mediaRef, @Nullable MediaArgs mediaArgs) {
public MediaRequest(@Nullable String mediaRef, @Nullable MediaArgs mediaArgs) {
this(null, mediaRef, mediaArgs, null);
}

Expand Down
20 changes: 11 additions & 9 deletions src/main/java/io/wcm/handler/media/format/MediaFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,16 +271,18 @@ private static String buildratioDisplayString(MediaFormat mf) {
* @return Ratio display string or null if no nice string was found
*/
private static String guessHumanReadableRatioString(double ratio, NumberFormat numberFormat) {
for (long width = 1; width <= 50; width++) {
double height = width / ratio;
if (isLong(height)) {
return numberFormat.format(width) + ":" + numberFormat.format(height);
if (ratio > 0) {
for (long width = 1; width <= 50; width++) {
double height = width / ratio;
if (isLong(height)) {
return numberFormat.format(width) + ":" + numberFormat.format(height);
}
}
}
for (long width = 1; width <= 200; width++) {
double height = width / 2d / ratio;
if (isHalfLong(height)) {
return numberFormat.format(width / 2d) + ":" + numberFormat.format(height);
for (long width = 1; width <= 200; width++) {
double height = width / 2d / ratio;
if (isHalfLong(height)) {
return numberFormat.format(width / 2d) + ":" + numberFormat.format(height);
}
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public final class DummyImageServlet extends AbstractImageServlet {
/**
* Dummy image content path
*/
@SuppressWarnings("java:S1075") // no file path
public static final @NotNull String PATH = "/apps/wcm-io/handler/media/content/dummyImage";

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,7 @@ private boolean addResponsiveImageMediaFormats(MediaArgs mediaArgs) {
if (!resolveForImageSizes(mediaArgs, additionalMediaFormats)) {
return false;
}
if (!resolveForResponsivePictureSources(mediaArgs, additionalMediaFormats)) {
return false;
}
resolveForResponsivePictureSources(mediaArgs, additionalMediaFormats);

// if additional media formats where found add them to the media format list in media args
if (!additionalMediaFormats.isEmpty()) {
Expand Down Expand Up @@ -186,15 +184,14 @@ private boolean resolveForImageSizes(MediaArgs mediaArgs, Map<String, MediaForma
return true;
}

private boolean resolveForResponsivePictureSources(MediaArgs mediaArgs, Map<String, MediaFormatOption> additionalMediaFormats) {
private void resolveForResponsivePictureSources(MediaArgs mediaArgs, Map<String, MediaFormatOption> additionalMediaFormats) {
PictureSource[] pictureSources = mediaArgs.getPictureSources();
if (pictureSources == null || pictureSources.length == 0) {
return true;
return;
}
for (PictureSource pictureSource : pictureSources) {
generateMediaFormatsForWidths(additionalMediaFormats, pictureSource.getMediaFormat(), false, pictureSource.getWidthOptions());
}
return true;
}

private void generateMediaFormatsForWidths(@NotNull Map<String, MediaFormatOption> additionalMediaFormats,
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/io/wcm/handler/media/impl/MediaHandlerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -286,8 +286,6 @@ private boolean resolveDownloadMediaFormats(MediaArgs mediaArgs) {
}

@Override
@SuppressWarnings("null")
@SuppressFBWarnings("NP_NONNULL_PARAM_VIOLATION")
public Media invalid() {
// build invalid media with first media source
Class<? extends MediaSource> mediaSourceClass = mediaHandlerConfig.getSources().stream().findFirst().orElse(null);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/io/wcm/handler/media/impl/WidthUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public final class WidthUtils {
// example values:
// 800,1024,2048
// 800,1024?,2048? <- last two are optional
static final Pattern WIDTHS_PATTERN = Pattern.compile("^\\s*\\d+\\??\\s*(,\\s*\\d+\\??\\s*)*$");
static final Pattern WIDTHS_PATTERN = Pattern.compile("^\\s*\\d+\\??\\s*(,\\s*\\d+\\??\\s*)*+$");

private WidthUtils() {
// static methods only
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@
ResourceProvider.PROPERTY_NAME + "=wcmioHandlerIPEConfig",
ResourceProvider.PROPERTY_ROOT + "=" + IPEConfigResourceProvider.IPECONFIG_OVERLAY_ROOTPATH
})
public class IPEConfigResourceProvider extends ResourceProvider {
public class IPEConfigResourceProvider extends ResourceProvider<Void> {

/**
* Root path for IPE config overlay resources.
*/
@SuppressWarnings("java:S1075") // no file path
public static final String IPECONFIG_OVERLAY_ROOTPATH = "/wcmio:mediaHandler/ipeConfig";

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ private String getMediaInvalidReasonText(Media mediaMetadata) {
* allow drag&amp;drop of assets into an empty component.
* @return CSS class or null
*/
@SuppressWarnings("java:S4275") // naming is by intention
public @Nullable String getClassAppend() {
return this.classAppendCombined;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,15 +119,13 @@ else if (JcrBinary.isNtResource(mediaInlineResource)) {
}

// skip further processing if nor binary resource found
if (ntResourceResource == null) {
if (referencedResource == null || ntResourceResource == null) {
media.setMediaInvalidReason(MediaInvalidReason.MEDIA_REFERENCE_INVALID);
return media;
}

// Update media args settings from resource (e.g. alt. text setings)
if (referencedResource != null) {
updateMediaArgsFromResource(mediaArgs, referencedResource, mediaHandlerConfig);
}
updateMediaArgsFromResource(mediaArgs, referencedResource, mediaHandlerConfig);

// Check for transformations
media.setCropDimension(getMediaCropDimension(media.getMediaRequest(), mediaHandlerConfig));
Expand Down Expand Up @@ -176,16 +174,16 @@ private Asset getInlineAsset(Resource ntResourceResource, Media media, String fi
* @param ntResourceResource nt:resource resource
* @return Detected or virtual filename. Never null.
*/
private String detectFileName(Resource referencedResource, Resource ntFileResource,
Resource ntResourceResource) {
private String detectFileName(@NotNull Resource referencedResource, @Nullable Resource ntFileResource,
@Nullable Resource ntResourceResource) {
// detect file name
String fileName = null;
// if referenced resource is not the nt:file node check for <nodename>Name property
if (ntFileResource != null && !referencedResource.equals(ntFileResource)) {
fileName = referencedResource.getValueMap().get(ntFileResource.getName() + "Name", String.class);
}
// if not nt:file node exists and the referenced resource is not the nt:resource node check for <nodename>Name property
else if (ntFileResource == null && !referencedResource.equals(ntResourceResource)) {
else if (ntFileResource == null && ntResourceResource != null && !referencedResource.equals(ntResourceResource)) {
fileName = referencedResource.getValueMap().get(ntResourceResource.getName() + "Name", String.class);
}
// otherwise use node name of nt:file resource if it exists
Expand Down
71 changes: 36 additions & 35 deletions src/test/java/io/wcm/handler/media/CropDimensionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -57,41 +58,41 @@ void testEquals() {
CropDimension dimension5 = new CropDimension(16, 5, 20, 10);
CropDimension dimension6 = new CropDimension(15, 6, 20, 10);

assertTrue(dimension1.equals(dimension2));
assertFalse(dimension1.equals(dimension3));
assertFalse(dimension1.equals(dimension4));
assertFalse(dimension1.equals(dimension5));
assertFalse(dimension1.equals(dimension6));

assertTrue(dimension2.equals(dimension1));
assertFalse(dimension2.equals(dimension3));
assertFalse(dimension2.equals(dimension4));
assertFalse(dimension2.equals(dimension5));
assertFalse(dimension2.equals(dimension6));

assertFalse(dimension3.equals(dimension1));
assertFalse(dimension3.equals(dimension2));
assertFalse(dimension3.equals(dimension4));
assertFalse(dimension3.equals(dimension5));
assertFalse(dimension3.equals(dimension6));

assertFalse(dimension4.equals(dimension1));
assertFalse(dimension4.equals(dimension2));
assertFalse(dimension4.equals(dimension3));
assertFalse(dimension4.equals(dimension5));
assertFalse(dimension4.equals(dimension6));

assertFalse(dimension5.equals(dimension1));
assertFalse(dimension5.equals(dimension2));
assertFalse(dimension5.equals(dimension3));
assertFalse(dimension5.equals(dimension4));
assertFalse(dimension5.equals(dimension6));

assertFalse(dimension6.equals(dimension1));
assertFalse(dimension6.equals(dimension2));
assertFalse(dimension6.equals(dimension3));
assertFalse(dimension6.equals(dimension4));
assertFalse(dimension6.equals(dimension5));
assertEquals(dimension1, dimension2);
assertNotEquals(dimension1, dimension3);
assertNotEquals(dimension1, dimension4);
assertNotEquals(dimension1, dimension5);
assertNotEquals(dimension1, dimension6);

assertEquals(dimension2, dimension1);
assertNotEquals(dimension2, dimension3);
assertNotEquals(dimension2, dimension4);
assertNotEquals(dimension2, dimension5);
assertNotEquals(dimension2, dimension6);

assertNotEquals(dimension3, dimension1);
assertNotEquals(dimension3, dimension2);
assertNotEquals(dimension3, dimension4);
assertNotEquals(dimension3, dimension5);
assertNotEquals(dimension3, dimension6);

assertNotEquals(dimension4, dimension1);
assertNotEquals(dimension4, dimension2);
assertNotEquals(dimension4, dimension3);
assertNotEquals(dimension4, dimension5);
assertNotEquals(dimension4, dimension6);

assertNotEquals(dimension5, dimension1);
assertNotEquals(dimension5, dimension2);
assertNotEquals(dimension5, dimension3);
assertNotEquals(dimension5, dimension4);
assertNotEquals(dimension5, dimension6);

assertNotEquals(dimension6, dimension1);
assertNotEquals(dimension6, dimension2);
assertNotEquals(dimension6, dimension3);
assertNotEquals(dimension6, dimension4);
assertNotEquals(dimension6, dimension5);
}

@Test
Expand Down
27 changes: 13 additions & 14 deletions src/test/java/io/wcm/handler/media/DimensionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@
package io.wcm.handler.media;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

import org.junit.jupiter.api.Test;

Expand All @@ -43,21 +42,21 @@ void testEquals() {
Dimension dimension3 = new Dimension(21, 10);
Dimension dimension4 = new Dimension(20, 11);

assertTrue(dimension1.equals(dimension2));
assertFalse(dimension1.equals(dimension3));
assertFalse(dimension1.equals(dimension4));
assertEquals(dimension1, dimension2);
assertNotEquals(dimension1, dimension3);
assertNotEquals(dimension1, dimension4);

assertTrue(dimension2.equals(dimension1));
assertFalse(dimension2.equals(dimension3));
assertFalse(dimension2.equals(dimension4));
assertEquals(dimension2, dimension1);
assertNotEquals(dimension2, dimension3);
assertNotEquals(dimension2, dimension4);

assertFalse(dimension3.equals(dimension1));
assertFalse(dimension3.equals(dimension2));
assertFalse(dimension3.equals(dimension4));
assertNotEquals(dimension3, dimension1);
assertNotEquals(dimension3, dimension2);
assertNotEquals(dimension3, dimension4);

assertFalse(dimension4.equals(dimension1));
assertFalse(dimension4.equals(dimension2));
assertFalse(dimension4.equals(dimension3));
assertNotEquals(dimension4, dimension1);
assertNotEquals(dimension4, dimension2);
assertNotEquals(dimension4, dimension3);
}

}
10 changes: 5 additions & 5 deletions src/test/java/io/wcm/handler/media/MediaArgsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import static io.wcm.handler.media.testcontext.DummyMediaFormats.EDITORIAL_2COL;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotSame;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -239,10 +239,10 @@ void testEquals() {
MediaArgs mediaArgs2 = new MediaArgs().mediaFormat(EDITORIAL_1COL).urlMode(UrlModes.FULL_URL).altText("abc");
MediaArgs mediaArgs3 = new MediaArgs().mediaFormat(EDITORIAL_2COL).urlMode(UrlModes.FULL_URL).altText("abc");

assertTrue(mediaArgs1.equals(mediaArgs2));
assertTrue(mediaArgs2.equals(mediaArgs1));
assertFalse(mediaArgs1.equals(mediaArgs3));
assertFalse(mediaArgs2.equals(mediaArgs3));
assertEquals(mediaArgs1, mediaArgs2);
assertEquals(mediaArgs2, mediaArgs1);
assertNotEquals(mediaArgs1, mediaArgs3);
assertNotEquals(mediaArgs2, mediaArgs3);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -371,13 +371,13 @@ void testDetectMediaFormat() {
assertNull(underTest.detectMediaFormat("txt", 100, 570, 270));

// test extension match
assertEquals("download", "download", underTest.detectMediaFormat("zip", 0, 0, 0).getName());
assertEquals("download", underTest.detectMediaFormat("zip", 0, 0, 0).getName());

// test direct match
assertEquals("editorial_2col", "editorial_2col", underTest.detectMediaFormat("jpg", 100, 450, 213).getName());
assertEquals("editorial_2col", underTest.detectMediaFormat("jpg", 100, 450, 213).getName());

// test ranking match
assertEquals("showroom_campaign", "showroom_campaign", underTest.detectMediaFormat("jpg", 100, 960, 455).getName());
assertEquals("showroom_campaign", underTest.detectMediaFormat("jpg", 100, 960, 455).getName());

}

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

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -72,40 +73,40 @@ void setUp() throws Exception {
*/
@Test
void testCompareOriginalRenditionToItself() {
assertTrue(originalRendition.compareTo(originalRendition) == 0, "original rendition is not equal to itself");
assertEquals(originalRendition.compareTo(originalRendition), 0, "original rendition is not equal to itself");
}

/**
* Small and big rendition
*/
@Test
void testCompareSmallToBigRendition() {
assertTrue(smallestRendition.compareTo(biggestRendition) == -1, "smaller rendition is not smaller");
assertEquals(smallestRendition.compareTo(biggestRendition), -1, "smaller rendition is not smaller");
}

/**
* Big and small rendition
*/
@Test
void testCompareBigToSmallRendition() {
assertTrue(biggestRendition.compareTo(smallestRendition) == 1, "bigger rendition is not bigger");
assertEquals(biggestRendition.compareTo(smallestRendition), 1, "bigger rendition is not bigger");
}

/**
* Two equal renditions
*/
@Test
void testCompareTwoEqualRenditions() {
assertTrue(biggestRendition.compareTo(biggestRendition) == 0, "two equal renditions are not equal");
assertEquals(biggestRendition.compareTo(biggestRendition), 0, "two equal renditions are not equal");
}

/**
* Original rendition and a rendition, with the same dimension as original rendition
*/
@Test
void testCompareOriginalRenditionToEqualRendition() {
assertTrue(originalRendition.compareTo(originalRenditionCopy) == -1, "original rendition is not preferred over the equal rendition");
assertTrue(originalRenditionCopy.compareTo(originalRendition) == 1, "original rendition is not preferred over the equal rendition");
assertEquals(originalRendition.compareTo(originalRenditionCopy), -1, "original rendition is not preferred over the equal rendition");
assertEquals(originalRenditionCopy.compareTo(originalRendition), 1, "original rendition is not preferred over the equal rendition");
}

@Test
Expand Down Expand Up @@ -179,8 +180,8 @@ void testAdaptTo() throws Exception {

@Test
void testEquals() {
assertTrue(smallestRendition.equals(smallestRendition));
assertFalse(smallestRendition.equals(biggestRendition));
assertEquals(smallestRendition,smallestRendition);
assertNotEquals(smallestRendition, biggestRendition);
}

private void loadImageBinary_originalRenditionCopy() throws PersistenceException {
Expand Down
Loading