From: Gustavo Martin Morcuende Date: Mon, 22 Aug 2016 21:01:02 +0000 (+0200) Subject: SonarQube custom JavaScript plugin, unit tests X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=0259040bb5f566567fbd853ed535c843ad869d12;p=JavaForFun SonarQube custom JavaScript plugin, unit tests --- diff --git a/Sonar/Plugins/sonar-custom-javascript-plugin/pom.xml b/Sonar/Plugins/sonar-custom-javascript-plugin/pom.xml index 9f3b843..7cc18bc 100644 --- a/Sonar/Plugins/sonar-custom-javascript-plugin/pom.xml +++ b/Sonar/Plugins/sonar-custom-javascript-plugin/pom.xml @@ -19,6 +19,14 @@ 5.6.1 2.15 1.8 + 2.10.4 + 3.0.1 + + ${project.basedir}/target/jacoco-it.exec + ${project.basedir}/target/jacoco.exec + + false + true customjavascriptplugin @@ -107,6 +115,24 @@ + + + + + org.apache.maven.plugins + maven-site-plugin + 3.5.1 + + + org.apache.maven.wagon + wagon-ssh + 2.10 + + + + + + org.sonarsource.sonar-packaging-maven-plugin @@ -127,7 +153,235 @@ ${project.build.sourceEncoding} + + org.apache.maven.plugins + maven-resources-plugin + 2.7 + + ${project.build.sourceEncoding} + + + + maven-site-plugin + + + attach-descriptor + + attach-descriptor + + + + + + org.apache.maven.plugins + maven-jar-plugin + 2.6 + + + + ${project.description} + ${project.version} + ${project.organization.name} + ${project.description} + ${project.version} + ${project.organization.name} + + + + + + org.apache.maven.plugins + maven-javadoc-plugin + ${maven.javadoc.version} + + true + ${project.build.sourceEncoding} + ${project.build.sourceEncoding} + ${project.build.sourceEncoding} + -Xdoclint:none + + + + org.apache.maven.plugins + maven-source-plugin + ${maven.source.version} + + + attach-sources + package + + jar-no-fork + + + + + + org.jacoco + jacoco-maven-plugin + 0.7.7.201606060606 + + + + pre-unit-test + + prepare-agent + + + + ${jacoco.ut.execution.data.file} + + surefireArgLine + + + + + post-unit-test + test + + report + + + + ${jacoco.ut.execution.data.file} + + ${project.reporting.outputDirectory}/jacoco-ut + + + + + pre-integration-test + pre-integration-test + + prepare-agent + + + + ${jacoco.it.execution.data.file} + + failsafeArgLine + + + + + post-integration-test + post-integration-test + + report + + + + ${jacoco.it.execution.data.file} + + ${project.reporting.outputDirectory}/jacoco-it + + + + + + org.eclipse.m2e + lifecycle-mapping + 1.0.0 + + + + + + org.jacoco + jacoco-maven-plugin + [0.5,) + + + prepare-agent + + + + + + + + + + + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.19.1 + + + org.apache.maven.surefire + surefire-junit47 + 2.19.1 + + + + + -Dfile.encoding=${project.build.sourceEncoding} + ${surefireArgLine} + + ${skip.unit.tests} + + + **/*IT.java + **/*IntegrationTest.java + + + + + + + + org.apache.maven.plugins + maven-project-info-reports-plugin + 2.9 + + + org.apache.maven.plugins + maven-javadoc-plugin + ${maven.javadoc.version} + + + html + + + API for ${project.name} ${project.version} + API for ${project.name} ${project.version} + + + javadoc + aggregate + + + + + + org.apache.maven.plugins + maven-surefire-report-plugin + 2.19.1 + + + + report-only + + + + + + org.apache.maven.plugins + maven-jxr-plugin + 2.5 + + + diff --git a/Sonar/Plugins/sonar-custom-javascript-plugin/src/test/java/de/example/custom/javascript/checks/AngularJSRootOnEventSubscriptionCheckTest.java b/Sonar/Plugins/sonar-custom-javascript-plugin/src/test/java/de/example/custom/javascript/checks/AngularJSRootOnEventSubscriptionCheckTest.java index 40994ec..ddada54 100644 --- a/Sonar/Plugins/sonar-custom-javascript-plugin/src/test/java/de/example/custom/javascript/checks/AngularJSRootOnEventSubscriptionCheckTest.java +++ b/Sonar/Plugins/sonar-custom-javascript-plugin/src/test/java/de/example/custom/javascript/checks/AngularJSRootOnEventSubscriptionCheckTest.java @@ -10,7 +10,7 @@ public class AngularJSRootOnEventSubscriptionCheckTest { private AngularJSRootOnEventSubscriptionCheck check = new AngularJSRootOnEventSubscriptionCheck(); @Test - public void testDefault() { + public void whenUsingRootScopeOnEventThenGenerateIssues() { JavaScriptCheckVerifier.verify(check, new File("src/test/files/checks/AngularJSRootOnEventSubscriptionCheck.js")); } } diff --git a/Sonar/Plugins/sonar-custom-javascript-plugin/src/test/java/de/example/custom/javascript/checks/CheckListTest.java b/Sonar/Plugins/sonar-custom-javascript-plugin/src/test/java/de/example/custom/javascript/checks/CheckListTest.java new file mode 100644 index 0000000..86f1820 --- /dev/null +++ b/Sonar/Plugins/sonar-custom-javascript-plugin/src/test/java/de/example/custom/javascript/checks/CheckListTest.java @@ -0,0 +1,29 @@ +package de.example.custom.javascript.checks; + +import static org.junit.Assert.*; +import static org.hamcrest.CoreMatchers.*; + +import java.io.File; +import java.util.Collection; + +import org.apache.commons.io.FileUtils; +import org.junit.Test; + +public class CheckListTest { + + @Test + public void whenRetrievingJavaScriptChecksThenGetRightNumberOfClasses() { + int count = 0; + Collection files = FileUtils.listFiles( + new File("src/main/java/de/example/custom/javascript/checks/"), + new String[] {"java"}, + true); + for (File file : files) { + if (file.getName().endsWith("Check.java")) { + count++; + } + } + assertThat(CheckList.getChecks().size(), is(count)); + } + +} diff --git a/Sonar/Plugins/sonar-custom-javascript-plugin/src/test/java/de/example/plugins/custom/javascript/CustomPluginTest.java b/Sonar/Plugins/sonar-custom-javascript-plugin/src/test/java/de/example/plugins/custom/javascript/CustomPluginTest.java new file mode 100644 index 0000000..5d0912c --- /dev/null +++ b/Sonar/Plugins/sonar-custom-javascript-plugin/src/test/java/de/example/plugins/custom/javascript/CustomPluginTest.java @@ -0,0 +1,22 @@ +package de.example.plugins.custom.javascript; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import org.junit.Test; +import org.sonar.api.Plugin; +import org.sonar.api.SonarQubeVersion; + +public class CustomPluginTest { + + @Test + public void whenCreatingCustomJavaScriptPluginThenRetrieveRightNumberOfExtensions() { + CustomPlugin javaPlugin = new CustomPlugin(); + Plugin.Context context = new Plugin.Context(SonarQubeVersion.V5_6); + + javaPlugin.define(context); + + assertThat(context.getExtensions().size(), is(1)); + } + +} diff --git a/Sonar/Plugins/sonar-custom-javascript-plugin/src/test/java/de/example/plugins/custom/javascript/CustomRulesDefinitionTest.java b/Sonar/Plugins/sonar-custom-javascript-plugin/src/test/java/de/example/plugins/custom/javascript/CustomRulesDefinitionTest.java new file mode 100644 index 0000000..dd845ab --- /dev/null +++ b/Sonar/Plugins/sonar-custom-javascript-plugin/src/test/java/de/example/plugins/custom/javascript/CustomRulesDefinitionTest.java @@ -0,0 +1,159 @@ +package de.example.plugins.custom.javascript; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.assertThat; + +import java.util.List; + +import org.junit.Test; +import org.sonar.api.server.rule.RulesDefinition; +import org.sonar.check.Rule; +import org.sonar.plugins.javascript.JavaScriptLanguage; +import org.sonar.plugins.javascript.api.JavaScriptCheck; +import org.sonar.plugins.javascript.api.tree.Tree; +import org.sonar.plugins.javascript.api.visitors.Issue; +import org.sonar.plugins.javascript.api.visitors.LineIssue; +import org.sonar.plugins.javascript.api.visitors.PreciseIssue; +import org.sonar.plugins.javascript.api.visitors.TreeVisitorContext; + +import de.example.custom.javascript.checks.CheckList; + +public class CustomRulesDefinitionTest { + + @Test + public void whenCreatingCustomJavaRulesDefinitionThenGenerateRulesDefinition() { + RulesDefinition.Context context = new RulesDefinition.Context(); + CustomRulesDefinition rulesDefinition = new CustomRulesDefinition(); + + rulesDefinition.define(context); + + RulesDefinition.Repository repository = context.repository(CheckList.REPOSITORY_KEY); + assertThat(repository.name(), is(CheckList.REPOSITORY_NAME)); + assertThat(repository.language(), is(JavaScriptLanguage.KEY)); + assertThat(repository.rules().size(), is(CheckList.getChecks().size())); + } + + @Test(expected=IllegalArgumentException.class) + public void shouldThrowIllegalArgumentExceptionWhenNoRuleAnnotationIsFound() { + RulesDefinition.Context context = new RulesDefinition.Context(); + RulesDefinition.NewRepository newRepository = context.createRepository(CheckList.REPOSITORY_KEY, CheckList.REPOSITORY_NAME); + CustomRulesDefinition rulesDefinition = new CustomRulesDefinition(); + + rulesDefinition.newRule(CheckWithNoAnnotation.class, newRepository); + } + + @Test(expected=IllegalArgumentException.class) + public void shouldThrowIllegalArgumentExceptionWhenNoKeyIsFoundInRuleAnnotation() { + RulesDefinition.Context context = new RulesDefinition.Context(); + RulesDefinition.NewRepository newRepository = context.createRepository(CheckList.REPOSITORY_KEY, CheckList.REPOSITORY_NAME); + CustomRulesDefinition rulesDefinition = new CustomRulesDefinition(); + + rulesDefinition.newRule(EmptyRuleKey.class, newRepository); + } + + @Test(expected=IllegalStateException.class) + public void shouldThrowIllegalStateExceptionWhenNoKeyIsFoundInRuleAnnotation() { + RulesDefinition.Context context = new RulesDefinition.Context(); + RulesDefinition.NewRepository newRepository = context.createRepository(CheckList.REPOSITORY_KEY, CheckList.REPOSITORY_NAME); + newRepository.createRule("myCardinality"); + newRepository.createRule("correctRule"); + CustomRulesDefinition rulesDefinition = new CustomRulesDefinition(); + + rulesDefinition.newRule(UnregisteredRule.class, newRepository); + } + + private class CheckWithNoAnnotation implements JavaScriptCheck { + + @Override + public T addIssue(T arg0) { + return null; + } + + @Override + public PreciseIssue addIssue(Tree arg0, String arg1) { + return null; + } + + @Override + public LineIssue addLineIssue(Tree arg0, String arg1) { + return null; + } + + @Override + public List scanFile(TreeVisitorContext arg0) { + return null; + } + } + + @Rule(key = "") + private class EmptyRuleKey implements JavaScriptCheck { + + @Override + public T addIssue(T arg0) { + return null; + } + + @Override + public PreciseIssue addIssue(Tree arg0, String arg1) { + return null; + } + + @Override + public LineIssue addLineIssue(Tree arg0, String arg1) { + return null; + } + + @Override + public List scanFile(TreeVisitorContext arg0) { + return null; + } + } + + @Rule(key = "myKey") + private class UnregisteredRule implements JavaScriptCheck { + + @Override + public T addIssue(T arg0) { + return null; + } + + @Override + public PreciseIssue addIssue(Tree arg0, String arg1) { + return null; + } + + @Override + public LineIssue addLineIssue(Tree arg0, String arg1) { + return null; + } + + @Override + public List scanFile(TreeVisitorContext arg0) { + return null; + } + } + + @Rule(key = "correctRule") + private class CorrectRule implements JavaScriptCheck { + + @Override + public T addIssue(T arg0) { + return null; + } + + @Override + public PreciseIssue addIssue(Tree arg0, String arg1) { + return null; + } + + @Override + public LineIssue addLineIssue(Tree arg0, String arg1) { + return null; + } + + @Override + public List scanFile(TreeVisitorContext arg0) { + return null; + } + } +}