-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Categories based on sonar's tags (#48)
- Loading branch information
1 parent
5bee389
commit d5fe9f6
Showing
6 changed files
with
195 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,95 @@ | ||
package cc.models; | ||
|
||
import com.google.gson.annotations.SerializedName; | ||
import org.sonarsource.sonarlint.core.client.api.common.RuleDetails; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
class Categories extends ArrayList<String> { | ||
public Categories(String ruleType) { | ||
String category; | ||
switch (ruleType) { | ||
import static cc.models.Categories.Category.*; | ||
|
||
class Categories extends ArrayList<Categories.Category> { | ||
public enum Category { | ||
@SerializedName("Bug Risk") | ||
BUG_RISK, | ||
|
||
@SerializedName("Clarity") | ||
CLARITY, | ||
|
||
@SerializedName("Compatibility") | ||
COMPATIBILITY, | ||
|
||
@SerializedName("Complexity") | ||
COMPLEXITY, | ||
|
||
@SerializedName("Duplication") | ||
DUPLICATION, | ||
|
||
@SerializedName("Performance") | ||
PERFORMANCE, | ||
|
||
@SerializedName("Security") | ||
SECURITY, | ||
|
||
@SerializedName("Style") | ||
STYLE; | ||
} | ||
|
||
public Categories(RuleDetails rule) { | ||
switch (rule.getType()) { | ||
case "VULNERABILITY": { | ||
category = "Security"; | ||
add(SECURITY); | ||
break; | ||
} | ||
case "BUG": { | ||
add(BUG_RISK); | ||
break; | ||
} | ||
case "CODE_SMELL": | ||
default: { | ||
category = "Bug Risk"; | ||
Category category = fromTags(rule.getTags()); | ||
add(category); | ||
break; | ||
} | ||
} | ||
add(category); | ||
} | ||
|
||
private Category fromTags(String[] tags) { | ||
List<String> tagList = Arrays.asList(tags); | ||
if (tagList.contains("brain-overload")) { | ||
return COMPLEXITY; | ||
} | ||
|
||
if (tagList.contains("duplicate")) { | ||
return DUPLICATION; | ||
} | ||
|
||
if (tagList.contains("deadlock") || tagList.contains("unpredictable") | ||
|| tagList.contains("bad-practice") || tagList.contains("suspicious")) { | ||
return BUG_RISK; | ||
} | ||
|
||
if (tagList.contains("maven")) { | ||
return COMPATIBILITY; | ||
} | ||
|
||
if (tagList.contains("performance")) { | ||
return PERFORMANCE; | ||
} | ||
|
||
if (tagList.contains("convention") || tagList.contains("style")) { | ||
return STYLE; | ||
} | ||
|
||
if (tagList.contains("confusing")) { | ||
return CLARITY; | ||
} | ||
|
||
return CLARITY; | ||
} | ||
|
||
public static Categories from(RuleDetails ruleDetails) { | ||
return new Categories(ruleDetails.getType()); | ||
return new Categories(ruleDetails); | ||
} | ||
} |
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,85 @@ | ||
package cc.models; | ||
|
||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
import org.sonar.api.batch.rule.Rule; | ||
import org.sonarsource.sonarlint.core.client.api.common.RuleDetails; | ||
import support.Factory; | ||
|
||
import java.util.Collection; | ||
|
||
import static cc.models.Categories.Category.*; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class CategoriesTest { | ||
|
||
@BeforeClass | ||
public static void beforeAll() throws Exception { | ||
System.setProperty("sonarlint.home", "build"); | ||
} | ||
|
||
@Test | ||
public void set_clarity_for_generic_code_smells() throws Exception { | ||
assertThat(getCategoriesForRule("S1602")).contains(CLARITY); | ||
} | ||
|
||
@Test | ||
public void set_complexity_for_brain_overload_code_smells() throws Exception { | ||
assertThat(getCategoriesForRule("S1067")).contains(COMPLEXITY); | ||
} | ||
|
||
@Test | ||
public void set_duplication_for_duplicate_code_smells() throws Exception { | ||
assertThat(getCategoriesForRule("S4144")).contains(DUPLICATION); | ||
} | ||
|
||
@Test | ||
public void set_clarity_for_confusing_code_smells() throws Exception { | ||
assertThat(getCategoriesForRule("S1141")).contains(CLARITY); | ||
} | ||
|
||
@Test | ||
public void set_bug_risk_for_deadlock_code_smells() throws Exception { | ||
assertThat(getCategoriesForRule("S3046")).contains(BUG_RISK); | ||
} | ||
|
||
@Test | ||
public void set_compatibility_for_maven_code_smells() throws Exception { | ||
assertThat(getCategoriesForRule("S3423")).contains(COMPATIBILITY); | ||
} | ||
|
||
@Test | ||
public void set_performance_for_performance_code_smells() throws Exception { | ||
assertThat(getCategoriesForRule("S1149")).contains(PERFORMANCE); | ||
} | ||
|
||
@Test | ||
public void set_style_for_convention_code_smells() throws Exception { | ||
assertThat(getCategoriesForRule("S00115")).contains(Categories.Category.STYLE); | ||
} | ||
|
||
@Test | ||
public void set_style_for_style_code_smells() throws Exception { | ||
assertThat(getCategoriesForRule("S00122")).contains(Categories.Category.STYLE); | ||
} | ||
|
||
@Test | ||
public void set_bug_risk_for_unpredictable_code_smells() throws Exception { | ||
assertThat(getCategoriesForRule("S1215")).contains(Categories.Category.BUG_RISK); | ||
} | ||
|
||
@Test | ||
public void set_bug_risk_for_bad_practice_code_smells() throws Exception { | ||
assertThat(getCategoriesForRule("S106")).contains(Categories.Category.BUG_RISK); | ||
} | ||
|
||
@Test | ||
public void set_bug_risk_for_suspicious_code_smells() throws Exception { | ||
assertThat(getCategoriesForRule("S1186")).contains(Categories.Category.BUG_RISK); | ||
} | ||
|
||
private Categories getCategoriesForRule(String key) { | ||
RuleDetails rule = Factory.createRule(key); | ||
return Categories.from(rule); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package support; | ||
|
||
import cc.analysis.SonarLintFactory; | ||
import org.sonarlint.cli.analysis.LogOutputWrapper; | ||
import org.sonarsource.sonarlint.core.StandaloneSonarLintEngineImpl; | ||
import org.sonarsource.sonarlint.core.client.api.common.RuleDetails; | ||
|
||
import java.nio.file.Paths; | ||
|
||
public class Factory { | ||
public static StandaloneSonarLintEngineImpl sonarlint() { | ||
SonarLintFactory factory = new SonarLintFactory(null, Paths.get("/tmp/sonarlint")); | ||
return factory.createEngine(new LogOutputWrapper(false)); | ||
} | ||
|
||
public static RuleDetails createRule(String key) { | ||
return sonarlint().getRuleDetails("squid:" + key); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -34,7 +34,7 @@ | |
} | ||
}, | ||
"categories": [ | ||
"Bug Risk" | ||
"Clarity" | ||
] | ||
}, | ||
{ | ||
|