Skip to content

Commit

Permalink
SONAR-6713 Load rules in global container
Browse files Browse the repository at this point in the history
  • Loading branch information
dbmeneses committed Jul 28, 2015
1 parent 7cb72d0 commit b36bc46
Show file tree
Hide file tree
Showing 50 changed files with 771 additions and 103 deletions.
Expand Up @@ -105,4 +105,5 @@ public List getExtensions() {
XooProjectBuilder.class, XooProjectBuilder.class,
XooPostJob.class); XooPostJob.class);
} }

} }
Expand Up @@ -76,7 +76,13 @@ public void define(Context context) {


repo.createRule(CustomMessageSensor.RULE_KEY).setName("Issue With Custom Message") repo.createRule(CustomMessageSensor.RULE_KEY).setName("Issue With Custom Message")
.setHtmlDescription("Generate an issue on each file with a custom message"); .setHtmlDescription("Generate an issue on each file with a custom message");

repo.createRule(RandomAccessSensor.RULE_KEY).setName("One Issue Per File with Random Access")
.setHtmlDescription("This issue is generated on each file");


repo.createRule(DeprecatedResourceApiSensor.RULE_KEY).setName("Issue created using deprecated API")
.setHtmlDescription("Issue created using deprecated API");

repo.done(); repo.done();


} }
Expand Down
Expand Up @@ -37,7 +37,7 @@ public void define_xoo_rules() {
assertThat(repo).isNotNull(); assertThat(repo).isNotNull();
assertThat(repo.name()).isEqualTo("Xoo"); assertThat(repo.name()).isEqualTo("Xoo");
assertThat(repo.language()).isEqualTo("xoo"); assertThat(repo.language()).isEqualTo("xoo");
assertThat(repo.rules()).hasSize(8); assertThat(repo.rules()).hasSize(10);


RulesDefinition.Rule rule = repo.rule(OneIssuePerLineSensor.RULE_KEY); RulesDefinition.Rule rule = repo.rule(OneIssuePerLineSensor.RULE_KEY);
assertThat(rule.name()).isNotEmpty(); assertThat(rule.name()).isNotEmpty();
Expand Down
@@ -0,0 +1,74 @@
/*
* 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.protocol.input;

import javax.annotation.CheckForNull;
import javax.annotation.Nullable;

public class Rule {
private final String key;
private final String repo;
private final String internalKey;
private final String name;
private final String severity;
private final String lang;

public Rule(String ruleKey, String repositoryKey, String internalKey, String name, @Nullable String severity, @Nullable String language) {
this.key = ruleKey;
this.repo = repositoryKey;
this.internalKey = internalKey;
this.name = name;
this.severity = severity;
this.lang = language;
}

public String ruleKey() {
return key;
}

public String repositoryKey() {
return repo;
}

public String internalKey() {
return internalKey;
}

public String name() {
return name;
}

/**
* Is null on manual rules
*/
@CheckForNull
public String severity() {
return severity;
}

/**
* Is null on manual rules
*/
@CheckForNull
public String language() {
return lang;
}

}
@@ -0,0 +1,45 @@
/*
* 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.protocol.input;

import java.util.List;

import org.sonar.batch.protocol.GsonHelper;

public class RulesSearchResult {
List<Rule> rules;

public List<Rule> getRules() {
return rules;
}

public void setRules(List<Rule> rules) {
this.rules = rules;
}

public String toJson() {
return GsonHelper.create().toJson(this);
}

public static RulesSearchResult fromJson(String json) {
return GsonHelper.create().fromJson(json, RulesSearchResult.class);
}

}
@@ -0,0 +1,56 @@
/*
* 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.protocol.input;

import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.net.URL;

import static org.assertj.core.api.Assertions.assertThat;

import org.sonar.test.JsonAssert;
import org.assertj.core.util.Lists;
import org.junit.Test;

public class RulesSearchResultTest {
@Test
public void testJsonParsing() {
Rule rule1 = new Rule("squid:S1194", "squid", "S1194", "\"java.lang.Error\" should not be extended", "MAJOR", "java");
Rule rule2 = new Rule("squid:ObjectFinalizeOverridenCallsSuperFinalizeCheck", "squid", "ObjectFinalizeOverridenCallsSuperFinalizeCheck",
"super.finalize() should be called at the end of Object.finalize() implementations", "BLOCKER", "java");

RulesSearchResult rules = new RulesSearchResult();
rules.setRules(Lists.newArrayList(rule1, rule2));

JsonAssert
.assertJson(getClass().getResource("RulesSearchTest/expected.json"))
.isSimilarTo(rules.toJson());
}

@Test
public void testJsonParsingEmpty() throws IOException {
URL resource = getClass().getResource("RulesSearchTest/empty.json");
String json = IOUtils.toString(resource);
RulesSearchResult result = RulesSearchResult.fromJson(json);

assertThat(result.getRules()).isEmpty();
}
}
@@ -0,0 +1 @@
{"total":3225,"p":30,"ps":500,"rules":[]}
@@ -0,0 +1 @@
{"total":290,"p":1,"ps":2,"rules":[{"key":"squid:S1194","internalKey":"S1194","repo":"squid","name":"\"java.lang.Error\" should not be extended","severity":"MAJOR","lang":"java"},{"key":"squid:ObjectFinalizeOverridenCallsSuperFinalizeCheck","internalKey":"ObjectFinalizeOverridenCallsSuperFinalizeCheck","repo":"squid","name":"super.finalize() should be called at the end of Object.finalize() implementations","severity":"BLOCKER","lang":"java"}]}
Expand Up @@ -42,8 +42,8 @@ private BatchComponents() {
// only static stuff // only static stuff
} }


public static Collection all(GlobalMode analysisMode) { public static Collection<Object> all(GlobalMode analysisMode) {
List components = Lists.newArrayList( List<Object> components = Lists.newArrayList(
DefaultResourceTypes.get(), DefaultResourceTypes.get(),
// SCM // SCM
ScmConfiguration.class, ScmConfiguration.class,
Expand Down
Expand Up @@ -19,6 +19,11 @@
*/ */
package org.sonar.batch.bootstrap; package org.sonar.batch.bootstrap;


import org.sonar.batch.rule.RulesLoader;

import org.sonar.batch.rule.DefaultRulesLoader;
import org.sonar.batch.rule.RulesProvider;

import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import org.sonar.api.CoreProperties; import org.sonar.api.CoreProperties;
Expand Down Expand Up @@ -81,6 +86,7 @@ private void addBootstrapComponents() {
CachesManager.class, CachesManager.class,
GlobalMode.class, GlobalMode.class,
GlobalSettings.class, GlobalSettings.class,
new RulesProvider(),
ServerClient.class, ServerClient.class,
Logback.class, Logback.class,
DefaultServer.class, DefaultServer.class,
Expand All @@ -94,6 +100,7 @@ private void addBootstrapComponents() {
new GlobalRepositoriesProvider(), new GlobalRepositoriesProvider(),
UserRepository.class); UserRepository.class);
addIfMissing(BatchPluginInstaller.class, PluginInstaller.class); addIfMissing(BatchPluginInstaller.class, PluginInstaller.class);
addIfMissing(DefaultRulesLoader.class, RulesLoader.class);
addIfMissing(DefaultGlobalRepositoriesLoader.class, GlobalRepositoriesLoader.class); addIfMissing(DefaultGlobalRepositoriesLoader.class, GlobalRepositoriesLoader.class);
addIfMissing(DefaultProjectRepositoriesLoader.class, ProjectRepositoriesLoader.class); addIfMissing(DefaultProjectRepositoriesLoader.class, ProjectRepositoriesLoader.class);
addIfMissing(DefaultServerIssuesLoader.class, ServerIssuesLoader.class); addIfMissing(DefaultServerIssuesLoader.class, ServerIssuesLoader.class);
Expand Down
Expand Up @@ -72,7 +72,7 @@ public String getURL() {
} }


public URI getURI(String pathStartingWithSlash) { public URI getURI(String pathStartingWithSlash) {
Preconditions.checkArgument(pathStartingWithSlash.startsWith("/"), "Path must start with slash /"); Preconditions.checkArgument(pathStartingWithSlash.startsWith("/"), "Path must start with slash /: " + pathStartingWithSlash);
String path = StringEscapeUtils.escapeHtml(pathStartingWithSlash); String path = StringEscapeUtils.escapeHtml(pathStartingWithSlash);
return URI.create(getURL() + path); return URI.create(getURL() + path);
} }
Expand Down
Expand Up @@ -20,15 +20,17 @@
package org.sonar.batch.cpd; package org.sonar.batch.cpd;


import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;

import java.util.List; import java.util.List;

import org.sonar.batch.cpd.index.IndexFactory; import org.sonar.batch.cpd.index.IndexFactory;


public final class CpdComponents { public final class CpdComponents {


private CpdComponents() { private CpdComponents() {
} }


public static List all() { public static List<? extends Object> all() {
return ImmutableList.of( return ImmutableList.of(
CpdSensor.class, CpdSensor.class,
CpdMappings.class, CpdMappings.class,
Expand Down
16 changes: 4 additions & 12 deletions sonar-batch/src/main/java/org/sonar/batch/issue/ModuleIssues.java
Expand Up @@ -25,7 +25,6 @@
import org.sonar.api.batch.rule.ActiveRules; import org.sonar.api.batch.rule.ActiveRules;
import org.sonar.api.batch.rule.Rule; import org.sonar.api.batch.rule.Rule;
import org.sonar.api.batch.rule.Rules; import org.sonar.api.batch.rule.Rules;
import org.sonar.api.batch.rule.internal.DefaultActiveRule;
import org.sonar.api.resources.Project; import org.sonar.api.resources.Project;
import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleKey;
import org.sonar.api.utils.MessageException; import org.sonar.api.utils.MessageException;
Expand All @@ -42,25 +41,18 @@ public class ModuleIssues {
private final Project project; private final Project project;
private final IssueFilters filters; private final IssueFilters filters;


public ModuleIssues(ActiveRules activeRules, @Nullable Rules rules, IssueCache cache, Project project, IssueFilters filters) { public ModuleIssues(ActiveRules activeRules, Rules rules, IssueCache cache, Project project, IssueFilters filters) {
this.activeRules = activeRules; this.activeRules = activeRules;
this.rules = rules; this.rules = rules;
this.cache = cache; this.cache = cache;
this.project = project; this.project = project;
this.filters = filters; this.filters = filters;
} }


public ModuleIssues(ActiveRules activeRules, IssueCache cache, Project project, IssueFilters filters) {
this(activeRules, null, cache, project, filters);
}

public boolean initAndAddIssue(DefaultIssue issue) { public boolean initAndAddIssue(DefaultIssue issue) {
RuleKey ruleKey = issue.ruleKey(); RuleKey ruleKey = issue.ruleKey();
Rule rule = null; Rule rule = rules.find(ruleKey);
if (rules != null) { validateRule(issue, rule);
rule = rules.find(ruleKey);
validateRule(issue, rule);
}
ActiveRule activeRule = activeRules.find(ruleKey); ActiveRule activeRule = activeRules.find(ruleKey);
if (activeRule == null) { if (activeRule == null) {
// rule does not exist or is not enabled -> ignore the issue // rule does not exist or is not enabled -> ignore the issue
Expand All @@ -86,7 +78,7 @@ private static void validateRule(DefaultIssue issue, @Nullable Rule rule) {


private void updateIssue(DefaultIssue issue, @Nullable Rule rule, ActiveRule activeRule) { private void updateIssue(DefaultIssue issue, @Nullable Rule rule, ActiveRule activeRule) {
if (Strings.isNullOrEmpty(issue.message())) { if (Strings.isNullOrEmpty(issue.message())) {
issue.setMessage(((DefaultActiveRule) activeRule).name()); issue.setMessage(rule.name());
} }
if (project != null) { if (project != null) {
issue.setCreationDate(project.getAnalysisDate()); issue.setCreationDate(project.getAnalysisDate());
Expand Down

0 comments on commit b36bc46

Please sign in to comment.