From 572e65b794dd6ae64fa96f5f391167b004ed40c4 Mon Sep 17 00:00:00 2001 From: Gustavo Martin Morcuende Date: Mon, 22 Aug 2016 21:28:23 +0200 Subject: [PATCH] sonar java plugin: unit tests --- Sonar/Plugins/sonar-custom-java-plugin/pom.xml | 256 ++++++++++++++++++++- .../de/example/custom/java/checks/CheckList.java | 1 + .../example/custom/java/checks/ParameterCheck.java | 2 - .../example/plugins/custom/java/CustomProfile.java | 6 +- ...oSpringServiceAnnotationInstanceFieldCheck.java | 15 ++ .../example/custom/java/checks/CheckListTest.java | 29 +++ .../custom/java/checks/ParameterCheckTest.java | 2 +- .../SpringServiceInstanceFieldCheckTest.java | 10 +- .../plugins/custom/java/CustomPluginTest.java | 22 ++ .../plugins/custom/java/CustomProfileTest.java | 42 ++++ .../custom/java/CustomRulesCheckRegistrarTest.java | 33 +++ 11 files changed, 409 insertions(+), 9 deletions(-) create mode 100644 Sonar/Plugins/sonar-custom-java-plugin/src/test/files/checks/NoSpringServiceAnnotationInstanceFieldCheck.java create mode 100644 Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/custom/java/checks/CheckListTest.java create mode 100644 Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomPluginTest.java create mode 100644 Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomProfileTest.java create mode 100644 Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomRulesCheckRegistrarTest.java diff --git a/Sonar/Plugins/sonar-custom-java-plugin/pom.xml b/Sonar/Plugins/sonar-custom-java-plugin/pom.xml index 46a5ffa..05a04de 100644 --- a/Sonar/Plugins/sonar-custom-java-plugin/pom.xml +++ b/Sonar/Plugins/sonar-custom-java-plugin/pom.xml @@ -19,6 +19,14 @@ 5.6.1 4.0 1.8 + 2.10.4 + 3.0.1 + + ${project.basedir}/target/jacoco-it.exec + ${project.basedir}/target/jacoco.exec + + false + true customjavaplugin @@ -49,7 +57,7 @@ ${java.plugin.version} test - + org.easytesting fest-assert 1.4 @@ -98,6 +106,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 @@ -118,7 +144,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-java-plugin/src/main/java/de/example/custom/java/checks/CheckList.java b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/custom/java/checks/CheckList.java index 4ced8a5..0851d0f 100644 --- a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/custom/java/checks/CheckList.java +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/custom/java/checks/CheckList.java @@ -18,6 +18,7 @@ public final class CheckList { .addAll(getJavaChecks()) .addAll(getJavaTestChecks()) .addAll(getXmlChecks()) + .addAll(getMavenChecks()) .build(); } diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/custom/java/checks/ParameterCheck.java b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/custom/java/checks/ParameterCheck.java index 372bbf4..95feff7 100644 --- a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/custom/java/checks/ParameterCheck.java +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/custom/java/checks/ParameterCheck.java @@ -26,8 +26,6 @@ public class ParameterCheck extends IssuableSubscriptionVisitor { @Override public void visitNode(Tree tree) { - LOG.info("Visiting Node"); - MethodTree method = (MethodTree) tree; if (method.parameters().size() == 1) { diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomProfile.java b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomProfile.java index 270fe70..3df2c08 100644 --- a/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomProfile.java +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/main/java/de/example/plugins/custom/java/CustomProfile.java @@ -25,7 +25,7 @@ public class CustomProfile extends ProfileDefinition { Profile jsonProfile = gson.fromJson(readResource(resource), Profile.class); RulesProfile profile = RulesProfile.create(jsonProfile.getName(), Java.KEY); - for (String key : jsonProfile.ruleKeys) { + for (String key : jsonProfile.getRuleKeys()) { Rule rule = Rule.create(CheckList.REPOSITORY_KEY, key); profile.activateRule(rule, null); } @@ -42,8 +42,8 @@ public class CustomProfile extends ProfileDefinition { } private static class Profile { - String name; - List ruleKeys; + private String name; + private List ruleKeys; public String getName() { return name; diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/test/files/checks/NoSpringServiceAnnotationInstanceFieldCheck.java b/Sonar/Plugins/sonar-custom-java-plugin/src/test/files/checks/NoSpringServiceAnnotationInstanceFieldCheck.java new file mode 100644 index 0000000..254e4a4 --- /dev/null +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/test/files/checks/NoSpringServiceAnnotationInstanceFieldCheck.java @@ -0,0 +1,15 @@ + +public class NoService { + private static final Integer FIELD1; + + private final Integer field2; + + private Integer field3; + + public static final Integer field4; + + public final Integer field5; + + public Integer field6; + +} diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/custom/java/checks/CheckListTest.java b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/custom/java/checks/CheckListTest.java new file mode 100644 index 0000000..306e897 --- /dev/null +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/custom/java/checks/CheckListTest.java @@ -0,0 +1,29 @@ +package de.example.custom.java.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 whenRetrievingJavaChecksThenGetRightNumberOfClasses() { + int count = 0; + Collection files = FileUtils.listFiles( + new File("src/main/java/de/example/custom/java/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-java-plugin/src/test/java/de/example/custom/java/checks/ParameterCheckTest.java b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/custom/java/checks/ParameterCheckTest.java index 4c60044..983efa4 100644 --- a/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/custom/java/checks/ParameterCheckTest.java +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/custom/java/checks/ParameterCheckTest.java @@ -6,7 +6,7 @@ import org.sonar.java.checks.verifier.JavaCheckVerifier; public class ParameterCheckTest { @Test - public void test() { + public void whenCheckingFunctionParametersThenGenerateIssues() { JavaCheckVerifier.verify("src/test/files/checks/ParameterCheck.java", new ParameterCheck()); } } diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/custom/java/checks/SpringServiceInstanceFieldCheckTest.java b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/custom/java/checks/SpringServiceInstanceFieldCheckTest.java index b373241..5ba3b12 100644 --- a/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/custom/java/checks/SpringServiceInstanceFieldCheckTest.java +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/custom/java/checks/SpringServiceInstanceFieldCheckTest.java @@ -6,14 +6,20 @@ import org.sonar.java.checks.verifier.JavaCheckVerifier; public class SpringServiceInstanceFieldCheckTest { private static final String NAMED_ANNOTATION = "src/test/files/checks/SpringServiceNamedAnnotationInstanceFieldCheck.java"; private static final String SPRING_SERVICE_ANNOTATION = "src/test/files/checks/SpringServiceAnnotationInstanceFieldCheck.java"; + private static final String NO_SPRING_SERVICE_ANNOTATION = "src/test/files/checks/NoSpringServiceAnnotationInstanceFieldCheck.java"; @Test - public void whenNamedAnnotationAndNoStaticOrFinalField() { + public void whenNamedAnnotationAndNoStaticOrFinalFieldThenGenerateIssues() { JavaCheckVerifier.verify(NAMED_ANNOTATION, new SpringServiceInstanceFieldCheck()); } @Test - public void whenSpringServiceAnnotationAndNoStaticOrFinalField() { + public void whenSpringServiceAnnotationAndNoStaticOrFinalFieldThenGenerateIssues() { JavaCheckVerifier.verify(SPRING_SERVICE_ANNOTATION, new SpringServiceInstanceFieldCheck()); } + + @Test + public void whenNoSpringServiceOrNamedAnnotationThenNoIssues() { + JavaCheckVerifier.verifyNoIssue(NO_SPRING_SERVICE_ANNOTATION, new SpringServiceInstanceFieldCheck()); + } } diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomPluginTest.java b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomPluginTest.java new file mode 100644 index 0000000..296a57b --- /dev/null +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomPluginTest.java @@ -0,0 +1,22 @@ +package de.example.plugins.custom.java; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import org.junit.Test; +import org.sonar.api.Plugin; +import org.sonar.api.SonarQubeVersion; + +public class CustomPluginTest { + + @Test + public void whenCreatingCustomJavaPluginThenRetrieveRightNumberOfExtensions() { + CustomPlugin javaPlugin = new CustomPlugin(); + Plugin.Context context = new Plugin.Context(SonarQubeVersion.V5_6); + + javaPlugin.define(context); + + assertThat(context.getExtensions().size(), is(3)); + } + +} diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomProfileTest.java b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomProfileTest.java new file mode 100644 index 0000000..ea4da21 --- /dev/null +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomProfileTest.java @@ -0,0 +1,42 @@ +package de.example.plugins.custom.java; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import org.junit.Test; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.rules.ActiveRule; +import org.sonar.plugins.java.Java; + +import de.example.custom.java.checks.CheckList; + +public class CustomProfileTest { + + @Test + public void whenCreatingJavaCustomProfileThenGenerateProfileWithRules() { + CustomProfile profile = new CustomProfile(); + + RulesProfile rulesProfile = profile.createProfile(null); + + List activeRules = rulesProfile.getActiveRulesByRepository(CheckList.REPOSITORY_KEY); + Set ruleKeys = getRuleKeys(activeRules); + assertThat(ruleKeys, hasItem("GUJ0002")); + assertThat(ruleKeys, hasItem("GUJ0002")); + assertThat(ruleKeys.size(), is(2)); + assertThat(rulesProfile.getLanguage(), is(Java.KEY)); + assertThat(rulesProfile.getName(), is("Custom Java Profile")); + } + + private Set getRuleKeys(List activeRules) { + Set keys = new HashSet<>(); + for (ActiveRule activeRule : activeRules) { + keys.add(activeRule.getRuleKey()); + } + + return keys; + } +} diff --git a/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomRulesCheckRegistrarTest.java b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomRulesCheckRegistrarTest.java new file mode 100644 index 0000000..1c07e4b --- /dev/null +++ b/Sonar/Plugins/sonar-custom-java-plugin/src/test/java/de/example/plugins/custom/java/CustomRulesCheckRegistrarTest.java @@ -0,0 +1,33 @@ +package de.example.plugins.custom.java; + +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.*; + +import java.util.List; + +import org.junit.Test; +import org.sonar.plugins.java.api.CheckRegistrar; +import org.sonar.plugins.java.api.CheckRegistrar.RegistrarContext; +import org.sonar.plugins.java.api.JavaCheck; + +import com.google.common.collect.Lists; + +import de.example.custom.java.checks.CheckList; + +public class CustomRulesCheckRegistrarTest { + + @Test + public void whenCreatingCustomJavaCheckRegistrarThenGenerateClassWithSuccess() { + CustomRulesCheckRegistrar registrar = new CustomRulesCheckRegistrar(); + RegistrarContext registrarContext = new CheckRegistrar.RegistrarContext(); + + registrar.register(registrarContext); + + List> checkClasses = Lists.newArrayList(registrarContext.checkClasses()); + List> testCheckClasses = Lists.newArrayList(registrarContext.testCheckClasses()); + assertThat(checkClasses.size(), is(2)); + assertThat(testCheckClasses.size(), is(0)); + assertThat(registrarContext.repositoryKey(), is(CheckList.REPOSITORY_KEY)); + } + +} -- 2.1.4