package org.sonar.batch.protocol.output.resource;
import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Collection;
return path;
}
- public ReportComponent setName(String name) {
+ public ReportComponent setName(@Nullable String name) {
this.name = name;
return this;
}
+ /**
+ * @return null for files and directories since it is the same as the path
+ */
+ @CheckForNull
public String name() {
return name;
}
return type;
}
- public ReportComponent setTest(Boolean isTest) {
+ public ReportComponent setTest(@Nullable Boolean isTest) {
this.isTest = isTest;
return this;
}
return isTest;
}
- public ReportComponent setLanguageKey(String languageKey) {
+ public ReportComponent setLanguageKey(@Nullable String languageKey) {
this.languageKey = languageKey;
return this;
}
+ /**
+ * @return null when not a file
+ */
+ @CheckForNull
public String languageKey() {
return languageKey;
}
Resource resource = getResource(reference);
if (resource instanceof File) {
File file = (File) resource;
- Project module = (Project) file.getParent().getParent();
+ Project module = currentProject;
ProjectDefinition def = projectTree.getProjectDefinition(module);
try {
return FileUtils.readFileToString(new java.io.File(def.getBaseDir(), file.getPath()));
return result;
}
+ @CheckForNull
private Boolean isTest(Resource r) {
return ResourceUtils.isFile(r) ? ResourceUtils.isUnitTestClass(r) : null;
}
return ResourceUtils.isFile(r) && language != null ? language.getKey() : null;
}
+ @CheckForNull
private String getName(Resource r) {
// Don't return name for directories and files since it can be guessed from the path
return (ResourceUtils.isFile(r) || ResourceUtils.isDirectory(r)) ? null : r.getName();
*/
package org.sonar.batch.index;
+import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Project moduleB;
Project moduleB1;
+ private java.io.File baseDir;
+
@Before
public void createIndex() throws IOException {
deprecatedViolations = mock(DeprecatedViolations.class);
mock(ResourceKeyMigration.class),
mock(MeasureCache.class));
- java.io.File baseDir = temp.newFolder();
+ baseDir = temp.newFolder();
project = new Project("project");
when(projectTree.getProjectDefinition(project)).thenReturn(ProjectDefinition.create().setBaseDir(baseDir));
moduleA = new Project("moduleA").setParent(project);
assertThat(index.getParent(fileRef)).isInstanceOf(Directory.class);
}
+ @Test
+ public void shouldGetSource() throws Exception {
+ Directory directory = Directory.create("src/org/foo", "org/foo");
+ File file = File.create("src/org/foo/Bar.java", "org/foo/Bar.java", Java.INSTANCE, false);
+ FileUtils.write(new java.io.File(baseDir, "src/org/foo/Bar.java"), "Foo bar");
+
+ assertThat(index.index(directory)).isTrue();
+ assertThat(index.index(file, directory)).isTrue();
+
+ File fileRef = File.create("src/org/foo/Bar.java", "org/foo/Bar.java", null, false);
+ assertThat(index.getSource(fileRef)).isEqualTo("Foo bar");
+ }
+
@Test
public void shouldIndexLibraryOutsideProjectTree() {
Library lib = new Library("junit", "4.8");