import org.apache.commons.io.FileUtils;
import org.sonar.api.batch.bootstrap.ProjectReactor;
+import org.sonar.api.resources.Language;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.resources.ResourceUtils;
import org.sonar.batch.protocol.output.resource.ReportComponent;
import org.sonar.batch.protocol.output.resource.ReportComponents;
+import javax.annotation.CheckForNull;
+
import java.io.File;
import java.io.IOException;
return ResourceUtils.isFile(r) ? ResourceUtils.isUnitTestClass(r) : null;
}
+ @CheckForNull
private String getLanguageKey(Resource r) {
- return ResourceUtils.isFile(r) ? r.getLanguage().getKey() : null;
+ Language language = r.getLanguage();
+ return ResourceUtils.isFile(r) && language != null ? language.getKey() : null;
}
private String getName(Resource r) {
import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.bootstrap.ProjectReactor;
import org.sonar.api.database.model.Snapshot;
-import org.sonar.api.resources.Directory;
-import org.sonar.api.resources.Java;
-import org.sonar.api.resources.Project;
+import org.sonar.api.resources.*;
import org.sonar.batch.index.ResourceCache;
import java.io.File;
File exportDir = temp.newFolder();
publisher.export(exportDir);
- System.out.println(FileUtils.readFileToString(new File(exportDir, "components.json")));
-
JSONAssert
.assertEquals(
IOUtils.toString(this.getClass().getResourceAsStream("ComponentsPublisherTest/expected.json"), "UTF-8"),
FileUtils.readFileToString(new File(exportDir, "components.json")), true);
}
+
+ @Test
+ public void testComponentPublisher_containing_file_without_language() throws Exception {
+ ProjectReactor reactor = new ProjectReactor(ProjectDefinition.create().setKey("ALL_PROJECT"));
+ ResourceCache resourceCache = new ResourceCache();
+ ComponentsPublisher publisher = new ComponentsPublisher(reactor, resourceCache);
+
+ View view = new View("ALL_PROJECT");
+ view.setId(1);
+ view.setAnalysisDate(new SimpleDateFormat("dd/MM/yyyy").parse("12/12/2012"));
+ resourceCache.add(view, null, new Snapshot().setId(11));
+
+ org.sonar.api.resources.File mainFile = org.sonar.api.resources.File.create("ALL_PROJECTsample", "ALL_PROJECTsample", null, false);
+ mainFile.setEffectiveKey("ALL_PROJECTsample");
+ mainFile.setId(2);
+ resourceCache.add(mainFile, view, new Snapshot().setId(12));
+
+ File exportDir = temp.newFolder();
+ publisher.export(exportDir);
+
+ JSONAssert
+ .assertEquals(
+ IOUtils.toString(this.getClass().getResourceAsStream("ComponentsPublisherTest/testComponentPublisher_containing_file_without_language.json"), "UTF-8"),
+ FileUtils.readFileToString(new File(exportDir, "components.json")), true);
+ }
+
+ private static class View extends Project {
+
+ private View(String key) {
+ super(key);
+ }
+
+ @Override
+ public String getName() {
+ return "All Projects";
+ }
+
+ @Override
+ public String getLongName() {
+ return null;
+ }
+
+ @Override
+ public String getDescription() {
+ return null;
+ }
+
+ @Override
+ public Language getLanguage() {
+ return null;
+ }
+
+ @Override
+ public String getScope() {
+ return Scopes.PROJECT;
+ }
+
+ @Override
+ public String getQualifier() {
+ return Qualifiers.VIEW;
+ }
+
+ @Override
+ public Project getParent() {
+ return null;
+ }
+
+ @Override
+ public boolean matchFilePattern(String antPattern) {
+ return false;
+ }
+ }
+
}