aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/sonar-checkstyle-plugin
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2010-09-07 09:11:52 +0000
committersimonbrandhof <simon.brandhof@gmail.com>2010-09-07 09:11:52 +0000
commite51183ff04b26f3e6481233fe7243d2b28b2411e (patch)
treebd0c86ac0e27ed41349252215ee3c175feecb4ef /plugins/sonar-checkstyle-plugin
parentaef2286bf00aca219862aa62ef5dcac4bb9d2e9a (diff)
downloadsonarqube-e51183ff04b26f3e6481233fe7243d2b28b2411e.tar.gz
sonarqube-e51183ff04b26f3e6481233fe7243d2b28b2411e.zip
checkstyle plugin : add some unit tests
Diffstat (limited to 'plugins/sonar-checkstyle-plugin')
-rw-r--r--plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleAuditListener.java26
-rw-r--r--plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleConfiguration.java6
-rw-r--r--plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleExecutor.java5
-rw-r--r--plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporter.java8
-rw-r--r--plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleExecutorTest.java89
-rw-r--r--plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest.java12
-rw-r--r--plugins/sonar-checkstyle-plugin/test-resources/Hello.java11
-rw-r--r--plugins/sonar-checkstyle-plugin/test-resources/World.java8
-rw-r--r--plugins/sonar-checkstyle-plugin/test-resources/checkstyle-conf.xml18
9 files changed, 157 insertions, 26 deletions
diff --git a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleAuditListener.java b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleAuditListener.java
index dd1dfd29768..9cb7b06fb88 100644
--- a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleAuditListener.java
+++ b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleAuditListener.java
@@ -1,22 +1,4 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2009 SonarSource SA
- * mailto:contact AT sonarsource DOT com
- *
- * Sonar 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.
- *
- * Sonar 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 Sonar; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
- */
+
package org.sonar.plugins.checkstyle;
import com.puppycrawl.tools.checkstyle.api.AuditEvent;
@@ -34,7 +16,7 @@ import org.sonar.api.rules.Violation;
/**
* @since 2.3
*/
-public final class CheckstyleAuditListener implements AuditListener, BatchExtension {
+public class CheckstyleAuditListener implements AuditListener, BatchExtension {
private final SensorContext context;
private final Project project;
@@ -124,4 +106,8 @@ public final class CheckstyleAuditListener implements AuditListener, BatchExtens
public void addException(AuditEvent event, Throwable throwable) {
// TODO waiting for sonar technical events ?
}
+
+ Resource getCurrentResource() {
+ return currentResource;
+ }
}
diff --git a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleConfiguration.java b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleConfiguration.java
index 3a5332125cb..88ed5f9aabf 100644
--- a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleConfiguration.java
+++ b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleConfiguration.java
@@ -109,11 +109,15 @@ public class CheckstyleConfiguration implements BatchExtension {
File xmlConfig = getXMLDefinitionFile();
LOG.info("Checkstyle configuration: " + xmlConfig.getAbsolutePath());
- Configuration configuration = ConfigurationLoader.loadConfiguration(xmlConfig.getAbsolutePath(), new PropertiesExpander(new Properties()));
+ Configuration configuration = toCheckstyleConfiguration(xmlConfig);
defineCharset(configuration);
return configuration;
}
+ static Configuration toCheckstyleConfiguration(File xmlConfig) throws CheckstyleException {
+ return ConfigurationLoader.loadConfiguration(xmlConfig.getAbsolutePath(), new PropertiesExpander(new Properties()));
+ }
+
private void defineCharset(Configuration configuration) {
Configuration[] modules = configuration.getChildren();
diff --git a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleExecutor.java b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleExecutor.java
index 2adf90c7930..418e77f67f4 100644
--- a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleExecutor.java
+++ b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleExecutor.java
@@ -32,13 +32,12 @@ import org.sonar.api.utils.TimeProfiler;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
-import java.net.URLClassLoader;
public class CheckstyleExecutor implements BatchExtension {
private static Logger LOG = LoggerFactory.getLogger(CheckstyleExecutor.class);
private CheckstyleConfiguration configuration;
- private URLClassLoader projectClassloader;
+ private ClassLoader projectClassloader;
private CheckstyleAuditListener listener;
public CheckstyleExecutor(CheckstyleConfiguration configuration, CheckstyleAuditListener listener, ProjectClasspath classpath) {
@@ -48,7 +47,7 @@ public class CheckstyleExecutor implements BatchExtension {
}
- CheckstyleExecutor(CheckstyleConfiguration configuration, CheckstyleAuditListener listener, URLClassLoader projectClassloader) {
+ CheckstyleExecutor(CheckstyleConfiguration configuration, CheckstyleAuditListener listener, ClassLoader projectClassloader) {
this.configuration = configuration;
this.listener = listener;
this.projectClassloader = projectClassloader;
diff --git a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporter.java b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporter.java
index 883013caf3a..6902348606c 100644
--- a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporter.java
+++ b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporter.java
@@ -141,9 +141,14 @@ public class CheckstyleProfileExporter extends ProfileExporter {
appendModuleProperty(writer, "id", activeRule.getRuleKey());
}
appendModuleProperty(writer, "severity", toCheckstyleSeverity(activeRule.getPriority()));
+ appendRuleParameters(writer, activeRule);
+ writer.append("</module>");
+ }
+
+ private void appendRuleParameters(Writer writer, ActiveRule activeRule) throws IOException {
for (RuleParam ruleParam : activeRule.getRule().getParams()) {
ActiveRuleParam activeParam = activeRule.getParameter(ruleParam.getKey());
- String value = null;
+ String value;
if (activeParam == null) {
value = ruleParam.getDefaultValue();
} else {
@@ -153,7 +158,6 @@ public class CheckstyleProfileExporter extends ProfileExporter {
appendModuleProperty(writer, ruleParam.getKey(), value);
}
}
- writer.append("</module>");
}
private void appendModuleProperty(Writer writer, String propertyKey, String propertyValue) throws IOException {
diff --git a/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleExecutorTest.java b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleExecutorTest.java
new file mode 100644
index 00000000000..0a0c5df535a
--- /dev/null
+++ b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleExecutorTest.java
@@ -0,0 +1,89 @@
+/*
+ * Sonar, open source software quality management tool.
+ * Copyright (C) 2009 SonarSource SA
+ * mailto:contact AT sonarsource DOT com
+ *
+ * Sonar 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.
+ *
+ * Sonar 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 Sonar; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
+ */
+package org.sonar.plugins.checkstyle;
+
+import com.puppycrawl.tools.checkstyle.api.AuditEvent;
+import org.apache.commons.lang.StringUtils;
+import org.hamcrest.BaseMatcher;
+import org.hamcrest.Description;
+import org.junit.Test;
+
+import java.io.File;
+import java.nio.charset.Charset;
+import java.util.Arrays;
+
+import static org.mockito.Matchers.anyObject;
+import static org.mockito.Mockito.*;
+
+public class CheckstyleExecutorTest {
+
+ @Test
+ public void execute() throws Exception {
+ CheckstyleConfiguration conf = mockConf();
+ CheckstyleAuditListener listener = mockListener();
+ CheckstyleExecutor executor = new CheckstyleExecutor(conf, listener, getClass().getClassLoader());
+ executor.execute();
+
+ verify(listener, times(1)).auditStarted((AuditEvent) anyObject());
+ verify(listener, times(1)).auditFinished((AuditEvent) anyObject());
+ verify(listener, times(1)).fileStarted(argThat(newFilenameMatcher("Hello.java")));
+ verify(listener, times(1)).fileFinished(argThat(newFilenameMatcher("Hello.java")));
+ verify(listener, times(1)).fileStarted(argThat(newFilenameMatcher("World.java")));
+ verify(listener, times(1)).fileFinished(argThat(newFilenameMatcher("World.java")));
+
+ verify(listener, atLeast(1)).addError(argThat(newErrorMatcher("Hello.java", "com.puppycrawl.tools.checkstyle.checks.coding.EmptyStatementCheck")));
+ }
+
+ private BaseMatcher<AuditEvent> newErrorMatcher(final String filename, final String rule) {
+ return new BaseMatcher<AuditEvent>(){
+ public boolean matches(Object o) {
+ AuditEvent event = (AuditEvent)o;
+ return StringUtils.endsWith(event.getFileName(), filename) && StringUtils.equals(event.getSourceName(), rule);
+ }
+
+ public void describeTo(Description description) {
+ }
+ };
+ }
+
+ private BaseMatcher<AuditEvent> newFilenameMatcher(final String filename) {
+ return new BaseMatcher<AuditEvent>(){
+ public boolean matches(Object o) {
+ AuditEvent event = (AuditEvent)o;
+ return StringUtils.endsWith(event.getFileName(), filename);
+ }
+
+ public void describeTo(Description description) {
+ }
+ };
+ }
+
+ private CheckstyleAuditListener mockListener() {
+ return mock(CheckstyleAuditListener.class);
+ }
+
+ private CheckstyleConfiguration mockConf() throws Exception {
+ CheckstyleConfiguration conf = mock(CheckstyleConfiguration.class);
+ when(conf.getCharset()).thenReturn(Charset.defaultCharset());
+ when(conf.getCheckstyleConfiguration()).thenReturn(CheckstyleConfiguration.toCheckstyleConfiguration(new File("test-resources/checkstyle-conf.xml")));
+ when(conf.getSourceFiles()).thenReturn(Arrays.<File>asList(new File("test-resources/Hello.java"), new File("test-resources/World.java")));
+ return conf;
+ }
+}
diff --git a/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest.java b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest.java
index b81557b37b8..e4ea3ea1197 100644
--- a/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest.java
+++ b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest.java
@@ -31,6 +31,9 @@ import org.xml.sax.SAXException;
import java.io.IOException;
import java.io.StringWriter;
+import static org.hamcrest.core.Is.is;
+import static org.junit.Assert.assertThat;
+
public class CheckstyleProfileExporterTest {
@Test
@@ -139,4 +142,13 @@ public class CheckstyleProfileExporterTest {
TestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/addCustomFilters.xml"),
writer.toString());
}
+
+ @Test
+ public void testPriorityMappings() {
+ assertThat(CheckstyleProfileExporter.toCheckstyleSeverity(RulePriority.BLOCKER), is("error"));
+ assertThat(CheckstyleProfileExporter.toCheckstyleSeverity(RulePriority.CRITICAL), is("error"));
+ assertThat(CheckstyleProfileExporter.toCheckstyleSeverity(RulePriority.MAJOR), is("warning"));
+ assertThat(CheckstyleProfileExporter.toCheckstyleSeverity(RulePriority.MINOR), is("info"));
+ assertThat(CheckstyleProfileExporter.toCheckstyleSeverity(RulePriority.INFO), is("info"));
+ }
}
diff --git a/plugins/sonar-checkstyle-plugin/test-resources/Hello.java b/plugins/sonar-checkstyle-plugin/test-resources/Hello.java
new file mode 100644
index 00000000000..4e6f2dd42d3
--- /dev/null
+++ b/plugins/sonar-checkstyle-plugin/test-resources/Hello.java
@@ -0,0 +1,11 @@
+class Hello {
+
+ public boolean methodWithViolations() {
+ String foo = "foo";
+ String bar = "bar";
+ if (true) {
+ ;;
+ }
+ return foo==bar;
+ }
+} \ No newline at end of file
diff --git a/plugins/sonar-checkstyle-plugin/test-resources/World.java b/plugins/sonar-checkstyle-plugin/test-resources/World.java
new file mode 100644
index 00000000000..3e78a97aeaf
--- /dev/null
+++ b/plugins/sonar-checkstyle-plugin/test-resources/World.java
@@ -0,0 +1,8 @@
+public class World {
+
+ public void hello() {
+ if (true) {
+
+ }
+ }
+} \ No newline at end of file
diff --git a/plugins/sonar-checkstyle-plugin/test-resources/checkstyle-conf.xml b/plugins/sonar-checkstyle-plugin/test-resources/checkstyle-conf.xml
new file mode 100644
index 00000000000..097456f6ebf
--- /dev/null
+++ b/plugins/sonar-checkstyle-plugin/test-resources/checkstyle-conf.xml
@@ -0,0 +1,18 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE module PUBLIC "-//Puppy Crawl//DTD Check Configuration 1.2//EN" "http://www.puppycrawl.com/dtds/configuration_1_2.dtd">
+<!-- Generated by Sonar -->
+<module name="Checker">
+ <module name="SuppressionCommentFilter"/>
+ <module name="JavadocPackage">
+ <property name="severity" value="warning"/>
+ </module>
+ <module name="TreeWalker">
+ <module name="FileContentsHolder"/>
+ <module name="EmptyStatement">
+ <property name="severity" value="info"/>
+ </module>
+ <module name="StringLiteralEqualityCheck">
+ <property name="severity" value="error"/>
+ </module>
+ </module>
+</module>