From b054b6a871861db92348b555f3296976f74d1ee9 Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Wed, 2 Feb 2011 15:17:10 +0300 Subject: [PATCH] SONAR-2161: Normalize Java version for PMD --- .../org/sonar/plugins/pmd/PmdExecutor.java | 20 ++++++++++++------- .../sonar/plugins/pmd/PmdExecutorTest.java | 14 ++++++++++++- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java index d68e61ddddd..5c613c42448 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdExecutor.java @@ -142,13 +142,19 @@ public class PmdExecutor implements BatchExtension { return xmlReport; } - private void setJavaVersion(PMD pmd, Project project) { - String javaVersion = JavaUtils.getSourceVersion(project); - if (StringUtils.isNotBlank(javaVersion)) { - if ("1.1".equals(javaVersion) || "1.2".equals(javaVersion)) { - javaVersion = "1.3"; - } + static String getNormalizedJavaVersion(String javaVersion) { + if (StringUtils.equals("1.1", javaVersion) || StringUtils.equals("1.2", javaVersion)) { + javaVersion = "1.3"; + } else if (StringUtils.equals("5", javaVersion)) { + javaVersion = "1.5"; + } else if (StringUtils.equals("6", javaVersion)) { + javaVersion = "1.6"; } + return javaVersion; + } + + private void setJavaVersion(PMD pmd, Project project) { + String javaVersion = getNormalizedJavaVersion(JavaUtils.getSourceVersion(project)); if (javaVersion != null) { SourceType sourceType = SourceType.getSourceTypeForId("java " + javaVersion); if (sourceType != null) { @@ -159,4 +165,4 @@ public class PmdExecutor implements BatchExtension { } } } -} \ No newline at end of file +} diff --git a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdExecutorTest.java b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdExecutorTest.java index eabaddb6abb..07e9fb3a677 100644 --- a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdExecutorTest.java +++ b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdExecutorTest.java @@ -33,13 +33,13 @@ import java.net.URISyntaxException; import java.nio.charset.Charset; import java.util.Arrays; +import static org.hamcrest.Matchers.nullValue; import static org.hamcrest.core.Is.is; import static org.hamcrest.number.OrderingComparisons.greaterThan; import static org.junit.Assert.assertThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; - public class PmdExecutorTest { @Test @@ -85,4 +85,16 @@ public class PmdExecutorTest { File xmlReport = executor.execute(); assertThat(xmlReport.exists(), is(true)); } + + @Test + public void shouldNormalizeJavaVersion() { + assertThat(PmdExecutor.getNormalizedJavaVersion(null), nullValue()); + assertThat(PmdExecutor.getNormalizedJavaVersion(""), is("")); + assertThat(PmdExecutor.getNormalizedJavaVersion("1.1"), is("1.3")); + assertThat(PmdExecutor.getNormalizedJavaVersion("1.2"), is("1.3")); + assertThat(PmdExecutor.getNormalizedJavaVersion("1.4"), is("1.4")); + assertThat(PmdExecutor.getNormalizedJavaVersion("5"), is("1.5")); + assertThat(PmdExecutor.getNormalizedJavaVersion("6"), is("1.6")); + } + } -- 2.39.5