Skip to content

Commit

Permalink
SONAR-8701 Store number of lines of files in compute engine Component
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Lancelot authored and dbmeneses committed Jan 27, 2017
1 parent 976e863 commit f3741d7
Show file tree
Hide file tree
Showing 28 changed files with 125 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,8 @@ static FileAttributes createFileAttributes(ScannerReport.Component component) {

return new FileAttributes(
component.getIsTest(),
trimToNull(component.getLanguage()));
trimToNull(component.getLanguage()),
component.getLines());
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import static com.google.common.base.Preconditions.checkArgument;

/**
* The attributes specific to a Component of type {@link org.sonar.server.computation.task.projectanalysis.component.Component.Type#FILE}.
*/
Expand All @@ -31,10 +33,13 @@ public class FileAttributes {
private final boolean unitTest;
@CheckForNull
private final String languageKey;
private final int lines;

public FileAttributes(boolean unitTest, @Nullable String languageKey) {
public FileAttributes(boolean unitTest, @Nullable String languageKey, int lines) {
this.unitTest = unitTest;
this.languageKey = languageKey;
checkArgument(lines > 0, "Lines has not been set for this file");
this.lines = lines;
}

public boolean isUnitTest() {
Expand All @@ -46,11 +51,16 @@ public String getLanguageKey() {
return languageKey;
}

public int getLines() {
return lines;
}

@Override
public String toString() {
return "FileAttributes{" +
"languageKey='" + languageKey + '\'' +
", unitTest=" + unitTest +
", lines=" + lines +
'}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -105,15 +105,15 @@ public void getFileAttributes_throws_ISE_if_BatchComponent_does_not_have_type_FI

@Test
public void isUnitTest_returns_true_if_IsTest_is_set_in_BatchComponent() {
ComponentImpl component = buildSimpleComponent(FILE, "file").setFileAttributes(new FileAttributes(true, null)).build();
ComponentImpl component = buildSimpleComponent(FILE, "file").setFileAttributes(new FileAttributes(true, null, 1)).build();

assertThat(component.getFileAttributes().isUnitTest()).isTrue();
}

@Test
public void isUnitTest_returns_value_of_language_of_BatchComponent() {
String languageKey = "some language key";
ComponentImpl component = buildSimpleComponent(FILE, "file").setFileAttributes(new FileAttributes(false, languageKey)).build();
ComponentImpl component = buildSimpleComponent(FILE, "file").setFileAttributes(new FileAttributes(false, languageKey, 1)).build();

assertThat(component.getFileAttributes().getLanguageKey()).isEqualTo(languageKey);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.util.function.Function;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.rules.ExternalResource;
import org.sonar.db.component.ComponentDto;
import org.sonar.db.component.SnapshotDto;
Expand Down Expand Up @@ -68,6 +69,9 @@ public class ComponentRootBuilderTest {
private static final String PROJECT_UUID = "project uuid";
private static final String DEFAULT_VERSION = "not provided";

@Rule
public ExpectedException expectedException = ExpectedException.none();

@Rule
public ScannerComponentProvider scannerComponentProvider = new ScannerComponentProvider();

Expand Down Expand Up @@ -139,7 +143,7 @@ public void name_of_module_directory_and_file_contains_branch_when_non_empty() {
ScannerReport.Component project = newBuilder().setType(PROJECT).setRef(1).addChildRef(2).build();
scannerComponentProvider.add(newBuilder().setRef(2).setType(MODULE).setKey(MODULE_KEY).addChildRef(3));
scannerComponentProvider.add(newBuilder().setRef(3).setType(DIRECTORY).setPath(DIRECTORY_PATH).addChildRef(4));
scannerComponentProvider.add(newBuilder().setRef(4).setType(FILE).setPath(FILE_PATH));
scannerComponentProvider.add(newBuilder().setRef(4).setType(FILE).setPath(FILE_PATH).setLines(1));

String branch = "BRANCH";
ComponentRootBuilder builder = new ComponentRootBuilder(branch, SIMPLE_UUID_GENERATOR, scannerComponentProvider, NO_COMPONENT_DTO_FOR_PROJECT, NO_BASEANALYSIS);
Expand All @@ -163,7 +167,7 @@ public void name_of_module_directory_and_file_is_key_of_Scanner_Component_when_n
ScannerReport.Component project = newBuilder().setType(PROJECT).setRef(1).addChildRef(2).build();
scannerComponentProvider.add(newBuilder().setRef(2).setType(MODULE).setKey(MODULE_KEY).addChildRef(3));
scannerComponentProvider.add(newBuilder().setRef(3).setType(DIRECTORY).setPath(DIRECTORY_PATH).addChildRef(4));
scannerComponentProvider.add(newBuilder().setRef(4).setType(FILE).setPath(FILE_PATH));
scannerComponentProvider.add(newBuilder().setRef(4).setType(FILE).setPath(FILE_PATH).setLines(1));

Component root = underTest.build(project, PROJECT_KEY);
assertThat(root.getKey()).isEqualTo(PROJECT_KEY);
Expand All @@ -180,7 +184,7 @@ public void name_of_module_directory_and_file_is_key_of_Scanner_Component_when_n
ScannerReport.Component project = newBuilder().setType(PROJECT).setRef(1).setName("").addChildRef(2).build();
scannerComponentProvider.add(newBuilder().setRef(2).setType(MODULE).setKey(MODULE_KEY).setName("").addChildRef(3));
scannerComponentProvider.add(newBuilder().setRef(3).setType(DIRECTORY).setPath(DIRECTORY_PATH).setName("").addChildRef(4));
scannerComponentProvider.add(newBuilder().setRef(4).setType(FILE).setPath(FILE_PATH).setName(""));
scannerComponentProvider.add(newBuilder().setRef(4).setType(FILE).setPath(FILE_PATH).setName("").setLines(1));

Component root = underTest.build(project, PROJECT_KEY);
assertThat(root.getKey()).isEqualTo(PROJECT_KEY);
Expand All @@ -202,14 +206,14 @@ public void name_of_module_directory_and_files_includes_name_of_closest_module()
scannerComponentProvider.add(newBuilder().setRef(22).setType(DIRECTORY).setPath("directory in module 1").addChildRef(36));
scannerComponentProvider.add(newBuilder().setRef(23).setType(DIRECTORY).setPath("directory in module 2").addChildRef(37));
scannerComponentProvider.add(newBuilder().setRef(24).setType(DIRECTORY).setPath("directory in module 3").addChildRef(38));
scannerComponentProvider.add(newBuilder().setRef(31).setType(FILE).setPath("file in project"));
scannerComponentProvider.add(newBuilder().setRef(32).setType(FILE).setPath("file in module 1"));
scannerComponentProvider.add(newBuilder().setRef(33).setType(FILE).setPath("file in module 2"));
scannerComponentProvider.add(newBuilder().setRef(34).setType(FILE).setPath("file in module 3"));
scannerComponentProvider.add(newBuilder().setRef(35).setType(FILE).setPath("file in directory in project"));
scannerComponentProvider.add(newBuilder().setRef(36).setType(FILE).setPath("file in directory in module 1"));
scannerComponentProvider.add(newBuilder().setRef(37).setType(FILE).setPath("file in directory in module 2"));
scannerComponentProvider.add(newBuilder().setRef(38).setType(FILE).setPath("file in directory in module 3"));
scannerComponentProvider.add(newBuilder().setRef(31).setType(FILE).setPath("file in project").setLines(1));
scannerComponentProvider.add(newBuilder().setRef(32).setType(FILE).setPath("file in module 1").setLines(1));
scannerComponentProvider.add(newBuilder().setRef(33).setType(FILE).setPath("file in module 2").setLines(1));
scannerComponentProvider.add(newBuilder().setRef(34).setType(FILE).setPath("file in module 3").setLines(1));
scannerComponentProvider.add(newBuilder().setRef(35).setType(FILE).setPath("file in directory in project").setLines(1));
scannerComponentProvider.add(newBuilder().setRef(36).setType(FILE).setPath("file in directory in module 1").setLines(1));
scannerComponentProvider.add(newBuilder().setRef(37).setType(FILE).setPath("file in directory in module 2").setLines(1));
scannerComponentProvider.add(newBuilder().setRef(38).setType(FILE).setPath("file in directory in module 3").setLines(1));

Component root = underTest.build(project, PROJECT_KEY);
Map<Integer, Component> componentsByRef = indexComponentByRef(root);
Expand Down Expand Up @@ -337,7 +341,7 @@ public void uuid_is_value_from_uuid_supplier_for_project_module_directory_and_fi
ScannerReport.Component project = newBuilder().setType(PROJECT).setRef(1).addChildRef(2).build();
scannerComponentProvider.add(newBuilder().setRef(2).setType(MODULE).setKey(MODULE_KEY).addChildRef(3));
scannerComponentProvider.add(newBuilder().setRef(3).setType(DIRECTORY).setPath(DIRECTORY_PATH).addChildRef(4));
scannerComponentProvider.add(newBuilder().setRef(4).setType(FILE).setPath(FILE_PATH));
scannerComponentProvider.add(newBuilder().setRef(4).setType(FILE).setPath(FILE_PATH).setLines(1));

Component root = underTest.build(project, PROJECT_KEY);
Map<Integer, Component> componentByRef = indexComponentByRef(root);
Expand All @@ -353,7 +357,7 @@ public void description_of_project_module_directory_and_file_is_null_when_unset_
ScannerReport.Component project = newBuilder().setType(PROJECT).setRef(1).addChildRef(2).build();
scannerComponentProvider.add(newBuilder().setRef(2).setType(MODULE).addChildRef(3));
scannerComponentProvider.add(newBuilder().setRef(3).setType(DIRECTORY).addChildRef(4));
scannerComponentProvider.add(newBuilder().setRef(4).setType(FILE));
scannerComponentProvider.add(newBuilder().setRef(4).setType(FILE).setLines(1));

Component root = underTest.build(project, PROJECT_KEY);
Map<Integer, Component> componentByRef = indexComponentByRef(root);
Expand All @@ -368,7 +372,7 @@ public void description_of_project_module_directory_and_file_is_null_when_empty_
ScannerReport.Component project = newBuilder().setType(PROJECT).setRef(1).setDescription("").addChildRef(2).build();
scannerComponentProvider.add(newBuilder().setRef(2).setType(MODULE).setDescription("").addChildRef(3));
scannerComponentProvider.add(newBuilder().setRef(3).setType(DIRECTORY).setDescription("").addChildRef(4));
scannerComponentProvider.add(newBuilder().setRef(4).setType(FILE).setDescription(""));
scannerComponentProvider.add(newBuilder().setRef(4).setType(FILE).setLines(1).setDescription(""));

Component root = underTest.build(project, PROJECT_KEY);
Map<Integer, Component> componentByRef = indexComponentByRef(root);
Expand All @@ -383,7 +387,7 @@ public void description_of_project_module_directory_and_file_is_description_of_S
ScannerReport.Component project = newBuilder().setType(PROJECT).setRef(1).setDescription("desc of project").addChildRef(2).build();
scannerComponentProvider.add(newBuilder().setRef(2).setType(MODULE).setDescription("desc of module").addChildRef(3));
scannerComponentProvider.add(newBuilder().setRef(3).setType(DIRECTORY).setDescription("desc of directory").addChildRef(4));
scannerComponentProvider.add(newBuilder().setRef(4).setType(FILE).setDescription("desc of file"));
scannerComponentProvider.add(newBuilder().setRef(4).setType(FILE).setLines(1).setDescription("desc of file"));

Component root = underTest.build(project, PROJECT_KEY);
Map<Integer, Component> componentByRef = indexComponentByRef(root);
Expand Down Expand Up @@ -466,18 +470,37 @@ public void createFileAttributes_returns_null_when_type_is_not_FILE() {

@Test
public void createFileAttributes_sets_language_to_null_when_unset_in_Scanner_Component() {
assertThat(createFileAttributes(newBuilder().setType(FILE).build()).getLanguageKey()).isNull();
assertThat(createFileAttributes(newBuilder().setType(FILE).setLines(1).build()).getLanguageKey()).isNull();
}

@Test
public void createFileAttributes_sets_language_to_null_when_empty_in_Scanner_Component() {
assertThat(createFileAttributes(newBuilder().setType(FILE).setLanguage("").build()).getLanguageKey()).isNull();
assertThat(createFileAttributes(newBuilder().setType(FILE).setLanguage("").setLines(1).build()).getLanguageKey()).isNull();
}

@Test
public void createFileAttributes_sets_unitTest_from_Scanner_Component() {
assertThat(createFileAttributes(newBuilder().setType(FILE).build()).isUnitTest()).isFalse();
assertThat(createFileAttributes(newBuilder().setType(FILE).setIsTest(true).build()).isUnitTest()).isTrue();
assertThat(createFileAttributes(newBuilder().setType(FILE).setLines(1).build()).isUnitTest()).isFalse();
assertThat(createFileAttributes(newBuilder().setType(FILE).setIsTest(true).setLines(1).build()).isUnitTest()).isTrue();
}

@Test
public void createFileAttributes_sets_lines_in_Scanner_Component() {
assertThat(createFileAttributes(newBuilder().setType(FILE).setLines(10).build()).getLines()).isEqualTo(10);
}

@Test
public void fail_with_IAE_when_createFileAttributes_sets_lines_to_0() throws Exception {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Lines has not been set for this file");
createFileAttributes(newBuilder().setType(FILE).setLines(0).build());
}

@Test
public void fail_with_IAE_when_createFileAttributes_lines_is_not_set() throws Exception {
expectedException.expect(IllegalArgumentException.class);
expectedException.expectMessage("Lines has not been set for this file");
createFileAttributes(newBuilder().setType(FILE).build());
}

private static class ScannerComponentProvider extends ExternalResource implements Function<Integer, ScannerReport.Component> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
*/
public class ReportComponent implements Component {

private static final FileAttributes DEFAULT_FILE_ATTRIBUTES = new FileAttributes(false, null);
private static final FileAttributes DEFAULT_FILE_ATTRIBUTES = new FileAttributes(false, null, 1);

public static final Component DUMB_PROJECT = builder(Type.PROJECT, 1).setKey("PROJECT_KEY").setUuid("PROJECT_UUID").setName("Project Name").setVersion("1.0-SNAPSHOT").build();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import java.util.Collections;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.config.Settings;
import org.sonar.api.config.MapSettings;
import org.sonar.api.config.Settings;
import org.sonar.api.utils.log.LogTester;
import org.sonar.api.utils.log.LoggerLevel;
import org.sonar.duplications.block.Block;
Expand Down Expand Up @@ -54,7 +54,7 @@ public class IntegrateCrossProjectDuplicationsTest {
static final String ORIGIN_FILE_KEY = "ORIGIN_FILE_KEY";
static final Component ORIGIN_FILE = builder(FILE, 1)
.setKey(ORIGIN_FILE_KEY)
.setFileAttributes(new FileAttributes(false, XOO_LANGUAGE))
.setFileAttributes(new FileAttributes(false, XOO_LANGUAGE, 1))
.build();

static final String OTHER_FILE_KEY = "OTHER_FILE_KEY";
Expand Down Expand Up @@ -226,7 +226,7 @@ public void add_no_duplication_when_no_duplicated_blocks() {
public void add_duplication_for_java_even_when_no_token() {
Component javaFile = builder(FILE, 1)
.setKey(ORIGIN_FILE_KEY)
.setFileAttributes(new FileAttributes(false, "java"))
.setFileAttributes(new FileAttributes(false, "java", 10))
.build();

Collection<Block> originBlocks = singletonList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.Severity;
import org.sonar.core.issue.DefaultIssue;
import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
import org.sonar.server.computation.task.projectanalysis.component.Component;
import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
import org.sonar.server.computation.task.projectanalysis.component.FileAttributes;
import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
import org.sonar.server.computation.task.projectanalysis.measure.Measure;
import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepositoryRule;
import org.sonar.server.computation.task.projectanalysis.metric.MetricRepositoryRule;
Expand All @@ -46,7 +46,7 @@ public class CommentDensityRuleTest {
static RuleKey RULE_KEY = RuleKey.of(CommonRuleKeys.commonRepositoryForLang("java"), CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY);

static ReportComponent FILE = ReportComponent.builder(Component.Type.FILE, 1)
.setFileAttributes(new FileAttributes(false, "java"))
.setFileAttributes(new FileAttributes(false, "java", 1))
.build();

@Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.junit.Test;
import org.sonar.core.issue.DefaultIssue;
import org.sonar.server.computation.task.projectanalysis.component.Component;
import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
import org.sonar.server.computation.task.projectanalysis.component.FileAttributes;
import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
Expand All @@ -42,7 +42,7 @@ public class CommonRuleEngineImplTest {
public void process_files_with_known_language() throws Exception {
ReportComponent file = ReportComponent.builder(Component.Type.FILE, 1)
.setKey("FILE_KEY").setUuid("FILE_UUID")
.setFileAttributes(new FileAttributes(false, "java"))
.setFileAttributes(new FileAttributes(false, "java", 1))
.build();
DefaultIssue issue = new DefaultIssue();
when(rule1.processFile(file, "java")).thenReturn(issue);
Expand All @@ -56,7 +56,7 @@ public void process_files_with_known_language() throws Exception {
public void do_not_process_files_with_unknown_language() throws Exception {
ReportComponent file = ReportComponent.builder(Component.Type.FILE, 1)
.setKey("FILE_KEY").setUuid("FILE_UUID")
.setFileAttributes(new FileAttributes(false, null))
.setFileAttributes(new FileAttributes(false, null, 1))
.build();

Collection<DefaultIssue> issues = underTest.process(file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.Severity;
import org.sonar.core.issue.DefaultIssue;
import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
import org.sonar.server.computation.task.projectanalysis.component.Component;
import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
import org.sonar.server.computation.task.projectanalysis.component.FileAttributes;
import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
import org.sonar.server.computation.task.projectanalysis.measure.Measure;
import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepositoryRule;
import org.sonar.server.computation.task.projectanalysis.metric.MetricRepositoryRule;
Expand All @@ -43,7 +43,7 @@
public abstract class CoverageRuleTest {

static ReportComponent FILE = ReportComponent.builder(Component.Type.FILE, 1)
.setFileAttributes(new FileAttributes(false, "java"))
.setFileAttributes(new FileAttributes(false, "java", 1))
.build();

@Rule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.Severity;
import org.sonar.core.issue.DefaultIssue;
import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
import org.sonar.server.computation.task.projectanalysis.component.Component;
import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
import org.sonar.server.computation.task.projectanalysis.component.FileAttributes;
import org.sonar.server.computation.task.projectanalysis.component.ReportComponent;
import org.sonar.server.computation.task.projectanalysis.component.TreeRootHolderRule;
import org.sonar.server.computation.task.projectanalysis.measure.Measure;
import org.sonar.server.computation.task.projectanalysis.measure.MeasureRepositoryRule;
import org.sonar.server.computation.task.projectanalysis.metric.MetricRepositoryRule;
Expand All @@ -45,7 +45,7 @@ public class DuplicatedBlockRuleTest {
static RuleKey RULE_KEY = RuleKey.of(CommonRuleKeys.commonRepositoryForLang("java"), CommonRuleKeys.DUPLICATED_BLOCKS);

static ReportComponent FILE = ReportComponent.builder(Component.Type.FILE, 1)
.setFileAttributes(new FileAttributes(false, "java"))
.setFileAttributes(new FileAttributes(false, "java", 1))
.build();

@Rule
Expand Down
Loading

0 comments on commit f3741d7

Please sign in to comment.