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.

FileSystemMediumTest.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube 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. * SonarQube 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.batch.mediumtest.fs;
  21. import com.google.common.collect.ImmutableMap;
  22. import org.apache.commons.io.FileUtils;
  23. import org.junit.After;
  24. import org.junit.Before;
  25. import org.junit.Rule;
  26. import org.junit.Test;
  27. import org.junit.rules.ExpectedException;
  28. import org.junit.rules.TemporaryFolder;
  29. import org.sonar.api.batch.fs.InputFile;
  30. import org.sonar.api.rule.RuleKey;
  31. import org.sonar.api.utils.MessageException;
  32. import org.sonar.batch.mediumtest.AnalyzerMediumTester;
  33. import org.sonar.batch.mediumtest.AnalyzerMediumTester.TaskResult;
  34. import org.sonar.batch.mediumtest.xoo.plugin.XooPlugin;
  35. import org.sonar.batch.mediumtest.xoo.plugin.base.Xoo;
  36. import java.io.File;
  37. import java.io.IOException;
  38. import static org.fest.assertions.Assertions.assertThat;
  39. public class FileSystemMediumTest {
  40. @Rule
  41. public TemporaryFolder temp = new TemporaryFolder();
  42. @Rule
  43. public ExpectedException thrown = ExpectedException.none();
  44. public AnalyzerMediumTester tester = AnalyzerMediumTester.builder()
  45. .registerPlugin("xoo", new XooPlugin())
  46. .registerLanguage(new Xoo())
  47. .addDefaultQProfile("xoo", "Sonar Way")
  48. .activateRule(RuleKey.of("xoo", "OneIssuePerLine"))
  49. .bootstrapProperties(ImmutableMap.of("sonar.analysis.mode", "sensor"))
  50. .build();
  51. private File baseDir;
  52. private ImmutableMap.Builder<String, String> builder;
  53. @Before
  54. public void prepare() throws IOException {
  55. tester.start();
  56. baseDir = temp.newFolder();
  57. builder = ImmutableMap.<String, String>builder()
  58. .put("sonar.task", "scan")
  59. .put("sonar.projectBaseDir", baseDir.getAbsolutePath())
  60. .put("sonar.projectKey", "com.foo.project")
  61. .put("sonar.projectName", "Foo Project")
  62. .put("sonar.projectVersion", "1.0-SNAPSHOT")
  63. .put("sonar.projectDescription", "Description of Foo Project");
  64. }
  65. @After
  66. public void stop() {
  67. tester.stop();
  68. }
  69. @Test
  70. public void scanProjectWithSourceDir() throws IOException {
  71. File srcDir = new File(baseDir, "src");
  72. srcDir.mkdir();
  73. File xooFile = new File(srcDir, "sample.xoo");
  74. FileUtils.write(xooFile, "Sample xoo\ncontent");
  75. TaskResult result = tester.newTask()
  76. .properties(builder
  77. .put("sonar.sources", "src")
  78. .build())
  79. .start();
  80. assertThat(result.inputFiles()).hasSize(1);
  81. assertThat(result.inputFiles().get(0).type()).isEqualTo(InputFile.Type.MAIN);
  82. }
  83. @Test
  84. public void scanProjectWithTestDir() throws IOException {
  85. File test = new File(baseDir, "test");
  86. test.mkdir();
  87. File xooFile = new File(test, "sampleTest.xoo");
  88. FileUtils.write(xooFile, "Sample test xoo\ncontent");
  89. TaskResult result = tester.newTask()
  90. .properties(builder
  91. .put("sonar.sources", "")
  92. .put("sonar.tests", "test")
  93. .build())
  94. .start();
  95. assertThat(result.inputFiles()).hasSize(1);
  96. assertThat(result.inputFiles().get(0).type()).isEqualTo(InputFile.Type.TEST);
  97. }
  98. @Test
  99. public void scanProjectWithMixedSourcesAndTests() throws IOException {
  100. File srcDir = new File(baseDir, "src");
  101. srcDir.mkdir();
  102. File xooFile = new File(srcDir, "sample.xoo");
  103. FileUtils.write(xooFile, "Sample xoo\ncontent");
  104. File xooFile2 = new File(baseDir, "another.xoo");
  105. FileUtils.write(xooFile2, "Sample xoo 2\ncontent");
  106. File testDir = new File(baseDir, "test");
  107. testDir.mkdir();
  108. File xooTestFile = new File(baseDir, "sampleTest2.xoo");
  109. FileUtils.write(xooTestFile, "Sample test xoo\ncontent");
  110. File xooTestFile2 = new File(testDir, "sampleTest.xoo");
  111. FileUtils.write(xooTestFile2, "Sample test xoo 2\ncontent");
  112. TaskResult result = tester.newTask()
  113. .properties(builder
  114. .put("sonar.sources", "src,another.xoo")
  115. .put("sonar.tests", "test,sampleTest2.xoo")
  116. .build())
  117. .start();
  118. assertThat(result.inputFiles()).hasSize(4);
  119. }
  120. @Test
  121. public void failForDuplicateInputFile() throws IOException {
  122. File srcDir = new File(baseDir, "src");
  123. srcDir.mkdir();
  124. File xooFile = new File(srcDir, "sample.xoo");
  125. FileUtils.write(xooFile, "Sample xoo\ncontent");
  126. thrown.expect(MessageException.class);
  127. thrown.expectMessage("can't be indexed twice. Please check that inclusion/exclusion patterns produce disjoint sets for main and test files");
  128. tester.newTask()
  129. .properties(builder
  130. .put("sonar.sources", "src,src/sample.xoo")
  131. .build())
  132. .start();
  133. }
  134. }