From e563e04cbdce81b04ad09e9ca1212a031846a12a Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Wed, 28 Mar 2012 11:02:36 +0600 Subject: [PATCH] Fix some quality flaws --- .../sonar/plugins/pmd/PmdConfiguration.java | 3 +- .../org/sonar/plugins/pmd/PmdExecutor.java | 2 +- .../java/org/sonar/plugins/pmd/PmdSensor.java | 5 +- .../plugins/pmd/PmdViolationsXmlParser.java | 92 ---------- .../plugins/pmd/PmdConfigurationTest.java | 21 ++- .../pmd/PmdViolationsXmlParserTest.java | 139 --------------- .../plugins/pmd/pmd-class-without-package.xml | 20 --- .../pmd/pmd-result-with-control-char.xml | Bin 709 -> 0 bytes .../pmd/pmd-result-with-unknown-entity.xml | 11 -- .../org/sonar/plugins/pmd/pmd-result.xml | 165 ------------------ 10 files changed, 26 insertions(+), 432 deletions(-) delete mode 100644 plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationsXmlParser.java delete mode 100644 plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationsXmlParserTest.java delete mode 100644 plugins/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/pmd-class-without-package.xml delete mode 100644 plugins/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/pmd-result-with-control-char.xml delete mode 100644 plugins/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/pmd-result-with-unknown-entity.xml delete mode 100644 plugins/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/pmd-result.xml diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdConfiguration.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdConfiguration.java index 6ec082908b1..3485b970892 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdConfiguration.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdConfiguration.java @@ -24,6 +24,7 @@ import org.sonar.api.Property; import org.sonar.api.config.Settings; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.resources.Project; +import org.sonar.api.utils.SonarException; import java.io.File; import java.io.IOException; @@ -67,7 +68,7 @@ public class PmdConfiguration implements BatchExtension { return project.getFileSystem().writeToWorkingDirectory(pmdConfiguration.toString(), "pmd.xml"); } catch (IOException e) { - throw new RuntimeException("Fail to save the PMD configuration", e); + throw new SonarException("Fail to save the PMD configuration", e); } } 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 f0a5a3a00bd..5beb5a20f7d 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 @@ -120,7 +120,7 @@ public class PmdExecutor implements BatchExtension { } InputStream stream = getClass().getResourceAsStream(rulesetPath); if (stream == null) { - throw new RuntimeException("The PMD ruleset can not be found: " + rulesetPath); + throw new SonarException("The PMD ruleset can not be found: " + rulesetPath); } return stream; diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdSensor.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdSensor.java index c129ec1dfda..2b406deb787 100644 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdSensor.java +++ b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdSensor.java @@ -25,6 +25,7 @@ import org.sonar.api.CoreProperties; import org.sonar.api.batch.Sensor; import org.sonar.api.batch.SensorContext; import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.resources.Java; import org.sonar.api.resources.JavaFile; import org.sonar.api.resources.Project; import org.sonar.api.resources.Resource; @@ -80,8 +81,8 @@ public class PmdSensor implements Sensor { } public boolean shouldExecuteOnProject(Project project) { - return project.getFileSystem().hasJavaSourceFiles() && - !profile.getActiveRulesByRepository(PmdConstants.REPOSITORY_KEY).isEmpty(); + return !project.getFileSystem().mainFiles(Java.KEY).isEmpty() && + !profile.getActiveRulesByRepository(PmdConstants.REPOSITORY_KEY).isEmpty(); } @Override diff --git a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationsXmlParser.java b/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationsXmlParser.java deleted file mode 100644 index f38b8cb8ba3..00000000000 --- a/plugins/sonar-pmd-plugin/src/main/java/org/sonar/plugins/pmd/PmdViolationsXmlParser.java +++ /dev/null @@ -1,92 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * 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.pmd; - -import org.apache.commons.lang.StringUtils; -import org.codehaus.staxmate.in.SMHierarchicCursor; -import org.codehaus.staxmate.in.SMInputCursor; -import org.sonar.api.CoreProperties; -import org.sonar.api.batch.SensorContext; -import org.sonar.api.resources.JavaFile; -import org.sonar.api.resources.Project; -import org.sonar.api.resources.Resource; -import org.sonar.api.rules.Rule; -import org.sonar.api.rules.RuleFinder; -import org.sonar.api.rules.Violation; -import org.sonar.api.utils.StaxParser; - -import javax.xml.stream.XMLStreamException; - -import java.io.File; - -@Deprecated -class PmdViolationsXmlParser { - - private Project project; - private RuleFinder ruleFinder; - private SensorContext context; - - public PmdViolationsXmlParser(Project project, RuleFinder ruleFinder, SensorContext context) { - this.project = project; - this.ruleFinder = ruleFinder; - this.context = context; - } - - public void parse(File file) throws XMLStreamException { - StaxParser parser = new StaxParser(new StreamHandler(), true); - parser.parse(file); - } - - private class StreamHandler implements StaxParser.XmlStreamHandler { - public void stream(SMHierarchicCursor rootCursor) throws XMLStreamException { - rootCursor.advance(); - - SMInputCursor fileCursor = rootCursor.descendantElementCursor("file"); - while (fileCursor.getNext() != null) { - String name = fileCursor.getAttrValue("name"); - Resource resource = JavaFile.fromAbsolutePath(name, project.getFileSystem().getSourceDirs(), false); - - // Save violations only for existing resources - if (context.getResource(resource) != null) { - streamViolations(fileCursor, resource); - } else { - fileCursor.advance(); - } - } - } - - private void streamViolations(SMInputCursor fileCursor, Resource resource) throws XMLStreamException { - SMInputCursor violationCursor = fileCursor.descendantElementCursor("violation"); - while (violationCursor.getNext() != null) { - int lineId = Integer.parseInt(violationCursor.getAttrValue("beginline")); - String ruleKey = violationCursor.getAttrValue("rule"); - String message = StringUtils.trim(violationCursor.collectDescendantText()); - - Rule rule = ruleFinder.findByKey(CoreProperties.PMD_PLUGIN, ruleKey); - // Save violations only for enabled rules - if (rule != null) { - Violation violation = Violation.create(rule, resource).setLineId(lineId).setMessage(message); - context.saveViolation(violation); - } - } - } - } - -} diff --git a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdConfigurationTest.java b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdConfigurationTest.java index 3475f2db96e..127cf94c85a 100644 --- a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdConfigurationTest.java +++ b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdConfigurationTest.java @@ -21,16 +21,20 @@ package org.sonar.plugins.pmd; import org.apache.commons.io.FileUtils; import org.junit.Test; +import org.sonar.api.config.Settings; import org.sonar.api.profiles.RulesProfile; import org.sonar.api.resources.Project; +import org.sonar.api.resources.ProjectFileSystem; import org.sonar.api.test.MavenTestUtils; import java.io.File; import java.io.IOException; import java.util.List; -import static org.hamcrest.Matchers.is; +import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; public class PmdConfigurationTest { @@ -47,4 +51,19 @@ public class PmdConfigurationTest { assertThat(FileUtils.readFileToString(xmlFile), is("\r\n\r\n\r\n")); } + @Test + public void shouldReturnTargetXMLReport() { + Project project = new Project("key"); + ProjectFileSystem fs = mock(ProjectFileSystem.class); + when(fs.getSonarWorkingDirectory()).thenReturn(new File("/tmp")); + project.setFileSystem(fs); + Settings settings = new Settings(); + PmdConfiguration configuration = new PmdConfiguration(null, null, project, settings); + + assertThat(configuration.getTargetXMLReport(), nullValue()); + + settings.setProperty(PmdConfiguration.PROPERTY_GENERATE_XML, true); + assertThat(configuration.getTargetXMLReport(), equalTo(new File("/tmp/pmd-result.xml"))); + } + } diff --git a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationsXmlParserTest.java b/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationsXmlParserTest.java deleted file mode 100644 index f6aa0b670ca..00000000000 --- a/plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdViolationsXmlParserTest.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Sonar, open source software quality management tool. - * Copyright (C) 2008-2012 SonarSource - * 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.pmd; - -import org.hamcrest.BaseMatcher; -import org.hamcrest.Description; -import org.junit.Test; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; -import org.sonar.api.batch.SensorContext; -import org.sonar.api.resources.JavaFile; -import org.sonar.api.resources.Project; -import org.sonar.api.resources.ProjectFileSystem; -import org.sonar.api.rules.Rule; -import org.sonar.api.rules.RuleFinder; -import org.sonar.api.rules.Violation; -import org.sonar.api.test.IsViolation; - -import java.io.File; -import java.util.Arrays; - -import static org.mockito.Matchers.any; -import static org.mockito.Matchers.anyString; -import static org.mockito.Matchers.argThat; -import static org.mockito.Mockito.*; - -public class PmdViolationsXmlParserTest { - - private void parse(SensorContext context, String xmlPath, boolean useIndexedResources) throws Exception { - ProjectFileSystem fileSystem = mock(ProjectFileSystem.class); - when(fileSystem.getSourceDirs()).thenReturn(Arrays.asList(new File("/test/src/main/java"))); - - Project project = mock(Project.class); - when(project.getFileSystem()).thenReturn(fileSystem); - - RuleFinder ruleFinder = mock(RuleFinder.class); - when(ruleFinder.findByKey(anyString(), anyString())).thenAnswer(new Answer() { - public Rule answer(InvocationOnMock invocation) throws Throwable { - Object[] args = invocation.getArguments(); - return Rule.create((String) args[0], (String) args[1], ""); - } - }); - - if (useIndexedResources) { - when(context.getResource((JavaFile) any())).thenReturn(new JavaFile("")); - } else { - when(context.getResource((JavaFile) any())).thenReturn(null); - } - - PmdViolationsXmlParser parser = new PmdViolationsXmlParser(project, ruleFinder, context); - - File xmlFile = new File(getClass().getResource(xmlPath).toURI()); - parser.parse(xmlFile); - } - - @Test - public void shouldSaveViolationsOnFiles() throws Exception { - SensorContext context = mock(SensorContext.class); - parse(context, "/org/sonar/plugins/pmd/pmd-result.xml", true); - - verify(context, times(30)).saveViolation(argThat(new IsViolationOnJavaFile())); - verify(context, times(4)).saveViolation(argThat(new IsViolationOnJavaFile(new JavaFile("ch.hortis.sonar.mvn.ClassWithComments")))); - - Violation wanted = Violation.create((Rule) null, new JavaFile("ch.hortis.sonar.mvn.ClassWithComments")) - .setMessage("Avoid unused local variables such as 'toto'.") - .setLineId(22); - verify(context, times(1)).saveViolation(argThat(new IsViolation(wanted))); - } - - @Test - public void shouldIgnoreNonIndexedResources() throws Exception { - SensorContext context = mock(SensorContext.class); - parse(context, "/org/sonar/plugins/pmd/pmd-result.xml", false); - - verify(context, never()).saveViolation(argThat(new IsViolationOnJavaFile())); - } - - @Test - public void defaultPackageShouldBeSetOnClassWithoutPackage() throws Exception { - SensorContext context = mock(SensorContext.class); - parse(context, "/org/sonar/plugins/pmd/pmd-class-without-package.xml", true); - verify(context, times(3)).saveViolation(argThat(new IsViolationOnJavaFile(new JavaFile("ClassOnDefaultPackage")))); - } - - @Test - public void unknownXMLEntity() throws Exception { - SensorContext context = mock(SensorContext.class); - parse(context, "/org/sonar/plugins/pmd/pmd-result-with-unknown-entity.xml", true); - verify(context, times(2)).saveViolation(argThat(new IsViolationOnJavaFile(new JavaFile("test.Test")))); - } - - @Test - public void ISOControlCharsXMLFile() throws Exception { - SensorContext context = mock(SensorContext.class); - parse(context, "/org/sonar/plugins/pmd/pmd-result-with-control-char.xml", true); - verify(context, times(1)).saveViolation(argThat(new IsViolationOnJavaFile(new JavaFile("test.Test")))); - } - - private class IsViolationOnJavaFile extends BaseMatcher { - private JavaFile javaFile; - - private IsViolationOnJavaFile(JavaFile javaFile) { - this.javaFile = javaFile; - } - - private IsViolationOnJavaFile() { - } - - public boolean matches(Object o) { - Violation v = (Violation) o; - boolean ok = (v.getResource() != null) && (v.getResource() instanceof JavaFile); - if (ok && javaFile != null) { - ok = javaFile.equals(v.getResource()); - } - return ok; - } - - public void describeTo(Description description) { - - } - } -} diff --git a/plugins/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/pmd-class-without-package.xml b/plugins/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/pmd-class-without-package.xml deleted file mode 100644 index ecd92dc2100..00000000000 --- a/plugins/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/pmd-class-without-package.xml +++ /dev/null @@ -1,20 +0,0 @@ - - - - - Avoid unused constructor parameters such as 'j'. - - - Avoid unused local variables such as 'j'. - - - Avoid unused private methods such as 'myMethod()'. - - - diff --git a/plugins/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/pmd-result-with-control-char.xml b/plugins/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/pmd-result-with-control-char.xml deleted file mode 100644 index b303effaa83b1b9f04359d5efef231d0ee28d542..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 709 zcmZ`%+invv5KWPILE;}Q7nP@YFR2oebXyg1QKcfPlJdmMushipd}Hhl;qMt|xxKIt zs~L~a_?($@adogp?2)4Ou91~0r9jvEPP@L551TjB7xL=z>643KyT8}YRjq0%lC~HV z*wDzjESJ-AJ}s+FRjy|BYF4RvxsVyU)dqzFi;XN21Bys(VL`j%3+!P$+VHu`=@GHl z-aukT;xqQz8SNNbSM$uR`zJeATW!U9VAbzakC z4`dp-ODU|1g4dv;AE?ExryiBVRAiO0h({4o?UFU8|C1a@d(!DgBWHQgn;rgAEX+6- zarSNSX?=EDEo%PPv&DMyIMU?P^DI6NPT9CuJ%P1ADiI#kYlC?LcaSpW$@rZ-nw;ED z#%2OSK%gj=qho}2BJFe(*%)gMkt2#p__iI0*nTLu%G=MY7QA;hQSrwU*-}M@9s?f8 F - - - -The String literal "\1\14\13&uffff;\12\14\7&uffff;\32\14\4&uffff;\1\14\1&uffff;\32" appears 10 times in this file; the first occurrence is on line 10 - - -The String literal "\14" appears 9 times in this file; the first occurrence is on line 12 - - - \ No newline at end of file diff --git a/plugins/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/pmd-result.xml b/plugins/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/pmd-result.xml deleted file mode 100644 index b227c5a3a0e..00000000000 --- a/plugins/sonar-pmd-plugin/src/test/resources/org/sonar/plugins/pmd/pmd-result.xml +++ /dev/null @@ -1,165 +0,0 @@ - - - - - Avoid unused constructor parameters such as 'j'. - - - Avoid unused local variables such as 'j'. - - - Avoid unused private methods such as 'myMethod()'. - - - - - A method/constructor shouldn't explicitly throw java.lang.Exception - - - Avoid unused local variables such as 'i'. - - - - - Avoid unused constructor parameters such as 'j'. - - - Avoid unused local variables such as 'j'. - - - Avoid unused private methods such as 'myMethod()'. - - - Avoid unused local variables such as 'toto'. - - - - - Avoid unused private fields such as 'id'. - - - Avoid unused private fields such as 'column1'. - - - Perhaps 'column1' could be replaced by a local variable. - - - - - Avoid too many methods - - - Avoid unused local variables such as 'i'. - - - Avoid unused local variables such as 'i'. - - - Avoid too many methods - - - System.out.print is used - - - System.out.print is used - - - Avoid too many methods - - - System.out.print is used - - - System.out.print is used - - - Do not use if statements that are always true or always false - - - System.out.print is used - - - Do not use if statements that are always true or always false - - - System.out.print is used - - - - - Avoid unused constructor parameters such as 't'. - - - Avoid unused local variables such as 't'. - - - Avoid if without using brace - - - Avoid using if statements without curly braces - - - Do not use if statements that are always true or always false - - - \ No newline at end of file -- 2.39.5