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.

DefaultInputModuleTest.java 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.api.batch.fs.internal.fs;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.nio.charset.Charset;
  24. import java.nio.file.Files;
  25. import java.nio.file.LinkOption;
  26. import java.nio.file.Path;
  27. import org.junit.Rule;
  28. import org.junit.Test;
  29. import org.junit.rules.TemporaryFolder;
  30. import org.sonar.api.batch.bootstrap.ProjectDefinition;
  31. import org.sonar.api.batch.fs.internal.DefaultInputModule;
  32. import static org.assertj.core.api.Assertions.assertThat;
  33. public class DefaultInputModuleTest {
  34. private static final String FILE_1 = "file1";
  35. private static final String TEST_1 = "test1";
  36. @Rule
  37. public TemporaryFolder temp = new TemporaryFolder();
  38. @Test
  39. public void check_getters() throws IOException {
  40. ProjectDefinition def = ProjectDefinition.create();
  41. def.setKey("moduleKey");
  42. File baseDir = temp.newFolder();
  43. Path src = baseDir.toPath().resolve(FILE_1);
  44. Files.createFile(src);
  45. Path test = baseDir.toPath().resolve(TEST_1);
  46. Files.createFile(test);
  47. def.setBaseDir(baseDir);
  48. File workDir = temp.newFolder();
  49. def.setWorkDir(workDir);
  50. def.setSources(FILE_1);
  51. def.setTests(TEST_1);
  52. DefaultInputModule module = new DefaultInputModule(def);
  53. assertThat(module.key()).isEqualTo("moduleKey");
  54. assertThat(module.definition()).isEqualTo(def);
  55. assertThat(module.getBaseDir())
  56. .isEqualTo(baseDir.toPath().toRealPath(LinkOption.NOFOLLOW_LINKS));
  57. assertThat(module.getWorkDir()).isEqualTo(workDir.toPath());
  58. assertThat(module.getEncoding()).isEqualTo(Charset.defaultCharset());
  59. assertThat(module.getSourceDirsOrFiles().get()).containsExactlyInAnyOrder(src.toRealPath(LinkOption.NOFOLLOW_LINKS));
  60. assertThat(module.getTestDirsOrFiles().get()).containsExactlyInAnyOrder(test.toRealPath(LinkOption.NOFOLLOW_LINKS));
  61. assertThat(module.getEncoding()).isEqualTo(Charset.defaultCharset());
  62. assertThat(module.isFile()).isFalse();
  63. }
  64. @Test
  65. public void no_sources() throws IOException {
  66. ProjectDefinition def = ProjectDefinition.create();
  67. def.setKey("moduleKey");
  68. File baseDir = temp.newFolder();
  69. Path src = baseDir.toPath().resolve(FILE_1);
  70. Files.createFile(src);
  71. Path test = baseDir.toPath().resolve(TEST_1);
  72. Files.createFile(test);
  73. def.setBaseDir(baseDir);
  74. File workDir = temp.newFolder();
  75. def.setWorkDir(workDir);
  76. DefaultInputModule module = new DefaultInputModule(def);
  77. assertThat(module.key()).isEqualTo("moduleKey");
  78. assertThat(module.definition()).isEqualTo(def);
  79. assertThat(module.getBaseDir()).isEqualTo(baseDir.toPath().toRealPath(LinkOption.NOFOLLOW_LINKS));
  80. assertThat(module.getWorkDir()).isEqualTo(workDir.toPath());
  81. assertThat(module.getEncoding()).isEqualTo(Charset.defaultCharset());
  82. assertThat(module.getSourceDirsOrFiles()).isNotPresent();
  83. assertThat(module.getTestDirsOrFiles()).isNotPresent();
  84. assertThat(module.getEncoding()).isEqualTo(Charset.defaultCharset());
  85. assertThat(module.isFile()).isFalse();
  86. }
  87. @Test
  88. public void working_directory_should_be_hidden() throws IOException {
  89. ProjectDefinition def = ProjectDefinition.create();
  90. File workDir = temp.newFolder(".sonar");
  91. def.setWorkDir(workDir);
  92. File baseDir = temp.newFolder();
  93. def.setBaseDir(baseDir);
  94. DefaultInputModule module = new DefaultInputModule(def);
  95. assertThat(workDir.isHidden()).isTrue();
  96. }
  97. }