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

[EC203] Detect unoptimized image format #47

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- [#47](https://github.com/green-code-initiative/ecoCode-java/pull/47) EC203 rule : Detect unoptimized image format

### Changed

### Deleted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import fr.greencodeinitiative.java.checks.AvoidSpringRepositoryCallInLoopOrStreamCheck;
import fr.greencodeinitiative.java.checks.AvoidStatementForDMLQueries;
import fr.greencodeinitiative.java.checks.AvoidUsageOfStaticCollections;
import fr.greencodeinitiative.java.checks.DetectUnoptimizedImageFormat;
import fr.greencodeinitiative.java.checks.FreeResourcesOfAutoCloseableInterface;
import fr.greencodeinitiative.java.checks.IncrementCheck;
import fr.greencodeinitiative.java.checks.InitializeBufferWithAppropriateSize;
Expand Down Expand Up @@ -61,6 +62,7 @@ public class JavaCheckRegistrar implements CheckRegistrar {
OptimizeReadFileExceptions.class,
InitializeBufferWithAppropriateSize.class,
AvoidSetConstantInBatchUpdate.class,
DetectUnoptimizedImageFormat.class,
FreeResourcesOfAutoCloseableInterface.class,
AvoidMultipleIfElseStatement.class
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.greencodeinitiative.java.checks;

import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;

import org.sonar.check.Rule;
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor;
import org.sonar.plugins.java.api.tree.BaseTreeVisitor;
import org.sonar.plugins.java.api.tree.LiteralTree;
import org.sonar.plugins.java.api.tree.Tree;

@Rule(key = "EC203")
public class DetectUnoptimizedImageFormat extends IssuableSubscriptionVisitor {

protected static final String MESSAGERULE = "Detect unoptimized image format";
protected static final Pattern IMG_EXTENSION = Pattern.compile("\\.(bmp|ico|tiff|webp|png|jpg|jpeg|jfif|pjpeg|pjp|gif|avif|apng)");

private final DetectUnoptimizedImageFormatVisitor visitor = new DetectUnoptimizedImageFormatVisitor();

@Override
public List<Tree.Kind> nodesToVisit() {
return Collections.singletonList(
Tree.Kind.STRING_LITERAL
);
}

@Override
public void visitNode(@Nonnull Tree tree) {
tree.accept(visitor);
}

private class DetectUnoptimizedImageFormatVisitor extends BaseTreeVisitor {

@Override
public void visitLiteral(@Nonnull LiteralTree tree) {
final String strValue = tree.token().text();
final Matcher matcher = IMG_EXTENSION.matcher(strValue);
if (matcher.find()) {
reportIssue(tree, MESSAGERULE);
} else {
super.visitLiteral(tree);
}
}
}
}
61 changes: 61 additions & 0 deletions src/test/files/DetectUnoptimizedImageFormat.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.greencodeinitiative.java.checks;

/**
* Not compliant
*/
public class DetectUnoptimizedImageFormat {

public String testImage(String image) {
return "path/to/" + image;
}

public String testImageFormat2() {

String img_bmp = "test/image.bmp"; // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
String img_ico = "image.ico"; // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
String img_tiff = "test/path/to/image.tiff"; // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
String img_webp = "test/path/to/" + "image.webp"; // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
String img_jpg = "image.jpg"; // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
String img_jpeg = "image.jpeg"; // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
String img_jfif = "image.jfif"; // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
String img_pjpeg = "image.pjpeg"; // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
String img_pjp = "image.pjp"; // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
String img_gif = "image.gif"; // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
String img_avif = "image.avif"; // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
String img_apng = "image.apng"; // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}

String image_format = testImage("image.jpg"); // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}

return "<html><img src=\"xx/xx/image.bmp\" >" // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
+ "<img src=\"xx/xx/image.ico\" >" // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
+ "<img src=\"xx/xx/image.tiff\" >" // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
+ "<img src=\"xx/xx/image.webp\" >" // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
+ "<img src=\"xx/xx/image.png\" >" // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
+ "<img src=\"xx/xx/image.jpg\" >" // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
+ "<img src=\"xx/xx/image.jpeg\" >" // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
+ "<img src=\"xx/xx/image.jfif\" >" // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
+ "<img src=\"xx/xx/image.pjpeg\" >" // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
+ "<img src=\"xx/xx/image.pjp\" >" // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
+ "<img src=\"xx/xx/image.gif\" >" // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
+ "<img src=\"xx/xx/image.avif\" >" // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
+ "<img src=\"xx/xx/image.apng\" >" // Noncompliant {{If possible, the utilisation of svg image format (or <svg/> html tag) is recommended over other image format.}}
+ "</html>";
}
}
43 changes: 43 additions & 0 deletions src/test/files/DetectUnoptimizedImageFormatCompliant.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.greencodeinitiative.java.checks;

/**
* Compliant
*/
public class DetectUnoptimizedImageFormatCompliant {

public String testImage(String image) {
return "path/to/" + image;
}

public String testImageFormat2() {


String img_svg = "test/image.svg"; // Compliant

String image_format = testImage("image.svg"); // Compliant

String image_svg_html = "<html><svg width=\"100\" height=\"100\">" + // Compliant
"<circle cx=\"50\" cy=\"50\" r=\"40\" stroke=\"green\" stroke-width=\"4\" fill=\"yellow\" />" +
"</svg></html>";

return "<html><img src=\"xx/xx/image.svg\" >" // Compliant
+ "</html>";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void checkNumberRules() {
final JavaCheckRegistrar registrar = new JavaCheckRegistrar();
registrar.register(context);

assertThat(context.checkClasses()).hasSize(15);
assertThat(context.checkClasses()).hasSize(16);
assertThat(context.testCheckClasses()).isEmpty();

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* ecoCode - Java language - Provides rules to reduce the environmental footprint of your Java programs
* Copyright © 2023 Green Code Initiative (https://www.ecocode.io)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package fr.greencodeinitiative.java.checks;

import org.junit.jupiter.api.Test;
import org.sonar.java.checks.verifier.CheckVerifier;

public class DetectUnoptimizedImageFormatTest {

@Test
void testHasIssues() {
CheckVerifier.newVerifier()
.onFile("src/test/files/DetectUnoptimizedImageFormat.java")
.withCheck(new DetectUnoptimizedImageFormat())
.verifyIssues();
}

@Test
void testNoIssues() {
CheckVerifier.newVerifier()
.onFile("src/test/files/DetectUnoptimizedImageFormatCompliant.java")
.withCheck(new DetectUnoptimizedImageFormat())
.verifyNoIssues();
}
}
Loading