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.

CheckstyleProfileExporterTest.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.checkstyle;
  21. import org.apache.commons.configuration.BaseConfiguration;
  22. import org.apache.commons.configuration.Configuration;
  23. import org.junit.Test;
  24. import org.sonar.api.profiles.RulesProfile;
  25. import org.sonar.api.rules.Rule;
  26. import org.sonar.api.rules.RulePriority;
  27. import org.sonar.test.TestUtils;
  28. import org.xml.sax.SAXException;
  29. import java.io.IOException;
  30. import java.io.StringWriter;
  31. public class CheckstyleProfileExporterTest {
  32. @Test
  33. public void alwaysSetFileContentsHolderAndSuppressionCommentFilter() throws IOException, SAXException {
  34. RulesProfile profile = RulesProfile.create("sonar way", "java");
  35. StringWriter writer = new StringWriter();
  36. new CheckstyleProfileExporter().exportProfile(profile, writer);
  37. TestUtils.assertSimilarXml(
  38. TestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/alwaysSetFileContentsHolderAndSuppressionCommentFilter.xml"),
  39. writer.toString());
  40. }
  41. @Test
  42. public void noCheckstyleRulesToExport() throws IOException, SAXException {
  43. RulesProfile profile = RulesProfile.create("sonar way", "java");
  44. // this is a PMD rule
  45. profile.activateRule(Rule.create("pmd", "PmdRule1", "PMD rule one"), null);
  46. StringWriter writer = new StringWriter();
  47. new CheckstyleProfileExporter().exportProfile(profile, writer);
  48. TestUtils.assertSimilarXml(
  49. TestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/noCheckstyleRulesToExport.xml"),
  50. writer.toString());
  51. }
  52. @Test
  53. public void singleCheckstyleRulesToExport() throws IOException, SAXException {
  54. RulesProfile profile = RulesProfile.create("sonar way", "java");
  55. profile.activateRule(Rule.create("pmd", "PmdRule1", "PMD rule one"), null);
  56. profile.activateRule(
  57. Rule.create("checkstyle", "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck", "Javadoc").setConfigKey("Checker/JavadocPackage"),
  58. RulePriority.MAJOR
  59. );
  60. profile.activateRule(Rule.create("checkstyle", "com.puppycrawl.tools.checkstyle.checks.sizes.LineLengthCheck", "Line Length").setConfigKey("Checker/TreeWalker/LineLength"),
  61. RulePriority.CRITICAL);
  62. profile.activateRule(Rule.create("checkstyle", "com.puppycrawl.tools.checkstyle.checks.naming.LocalFinalVariableNameCheck", "Local Variable").setConfigKey("Checker/TreeWalker/Checker/TreeWalker/LocalFinalVariableName"),
  63. RulePriority.MINOR);
  64. StringWriter writer = new StringWriter();
  65. new CheckstyleProfileExporter().exportProfile(profile, writer);
  66. TestUtils.assertSimilarXml(
  67. TestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/singleCheckstyleRulesToExport.xml"),
  68. writer.toString());
  69. }
  70. @Test
  71. public void addTheIdPropertyWhenManyInstancesWithTheSameConfigKey() throws IOException, SAXException {
  72. RulesProfile profile = RulesProfile.create("sonar way", "java");
  73. Rule rule1 = Rule.create("checkstyle", "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck", "Javadoc").setConfigKey("Checker/JavadocPackage");
  74. Rule rule2 = Rule.create("checkstyle", "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck_12345", "Javadoc").setConfigKey("Checker/JavadocPackage");
  75. profile.activateRule(rule1, RulePriority.MAJOR);
  76. profile.activateRule(rule2, RulePriority.CRITICAL);
  77. StringWriter writer = new StringWriter();
  78. new CheckstyleProfileExporter().exportProfile(profile, writer);
  79. TestUtils.assertSimilarXml(
  80. TestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/addTheIdPropertyWhenManyInstancesWithTheSameConfigKey.xml"),
  81. writer.toString());
  82. }
  83. @Test
  84. public void exportParameters() throws IOException, SAXException {
  85. RulesProfile profile = RulesProfile.create("sonar way", "java");
  86. Rule rule = Rule.create("checkstyle", "com.puppycrawl.tools.checkstyle.checks.javadoc.JavadocPackageCheck", "Javadoc")
  87. .setConfigKey("Checker/JavadocPackage");
  88. rule.createParameter("format");
  89. rule.createParameter("message"); // not set in the profile and no default value => not exported in checkstyle
  90. rule.createParameter("ignore");
  91. profile.activateRule(rule, RulePriority.MAJOR)
  92. .setParameter("format", "abcde");
  93. StringWriter writer = new StringWriter();
  94. new CheckstyleProfileExporter().exportProfile(profile, writer);
  95. TestUtils.assertSimilarXml(
  96. TestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/exportParameters.xml"),
  97. writer.toString());
  98. }
  99. @Test
  100. public void addCustomFilters() throws IOException, SAXException {
  101. Configuration conf = new BaseConfiguration();
  102. conf.addProperty(CheckstyleConstants.FILTERS_KEY,
  103. "<module name=\"SuppressionCommentFilter\">"
  104. + "<property name=\"offCommentFormat\" value=\"BEGIN GENERATED CODE\"/>"
  105. + "<property name=\"onCommentFormat\" value=\"END GENERATED CODE\"/>" + "</module>"
  106. +"<module name=\"SuppressWithNearbyCommentFilter\">"
  107. + "<property name=\"commentFormat\" value=\"CHECKSTYLE IGNORE (\\w+) FOR NEXT (\\d+) LINES\"/>"
  108. + "<property name=\"checkFormat\" value=\"$1\"/>"
  109. + "<property name=\"messageFormat\" value=\"$2\"/>"
  110. + "</module>");
  111. RulesProfile profile = RulesProfile.create("sonar way", "java");
  112. StringWriter writer = new StringWriter();
  113. new CheckstyleProfileExporter(conf).exportProfile(profile, writer);
  114. TestUtils.assertSimilarXml(
  115. TestUtils.getResourceContent("/org/sonar/plugins/checkstyle/CheckstyleProfileExporterTest/addCustomFilters.xml"),
  116. writer.toString());
  117. }
  118. }