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.

ProjectBuilderMediumTest.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program 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. * This program 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 License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.scanner.mediumtest.fs;
  21. import com.google.common.collect.ImmutableMap;
  22. import java.io.File;
  23. import java.io.IOException;
  24. import java.util.List;
  25. import org.apache.commons.io.FileUtils;
  26. import org.hamcrest.BaseMatcher;
  27. import org.hamcrest.Description;
  28. import org.junit.Rule;
  29. import org.junit.Test;
  30. import org.junit.rules.ExpectedException;
  31. import org.junit.rules.TemporaryFolder;
  32. import org.sonar.api.SonarEdition;
  33. import org.sonar.api.batch.bootstrap.ProjectBuilder;
  34. import org.sonar.api.utils.MessageException;
  35. import org.sonar.scanner.mediumtest.AnalysisResult;
  36. import org.sonar.scanner.mediumtest.ScannerMediumTester;
  37. import org.sonar.scanner.protocol.output.ScannerReport.Issue;
  38. import org.sonar.xoo.XooPlugin;
  39. import org.sonar.xoo.rule.XooRulesDefinition;
  40. import static org.assertj.core.api.Assertions.assertThat;
  41. import static org.assertj.core.api.Assertions.tuple;
  42. import static org.mockito.ArgumentMatchers.any;
  43. import static org.mockito.Mockito.doThrow;
  44. import static org.mockito.Mockito.mock;
  45. public class ProjectBuilderMediumTest {
  46. @Rule
  47. public TemporaryFolder temp = new TemporaryFolder();
  48. @Rule
  49. public ExpectedException exception = ExpectedException.none();
  50. private ProjectBuilder projectBuilder = mock(ProjectBuilder.class);
  51. @Rule
  52. public ScannerMediumTester tester = new ScannerMediumTester()
  53. .setEdition(SonarEdition.SONARCLOUD)
  54. .registerPlugin("xoo", new XooPluginWithBuilder(projectBuilder))
  55. .addRules(new XooRulesDefinition())
  56. .addDefaultQProfile("xoo", "Sonar Way")
  57. .addActiveRule("xoo", "OneIssuePerLine", null, "One issue per line", "MAJOR", "OneIssuePerLine.internal", "xoo");
  58. private class XooPluginWithBuilder extends XooPlugin {
  59. private ProjectBuilder builder;
  60. XooPluginWithBuilder(ProjectBuilder builder) {
  61. this.builder = builder;
  62. }
  63. @Override
  64. public void define(Context context) {
  65. super.define(context);
  66. context.addExtension(builder);
  67. }
  68. }
  69. @Test
  70. public void testProjectReactorValidation() throws IOException {
  71. File baseDir = prepareProject();
  72. doThrow(new IllegalStateException("My error message")).when(projectBuilder).build(any(ProjectBuilder.Context.class));
  73. exception.expectMessage("Failed to execute project builder");
  74. exception.expect(MessageException.class);
  75. exception.expectCause(new BaseMatcher<Throwable>() {
  76. @Override
  77. public boolean matches(Object item) {
  78. if (!(item instanceof IllegalStateException)) {
  79. return false;
  80. }
  81. IllegalStateException e = (IllegalStateException) item;
  82. return "My error message".equals(e.getMessage());
  83. }
  84. @Override
  85. public void describeTo(Description description) {
  86. }
  87. });
  88. tester.newAnalysis()
  89. .properties(ImmutableMap.<String, String>builder()
  90. .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
  91. .put("sonar.projectKey", "com.foo.project")
  92. .put("sonar.sources", ".")
  93. .put("sonar.xoo.enableProjectBuilder", "true")
  94. .build())
  95. .execute();
  96. }
  97. @Test
  98. public void testProjectBuilder() throws IOException {
  99. File baseDir = prepareProject();
  100. AnalysisResult result = tester.newAnalysis()
  101. .properties(ImmutableMap.<String, String>builder()
  102. .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
  103. .put("sonar.projectKey", "com.foo.project")
  104. .put("sonar.sources", ".")
  105. .put("sonar.verbose", "true")
  106. .put("sonar.xoo.enableProjectBuilder", "true")
  107. .build())
  108. .execute();
  109. List<Issue> issues = result.issuesFor(result.inputFile("module1/src/sample.xoo"));
  110. assertThat(issues).hasSize(10);
  111. assertThat(issues)
  112. .extracting("msg", "textRange.startLine", "gap")
  113. .contains(tuple("This issue is generated on each line", 1, 0.0));
  114. }
  115. private File prepareProject() throws IOException {
  116. File baseDir = temp.newFolder();
  117. File module1Dir = new File(baseDir, "module1");
  118. module1Dir.mkdir();
  119. File srcDir = new File(module1Dir, "src");
  120. srcDir.mkdir();
  121. File xooFile = new File(srcDir, "sample.xoo");
  122. FileUtils.write(xooFile, "1\n2\n3\n4\n5\n6\n7\n8\n9\n10");
  123. return baseDir;
  124. }
  125. }