@Test public void testOnlyRoot() throws IOException { DefaultInputModule root = new DefaultInputModule(ProjectDefinition.create().setKey("root").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder())); moduleHierarchy = new DefaultInputModuleHierarchy(root); assertThat(moduleHierarchy.children(root)).isEmpty(); assertThat(moduleHierarchy.parent(root)).isNull(); assertThat(moduleHierarchy.root()).isEqualTo(root); }
@Test public void testRelativePathToRoot() throws IOException { File rootBaseDir = temp.newFolder(); File mod1BaseDir = new File(rootBaseDir, "mod1"); File mod2BaseDir = new File(rootBaseDir, "mod2"); FileUtils.forceMkdir(mod1BaseDir); FileUtils.forceMkdir(mod2BaseDir); DefaultInputModule root = new DefaultInputModule(ProjectDefinition.create().setKey("root") .setBaseDir(rootBaseDir).setWorkDir(rootBaseDir)); DefaultInputModule mod1 = new DefaultInputModule(ProjectDefinition.create().setKey("mod1") .setBaseDir(mod1BaseDir).setWorkDir(temp.newFolder())); DefaultInputModule mod2 = new DefaultInputModule(ProjectDefinition.create().setKey("mod2") .setBaseDir(mod2BaseDir).setWorkDir(temp.newFolder())); DefaultInputModule mod3 = new DefaultInputModule(ProjectDefinition.create().setKey("mod2") .setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder())); Map<DefaultInputModule, DefaultInputModule> parents = new HashMap<>(); parents.put(mod1, root); parents.put(mod2, mod1); parents.put(mod3, mod1); moduleHierarchy = new DefaultInputModuleHierarchy(root, parents); assertThat(moduleHierarchy.relativePathToRoot(root)).isEqualTo(""); assertThat(moduleHierarchy.relativePathToRoot(mod1)).isEqualTo("mod1"); assertThat(moduleHierarchy.relativePathToRoot(mod2)).isEqualTo("mod2"); assertThat(moduleHierarchy.relativePathToRoot(mod3)).isNull(); } }
public DefaultInputModuleHierarchy provide(ScannerComponentIdGenerator scannerComponentIdGenerator, DefaultInputProject project) { if (hierarchy == null) { LOG.debug("Creating module hierarchy"); DefaultInputModule root = createModule(project.definition(), project.scannerId()); Map<DefaultInputModule, DefaultInputModule> parents = createChildren(root, scannerComponentIdGenerator, new HashMap<>()); if (parents.isEmpty()) { hierarchy = new DefaultInputModuleHierarchy(root); } else { hierarchy = new DefaultInputModuleHierarchy(root, parents); } } return hierarchy; }
@Override @CheckForNull public String relativePath(DefaultInputModule module) { AbstractProjectOrModule parent = parent(module); if (parent == null) { return ""; } Path parentBaseDir = parent.getBaseDir(); Path moduleBaseDir = module.getBaseDir(); return PathResolver.relativize(parentBaseDir, moduleBaseDir).orElse(null); }
@Before public void prepare() throws Exception { activeRules = new ActiveRulesBuilder().build(); fs = new DefaultFileSystem(temp.newFolder().toPath()); MetricFinder metricFinder = mock(MetricFinder.class); when(metricFinder.<Integer>findByKey(CoreMetrics.NCLOC_KEY)).thenReturn(CoreMetrics.NCLOC); when(metricFinder.<String>findByKey(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION_KEY)).thenReturn(CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION); settings = new MapSettings(); sensorStorage = mock(SensorStorage.class); analysisMode = mock(AnalysisMode.class); runtime = SonarRuntimeImpl.forSonarQube(Version.parse("5.5"), SonarQubeSide.SCANNER); hierarchy = new DefaultInputModuleHierarchy(new DefaultInputModule(ProjectDefinition.create() .setWorkDir(temp.newFolder()) .setBaseDir(temp.newFolder()).setKey("foo"))); adaptor = new ModuleSensorContext(mock(DefaultInputProject.class), mock(InputModule.class), settings.asConfig(), settings, fs, activeRules, analysisMode, sensorStorage, runtime); }
@Override @CheckForNull public String relativePath(DefaultInputModule module) { AbstractProjectOrModule parent = parent(module); if (parent == null) { return ""; } Path parentBaseDir = parent.getBaseDir(); Path moduleBaseDir = module.getBaseDir(); return PathResolver.relativize(parentBaseDir, moduleBaseDir).orElse(null); }
@Test public void test() throws IOException { DefaultInputModule root = new DefaultInputModule(ProjectDefinition.create().setKey("root").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder())); DefaultInputModule mod1 = new DefaultInputModule(ProjectDefinition.create().setKey("mod1").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder())); DefaultInputModule mod2 = new DefaultInputModule(ProjectDefinition.create().setKey("mod2").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder())); DefaultInputModule mod3 = new DefaultInputModule(ProjectDefinition.create().setKey("mod3").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder())); DefaultInputModule mod4 = new DefaultInputModule(ProjectDefinition.create().setKey("mod4").setBaseDir(temp.newFolder()).setWorkDir(temp.newFolder())); Map<DefaultInputModule, DefaultInputModule> parents = new HashMap<>(); parents.put(mod1, root); parents.put(mod2, mod1); parents.put(mod3, root); parents.put(mod4, root); moduleHierarchy = new DefaultInputModuleHierarchy(root, parents); assertThat(moduleHierarchy.children(root)).containsOnly(mod1, mod3, mod4); assertThat(moduleHierarchy.children(mod4)).isEmpty(); assertThat(moduleHierarchy.children(mod1)).containsOnly(mod2); assertThat(moduleHierarchy.parent(mod4)).isEqualTo(root); assertThat(moduleHierarchy.parent(mod2)).isEqualTo(mod1); assertThat(moduleHierarchy.parent(mod1)).isEqualTo(root); assertThat(moduleHierarchy.parent(root)).isNull(); assertThat(moduleHierarchy.root()).isEqualTo(root); }
public DefaultInputModuleHierarchy provide(ScannerComponentIdGenerator scannerComponentIdGenerator, DefaultInputProject project) { if (hierarchy == null) { LOG.debug("Creating module hierarchy"); DefaultInputModule root = createModule(project.definition(), project.scannerId()); Map<DefaultInputModule, DefaultInputModule> parents = createChildren(root, scannerComponentIdGenerator, new HashMap<>()); if (parents.isEmpty()) { hierarchy = new DefaultInputModuleHierarchy(root); } else { hierarchy = new DefaultInputModuleHierarchy(root, parents); } } return hierarchy; }