diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2010-09-07 11:08:38 +0000 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2010-09-07 11:08:38 +0000 |
commit | 6b3ca3adbfb5bba54f706418151c8f35d5deda24 (patch) | |
tree | 349497f64339a44e07ea699ea9cad58c3803d1fc /plugins/sonar-checkstyle-plugin/src | |
parent | e51183ff04b26f3e6481233fe7243d2b28b2411e (diff) | |
download | sonarqube-6b3ca3adbfb5bba54f706418151c8f35d5deda24.tar.gz sonarqube-6b3ca3adbfb5bba54f706418151c8f35d5deda24.zip |
SONAR-440 Warning when some rules are not imported from a checkstyle and/or PMD configuration file(s)
SONAR-1229 Export/Import a given Sonar quality profile
Diffstat (limited to 'plugins/sonar-checkstyle-plugin/src')
11 files changed, 364 insertions, 33 deletions
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 418e77f67f4..1564e30b45e 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 @@ -22,15 +22,16 @@ package org.sonar.plugins.checkstyle; import com.puppycrawl.tools.checkstyle.Checker; import com.puppycrawl.tools.checkstyle.PackageNamesLoader; import com.puppycrawl.tools.checkstyle.XMLLogger; +import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.BatchExtension; import org.sonar.api.batch.ProjectClasspath; +import org.sonar.api.utils.SonarException; import org.sonar.api.utils.TimeProfiler; import java.io.File; -import java.io.FileOutputStream; import java.io.OutputStream; public class CheckstyleExecutor implements BatchExtension { @@ -73,7 +74,7 @@ public class CheckstyleExecutor implements BatchExtension { File xmlReport = configuration.getTargetXMLReport(); if (xmlReport != null) { LOG.info("Checkstyle output report: " + xmlReport.getAbsolutePath()); - xmlOutput = new FileOutputStream(xmlReport); + xmlOutput = FileUtils.openOutputStream(xmlReport); checker.addListener(new XMLLogger(xmlOutput, true)); } @@ -84,7 +85,7 @@ public class CheckstyleExecutor implements BatchExtension { profiler.stop(); } catch (Exception e) { - throw new RuntimeException("Can not execute Checkstyle", e); + throw new SonarException("Can not execute Checkstyle", e); } finally { if (checker != null) { diff --git a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstylePlugin.java b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstylePlugin.java index 07296554359..644aaeaeabf 100644 --- a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstylePlugin.java +++ b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstylePlugin.java @@ -23,9 +23,6 @@ import org.sonar.api.CoreProperties; import org.sonar.api.Plugin; import org.sonar.api.Properties; import org.sonar.api.Property; -import org.sonar.api.profiles.RulesProfile; -import org.sonar.api.profiles.XMLProfileDefinition; -import org.sonar.api.resources.Java; import java.util.Arrays; import java.util.List; @@ -60,6 +57,7 @@ public class CheckstylePlugin implements Plugin { CheckstyleExecutor.class, CheckstyleAuditListener.class, CheckstyleProfileExporter.class, + CheckstyleProfileImporter.class, CheckstyleRuleRepository.class, SonarWayProfile.class, SunConventionsProfile.class, 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 6902348606c..d5ea971a55e 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 @@ -31,7 +31,6 @@ import org.sonar.api.resources.Java; import org.sonar.api.rules.ActiveRule; import org.sonar.api.rules.ActiveRuleParam; import org.sonar.api.rules.RuleParam; -import org.sonar.api.rules.RulePriority; import org.sonar.api.utils.SonarException; import java.io.IOException; @@ -140,7 +139,7 @@ public class CheckstyleProfileExporter extends ProfileExporter { if (manyInstances) { appendModuleProperty(writer, "id", activeRule.getRuleKey()); } - appendModuleProperty(writer, "severity", toCheckstyleSeverity(activeRule.getPriority())); + appendModuleProperty(writer, "severity", CheckstyleSeverityUtils.toSeverity(activeRule.getPriority())); appendRuleParameters(writer, activeRule); writer.append("</module>"); } @@ -170,16 +169,5 @@ public class CheckstyleProfileExporter extends ProfileExporter { } } - static String toCheckstyleSeverity(RulePriority priority) { - if (RulePriority.BLOCKER.equals(priority) || RulePriority.CRITICAL.equals(priority)) { - return "error"; - } - if (RulePriority.MAJOR.equals(priority)) { - return "warning"; - } - if (RulePriority.MINOR.equals(priority) || RulePriority.INFO.equals(priority)) { - return "info"; - } - throw new IllegalArgumentException("Priority not supported: " + priority); - } + } diff --git a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporter.java b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporter.java new file mode 100644 index 00000000000..c9561aea69e --- /dev/null +++ b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporter.java @@ -0,0 +1,108 @@ +/* + * 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 org.apache.commons.lang.StringUtils; +import org.codehaus.stax2.XMLInputFactory2; +import org.codehaus.staxmate.SMInputFactory; +import org.codehaus.staxmate.in.SMHierarchicCursor; +import org.codehaus.staxmate.in.SMInputCursor; +import org.sonar.api.profiles.ProfileImporter; +import org.sonar.api.profiles.ProfilePrototype; +import org.sonar.api.resources.Java; +import org.sonar.api.utils.ValidationMessages; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; +import java.io.Reader; + +public class CheckstyleProfileImporter extends ProfileImporter { + + private static final String CHECKER_MODULE = "Checker"; + private static final String TREEWALKER_MODULE = "TreeWalker"; + private static final String MODULE_NODE = "module"; + + public CheckstyleProfileImporter() { + super(CheckstyleConstants.REPOSITORY_KEY, CheckstyleConstants.PLUGIN_NAME); + setSupportedLanguages(Java.KEY); + } + + @Override + public ProfilePrototype importProfile(Reader reader, ValidationMessages messages) { + SMInputFactory inputFactory = initStax(); + ProfilePrototype profile = ProfilePrototype.create(); + try { + SMHierarchicCursor rootC = inputFactory.rootElementCursor(reader); + rootC.advance(); // <module name="Checker"> + SMInputCursor rootModulesCursor = rootC.childElementCursor(MODULE_NODE); + while (rootModulesCursor.getNext() != null) { + String configKey = rootModulesCursor.getAttrValue("name"); + if (StringUtils.equals(TREEWALKER_MODULE, configKey)) { + SMInputCursor treewalkerCursor = rootModulesCursor.childElementCursor(MODULE_NODE); + while (treewalkerCursor.getNext() != null) { + processModule(profile, CHECKER_MODULE + "/" + TREEWALKER_MODULE + "/", treewalkerCursor, messages); + } + } else { + processModule(profile, CHECKER_MODULE + "/", rootModulesCursor, messages); + } + } + } catch (XMLStreamException e) { + messages.addError("unvalidXml", "XML is not valid: " + e.getMessage()); + } + return profile; + } + + private SMInputFactory initStax() { + XMLInputFactory xmlFactory = XMLInputFactory2.newInstance(); + xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE); + xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE); + xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); + xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE); + SMInputFactory inputFactory = new SMInputFactory(xmlFactory); + return inputFactory; + } + + private void processModule(ProfilePrototype profile, String path, SMInputCursor moduleCursor, ValidationMessages messages) throws XMLStreamException { + String configKey = path + moduleCursor.getAttrValue("name"); + ProfilePrototype.RulePrototype rule = ProfilePrototype.RulePrototype.createByConfigKey(CheckstyleConstants.REPOSITORY_KEY, configKey); + + SMInputCursor propertyCursor = moduleCursor.childElementCursor("property"); + while (propertyCursor.getNext() != null) { + processProperty(rule, propertyCursor, messages); + } + + profile.activateRule(rule); + + } + + private void processProperty(ProfilePrototype.RulePrototype rule, SMInputCursor propertyCursor, ValidationMessages messages) throws XMLStreamException { + String key = propertyCursor.getAttrValue("name"); + String value = propertyCursor.getAttrValue("value"); + if (StringUtils.equals("id", key)) { + messages.addWarning("checkstyle.idPropertyNotSupported", "The property 'id' is not supported."); + + } else if (StringUtils.equals("severity", key)) { + rule.setPriority(CheckstyleSeverityUtils.fromSeverity(value)); + + } else { + rule.setParameter(key, value); + } + } +} diff --git a/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleSeverityUtils.java b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleSeverityUtils.java new file mode 100644 index 00000000000..cfa31c923a1 --- /dev/null +++ b/plugins/sonar-checkstyle-plugin/src/main/java/org/sonar/plugins/checkstyle/CheckstyleSeverityUtils.java @@ -0,0 +1,56 @@ +/* + * 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 org.apache.commons.lang.StringUtils; +import org.sonar.api.rules.RulePriority; + +public final class CheckstyleSeverityUtils { + + private CheckstyleSeverityUtils() { + // only static methods + } + + public static String toSeverity(RulePriority priority) { + if (RulePriority.BLOCKER.equals(priority) || RulePriority.CRITICAL.equals(priority)) { + return "error"; + } + if (RulePriority.MAJOR.equals(priority)) { + return "warning"; + } + if (RulePriority.MINOR.equals(priority) || RulePriority.INFO.equals(priority)) { + return "info"; + } + throw new IllegalArgumentException("Priority not supported: " + priority); + } + + public static RulePriority fromSeverity(String severity) { + if (StringUtils.equals(severity, "error")) { + return RulePriority.BLOCKER; + } + if (StringUtils.equals(severity, "warning")) { + return RulePriority.MAJOR; + } + if (StringUtils.equals(severity, "info")) { + return RulePriority.INFO; + } + return null; + } +} 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 index 0a0c5df535a..5cf62a2b05d 100644 --- 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 @@ -20,6 +20,7 @@ package org.sonar.plugins.checkstyle; import com.puppycrawl.tools.checkstyle.api.AuditEvent; +import org.apache.commons.io.FileUtils; import org.apache.commons.lang.StringUtils; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; @@ -29,6 +30,9 @@ import java.io.File; import java.nio.charset.Charset; import java.util.Arrays; +import static org.hamcrest.core.Is.is; +import static org.junit.Assert.assertThat; +import static org.junit.internal.matchers.StringContains.containsString; import static org.mockito.Matchers.anyObject; import static org.mockito.Mockito.*; @@ -47,10 +51,22 @@ public class CheckstyleExecutorTest { 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"))); } + @Test + public void canGenerateXMLReport() throws Exception { + CheckstyleConfiguration conf = mockConf(); + File report = new File("target/test-tmp/checkstyle-report.xml"); + when(conf.getTargetXMLReport()).thenReturn(report); + CheckstyleAuditListener listener = mockListener(); + CheckstyleExecutor executor = new CheckstyleExecutor(conf, listener, getClass().getClassLoader()); + executor.execute(); + + assertThat(report.exists(), is(true)); + assertThat(FileUtils.readFileToString(report), containsString("<error")); + } + private BaseMatcher<AuditEvent> newErrorMatcher(final String filename, final String rule) { return new BaseMatcher<AuditEvent>(){ public boolean matches(Object o) { 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 e4ea3ea1197..b81557b37b8 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,9 +31,6 @@ 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 @@ -142,13 +139,4 @@ 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/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest.java b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest.java new file mode 100644 index 00000000000..63d9c2d40d9 --- /dev/null +++ b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest.java @@ -0,0 +1,105 @@ +/* + * 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 org.junit.Before; +import org.junit.Test; +import org.sonar.api.profiles.ProfilePrototype; +import org.sonar.api.rules.RulePriority; +import org.sonar.api.utils.ValidationMessages; +import org.sonar.test.TestUtils; + +import java.io.Reader; +import java.io.StringReader; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNull.nullValue; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertThat; + +public class CheckstyleProfileImporterTest { + + private ValidationMessages messages; + private CheckstyleProfileImporter importer; + + @Before + public void before() { + messages = ValidationMessages.create(); + importer = new CheckstyleProfileImporter(); + } + + @Test + public void importSimpleProfile() { + Reader reader = new StringReader(TestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml")); + ProfilePrototype profile = importer.importProfile(reader, messages); + + assertThat(profile.getRules().size(), is(2)); + assertNotNull(profile.getRuleByConfigKey("checkstyle", "Checker/TreeWalker/EqualsHashCode")); + assertNotNull(profile.getRuleByConfigKey("checkstyle", "Checker/JavadocPackage")); + assertThat(messages.hasErrors(), is(false)); + } + + @Test + public void importParameters() { + Reader reader = new StringReader(TestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml")); + ProfilePrototype profile = importer.importProfile(reader, messages); + + ProfilePrototype.RulePrototype javadocCheck = profile.getRuleByConfigKey("checkstyle", "Checker/JavadocPackage"); + assertThat(javadocCheck.getParameters().size(), is(2)); + assertThat(javadocCheck.getParameter("format"), is("abcde")); + assertThat(javadocCheck.getParameter("ignore"), is("true")); + assertThat(javadocCheck.getParameter("severity"), nullValue()); // checkstyle internal parameter + } + + @Test + public void importPriorities() { + Reader reader = new StringReader(TestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml")); + ProfilePrototype profile = importer.importProfile(reader, messages); + + ProfilePrototype.RulePrototype javadocCheck = profile.getRuleByConfigKey("checkstyle", "Checker/JavadocPackage"); + assertThat(javadocCheck.getPriority(), is(RulePriority.BLOCKER)); + } + + @Test + public void priorityIsOptional() { + Reader reader = new StringReader(TestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml")); + ProfilePrototype profile = importer.importProfile(reader, messages); + + ProfilePrototype.RulePrototype check = profile.getRuleByConfigKey("checkstyle", "Checker/TreeWalker/EqualsHashCode"); + assertThat(check.getPriority(), nullValue()); + } + + @Test + public void idPropertyIsNotSupported() { + Reader reader = new StringReader(TestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/idProperty.xml")); + ProfilePrototype profile = importer.importProfile(reader, messages); + + ProfilePrototype.RulePrototype check = profile.getRuleByConfigKey("checkstyle", "Checker/JavadocPackage"); + assertThat(check.getParameter("id"), nullValue()); + assertThat(messages.getWarnings().size(), is(1)); + } + + @Test + public void testUnvalidXML() { + Reader reader = new StringReader("not xml"); + importer.importProfile(reader, messages); + assertThat(messages.getErrors().size(), is(1)); + } +} diff --git a/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleSeverityUtilsTest.java b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleSeverityUtilsTest.java new file mode 100644 index 00000000000..2f1e2b14acb --- /dev/null +++ b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleSeverityUtilsTest.java @@ -0,0 +1,47 @@ +/* + * 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 org.junit.Test; +import org.sonar.api.rules.RulePriority; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNull.nullValue; +import static org.junit.Assert.assertThat; + +public class CheckstyleSeverityUtilsTest { + + @Test + public void testToSeverity() { + assertThat(CheckstyleSeverityUtils.toSeverity(RulePriority.BLOCKER), is("error")); + assertThat(CheckstyleSeverityUtils.toSeverity(RulePriority.CRITICAL), is("error")); + assertThat(CheckstyleSeverityUtils.toSeverity(RulePriority.MAJOR), is("warning")); + assertThat(CheckstyleSeverityUtils.toSeverity(RulePriority.MINOR), is("info")); + assertThat(CheckstyleSeverityUtils.toSeverity(RulePriority.INFO), is("info")); + } + + @Test + public void testFromSeverity() { + assertThat(CheckstyleSeverityUtils.fromSeverity("error"), is(RulePriority.BLOCKER)); + assertThat(CheckstyleSeverityUtils.fromSeverity("warning"), is(RulePriority.MAJOR)); + assertThat(CheckstyleSeverityUtils.fromSeverity("info"), is(RulePriority.INFO)); + assertThat(CheckstyleSeverityUtils.fromSeverity(""), nullValue()); + } +} diff --git a/plugins/sonar-checkstyle-plugin/src/test/resources/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/idProperty.xml b/plugins/sonar-checkstyle-plugin/src/test/resources/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/idProperty.xml new file mode 100644 index 00000000000..d8dec5d9efb --- /dev/null +++ b/plugins/sonar-checkstyle-plugin/src/test/resources/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/idProperty.xml @@ -0,0 +1,11 @@ +<?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="JavadocPackage"> + <property name="id" value="javadoc" /> + <property name="severity" value="error" /> + <property name="format" value="abcde" /> + <property name="ignore" value="true" /> + </module> +</module> diff --git a/plugins/sonar-checkstyle-plugin/src/test/resources/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml b/plugins/sonar-checkstyle-plugin/src/test/resources/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml new file mode 100644 index 00000000000..3cf5c96d710 --- /dev/null +++ b/plugins/sonar-checkstyle-plugin/src/test/resources/org/sonar/plugins/checkstyle/CheckstyleProfileImporterTest/simple.xml @@ -0,0 +1,13 @@ +<?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="JavadocPackage"> + <property name="severity" value="error" /> + <property name="format" value="abcde" /> + <property name="ignore" value="true" /> + </module> + <module name="TreeWalker"> + <module name="EqualsHashCode"/> + </module> +</module> |