Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SONAR-6162 Fix NPE when searching for Check of custom rules
- Loading branch information
Showing
14 changed files
with
284 additions
and
11 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
32 changes: 32 additions & 0 deletions
32
plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/checks/Check.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,32 @@ | ||
/* | ||
* SonarQube, open source software quality management tool. | ||
* Copyright (C) 2008-2014 SonarSource | ||
* mailto:contact AT sonarsource DOT com | ||
* | ||
* SonarQube is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* SonarQube 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 | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.xoo.checks; | ||
|
||
import org.sonar.api.batch.fs.InputFile; | ||
import org.sonar.api.batch.sensor.SensorContext; | ||
import org.sonar.api.rule.RuleKey; | ||
|
||
public interface Check { | ||
|
||
public Class<Check>[] ALL = new Class[] {TemplateRuleCheck.class}; | ||
|
||
void execute(SensorContext context, InputFile file, RuleKey ruleKey); | ||
|
||
} |
46 changes: 46 additions & 0 deletions
46
plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/checks/TemplateRuleCheck.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,46 @@ | ||
/* | ||
* SonarQube, open source software quality management tool. | ||
* Copyright (C) 2008-2014 SonarSource | ||
* mailto:contact AT sonarsource DOT com | ||
* | ||
* SonarQube is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* SonarQube 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 | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.xoo.checks; | ||
|
||
import org.sonar.api.batch.fs.InputFile; | ||
import org.sonar.api.batch.sensor.SensorContext; | ||
import org.sonar.api.rule.RuleKey; | ||
import org.sonar.check.Cardinality; | ||
import org.sonar.check.Rule; | ||
import org.sonar.check.RuleProperty; | ||
|
||
@Rule(key = TemplateRuleCheck.RULE_KEY, cardinality = Cardinality.MULTIPLE, name = "Template rule", description = "Sample template rule") | ||
public class TemplateRuleCheck implements Check { | ||
|
||
public static final String RULE_KEY = "TemplateRule"; | ||
|
||
@RuleProperty(key = "line") | ||
private int line; | ||
|
||
@Override | ||
public void execute(SensorContext sensorContext, InputFile file, RuleKey ruleKey) { | ||
sensorContext.newIssue() | ||
.onFile(file) | ||
.ruleKey(ruleKey) | ||
.atLine(line) | ||
.save(); | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
plugins/sonar-xoo-plugin/src/main/java/org/sonar/xoo/rule/ChecksSensor.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,62 @@ | ||
/* | ||
* SonarQube, open source software quality management tool. | ||
* Copyright (C) 2008-2014 SonarSource | ||
* mailto:contact AT sonarsource DOT com | ||
* | ||
* SonarQube is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* SonarQube 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 | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.xoo.rule; | ||
|
||
import org.sonar.api.batch.fs.FilePredicates; | ||
import org.sonar.api.batch.fs.InputFile; | ||
import org.sonar.api.batch.fs.InputFile.Type; | ||
import org.sonar.api.batch.rule.CheckFactory; | ||
import org.sonar.api.batch.rule.Checks; | ||
import org.sonar.api.batch.sensor.Sensor; | ||
import org.sonar.api.batch.sensor.SensorContext; | ||
import org.sonar.api.batch.sensor.SensorDescriptor; | ||
import org.sonar.xoo.Xoo; | ||
import org.sonar.xoo.checks.Check; | ||
|
||
public class ChecksSensor implements Sensor { | ||
|
||
private final CheckFactory checkFactory; | ||
|
||
public ChecksSensor(CheckFactory checkFactory) { | ||
this.checkFactory = checkFactory; | ||
} | ||
|
||
@Override | ||
public void describe(SensorDescriptor descriptor) { | ||
descriptor | ||
.name("ChecksSensor") | ||
.workOnLanguages(Xoo.KEY) | ||
.createIssuesForRuleRepositories(XooRulesDefinition.XOO_REPOSITORY) | ||
.workOnFileTypes(InputFile.Type.MAIN); | ||
} | ||
|
||
@Override | ||
public void execute(SensorContext context) { | ||
Checks<Check> checks = checkFactory.create(XooRulesDefinition.XOO_REPOSITORY); | ||
checks.addAnnotatedChecks(Check.ALL); | ||
FilePredicates p = context.fileSystem().predicates(); | ||
for (InputFile file : context.fileSystem().inputFiles(p.and(p.hasLanguages(Xoo.KEY), p.hasType(Type.MAIN)))) { | ||
for (Check check : checks.all()) { | ||
check.execute(context, file, checks.ruleKey(check)); | ||
} | ||
} | ||
} | ||
|
||
} |
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
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
105 changes: 105 additions & 0 deletions
105
sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/ChecksMediumTest.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,105 @@ | ||
/* | ||
* SonarQube, open source software quality management tool. | ||
* Copyright (C) 2008-2014 SonarSource | ||
* mailto:contact AT sonarsource DOT com | ||
* | ||
* SonarQube is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* SonarQube 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 | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonar.batch.mediumtest.issues; | ||
|
||
import com.google.common.collect.ImmutableMap; | ||
import org.apache.commons.io.FileUtils; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.rules.TemporaryFolder; | ||
import org.sonar.api.batch.fs.internal.DefaultInputFile; | ||
import org.sonar.api.batch.sensor.issue.Issue; | ||
import org.sonar.batch.mediumtest.BatchMediumTester; | ||
import org.sonar.batch.mediumtest.BatchMediumTester.TaskResult; | ||
import org.sonar.batch.protocol.input.ActiveRule; | ||
import org.sonar.xoo.XooPlugin; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
import static org.fest.assertions.Assertions.assertThat; | ||
|
||
public class ChecksMediumTest { | ||
|
||
@org.junit.Rule | ||
public TemporaryFolder temp = new TemporaryFolder(); | ||
|
||
public BatchMediumTester tester = BatchMediumTester.builder() | ||
.registerPlugin("xoo", new XooPlugin()) | ||
.addDefaultQProfile("xoo", "Sonar Way") | ||
.activateRule(new ActiveRule("xoo", "TemplateRule_1234", "TemplateRule", "A template rule", "MAJOR", null, "xoo").addParam("line", "1")) | ||
.activateRule(new ActiveRule("xoo", "TemplateRule_1235", "TemplateRule", "Another template rule", "MAJOR", null, "xoo").addParam("line", "2")) | ||
.bootstrapProperties(ImmutableMap.of("sonar.analysis.mode", "sensor")) | ||
.build(); | ||
|
||
@Before | ||
public void prepare() { | ||
tester.start(); | ||
} | ||
|
||
@After | ||
public void stop() { | ||
tester.stop(); | ||
} | ||
|
||
@Test | ||
public void testCheckWithTemplate() throws IOException { | ||
|
||
File baseDir = temp.newFolder(); | ||
File srcDir = new File(baseDir, "src"); | ||
srcDir.mkdir(); | ||
|
||
File xooFile = new File(srcDir, "sample.xoo"); | ||
FileUtils.write(xooFile, "foo"); | ||
|
||
TaskResult result = tester.newTask() | ||
.properties(ImmutableMap.<String, String>builder() | ||
.put("sonar.task", "scan") | ||
.put("sonar.projectBaseDir", baseDir.getAbsolutePath()) | ||
.put("sonar.projectKey", "com.foo.project") | ||
.put("sonar.projectName", "Foo Project") | ||
.put("sonar.projectVersion", "1.0-SNAPSHOT") | ||
.put("sonar.projectDescription", "Description of Foo Project") | ||
.put("sonar.sources", "src") | ||
.build()) | ||
.start(); | ||
|
||
assertThat(result.issues()).hasSize(2); | ||
|
||
boolean foundIssueAtLine1 = false; | ||
boolean foundIssueAtLine2 = false; | ||
for (Issue issue : result.issues()) { | ||
if (issue.line() == 1) { | ||
foundIssueAtLine1 = true; | ||
assertThat(issue.inputPath()).isEqualTo(new DefaultInputFile("com.foo.project", "src/sample.xoo")); | ||
assertThat(issue.message()).isEqualTo("A template rule"); | ||
} | ||
if (issue.line() == 2) { | ||
foundIssueAtLine2 = true; | ||
assertThat(issue.inputPath()).isEqualTo(new DefaultInputFile("com.foo.project", "src/sample.xoo")); | ||
assertThat(issue.message()).isEqualTo("Another template rule"); | ||
} | ||
} | ||
assertThat(foundIssueAtLine1).isTrue(); | ||
assertThat(foundIssueAtLine2).isTrue(); | ||
} | ||
|
||
} |
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
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
Oops, something went wrong.