diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-07-27 16:25:31 +0200 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2015-07-28 14:54:10 +0200 |
commit | b36bc464f98e0136d41e60774093aa946b207999 (patch) | |
tree | fabae7651b0705f3fd74144399e57350bc064565 /sonar-batch/src/test/java | |
parent | 7cb72d02dc84f620447d80b96cd518d8b7f54e9a (diff) | |
download | sonarqube-b36bc464f98e0136d41e60774093aa946b207999.tar.gz sonarqube-b36bc464f98e0136d41e60774093aa946b207999.zip |
SONAR-6713 Load rules in global container
Diffstat (limited to 'sonar-batch/src/test/java')
16 files changed, 254 insertions, 27 deletions
diff --git a/sonar-batch/src/test/java/org/sonar/batch/issue/ModuleIssuesTest.java b/sonar-batch/src/test/java/org/sonar/batch/issue/ModuleIssuesTest.java index 3199c7c8f03..a602f1f6549 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/issue/ModuleIssuesTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/issue/ModuleIssuesTest.java @@ -86,7 +86,7 @@ public class ModuleIssuesTest { @Test public void fail_if_rule_has_no_name_and_issue_has_no_message() { - ruleBuilder.add(RuleKey.of("squid", "AvoidCycle")); + ruleBuilder.add(SQUID_RULE_KEY).setInternalKey(SQUID_RULE_KEY.rule()); initModuleIssues(); DefaultIssue issue = new DefaultIssue().setRuleKey(SQUID_RULE_KEY).setMessage(""); diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/deprecated/DeprecatedApiMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/deprecated/DeprecatedApiMediumTest.java index 8287a310bfb..f73d1c60f46 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/deprecated/DeprecatedApiMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/deprecated/DeprecatedApiMediumTest.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.mediumtest.deprecated; +import org.sonar.xoo.rule.XooRulesDefinition; + import com.google.common.collect.ImmutableMap; import java.io.File; import java.io.IOException; @@ -42,6 +44,7 @@ public class DeprecatedApiMediumTest { public BatchMediumTester tester = BatchMediumTester.builder() .registerPlugin("xoo", new XooPlugin()) + .addRules(new XooRulesDefinition()) .addDefaultQProfile("xoo", "Sonar Way") .activateRule(new ActiveRule("xoo", "DeprecatedResourceApi", null, "One issue per line", "MAJOR", null, "xoo")) .build(); @@ -82,9 +85,9 @@ public class DeprecatedApiMediumTest { .start(); assertThat(result.issues()).extracting("componentKey", "message", "line").containsOnly( - tuple("com.foo.project:src/sample.xoo", "One issue per line", null), + tuple("com.foo.project:src/sample.xoo", "Issue created using deprecated API", null), tuple("com.foo.project:src/sample.xoo", "Issue created using deprecated API", 1), - tuple("com.foo.project:src/package/sample.xoo", "One issue per line", null), + tuple("com.foo.project:src/package/sample.xoo", "Issue created using deprecated API", null), tuple("com.foo.project:src/package/sample.xoo", "Issue created using deprecated API", 1), tuple("com.foo.project:src", "Issue created using deprecated API", null), tuple("com.foo.project:src/package", "Issue created using deprecated API", null)); diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/ProjectBuilderMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/ProjectBuilderMediumTest.java index 05c1efa99de..2beb8f133b5 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/ProjectBuilderMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/ProjectBuilderMediumTest.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.mediumtest.fs; +import org.sonar.xoo.rule.XooRulesDefinition; + import com.google.common.collect.ImmutableMap; import org.apache.commons.io.FileUtils; import org.junit.After; @@ -44,6 +46,7 @@ public class ProjectBuilderMediumTest { public BatchMediumTester tester = BatchMediumTester.builder() .registerPlugin("xoo", new XooPlugin()) + .addRules(new XooRulesDefinition()) .addDefaultQProfile("xoo", "Sonar Way") .bootstrapProperties(ImmutableMap.of(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_PREVIEW)) .setPreviousAnalysisDate(new Date()) diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/RandomFsAccessMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/RandomFsAccessMediumTest.java index 4e75ffc5474..f27f72c5769 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/RandomFsAccessMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/RandomFsAccessMediumTest.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.mediumtest.fs; +import org.sonar.xoo.rule.XooRulesDefinition; + import com.google.common.collect.ImmutableMap; import org.apache.commons.io.FileUtils; import org.junit.After; @@ -49,6 +51,7 @@ public class RandomFsAccessMediumTest { public BatchMediumTester tester = BatchMediumTester.builder() .registerPlugin("xoo", new XooPlugin()) .addDefaultQProfile("xoo", "Sonar Way") + .addRules(new XooRulesDefinition()) .activateRule(new ActiveRule("xoo", "RandomAccessIssue", null, "One issue per line", "MAJOR", null, "xoo")) .build(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/ChecksMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/ChecksMediumTest.java index 5cc57070ce5..7d39819f825 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/ChecksMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/ChecksMediumTest.java @@ -19,6 +19,9 @@ */ package org.sonar.batch.mediumtest.issues; +import org.sonar.batch.protocol.input.Rule; + +import org.sonar.xoo.rule.XooRulesDefinition; import com.google.common.collect.ImmutableMap; import org.apache.commons.io.FileUtils; import org.junit.After; @@ -42,7 +45,10 @@ public class ChecksMediumTest { public BatchMediumTester tester = BatchMediumTester.builder() .registerPlugin("xoo", new XooPlugin()) + .addRules(new XooRulesDefinition()) .addDefaultQProfile("xoo", "Sonar Way") + .addRule(new Rule("xoo:TemplateRule_1234", "xoo", "TemplateRule_1234", "A template rule", "MAJOR", "xoo")) + .addRule(new Rule("xoo:TemplateRule_1235", "xoo", "TemplateRule_1235", "Another template rule", "MAJOR", "xoo")) .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")) .build(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesMediumTest.java index 8f46a5d0800..6898c5bfcc9 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesMediumTest.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.mediumtest.issues; +import org.sonar.xoo.rule.XooRulesDefinition; + import com.google.common.collect.ImmutableMap; import org.apache.commons.io.FileUtils; import org.junit.After; @@ -44,6 +46,7 @@ public class IssuesMediumTest { public BatchMediumTester tester = BatchMediumTester.builder() .registerPlugin("xoo", new XooPlugin()) .addDefaultQProfile("xoo", "Sonar Way") + .addRules(new XooRulesDefinition()) .activateRule(new ActiveRule("xoo", "OneIssuePerLine", null, "One issue per line", "MAJOR", "OneIssuePerLine.internal", "xoo")) .build(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesOnDirMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesOnDirMediumTest.java index 10aef4cb60d..2d389a5d8cf 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesOnDirMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesOnDirMediumTest.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.mediumtest.issues; +import org.sonar.xoo.rule.XooRulesDefinition; + import com.google.common.collect.ImmutableMap; import org.apache.commons.io.FileUtils; import org.junit.After; @@ -43,6 +45,7 @@ public class IssuesOnDirMediumTest { public BatchMediumTester tester = BatchMediumTester.builder() .registerPlugin("xoo", new XooPlugin()) .addDefaultQProfile("xoo", "Sonar Way") + .addRules(new XooRulesDefinition()) .activateRule(new ActiveRule("xoo", "OneIssueOnDirPerFile", null, "One issue per line", "MINOR", "xoo", "xoo")) .build(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/MultilineIssuesMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/MultilineIssuesMediumTest.java index 749260c4233..c07dd6cdbc6 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/MultilineIssuesMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/MultilineIssuesMediumTest.java @@ -19,7 +19,12 @@ */ package org.sonar.batch.mediumtest.issues; +import org.sonar.xoo.rule.XooRulesDefinition; + +import org.sonar.batch.protocol.input.Rule; + import java.io.File; + import org.apache.commons.io.FileUtils; import org.junit.After; import org.junit.Before; @@ -29,7 +34,6 @@ import org.sonar.batch.mediumtest.BatchMediumTester; import org.sonar.batch.mediumtest.TaskResult; import org.sonar.batch.protocol.input.ActiveRule; import org.sonar.xoo.XooPlugin; - import static org.assertj.core.api.Assertions.assertThat; public class MultilineIssuesMediumTest { @@ -39,6 +43,8 @@ public class MultilineIssuesMediumTest { public BatchMediumTester tester = BatchMediumTester.builder() .registerPlugin("xoo", new XooPlugin()) + .addRules(new XooRulesDefinition()) + .addRule(new Rule("xoo:MultilineIssue", "xoo", null, "Multinile Issue", "MAJOR", "xoo")) .addDefaultQProfile("xoo", "Sonar Way") .activateRule(new ActiveRule("xoo", "MultilineIssue", null, "Multinile Issue", "MAJOR", null, "xoo")) .build(); diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/preview/EmptyFileTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/preview/EmptyFileTest.java index 385023ec1e3..3f7be697caf 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/preview/EmptyFileTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/preview/EmptyFileTest.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.mediumtest.preview; +import org.sonar.xoo.rule.XooRulesDefinition; + import com.google.common.collect.ImmutableMap; import org.junit.After; import org.junit.Before; @@ -48,6 +50,7 @@ public class EmptyFileTest { public BatchMediumTester tester = BatchMediumTester.builder() .bootstrapProperties(ImmutableMap.of(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_PREVIEW)) .registerPlugin("xoo", new XooPlugin()) + .addRules(new XooRulesDefinition()) .addDefaultQProfile("xoo", "Sonar Way") .activateRule(new ActiveRule("xoo", "OneIssuePerLine", null, "One issue per line", "MAJOR", "my/internal/key", "xoo")) .setPreviousAnalysisDate(new Date()) diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/preview/IncrementalModeMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/preview/IncrementalModeMediumTest.java index 1f1539388f3..86697dcea42 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/preview/IncrementalModeMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/preview/IncrementalModeMediumTest.java @@ -19,6 +19,8 @@ */ package org.sonar.batch.mediumtest.preview; +import org.sonar.xoo.rule.XooRulesDefinition; + import com.google.common.collect.ImmutableMap; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.FileUtils; @@ -63,6 +65,7 @@ public class IncrementalModeMediumTest { .builder() .bootstrapProperties(ImmutableMap.of(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_INCREMENTAL)) .registerPlugin("xoo", new XooPlugin()) + .addRules(new XooRulesDefinition()) .addDefaultQProfile("xoo", "Sonar Way") .activateRule(new ActiveRule("xoo", "OneIssuePerLine", null, "One issue per line", "MAJOR", null, "xoo")) .activateRule(new ActiveRule("manual", "MyManualIssue", null, "My manual issue", "MAJOR", null, null)) diff --git a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/preview/PreviewAndReportsMediumTest.java b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/preview/PreviewAndReportsMediumTest.java index ab2157d056d..acf1f5b588b 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/mediumtest/preview/PreviewAndReportsMediumTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/mediumtest/preview/PreviewAndReportsMediumTest.java @@ -19,12 +19,14 @@ */ package org.sonar.batch.mediumtest.preview; +import org.sonar.batch.protocol.input.Rule; + +import org.sonar.xoo.rule.XooRulesDefinition; import com.google.common.collect.ImmutableMap; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.FileUtils; import org.junit.After; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; import org.junit.rules.TemporaryFolder; import org.sonar.api.CoreProperties; @@ -46,10 +48,10 @@ import static org.assertj.core.api.Assertions.assertThat; public class PreviewAndReportsMediumTest { - @Rule + @org.junit.Rule public TemporaryFolder temp = new TemporaryFolder(); - @Rule + @org.junit.Rule public LogTester logTester = new LogTester(); private static SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy"); @@ -66,6 +68,8 @@ public class PreviewAndReportsMediumTest { .bootstrapProperties(ImmutableMap.of(CoreProperties.ANALYSIS_MODE, CoreProperties.ANALYSIS_MODE_PREVIEW)) .registerPlugin("xoo", new XooPlugin()) .addDefaultQProfile("xoo", "Sonar Way") + .addRules(new XooRulesDefinition()) + .addRule(new Rule("manual:MyManualIssue", "manual", "MyManualIssue", "My manual issue", "MAJOR", null)) .activateRule(new ActiveRule("xoo", "OneIssuePerLine", null, "One issue per line", "MAJOR", null, "xoo")) .activateRule(new ActiveRule("manual", "MyManualIssue", null, "My manual issue", "MAJOR", null, null)) .setPreviousAnalysisDate(new Date()) diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/DefaultRulesLoaderTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/DefaultRulesLoaderTest.java new file mode 100644 index 00000000000..49ecaa0bcc9 --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/rule/DefaultRulesLoaderTest.java @@ -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.batch.rule; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.io.IOException; +import java.util.Arrays; +import java.util.LinkedList; + +import static org.mockito.Matchers.contains; +import static org.assertj.core.api.Assertions.assertThat; +import org.sonar.batch.bootstrap.WSLoader; +import org.junit.Test; +import org.sonar.batch.protocol.input.Rule; +import org.sonar.batch.protocol.input.RulesSearchResult; + +public class DefaultRulesLoaderTest { + @Test + public void testLoadingJson() throws IOException { + 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"); + + // generate json + RulesSearchResult rulesSearch = new RulesSearchResult(); + rulesSearch.setRules(Arrays.asList(rule1, rule2)); + String json = rulesSearch.toJson(); + + RulesSearchResult empty = new RulesSearchResult(); + empty.setRules(new LinkedList<Rule>()); + String emptyJson = empty.toJson(); + + WSLoader wsLoader = mock(WSLoader.class); + when(wsLoader.loadString(contains("p=1"))).thenReturn(json); + when(wsLoader.loadString(contains("p=2"))).thenReturn(emptyJson); + + // load + RulesLoader loader = new DefaultRulesLoader(wsLoader); + RulesSearchResult rules = loader.load(); + + assertThat(rules.toJson()).isEqualTo(json); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/RuleFinderCompatibilityTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/RuleFinderCompatibilityTest.java index 1c61c091b68..268de8b4e0f 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/rule/RuleFinderCompatibilityTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/rule/RuleFinderCompatibilityTest.java @@ -19,15 +19,15 @@ */ package org.sonar.batch.rule; +import org.sonar.api.batch.rule.internal.RulesBuilder; + +import org.sonar.api.batch.rule.Rules; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; -import org.sonar.api.batch.rule.ActiveRules; -import org.sonar.api.batch.rule.internal.ActiveRulesBuilder; import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.RuleQuery; - import static org.assertj.core.api.Assertions.assertThat; public class RuleFinderCompatibilityTest { @@ -35,20 +35,18 @@ public class RuleFinderCompatibilityTest { @Rule public ExpectedException thrown = ExpectedException.none(); - private ActiveRules activeRules = new ActiveRulesBuilder() - .create(RuleKey.of("repo1", "rule1")) - .activate() - .create(RuleKey.of("repo1", "rule2")) - .setInternalKey("rule2_internal") - .activate() - .create(RuleKey.of("repo2", "rule1")) - .activate() - .build(); + private Rules rules; private RuleFinderCompatibility ruleFinder; @Before public void prepare() { - ruleFinder = new RuleFinderCompatibility(activeRules); + RulesBuilder builder = new RulesBuilder(); + builder.add(RuleKey.of("repo1", "rule1")); + builder.add(RuleKey.of("repo1", "rule2")).setInternalKey("rule2_internal"); + builder.add(RuleKey.of("repo2", "rule1")); + rules = builder.build(); + + ruleFinder = new RuleFinderCompatibility(rules); } @Test diff --git a/sonar-batch/src/test/java/org/sonar/batch/rule/RulesProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/rule/RulesProviderTest.java new file mode 100644 index 00000000000..84e36977b7c --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/rule/RulesProviderTest.java @@ -0,0 +1,61 @@ +/* + * 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.rule; + +import org.assertj.core.api.Condition; + +import org.sonar.api.batch.rule.Rules; + +import java.util.Arrays; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.when; +import static org.mockito.Mockito.mock; +import org.sonar.batch.protocol.input.Rule; +import org.sonar.batch.protocol.input.RulesSearchResult; +import org.junit.Test; + +public class RulesProviderTest { + @Test + public void testRuleTranslation() { + final Rule testRule = new Rule("repo1:key1", "repo1", "key1", "name", "severity", "language"); + + RulesSearchResult loadResult = new RulesSearchResult(); + loadResult.setRules(Arrays.asList(testRule)); + RulesLoader loader = mock(RulesLoader.class); + when(loader.load()).thenReturn(loadResult); + + RulesProvider provider = new RulesProvider(); + Rules rules = provider.provide(loader); + + assertThat(rules.findAll()).hasSize(1); + assertThat(rules.findAll()).are(new Condition<org.sonar.api.batch.rule.Rule>() { + + @Override + public boolean matches(org.sonar.api.batch.rule.Rule value) { + return value.key().rule().equals(testRule.internalKey()) && + value.internalKey().equals(testRule.internalKey()) && + value.name().equals(testRule.name()) && + value.severity().equals(testRule.severity()) && + value.key().repository().equals(testRule.repositoryKey()); + } + }); + } +} diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/report/JSONReportTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/report/JSONReportTest.java index afc0d4c39aa..544b1c065b3 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/scan/report/JSONReportTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/report/JSONReportTest.java @@ -19,6 +19,9 @@ */ package org.sonar.batch.scan.report; +import org.sonar.api.batch.rule.internal.RulesBuilder; + +import org.sonar.api.batch.rule.Rules; import com.google.common.collect.Lists; import com.google.common.io.Resources; import org.junit.Before; @@ -29,8 +32,6 @@ import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.internal.DefaultFileSystem; import org.sonar.api.batch.fs.internal.DefaultInputDir; import org.sonar.api.batch.fs.internal.DefaultInputFile; -import org.sonar.api.batch.rule.ActiveRules; -import org.sonar.api.batch.rule.internal.ActiveRulesBuilder; import org.sonar.api.config.Settings; import org.sonar.api.issue.Issue; import org.sonar.core.issue.DefaultIssue; @@ -68,7 +69,7 @@ public class JSONReportTest { Resource resource = mock(Resource.class); DefaultFileSystem fs; Server server = mock(Server.class); - ActiveRules activeRules = mock(ActiveRules.class); + Rules rules = mock(Rules.class); Settings settings = new Settings(); IssueCache issueCache = mock(IssueCache.class); private UserRepository userRepository; @@ -91,10 +92,11 @@ public class JSONReportTest { moduleA.setParent(rootModule).setPath("core"); Project moduleB = new Project("struts-ui"); moduleB.setParent(rootModule).setPath("ui"); - activeRules = new ActiveRulesBuilder() - .create(RuleKey.of("squid", "AvoidCycles")).setName("Avoid Cycles").activate() - .build(); - jsonReport = new JSONReport(settings, fs, server, activeRules, issueCache, rootModule, fileCache, userRepository); + + RulesBuilder builder = new RulesBuilder(); + builder.add(RuleKey.of("squid", "AvoidCycles")).setName("Avoid Cycles"); + rules = builder.build(); + jsonReport = new JSONReport(settings, fs, server, rules, issueCache, rootModule, fileCache, userRepository); } @Test diff --git a/sonar-batch/src/test/java/org/sonar/batch/scan/report/RuleNameProviderTest.java b/sonar-batch/src/test/java/org/sonar/batch/scan/report/RuleNameProviderTest.java new file mode 100644 index 00000000000..fb7e399068f --- /dev/null +++ b/sonar-batch/src/test/java/org/sonar/batch/scan/report/RuleNameProviderTest.java @@ -0,0 +1,67 @@ +/* + * 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.scan.report; + +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; +import static org.assertj.core.api.Assertions.assertThat; + +import static org.mockito.Matchers.any; + +import org.sonar.api.rule.RuleKey; +import org.sonar.api.batch.rule.Rule; +import org.junit.Test; +import org.junit.Before; +import org.sonar.api.batch.rule.Rules; + +public class RuleNameProviderTest { + RuleNameProvider provider; + Rules rules; + Rule rule; + RuleKey ruleKey; + + @Before + public void setUp() { + ruleKey = mock(RuleKey.class); + rule = mock(Rule.class); + rules = mock(Rules.class); + provider = new RuleNameProvider(rules); + + when(ruleKey.rule()).thenReturn("ruleKey"); + when(ruleKey.repository()).thenReturn("repoKey"); + + when(rule.name()).thenReturn("name"); + when(rule.key()).thenReturn(ruleKey); + + when(rules.find(any(RuleKey.class))).thenReturn(rule); + } + + @Test + public void testNameForHTML() { + assertThat(provider.nameForHTML(rule)).isEqualTo(rule.name()); + assertThat(provider.nameForHTML(ruleKey)).isEqualTo(rule.name()); + } + + @Test + public void testNameForJS() { + assertThat(provider.nameForJS("repoKey:ruleKey")).isEqualTo(rule.name()); + } + +} |