private Bucket getBucket(@Nullable Resource reference) { if (reference == null) { return null; } if (StringUtils.isNotBlank(reference.getKey())) { return buckets.get(reference); } String relativePathFromSourceDir = null; boolean isTest = false; boolean isDir = false; if (reference instanceof File) { File referenceFile = (File) reference; isTest = Qualifiers.UNIT_TEST_FILE.equals(referenceFile.getQualifier()); relativePathFromSourceDir = referenceFile.relativePathFromSourceDir(); } else if (reference instanceof Directory) { isDir = true; Directory referenceDir = (Directory) reference; relativePathFromSourceDir = referenceDir.relativePathFromSourceDir(); if (Directory.ROOT.equals(relativePathFromSourceDir)) { relativePathFromSourceDir = ""; } } if (relativePathFromSourceDir != null) { // Resolve using deprecated key List<String> dirs; ProjectDefinition projectDef = projectTree.getProjectDefinition(getProject()); if (isTest) { dirs = projectDef.getTestDirs(); } else { dirs = projectDef.getSourceDirs(); } for (String src : dirs) { java.io.File dirOrFile = pathResolver.relativeFile(projectDef.getBaseDir(), src); java.io.File abs = new java.io.File(dirOrFile, relativePathFromSourceDir); Bucket b = getBucket( isDir ? Directory.fromIOFile(abs, getProject()) : File.fromIOFile(abs, getProject())); if (b != null) { return b; } } } return null; }
@Override public <P extends Perspective> P as(Class<P> perspectiveClass, InputPath inputPath) { Resource r; if (inputPath instanceof InputDir) { r = Directory.create(((InputDir) inputPath).relativePath()); } else if (inputPath instanceof InputFile) { r = File.create(((InputFile) inputPath).relativePath()); } else { throw new IllegalArgumentException("Unknow input path type: " + inputPath); } return as(perspectiveClass, r); }
@Test public void should_get_measures() { Project p = new Project("struts"); Resource dir = Directory.create("foo/bar").setEffectiveKey("struts:foo/bar"); Resource file1 = Directory.create("foo/bar/File1.txt").setEffectiveKey("struts:foo/bar/File1.txt"); Resource file2 = Directory.create("foo/bar/File2.txt").setEffectiveKey("struts:foo/bar/File2.txt"); assertThat(measureCache.entries()).hasSize(0); assertThat(measureCache.byResource(p)).hasSize(0); assertThat(measureCache.byResource(dir)).hasSize(0); Measure mFile1 = new Measure(CoreMetrics.NCLOC, 1.0); measureCache.put(file1, mFile1); Measure mFile2 = new Measure(CoreMetrics.NCLOC, 3.0); measureCache.put(file2, mFile2); assertThat(measureCache.entries()).hasSize(2); assertThat(measureCache.byResource(p)).hasSize(0); assertThat(measureCache.byResource(dir)).hasSize(0); Measure mDir = new Measure(CoreMetrics.NCLOC, 4.0); measureCache.put(dir, mDir); assertThat(measureCache.entries()).hasSize(3); assertThat(measureCache.byResource(p)).hasSize(0); assertThat(measureCache.byResource(dir)).hasSize(1); assertThat(measureCache.byResource(dir).iterator().next()).isEqualTo(mDir); Measure mProj = new Measure(CoreMetrics.NCLOC, 4.0); measureCache.put(p, mProj); assertThat(measureCache.entries()).hasSize(4); assertThat(measureCache.byResource(p)).hasSize(1); assertThat(measureCache.byResource(p).iterator().next()).isEqualTo(mProj); assertThat(measureCache.byResource(dir)).hasSize(1); assertThat(measureCache.byResource(dir).iterator().next()).isEqualTo(mDir); }