-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #89 from green-code-initiative/GCI82
GCI82 - add rule + refacto ITs + small corrections UT
- Loading branch information
Showing
15 changed files
with
410 additions
and
157 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
28 changes: 0 additions & 28 deletions
28
src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/BaseIT.java
This file was deleted.
Oops, something went wrong.
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
39 changes: 0 additions & 39 deletions
39
src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCI69IT.java
This file was deleted.
Oops, something went wrong.
39 changes: 0 additions & 39 deletions
39
src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCI94IT.java
This file was deleted.
Oops, something went wrong.
93 changes: 93 additions & 0 deletions
93
src/it/java/org/greencodeinitiative/creedengo/java/integration/tests/GCIRulesIT.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,93 @@ | ||
package org.greencodeinitiative.creedengo.java.integration.tests; | ||
|
||
import org.assertj.core.groups.Tuple; | ||
import org.junit.jupiter.api.Test; | ||
import org.sonarqube.ws.Issues; | ||
import org.sonarqube.ws.Measures; | ||
|
||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static java.util.Optional.ofNullable; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.sonarqube.ws.Common.RuleType.CODE_SMELL; | ||
import static org.sonarqube.ws.Common.Severity.MINOR; | ||
|
||
class GCIRulesIT extends BuildProjectEngine { | ||
|
||
@Test | ||
void testMeasuresAndIssues() { | ||
String projectKey = analyzedProjects.get(0).getProjectKey(); | ||
|
||
Map<String, Measures.Measure> measures = getMeasures(projectKey); | ||
|
||
assertThat(ofNullable(measures.get("code_smells")).map(Measures.Measure::getValue).map(Integer::parseInt).orElse(0)) | ||
.isGreaterThan(1); | ||
|
||
List<Issues.Issue> projectIssues = issuesForComponent(projectKey); | ||
assertThat(projectIssues).isNotEmpty(); | ||
|
||
} | ||
|
||
@Test | ||
void testGCI69() { | ||
String projectKey = analyzedProjects.get(0).getProjectKey(); | ||
|
||
List<Issues.Issue> issues = issuesForFile(projectKey, | ||
"src/main/java/org/greencodeinitiative/creedengo/java/checks/AvoidGettingSizeCollectionInForLoopIgnored.java"); | ||
|
||
assertThat(issues) | ||
.hasSize(1) | ||
.extracting("rule", "message", "line", "textRange.startLine", "textRange.endLine", | ||
"textRange.startOffset", "textRange.endOffset", "severity", "type", "debt", "effort") | ||
.containsExactly( | ||
Tuple.tuple("creedengo-java:GCI69", "Do not call a function when declaring a for-type loop", | ||
18, 18, 18, 15, 27, MINOR, CODE_SMELL, "5min", "5min") | ||
); | ||
|
||
} | ||
|
||
@Test | ||
void testGCI82() { | ||
String projectKey = analyzedProjects.get(0).getProjectKey(); | ||
|
||
List<Issues.Issue> issues = issuesForFile(projectKey, | ||
"src/main/java/org/greencodeinitiative/creedengo/java/checks/MakeNonReassignedVariablesConstants.java"); | ||
|
||
assertThat(issues) | ||
.hasSize(4) | ||
.extracting("rule", "message", "line", "textRange.startLine", "textRange.endLine", | ||
"textRange.startOffset", "textRange.endOffset", "severity", "type", "debt", "effort") | ||
.contains( | ||
Tuple.tuple("creedengo-java:GCI82", "The variable is never reassigned and can be 'final'", | ||
7, 7, 7, 4, 67, MINOR, CODE_SMELL, "5min", "5min"), | ||
Tuple.tuple("creedengo-java:GCI82", "The variable is never reassigned and can be 'final'", | ||
12, 12, 12, 4, 56, MINOR, CODE_SMELL, "5min", "5min"), | ||
Tuple.tuple("creedengo-java:GCI82", "The variable is never reassigned and can be 'final'", | ||
13, 13, 13, 4, 50, MINOR, CODE_SMELL, "5min", "5min"), | ||
Tuple.tuple("creedengo-java:GCI82", "The variable is never reassigned and can be 'final'", | ||
45, 45, 45, 8, 25, MINOR, CODE_SMELL, "5min", "5min") | ||
); | ||
|
||
} | ||
|
||
@Test | ||
void testGCI94() { | ||
String projectKey = analyzedProjects.get(0).getProjectKey(); | ||
|
||
List<Issues.Issue> issues = issuesForFile(projectKey, | ||
"src/main/java/org/greencodeinitiative/creedengo/java/checks/UseOptionalOrElseGetVsOrElse.java"); | ||
|
||
assertThat(issues) | ||
.hasSize(1) | ||
.extracting("rule", "message", "line", "textRange.startLine", "textRange.endLine", | ||
"textRange.startOffset", "textRange.endOffset", "severity", "type", "debt", "effort") | ||
.containsExactly( | ||
Tuple.tuple( | ||
"creedengo-java:GCI94", "Use optional orElseGet instead of orElse.", | ||
25, 25, 25, 38, 69, MINOR, CODE_SMELL, "1min", "1min") | ||
); | ||
|
||
} | ||
|
||
} |
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
69 changes: 69 additions & 0 deletions
69
...va/org/greencodeinitiative/creedengo/java/checks/MakeNonReassignedVariablesConstants.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,69 @@ | ||
import java.util.logging.Logger; | ||
|
||
public class MakeNonReassignedVariablesConstants { | ||
|
||
private final Logger logger = Logger.getLogger(""); // Compliant | ||
|
||
private Object myNonFinalAndNotReassignedObject = new Object(); // Noncompliant {{The variable is never reassigned and can be 'final'}} | ||
private Object myNonFinalAndReassignedObject = new Object(); // Compliant | ||
private final Object myFinalAndNotReassignedObject = new Object(); // Compliant | ||
|
||
private static final String CONSTANT = "toto"; // Compliant | ||
private String varDefinedInClassNotReassigned = "0"; // Noncompliant {{The variable is never reassigned and can be 'final'}} | ||
private String varDefinedInClassNotUsed = "0"; // Noncompliant {{The variable is never reassigned and can be 'final'}} | ||
private String varDefinedInClassReassigned = "0"; // Compliant | ||
private String varDefinedInConstructorReassigned = "1"; // Compliant | ||
|
||
public MakeNonReassignedVariablesConstants() { | ||
varDefinedInConstructorReassigned = "3"; | ||
logger.info(varDefinedInConstructorReassigned); | ||
} | ||
|
||
void localVariableReassigned() { | ||
String y1 = "10"; // Compliant | ||
final String PI = "3.14159"; // Compliant | ||
|
||
y1 = "titi"; | ||
|
||
logger.info(y1); | ||
logger.info(PI); | ||
} | ||
|
||
void localVariableIncrement() { | ||
String y2 = "10"; // Compliant | ||
y2 += "titi"; | ||
logger.info(y2); | ||
} | ||
|
||
void localIntVariableIncrement() { | ||
int y3 = 10; // Compliant | ||
++y3; | ||
logger.info(y3+""); | ||
} | ||
|
||
void localVariableNotReassigned() { | ||
String y4 = "10"; // Noncompliant {{The variable is never reassigned and can be 'final'}} | ||
final String PI2 = "3.14159"; // Compliant | ||
|
||
logger.info(y4); | ||
logger.info(PI2); | ||
} | ||
|
||
void classVariableReassigned() { | ||
varDefinedInClassReassigned = "1"; | ||
|
||
logger.info(varDefinedInClassReassigned); | ||
logger.info(varDefinedInClassNotReassigned); | ||
logger.info(CONSTANT); | ||
} | ||
|
||
void classVariableReassignedBis() { | ||
varDefinedInClassReassigned = "2"; // method to avoid sonarqube error asking for moving class variable "varDefinedInClassReassigned" to local variable method | ||
myNonFinalAndReassignedObject = new Object(); | ||
|
||
logger.info(varDefinedInClassReassigned); | ||
logger.info(myNonFinalAndReassignedObject.toString()); | ||
logger.info(myFinalAndNotReassignedObject.toString()); | ||
} | ||
|
||
} |
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
Oops, something went wrong.