You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PmdViolationsXmlParserTest.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Sonar, open source software quality management tool.
  3. * Copyright (C) 2009 SonarSource SA
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * Sonar is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * Sonar is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Sonar; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
  19. */
  20. package org.sonar.plugins.pmd;
  21. import org.hamcrest.BaseMatcher;
  22. import org.hamcrest.Description;
  23. import org.junit.Test;
  24. import static org.mockito.Matchers.anyString;
  25. import static org.mockito.Matchers.argThat;
  26. import static org.mockito.Mockito.*;
  27. import org.mockito.invocation.InvocationOnMock;
  28. import org.mockito.stubbing.Answer;
  29. import org.sonar.api.batch.SensorContext;
  30. import org.sonar.api.profiles.RulesProfile;
  31. import org.sonar.api.resources.DefaultProjectFileSystem;
  32. import org.sonar.api.resources.JavaFile;
  33. import org.sonar.api.resources.Project;
  34. import org.sonar.api.rules.*;
  35. import org.sonar.api.test.IsViolation;
  36. import javax.xml.stream.XMLStreamException;
  37. import java.io.File;
  38. import java.net.URISyntaxException;
  39. import java.util.Arrays;
  40. public class PmdViolationsXmlParserTest {
  41. private void parse(SensorContext context, String xmlPath) throws URISyntaxException, XMLStreamException {
  42. DefaultProjectFileSystem fileSystem = mock(DefaultProjectFileSystem.class);
  43. when(fileSystem.getSourceDirs()).thenReturn(Arrays.asList(new File("/test/src/main/java")));
  44. Project project = mock(Project.class);
  45. when(project.getFileSystem()).thenReturn(fileSystem);
  46. RulesManager manager = mock(RulesManager.class);
  47. when(manager.getPluginRule(anyString(), anyString())).thenAnswer(new Answer<Rule>() {
  48. public Rule answer(InvocationOnMock invocation) {
  49. Object[] args = invocation.getArguments();
  50. return new Rule((String) args[1], (String) args[1], null, (String) args[0], "");
  51. }
  52. });
  53. RulesProfile profile = mock(RulesProfile.class);
  54. when(profile.getActiveRule(anyString(), anyString())).thenReturn(new ActiveRule(null, null, RulePriority.MINOR));
  55. PmdViolationsXmlParser parser = new PmdViolationsXmlParser(project, context, manager, profile);
  56. File xmlFile = new File(getClass().getResource(xmlPath).toURI());
  57. parser.parse(xmlFile);
  58. }
  59. @Test
  60. public void shouldSaveViolationsOnClasses() throws URISyntaxException, XMLStreamException {
  61. SensorContext context = mock(SensorContext.class);
  62. parse(context, "/org/sonar/plugins/pmd/pmd-result.xml");
  63. verify(context, times(30)).saveViolation(argThat(new IsViolationOnJavaClass()));
  64. verify(context, times(4)).saveViolation(argThat(new IsViolationOnJavaClass(new JavaFile("ch.hortis.sonar.mvn.ClassWithComments"))));
  65. Violation wanted = Violation.create((Rule)null, new JavaFile("ch.hortis.sonar.mvn.ClassWithComments"))
  66. .setMessage("Avoid unused local variables such as 'toto'.")
  67. .setLineId(22);
  68. verify(context, times(1)).saveViolation(argThat(new IsViolation(wanted)));
  69. }
  70. @Test
  71. public void defaultPackageShouldBeSetOnclassWithoutPackage() throws URISyntaxException, XMLStreamException {
  72. SensorContext context = mock(SensorContext.class);
  73. parse(context, "/org/sonar/plugins/pmd/pmd-class-without-package.xml");
  74. verify(context, times(3)).saveViolation(argThat(new IsViolationOnJavaClass(new JavaFile("ClassOnDefaultPackage"))));
  75. }
  76. @Test
  77. public void unknownXMLEntity() throws URISyntaxException, XMLStreamException {
  78. SensorContext context = mock(SensorContext.class);
  79. parse(context, "/org/sonar/plugins/pmd/pmd-result-with-unknown-entity.xml");
  80. verify(context, times(2)).saveViolation(argThat(new IsViolationOnJavaClass(new JavaFile("test.Test"))));
  81. }
  82. @Test
  83. public void ISOControlCharsXMLFile() throws URISyntaxException, XMLStreamException {
  84. SensorContext context = mock(SensorContext.class);
  85. parse(context, "/org/sonar/plugins/pmd/pmd-result-with-control-char.xml");
  86. verify(context, times(1)).saveViolation(argThat(new IsViolationOnJavaClass(new JavaFile("test.Test"))));
  87. }
  88. private class IsViolationOnJavaClass extends BaseMatcher<Violation> {
  89. private JavaFile javaClass;
  90. private IsViolationOnJavaClass(JavaFile javaClass) {
  91. this.javaClass = javaClass;
  92. }
  93. private IsViolationOnJavaClass() {
  94. }
  95. public boolean matches(Object o) {
  96. Violation v = (Violation) o;
  97. boolean ok = (v.getResource() != null) && (v.getResource() instanceof JavaFile);
  98. if (ok && javaClass != null) {
  99. ok = javaClass.equals(v.getResource());
  100. }
  101. return ok;
  102. }
  103. public void describeTo(Description description) {
  104. }
  105. }
  106. }