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.

DefaultInputModuleHierarchyTest.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.scan;
  21. import java.io.File;
  22. import java.io.IOException;
  23. import java.util.HashMap;
  24. import java.util.Map;
  25. import org.apache.commons.io.FileUtils;
  26. import org.junit.Rule;
  27. import org.junit.Test;
  28. import org.junit.rules.TemporaryFolder;
  29. import org.sonar.api.batch.bootstrap.ProjectDefinition;
  30. import org.sonar.api.batch.fs.internal.DefaultInputModule;
  31. import static org.assertj.core.api.Assertions.assertThat;
  32. public class DefaultInputModuleHierarchyTest {
  33. @Rule
  34. public TemporaryFolder temp = new TemporaryFolder();
  35. private DefaultInputModuleHierarchy moduleHierarchy;
  36. @Test
  37. public void test() throws IOException {
  38. DefaultInputModule root = new DefaultInputModule(ProjectDefinition.create().setKey("root").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder()));
  39. DefaultInputModule mod1 = new DefaultInputModule(ProjectDefinition.create().setKey("mod1").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder()));
  40. DefaultInputModule mod2 = new DefaultInputModule(ProjectDefinition.create().setKey("mod2").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder()));
  41. DefaultInputModule mod3 = new DefaultInputModule(ProjectDefinition.create().setKey("mod3").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder()));
  42. DefaultInputModule mod4 = new DefaultInputModule(ProjectDefinition.create().setKey("mod4").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder()));
  43. Map<DefaultInputModule, DefaultInputModule> parents = new HashMap<>();
  44. parents.put(mod1, root);
  45. parents.put(mod2, mod1);
  46. parents.put(mod3, root);
  47. parents.put(mod4, root);
  48. moduleHierarchy = new DefaultInputModuleHierarchy(root, parents);
  49. assertThat(moduleHierarchy.children(root)).containsOnly(mod1, mod3, mod4);
  50. assertThat(moduleHierarchy.children(mod4)).isEmpty();
  51. assertThat(moduleHierarchy.children(mod1)).containsOnly(mod2);
  52. assertThat(moduleHierarchy.parent(mod4)).isEqualTo(root);
  53. assertThat(moduleHierarchy.parent(mod2)).isEqualTo(mod1);
  54. assertThat(moduleHierarchy.parent(mod1)).isEqualTo(root);
  55. assertThat(moduleHierarchy.parent(root)).isNull();
  56. assertThat(moduleHierarchy.root()).isEqualTo(root);
  57. }
  58. @Test
  59. public void testOnlyRoot() throws IOException {
  60. DefaultInputModule root = new DefaultInputModule(ProjectDefinition.create().setKey("root").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder()));
  61. moduleHierarchy = new DefaultInputModuleHierarchy(root);
  62. assertThat(moduleHierarchy.children(root)).isEmpty();
  63. assertThat(moduleHierarchy.parent(root)).isNull();
  64. assertThat(moduleHierarchy.root()).isEqualTo(root);
  65. }
  66. @Test
  67. public void testRelativePathToRoot() throws IOException {
  68. File rootBaseDir = temp.newFolder();
  69. File mod1BaseDir = new File(rootBaseDir, "mod1");
  70. File mod2BaseDir = new File(rootBaseDir, "mod2");
  71. FileUtils.forceMkdir(mod1BaseDir);
  72. FileUtils.forceMkdir(mod2BaseDir);
  73. DefaultInputModule root = new DefaultInputModule(ProjectDefinition.create().setKey("root")
  74. .setBaseDir(rootBaseDir).setWorkDir(rootBaseDir));
  75. DefaultInputModule mod1 = new DefaultInputModule(ProjectDefinition.create().setKey("mod1")
  76. .setBaseDir(mod1BaseDir).setWorkDir(temp.newFolder()));
  77. DefaultInputModule mod2 = new DefaultInputModule(ProjectDefinition.create().setKey("mod2")
  78. .setBaseDir(mod2BaseDir).setWorkDir(temp.newFolder()));
  79. DefaultInputModule mod3 = new DefaultInputModule(ProjectDefinition.create().setKey("mod2")
  80. .setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder()));
  81. Map<DefaultInputModule, DefaultInputModule> parents = new HashMap<>();
  82. parents.put(mod1, root);
  83. parents.put(mod2, mod1);
  84. parents.put(mod3, mod1);
  85. moduleHierarchy = new DefaultInputModuleHierarchy(root, parents);
  86. assertThat(moduleHierarchy.relativePathToRoot(root)).isEqualTo("");
  87. assertThat(moduleHierarchy.relativePathToRoot(mod1)).isEqualTo("mod1");
  88. assertThat(moduleHierarchy.relativePathToRoot(mod2)).isEqualTo("mod2");
  89. assertThat(moduleHierarchy.relativePathToRoot(mod3)).isNull();
  90. }
  91. }