diff options
6 files changed, 20 insertions, 40 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/report/ComponentsPublisher.java b/sonar-batch/src/main/java/org/sonar/batch/report/ComponentsPublisher.java index 7bed3e9db12..fb20efee128 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/report/ComponentsPublisher.java +++ b/sonar-batch/src/main/java/org/sonar/batch/report/ComponentsPublisher.java @@ -23,6 +23,7 @@ import org.apache.commons.lang.StringUtils; import org.sonar.api.CoreProperties; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.bootstrap.ProjectReactor; +import org.sonar.api.batch.fs.InputFile; import org.sonar.api.resources.Language; import org.sonar.api.resources.Resource; import org.sonar.api.resources.ResourceUtils; @@ -83,6 +84,7 @@ public class ComponentsPublisher implements ReportPublisherStep { } if (ResourceUtils.isFile(r)) { builder.setIsTest(ResourceUtils.isUnitTestClass(r)); + builder.setLines(((InputFile) batchResource.inputPath()).lines()); } String name = getName(r); if (name != null) { diff --git a/sonar-batch/src/main/java/org/sonar/batch/scm/ScmSensor.java b/sonar-batch/src/main/java/org/sonar/batch/scm/ScmSensor.java index 54fd33a6f40..941f7d42f61 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/scm/ScmSensor.java +++ b/sonar-batch/src/main/java/org/sonar/batch/scm/ScmSensor.java @@ -26,7 +26,6 @@ import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputFile.Status; -import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.sensor.Sensor; import org.sonar.api.batch.sensor.SensorContext; import org.sonar.api.batch.sensor.SensorDescriptor; @@ -95,19 +94,19 @@ public final class ScmSensor implements Sensor { List<InputFile> filesToBlame = new LinkedList<InputFile>(); for (InputFile f : fs.inputFiles(fs.predicates().all())) { if (configuration.forceReloadAll()) { - addIfNotEmpty(filesToBlame, (DefaultInputFile) f); + addIfNotEmpty(filesToBlame, f); } else { FileData fileData = projectReferentials.fileData(projectDefinition.getKeyWithBranch(), f.relativePath()); if (f.status() != Status.SAME || fileData == null || fileData.needBlame()) { - addIfNotEmpty(filesToBlame, (DefaultInputFile) f); + addIfNotEmpty(filesToBlame, f); } } } return filesToBlame; } - private void addIfNotEmpty(List<InputFile> filesToBlame, DefaultInputFile f) { - if (!f.isEmpty()) { + private void addIfNotEmpty(List<InputFile> filesToBlame, InputFile f) { + if (f.lines() > 0) { filesToBlame.add(f); } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/report/ComponentsPublisherTest.java b/sonar-batch/src/test/java/org/sonar/batch/report/ComponentsPublisherTest.java index 812d8110339..5e806339a89 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/report/ComponentsPublisherTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/report/ComponentsPublisherTest.java @@ -26,6 +26,7 @@ import org.junit.rules.TemporaryFolder; import org.sonar.api.CoreProperties; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.bootstrap.ProjectReactor; +import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.database.model.Snapshot; import org.sonar.api.resources.Directory; import org.sonar.api.resources.Java; @@ -36,9 +37,7 @@ import org.sonar.batch.protocol.Constants.ComponentLinkType; import org.sonar.batch.protocol.Constants.EventCategory; import org.sonar.batch.protocol.output.BatchReport.Component; import org.sonar.batch.protocol.output.BatchReport.Event; -import org.sonar.batch.protocol.output.BatchReportReader; -import org.sonar.batch.protocol.output.BatchReportWriter; -import org.sonar.batch.protocol.output.FileStructure; +import org.sonar.batch.protocol.output.*; import java.io.File; import java.util.Arrays; @@ -88,17 +87,17 @@ public class ComponentsPublisherTest { org.sonar.api.resources.File file = org.sonar.api.resources.File.create("src/Foo.java", Java.INSTANCE, false); file.setEffectiveKey("module1:src/Foo.java"); file.setId(4).setUuid("FILE_UUID"); - resourceCache.add(file, dir).setSnapshot(new Snapshot().setId(14)); + resourceCache.add(file, dir).setSnapshot(new Snapshot().setId(14)).setInputPath(new DefaultInputFile("module1", "src/Foo.java").setLines(2)); org.sonar.api.resources.File fileWithoutLang = org.sonar.api.resources.File.create("src/make", null, false); fileWithoutLang.setEffectiveKey("module1:src/make"); fileWithoutLang.setId(5).setUuid("FILE_WITHOUT_LANG_UUID"); - resourceCache.add(fileWithoutLang, dir).setSnapshot(new Snapshot().setId(15)); + resourceCache.add(fileWithoutLang, dir).setSnapshot(new Snapshot().setId(15)).setInputPath(new DefaultInputFile("module1", "src/make").setLines(10)); org.sonar.api.resources.File testFile = org.sonar.api.resources.File.create("test/FooTest.java", Java.INSTANCE, true); testFile.setEffectiveKey("module1:test/FooTest.java"); testFile.setId(6).setUuid("TEST_FILE_UUID"); - resourceCache.add(testFile, dir).setSnapshot(new Snapshot().setId(16)); + resourceCache.add(testFile, dir).setSnapshot(new Snapshot().setId(16)).setInputPath(new DefaultInputFile("module1", "test/FooTest.java").setLines(4)); File outputDir = temp.newFolder(); BatchReportWriter writer = new BatchReportWriter(outputDir); @@ -151,7 +150,7 @@ public class ComponentsPublisherTest { org.sonar.api.resources.File file = org.sonar.api.resources.File.create("src/Foo.java", Java.INSTANCE, false); file.setEffectiveKey("module1:my_branch:my_branch:src/Foo.java"); file.setId(4).setUuid("FILE_UUID"); - resourceCache.add(file, dir).setSnapshot(new Snapshot().setId(14)); + resourceCache.add(file, dir).setSnapshot(new Snapshot().setId(14)).setInputPath(new DefaultInputFile("module1", "src/Foo.java").setLines(2)); File outputDir = temp.newFolder(); BatchReportWriter writer = new BatchReportWriter(outputDir); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java index 83b4473fbea..4f89a1c0dd3 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java @@ -51,7 +51,6 @@ public class DefaultInputFile implements InputFile { private String hash; private int nonBlankLines; private int[] originalLineOffsets; - private boolean empty; public DefaultInputFile(String moduleKey, String relativePath) { this.moduleKey = moduleKey; @@ -195,15 +194,6 @@ public class DefaultInputFile implements InputFile { return this; } - public boolean isEmpty() { - return this.empty; - } - - public DefaultInputFile setEmpty(boolean empty) { - this.empty = empty; - return this; - } - @Override public TextPointer newPointer(int line, int lineOffset) { DefaultTextPointer textPointer = new DefaultTextPointer(line, lineOffset); @@ -261,7 +251,6 @@ public class DefaultInputFile implements InputFile { this.setNonBlankLines(metadata.nonBlankLines); this.setHash(metadata.hash); this.setOriginalLineOffsets(metadata.originalLineOffsets); - this.setEmpty(metadata.empty); return this; } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java index d6ef257afa8..40a0cb87d89 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java @@ -66,8 +66,7 @@ public class FileMetadata implements BatchComponent { } private static class LineCounter extends CharHandler { - private boolean empty = true; - private int lines = 1; + private int lines = 0; private int nonBlankLines = 0; private boolean blankLine = true; boolean alreadyLoggedInvalidCharacter = false; @@ -81,7 +80,9 @@ public class FileMetadata implements BatchComponent { @Override void handleAll(char c) { - this.empty = false; + if (this.lines == 0) { + this.lines = 1; + } if (!alreadyLoggedInvalidCharacter && c == '\ufffd') { LOG.warn("Invalid character encountered in file {} at line {} for encoding {}. Please fix file content or configure the encoding to be used using property '{}'.", file, lines, encoding, CoreProperties.ENCODING_PROPERTY); @@ -120,9 +121,6 @@ public class FileMetadata implements BatchComponent { return nonBlankLines; } - public boolean isEmpty() { - return empty; - } } private static class FileHashComputer extends CharHandler { @@ -229,8 +227,7 @@ public class FileMetadata implements BatchComponent { LineOffsetCounter lineOffsetCounter = new LineOffsetCounter(); readFile(file, encoding, lineCounter, fileHashComputer, lineOffsetCounter); return new Metadata(lineCounter.lines(), lineCounter.nonBlankLines(), fileHashComputer.getHash(), lineOffsetCounter.getOriginalLineOffsets(), - lineOffsetCounter.getLastValidOffset(), - lineCounter.isEmpty()); + lineOffsetCounter.getLastValidOffset()); } /** @@ -246,8 +243,7 @@ public class FileMetadata implements BatchComponent { throw new IllegalStateException("Should never occurs", e); } return new Metadata(lineCounter.lines(), lineCounter.nonBlankLines(), fileHashComputer.getHash(), lineOffsetCounter.getOriginalLineOffsets(), - lineOffsetCounter.getLastValidOffset(), - lineCounter.isEmpty()); + lineOffsetCounter.getLastValidOffset()); } private static void readFile(File file, Charset encoding, CharHandler... handlers) { @@ -304,13 +300,11 @@ public class FileMetadata implements BatchComponent { final String hash; final int[] originalLineOffsets; final int lastValidOffset; - final boolean empty; - private Metadata(int lines, int nonBlankLines, String hash, List<Integer> originalLineOffsets, int lastValidOffset, boolean empty) { + private Metadata(int lines, int nonBlankLines, String hash, List<Integer> originalLineOffsets, int lastValidOffset) { this.lines = lines; this.nonBlankLines = nonBlankLines; this.hash = hash; - this.empty = empty; this.originalLineOffsets = Ints.toArray(originalLineOffsets); this.lastValidOffset = lastValidOffset; } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/FileMetadataTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/FileMetadataTest.java index 7696c1afcbf..93f3a8e57d7 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/FileMetadataTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/FileMetadataTest.java @@ -55,12 +55,11 @@ public class FileMetadataTest { FileUtils.touch(tempFile); FileMetadata.Metadata metadata = new FileMetadata().readMetadata(tempFile, Charsets.UTF_8); - assertThat(metadata.lines).isEqualTo(1); + assertThat(metadata.lines).isEqualTo(0); assertThat(metadata.nonBlankLines).isEqualTo(0); assertThat(metadata.hash).isNotEmpty(); assertThat(metadata.originalLineOffsets).containsOnly(0); assertThat(metadata.lastValidOffset).isEqualTo(0); - assertThat(metadata.empty).isTrue(); } @Test @@ -74,7 +73,6 @@ public class FileMetadataTest { assertThat(metadata.hash).isEqualTo(md5Hex("foo\nbar\nbaz")); assertThat(metadata.originalLineOffsets).containsOnly(0, 5, 10); assertThat(metadata.lastValidOffset).isEqualTo(13); - assertThat(metadata.empty).isFalse(); } @Test @@ -263,7 +261,6 @@ public class FileMetadataTest { assertThat(metadata.lines).isEqualTo(135); assertThat(metadata.nonBlankLines).isEqualTo(134); assertThat(metadata.hash).isNotEmpty(); - assertThat(metadata.empty).isFalse(); assertThat(logTester.logs(LoggerLevel.WARN).get(0)).contains("Invalid character encountered in file"); assertThat(logTester.logs(LoggerLevel.WARN).get(0)).contains( |