diff options
author | simonbrandhof <simon.brandhof@gmail.com> | 2011-07-25 22:19:40 +0200 |
---|---|---|
committer | simonbrandhof <simon.brandhof@gmail.com> | 2011-07-25 22:52:21 +0200 |
commit | f996705eeffd9314b4b29ba053a94fef621f3948 (patch) | |
tree | 28bbb505d08f990e6f47e37058ec8b3762166701 /plugins/sonar-checkstyle-plugin | |
parent | 6acbf886c2cc4b8f6998481e24236b1739ba079a (diff) | |
download | sonarqube-f996705eeffd9314b4b29ba053a94fef621f3948.tar.gz sonarqube-f996705eeffd9314b4b29ba053a94fef621f3948.zip |
SONAR-75 Configure Checkstyle with locale
Diffstat (limited to 'plugins/sonar-checkstyle-plugin')
4 files changed, 49 insertions, 25 deletions
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 57ef5053ddc..2fb74c374c5 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 @@ -19,47 +19,49 @@ */ package org.sonar.plugins.checkstyle; -import java.io.File; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.OutputStreamWriter; -import java.io.Writer; -import java.nio.charset.Charset; -import java.util.List; -import java.util.Properties; - import com.puppycrawl.tools.checkstyle.ConfigurationLoader; import com.puppycrawl.tools.checkstyle.DefaultConfiguration; import com.puppycrawl.tools.checkstyle.PropertiesExpander; import com.puppycrawl.tools.checkstyle.api.CheckstyleException; -import com.puppycrawl.tools.checkstyle.api.Configuration; +import org.apache.commons.configuration.Configuration; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.CharEncoding; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.sonar.api.BatchExtension; +import org.sonar.api.CoreProperties; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.resources.Java; import org.sonar.api.resources.Project; +import org.sonar.api.resources.ProjectFileSystem; import org.sonar.api.utils.SonarException; +import java.io.*; +import java.nio.charset.Charset; +import java.util.List; +import java.util.Locale; +import java.util.Properties; + + public class CheckstyleConfiguration implements BatchExtension { private static final Logger LOG = LoggerFactory.getLogger(CheckstyleConfiguration.class); private CheckstyleProfileExporter confExporter; private RulesProfile profile; - private Project project; + private Configuration conf; + private ProjectFileSystem fileSystem; - public CheckstyleConfiguration(CheckstyleProfileExporter confExporter, RulesProfile profile, Project project) { + public CheckstyleConfiguration(Configuration conf, CheckstyleProfileExporter confExporter, RulesProfile profile, ProjectFileSystem fileSystem) { + this.conf = conf; this.confExporter = confExporter; this.profile = profile; - this.project = project; + this.fileSystem = fileSystem; } public File getXMLDefinitionFile() { Writer writer = null; - File xmlFile = new File(project.getFileSystem().getSonarWorkingDirectory(), "checkstyle.xml"); + File xmlFile = new File(fileSystem.getSonarWorkingDirectory(), "checkstyle.xml"); try { writer = new OutputStreamWriter(new FileOutputStream(xmlFile, false), CharEncoding.UTF_8); confExporter.exportProfile(profile, writer); @@ -75,32 +77,32 @@ public class CheckstyleConfiguration implements BatchExtension { } public List<File> getSourceFiles() { - return project.getFileSystem().getSourceFiles(Java.INSTANCE); + return fileSystem.getSourceFiles(Java.INSTANCE); } public File getTargetXMLReport() { - if (project.getConfiguration().getBoolean(CheckstyleConstants.GENERATE_XML_KEY, CheckstyleConstants.GENERATE_XML_DEFAULT_VALUE)) { - return new File(project.getFileSystem().getSonarWorkingDirectory(), "checkstyle-result.xml"); + if (conf.getBoolean(CheckstyleConstants.GENERATE_XML_KEY, CheckstyleConstants.GENERATE_XML_DEFAULT_VALUE)) { + return new File(fileSystem.getSonarWorkingDirectory(), "checkstyle-result.xml"); } return null; } - public Configuration getCheckstyleConfiguration() throws IOException, CheckstyleException { + public com.puppycrawl.tools.checkstyle.api.Configuration getCheckstyleConfiguration() throws IOException, CheckstyleException { File xmlConfig = getXMLDefinitionFile(); LOG.info("Checkstyle configuration: " + xmlConfig.getAbsolutePath()); - Configuration configuration = toCheckstyleConfiguration(xmlConfig); + com.puppycrawl.tools.checkstyle.api.Configuration configuration = toCheckstyleConfiguration(xmlConfig); defineCharset(configuration); return configuration; } - static Configuration toCheckstyleConfiguration(File xmlConfig) throws CheckstyleException { + static com.puppycrawl.tools.checkstyle.api.Configuration toCheckstyleConfiguration(File xmlConfig) throws CheckstyleException { return ConfigurationLoader.loadConfiguration(xmlConfig.getAbsolutePath(), new PropertiesExpander(new Properties())); } - private void defineCharset(Configuration configuration) { - Configuration[] modules = configuration.getChildren(); - for (Configuration module : modules) { + private void defineCharset(com.puppycrawl.tools.checkstyle.api.Configuration configuration) { + com.puppycrawl.tools.checkstyle.api.Configuration[] modules = configuration.getChildren(); + for (com.puppycrawl.tools.checkstyle.api.Configuration module : modules) { if ("Checker".equals(module.getName()) || "com.puppycrawl.tools.checkstyle.Checker".equals(module.getName())) { if (module instanceof DefaultConfiguration) { Charset charset = getCharset(); @@ -111,8 +113,12 @@ public class CheckstyleConfiguration implements BatchExtension { } } + public Locale getLocale() { + return new Locale(conf.getString(CoreProperties.CORE_VIOLATION_LOCALE_PROPERTY, CoreProperties.CORE_VIOLATION_LOCALE_DEFAULT_VALUE)); + } + public Charset getCharset() { - Charset charset = project.getFileSystem().getSourceCharset(); + Charset charset = fileSystem.getSourceCharset(); if (charset == null) { charset = Charset.forName(System.getProperty("file.encoding", CharEncoding.UTF_8)); } 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 2b1f165540d..e6f07d85c6e 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 @@ -21,6 +21,7 @@ package org.sonar.plugins.checkstyle; import java.io.File; import java.io.OutputStream; +import java.util.Locale; import com.puppycrawl.tools.checkstyle.Checker; import com.puppycrawl.tools.checkstyle.PackageNamesLoader; @@ -78,6 +79,7 @@ public class CheckstyleExecutor implements BatchExtension { } checker.setCharset(configuration.getCharset().name()); + configureLocale(checker); checker.configure(configuration.getCheckstyleConfiguration()); checker.process(configuration.getSourceFiles()); @@ -95,4 +97,10 @@ public class CheckstyleExecutor implements BatchExtension { } } + private void configureLocale(Checker checker) { + Locale locale = configuration.getLocale(); + checker.setLocaleLanguage(locale.getLanguage()); + checker.setLocaleCountry(locale.getCountry()); + } + }
\ No newline at end of file diff --git a/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleConfigurationTest.java b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleConfigurationTest.java index e9bf6ce42bc..dac37703a98 100644 --- a/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleConfigurationTest.java +++ b/plugins/sonar-checkstyle-plugin/src/test/java/org/sonar/plugins/checkstyle/CheckstyleConfigurationTest.java @@ -19,6 +19,7 @@ */ package org.sonar.plugins.checkstyle; +import org.apache.commons.configuration.PropertiesConfiguration; import org.apache.commons.io.FileUtils; import org.junit.Test; import org.sonar.api.profiles.RulesProfile; @@ -28,6 +29,7 @@ import org.sonar.api.test.MavenTestUtils; import java.io.File; import java.io.IOException; import java.io.Writer; +import java.util.Locale; import static org.hamcrest.Matchers.is; import static org.junit.Assert.assertThat; @@ -39,13 +41,19 @@ public class CheckstyleConfigurationTest { Project project = MavenTestUtils.loadProjectFromPom(getClass(), "writeConfigurationToWorkingDir/pom.xml"); CheckstyleProfileExporter exporter = new FakeExporter(); - CheckstyleConfiguration configuration = new CheckstyleConfiguration(exporter, null, project); + CheckstyleConfiguration configuration = new CheckstyleConfiguration(null, exporter, null, project.getFileSystem()); File xmlFile = configuration.getXMLDefinitionFile(); assertThat(xmlFile.exists(), is(true)); assertThat(FileUtils.readFileToString(xmlFile), is("<conf/>")); } + @Test + public void shouldGetDefaultLocaleForMessages() { + CheckstyleConfiguration configuration = new CheckstyleConfiguration(new PropertiesConfiguration(), null, null, null); + assertThat(configuration.getLocale(), is(Locale.ENGLISH)); + } + public class FakeExporter extends CheckstyleProfileExporter { @Override public void exportProfile(RulesProfile profile, Writer writer) { 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 87e5aea56b5..9af1a30ecb8 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 @@ -29,6 +29,7 @@ import org.junit.Test; import java.io.File; import java.nio.charset.Charset; import java.util.Arrays; +import java.util.Locale; import static org.hamcrest.core.Is.is; import static org.junit.Assert.assertThat; @@ -100,6 +101,7 @@ public class CheckstyleExecutorTest { 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"))); + when(conf.getLocale()).thenReturn(Locale.ENGLISH); return conf; } } |