forked from green-code-initiative/creedengo-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implementing rule EC24 in Java: - Adding the rule, the example code and the tests - Registering the rule using SNAPSHOT of ecocode-rules-specifications, and updating registrar tests Fixes green-code-initiative/creedengo-rules-specifications#308
- Loading branch information
Showing
7 changed files
with
130 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/main/java/fr/greencodeinitiative/java/checks/LimitDbQueryResults.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
* 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 org.sonar.check.Rule; | ||
import org.sonar.plugins.java.api.IssuableSubscriptionVisitor; | ||
import org.sonar.plugins.java.api.tree.LiteralTree; | ||
import org.sonar.plugins.java.api.tree.Tree; | ||
import org.sonar.plugins.java.api.tree.Tree.Kind; | ||
|
||
import com.google.re2j.Pattern; | ||
|
||
@Rule(key = "EC24") | ||
public class LimitDbQueryResults extends IssuableSubscriptionVisitor { | ||
|
||
protected static final String MESSAGERULE = "Try and limit the number of data returned for a single query (by using the LIMIT keyword for example)"; | ||
|
||
private static final Pattern PATTERN = Pattern.compile("(LIMIT|TOP|ROW_NUMBER|FETCH FIRST|WHERE)", Pattern.CASE_INSENSITIVE); | ||
|
||
@Override | ||
public List<Kind> nodesToVisit() { | ||
return Collections.singletonList(Tree.Kind.STRING_LITERAL); | ||
} | ||
|
||
@Override | ||
public void visitNode(Tree tree) { | ||
String value = ((LiteralTree) tree).value().toUpperCase(); | ||
if (value.contains("SELECT") && value.contains("FROM") && !PATTERN.matcher(value).find()) { | ||
reportIssue(tree, MESSAGERULE); | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/* | ||
* 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; | ||
|
||
class LimitDbQueryResults { | ||
LimitDbQueryResults(LimitDbQueryResults mc) { | ||
} | ||
|
||
public void literalSQLrequest() { | ||
dummyCall("SELECT id, name, email FROM customers LIMIT 10;"); | ||
dummyCall("SELECT TOP 5 * FROM products;"); | ||
dummyCall("SELECT id, name, email FROM customers WHERE id = 1;"); | ||
dummyCall("SELECT * FROM orders FETCH FIRST 20 ROWS ONLY;"); | ||
dummyCall("WITH numbered_customers AS (SELECT *, ROW_NUMBER() OVER (ORDER BY customer_id) AS row_num FROM customers) SELECT * FROM numbered_customers WHERE row_num <= 50;"); | ||
|
||
dummyCall("SELECT * FROM bikes;"); // Noncompliant {{Try and limit the number of data returned for a single query (by using the LIMIT keyword for example)}} | ||
dummyCall("SELECT id FROM customers;"); // Noncompliant {{Try and limit the number of data returned for a single query (by using the LIMIT keyword for example)}} | ||
} | ||
|
||
private String dummyCall(String request) { | ||
return request; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/test/java/fr/greencodeinitiative/java/checks/LimitDbQueryResultsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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; | ||
|
||
class LimitDbQueryResultsTest { | ||
|
||
@Test | ||
void test() { | ||
CheckVerifier.newVerifier() | ||
.onFile("src/test/files/LimitDbQueryResults.java") | ||
.withCheck(new LimitDbQueryResults()) | ||
.verifyIssues(); | ||
} | ||
|
||
} |