Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

SourcePublisherTest.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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.report;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.nio.charset.StandardCharsets;
  24. import org.apache.commons.io.FileUtils;
  25. import org.junit.Before;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.junit.rules.TemporaryFolder;
  29. import org.sonar.api.batch.fs.internal.DefaultInputFile;
  30. import org.sonar.api.batch.fs.internal.DefaultInputProject;
  31. import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
  32. import org.sonar.scanner.protocol.output.ScannerReportWriter;
  33. import org.sonar.scanner.scan.branch.BranchConfiguration;
  34. import org.sonar.scanner.scan.filesystem.InputComponentStore;
  35. import static org.assertj.core.api.Assertions.assertThat;
  36. import static org.mockito.Mockito.mock;
  37. public class SourcePublisherTest {
  38. @Rule
  39. public TemporaryFolder temp = new TemporaryFolder();
  40. private SourcePublisher publisher;
  41. private File sourceFile;
  42. private ScannerReportWriter writer;
  43. private DefaultInputFile inputFile;
  44. @Before
  45. public void prepare() throws IOException {
  46. File baseDir = temp.newFolder();
  47. sourceFile = new File(baseDir, "src/Foo.php");
  48. String moduleKey = "foo";
  49. inputFile = new TestInputFileBuilder(moduleKey, "src/Foo.php")
  50. .setLines(5)
  51. .setModuleBaseDir(baseDir.toPath())
  52. .setCharset(StandardCharsets.ISO_8859_1)
  53. .build();
  54. DefaultInputProject rootProject = TestInputFileBuilder.newDefaultInputProject(moduleKey, baseDir);
  55. InputComponentStore componentStore = new InputComponentStore(mock(BranchConfiguration.class));
  56. componentStore.put(moduleKey, inputFile);
  57. publisher = new SourcePublisher(componentStore);
  58. File outputDir = temp.newFolder();
  59. writer = new ScannerReportWriter(outputDir);
  60. }
  61. @Test
  62. public void publishEmptySource() throws Exception {
  63. FileUtils.write(sourceFile, "", StandardCharsets.ISO_8859_1);
  64. publisher.publish(writer);
  65. File out = writer.getSourceFile(inputFile.scannerId());
  66. assertThat(FileUtils.readFileToString(out, StandardCharsets.UTF_8)).isEqualTo("");
  67. }
  68. @Test
  69. public void publishSourceWithLastEmptyLine() throws Exception {
  70. FileUtils.write(sourceFile, "1\n2\n3\n4\n", StandardCharsets.ISO_8859_1);
  71. publisher.publish(writer);
  72. File out = writer.getSourceFile(inputFile.scannerId());
  73. assertThat(FileUtils.readFileToString(out, StandardCharsets.UTF_8)).isEqualTo("1\n2\n3\n4\n");
  74. }
  75. @Test
  76. public void publishTestSource() throws Exception {
  77. FileUtils.write(sourceFile, "1\n2\n3\n4\n", StandardCharsets.ISO_8859_1);
  78. // sampleFile.setQualifier(Qualifiers.UNIT_TEST_FILE);
  79. publisher.publish(writer);
  80. File out = writer.getSourceFile(inputFile.scannerId());
  81. assertThat(FileUtils.readFileToString(out, StandardCharsets.UTF_8)).isEqualTo("1\n2\n3\n4\n");
  82. }
  83. @Test
  84. public void publishSourceWithLastLineNotEmpty() throws Exception {
  85. FileUtils.write(sourceFile, "1\n2\n3\n4\n5", StandardCharsets.ISO_8859_1);
  86. publisher.publish(writer);
  87. File out = writer.getSourceFile(inputFile.scannerId());
  88. assertThat(FileUtils.readFileToString(out, StandardCharsets.UTF_8)).isEqualTo("1\n2\n3\n4\n5");
  89. }
  90. @Test
  91. public void cleanLineEnds() throws Exception {
  92. FileUtils.write(sourceFile, "\n2\r\n3\n4\r5", StandardCharsets.ISO_8859_1);
  93. publisher.publish(writer);
  94. File out = writer.getSourceFile(inputFile.scannerId());
  95. assertThat(FileUtils.readFileToString(out, StandardCharsets.UTF_8)).isEqualTo("\n2\n3\n4\n5");
  96. }
  97. }