Skip to content

Commit

Permalink
SONAR-6664 add language to Component and a FileAttributes property
Browse files Browse the repository at this point in the history
FileAttributes property replaces the unitTest property and adds the languageKey
  • Loading branch information
sns-seb committed Jun 26, 2015
1 parent 0cf3bfc commit 4b42677
Show file tree
Hide file tree
Showing 11 changed files with 141 additions and 39 deletions.
Expand Up @@ -69,9 +69,11 @@ public boolean isDeeperThan(Type otherType) {
String getVersion(); String getVersion();


/** /**
* Is this component a unit test or test ? * The attributes of the Component if it's type is File.
*
* @throws IllegalStateException if the Component's type is not {@link Type#FILE}
*/ */
boolean isUnitTest(); FileAttributes getFileAttributes();


List<Component> getChildren(); List<Component> getChildren();


Expand Down
Expand Up @@ -26,15 +26,18 @@
import org.sonar.batch.protocol.Constants; import org.sonar.batch.protocol.Constants;
import org.sonar.batch.protocol.output.BatchReport; import org.sonar.batch.protocol.output.BatchReport;


import static com.google.common.base.Preconditions.checkState;
import static com.google.common.base.Predicates.notNull; import static com.google.common.base.Predicates.notNull;
import static com.google.common.collect.ImmutableList.copyOf; import static com.google.common.collect.ImmutableList.copyOf;
import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.filter;


public class ComponentImpl implements Component { public class ComponentImpl implements Component {
private final Type type; private final Type type;
private final int ref; private final int ref;
private final String name, version; private final String name;
private final boolean isUnitTest; private final String version;
@CheckForNull
private final FileAttributes fileAttributes;
private final List<Component> children; private final List<Component> children;


// Mutable values // Mutable values
Expand All @@ -46,10 +49,21 @@ public ComponentImpl(BatchReport.Component component, @Nullable Iterable<Compone
this.type = convertType(component.getType()); this.type = convertType(component.getType());
this.name = component.getName(); this.name = component.getName();
this.version = component.hasVersion() ? component.getVersion() : null; this.version = component.hasVersion() ? component.getVersion() : null;
this.isUnitTest = component.hasIsTest() && component.getIsTest(); this.fileAttributes = createFileAttributes(component);
this.children = children == null ? Collections.<Component>emptyList() : copyOf(filter(children, notNull())); this.children = children == null ? Collections.<Component>emptyList() : copyOf(filter(children, notNull()));
} }


@CheckForNull
private static FileAttributes createFileAttributes(BatchReport.Component component) {
if (component.getType() != Constants.ComponentType.FILE) {
return null;
}

return new FileAttributes(
component.hasIsTest() && component.getIsTest(),
component.hasLanguage() ? component.getLanguage() : null);
}

public static Type convertType(Constants.ComponentType type) { public static Type convertType(Constants.ComponentType type) {
switch (type) { switch (type) {
case PROJECT: case PROJECT:
Expand Down Expand Up @@ -113,8 +127,9 @@ public ComponentImpl setKey(String key) {
} }


@Override @Override
public boolean isUnitTest() { public FileAttributes getFileAttributes() {
return isUnitTest; checkState(this.type == Type.FILE, "Only component of type FILE have a FileAttributes object");
return this.fileAttributes;
} }


@Override @Override
Expand Down
@@ -0,0 +1,47 @@
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.computation.component;

import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

/**
* The attributes specific to a Component of type {@link org.sonar.server.computation.component.Component.Type#FILE}.
*/
@Immutable
public class FileAttributes {
private final boolean unitTest;
private final String languageKey;

public FileAttributes(boolean unitTest, @Nullable String languageKey) {
this.unitTest = unitTest;
this.languageKey = languageKey;
}

public boolean isUnitTest() {
return unitTest;
}

@CheckForNull
public String getLanguageKey() {
return languageKey;
}
}
Expand Up @@ -271,7 +271,7 @@ private static boolean updateComponent(ComponentDto existingComponent, Component
} }


private static String getFileQualifier(Component component) { private static String getFileQualifier(Component component) {
return component.isUnitTest() ? Qualifiers.UNIT_TEST_FILE : Qualifiers.FILE; return component.getFileAttributes().isUnitTest() ? Qualifiers.UNIT_TEST_FILE : Qualifiers.FILE;
} }


private static Map<String, ComponentDto> componentDtosByKey(List<ComponentDto> components) { private static Map<String, ComponentDto> componentDtosByKey(List<ComponentDto> components) {
Expand Down
Expand Up @@ -163,7 +163,7 @@ private void addToCache(Component component, SnapshotDto snapshotDto) {
} }


private static String getFileQualifier(Component component) { private static String getFileQualifier(Component component) {
return component.isUnitTest() ? Qualifiers.UNIT_TEST_FILE : Qualifiers.FILE; return component.getFileAttributes().isUnitTest() ? Qualifiers.UNIT_TEST_FILE : Qualifiers.FILE;
} }


@Override @Override
Expand Down
Expand Up @@ -115,7 +115,7 @@ public void handleResult(ResultContext context) {


@Override @Override
public void visitFile(Component file) { public void visitFile(Component file) {
if (file.isUnitTest()) { if (file.getFileAttributes().isUnitTest()) {
persistTestResults(file); persistTestResults(file);
} }
} }
Expand Down
Expand Up @@ -20,68 +20,98 @@
package org.sonar.server.computation.component; package org.sonar.server.computation.component;


import java.util.Collections; import java.util.Collections;
import java.util.List;
import org.junit.Test; import org.junit.Test;
import org.sonar.batch.protocol.Constants;
import org.sonar.batch.protocol.output.BatchReport; import org.sonar.batch.protocol.output.BatchReport;


import static com.google.common.base.Predicates.equalTo;
import static com.google.common.base.Predicates.not;
import static com.google.common.collect.FluentIterable.from;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.fail;
import static org.sonar.batch.protocol.Constants.ComponentType;


public class ComponentImplTest { public class ComponentImplTest {


private static final List<Component> EMPTY_CHILD_LIST = Collections.<Component>emptyList();

@Test(expected = NullPointerException.class) @Test(expected = NullPointerException.class)
public void constructor_throws_NPE_if_component_arg_is_Null() { public void constructor_throws_NPE_if_component_arg_is_Null() {
new ComponentImpl(null, null); new ComponentImpl(null, null);
} }


@Test(expected = UnsupportedOperationException.class) @Test(expected = UnsupportedOperationException.class)
public void getUuid_throws_UOE_if_uuid_has_not_been_set_yet() { public void getUuid_throws_UOE_if_uuid_has_not_been_set_yet() {
ComponentImpl component = new ComponentImpl(BatchReport.Component.newBuilder().build(), Collections.<Component>emptyList()); ComponentImpl component = new ComponentImpl(BatchReport.Component.newBuilder().build(), EMPTY_CHILD_LIST);
component.getUuid(); component.getUuid();
} }


@Test(expected = UnsupportedOperationException.class) @Test(expected = UnsupportedOperationException.class)
public void getKey_throws_UOE_if_uuid_has_not_been_set_yet() { public void getKey_throws_UOE_if_uuid_has_not_been_set_yet() {
ComponentImpl component = new ComponentImpl(BatchReport.Component.newBuilder().build(), Collections.<Component>emptyList()); ComponentImpl component = new ComponentImpl(BatchReport.Component.newBuilder().build(), EMPTY_CHILD_LIST);
component.getKey(); component.getKey();
} }


@Test @Test
public void verify_setUuid() { public void verify_setUuid() {
String uuid = "toto"; String uuid = "toto";
ComponentImpl component = new ComponentImpl(BatchReport.Component.newBuilder().build(), Collections.<Component>emptyList()).setUuid(uuid); ComponentImpl component = new ComponentImpl(BatchReport.Component.newBuilder().build(), EMPTY_CHILD_LIST).setUuid(uuid);
assertThat(component.getUuid()).isEqualTo(uuid); assertThat(component.getUuid()).isEqualTo(uuid);
} }


@Test @Test
public void verify_setKey() { public void verify_setKey() {
String key = "toto"; String key = "toto";
ComponentImpl component = new ComponentImpl(BatchReport.Component.newBuilder().build(), Collections.<Component>emptyList()).setKey(key); ComponentImpl component = new ComponentImpl(BatchReport.Component.newBuilder().build(), EMPTY_CHILD_LIST).setKey(key);
assertThat(component.getKey()).isEqualTo(key); assertThat(component.getKey()).isEqualTo(key);
} }


@Test @Test
public void get_name_from_batch_component() throws Exception { public void get_name_from_batch_component() throws Exception {
String name = "project"; String name = "project";
ComponentImpl component = new ComponentImpl(BatchReport.Component.newBuilder().setName(name).build(), Collections.<Component>emptyList()); ComponentImpl component = new ComponentImpl(BatchReport.Component.newBuilder().setName(name).build(), EMPTY_CHILD_LIST);
assertThat(component.getName()).isEqualTo(name); assertThat(component.getName()).isEqualTo(name);
} }


@Test @Test
public void get_version_from_batch_component() throws Exception { public void get_version_from_batch_component() throws Exception {
String version = "1.0"; String version = "1.0";
ComponentImpl component = new ComponentImpl(BatchReport.Component.newBuilder().setVersion(version).build(), Collections.<Component>emptyList()); ComponentImpl component = new ComponentImpl(BatchReport.Component.newBuilder().setVersion(version).build(), EMPTY_CHILD_LIST);
assertThat(component.getVersion()).isEqualTo(version); assertThat(component.getVersion()).isEqualTo(version);
} }


@Test @Test
public void get_is_unit_test_from_batch_component() throws Exception { public void getFileAttributes_throws_ISE_if_BatchComponent_does_not_have_type_FILE() throws Exception {
ComponentImpl component = new ComponentImpl(BatchReport.Component.newBuilder().setIsTest(true).build(), Collections.<Component>emptyList()); for (ComponentType componentType : from(asList(ComponentType.values())).filter(not(equalTo(ComponentType.FILE)))) {
assertThat(component.isUnitTest()).isTrue(); ComponentImpl component = new ComponentImpl(BatchReport.Component.newBuilder().setType(componentType).build(), EMPTY_CHILD_LIST);
try {
component.getFileAttributes();
fail("A IllegalStateException should have been raised");
} catch (IllegalStateException e) {
assertThat(e).hasMessage("Only component of type FILE have a FileAttributes object");
}
}
}

@Test
public void isUnitTest_returns_true_if_IsTest_is_set_in_BatchComponent() throws Exception {
ComponentImpl component = new ComponentImpl(BatchReport.Component.newBuilder().setType(ComponentType.FILE).setIsTest(true).build(), EMPTY_CHILD_LIST);

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

@Test
public void isUnitTest_returns_value_of_language_of_BatchComponent() throws Exception {
String languageKey = "some language key";
ComponentImpl component = new ComponentImpl(BatchReport.Component.newBuilder().setType(ComponentType.FILE).setLanguage(languageKey).build(), EMPTY_CHILD_LIST);

assertThat(component.getFileAttributes().getLanguageKey()).isEqualTo(languageKey);
} }


@Test @Test
public void convertType() { public void convertType() {
for (Constants.ComponentType componentType : Constants.ComponentType.values()) { for (ComponentType componentType : ComponentType.values()) {
assertThat(ComponentImpl.convertType(componentType)).isEqualTo(Component.Type.valueOf(componentType.name())); assertThat(ComponentImpl.convertType(componentType)).isEqualTo(Component.Type.valueOf(componentType.name()));
} }
} }
Expand Down
Expand Up @@ -19,26 +19,29 @@
*/ */
package org.sonar.server.computation.component; package org.sonar.server.computation.component;


import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import javax.annotation.CheckForNull; import javax.annotation.CheckForNull;
import javax.annotation.Nullable; import javax.annotation.Nullable;


import static com.google.common.base.Preconditions.checkState;
import static java.util.Arrays.asList; import static java.util.Arrays.asList;
import static java.util.Objects.requireNonNull;


/** /**
* Implementation of {@link Component} for unit tests. * Implementation of {@link Component} for unit tests.
*/ */
public class DumbComponent implements Component { public class DumbComponent implements Component {


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

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


private final Type type; private final Type type;
private final int ref; private final int ref;
private final String uuid, key, name, version; private final String uuid, key, name, version;
private final boolean isUnitTest; private final FileAttributes fileAttributes;
private final List<Component> children; private final List<Component> children;


private DumbComponent(Builder builder) { private DumbComponent(Builder builder) {
Expand All @@ -48,7 +51,7 @@ private DumbComponent(Builder builder) {
this.key = builder.key; this.key = builder.key;
this.name = builder.name; this.name = builder.name;
this.version = builder.version; this.version = builder.version;
this.isUnitTest = builder.isUnitTest; this.fileAttributes = builder.fileAttributes == null ? DEFAULT_FILE_ATTRIBUTES : builder.fileAttributes;
this.children = ImmutableList.copyOf(builder.children); this.children = ImmutableList.copyOf(builder.children);
} }


Expand Down Expand Up @@ -85,13 +88,14 @@ public String getVersion() {
} }


@Override @Override
public boolean isUnitTest() { public int getRef() {
return isUnitTest; return ref;
} }


@Override @Override
public int getRef() { public FileAttributes getFileAttributes() {
return ref; checkState(this.type == Type.FILE, "Only component of type FILE can have a FileAttributes object");
return this.fileAttributes;
} }


@Override @Override
Expand All @@ -100,7 +104,7 @@ public List<Component> getChildren() {
} }


@Override @Override
public boolean equals(Object o) { public boolean equals(@Nullable Object o) {
if (this == o) { if (this == o) {
return true; return true;
} }
Expand All @@ -124,11 +128,11 @@ public static final class Builder {
private final Type type; private final Type type;
private final int ref; private final int ref;
private String uuid, key, name, version; private String uuid, key, name, version;
private boolean isUnitTest; private FileAttributes fileAttributes;
private final List<Component> children = new ArrayList<>(); private final List<Component> children = new ArrayList<>();


private Builder(Type type, int ref) { private Builder(Type type, int ref) {
Preconditions.checkNotNull(type, "Component type must not be null"); requireNonNull(type, "Component type must not be null");
this.type = type; this.type = type;
this.ref = ref; this.ref = ref;
} }
Expand All @@ -153,8 +157,9 @@ public Builder setVersion(@Nullable String s) {
return this; return this;
} }


public Builder setUnitTest(boolean b){ public Builder setFileAttributes(FileAttributes fileAttributes){
this.isUnitTest = b; checkState(type == Type.FILE, "Only Component of type File can have File attributes");
this.fileAttributes = fileAttributes;
return this; return this;
} }


Expand Down
Expand Up @@ -43,6 +43,7 @@
import org.sonar.server.computation.component.Component; import org.sonar.server.computation.component.Component;
import org.sonar.server.computation.component.DbIdsRepository; import org.sonar.server.computation.component.DbIdsRepository;
import org.sonar.server.computation.component.DumbComponent; import org.sonar.server.computation.component.DumbComponent;
import org.sonar.server.computation.component.FileAttributes;
import org.sonar.server.db.DbClient; import org.sonar.server.db.DbClient;
import org.sonar.test.DbTests; import org.sonar.test.DbTests;


Expand Down Expand Up @@ -271,9 +272,9 @@ public void persist_unit_test() throws Exception {
.build()); .build());


treeRootHolder.setRoot(DumbComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren( treeRootHolder.setRoot(DumbComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(
DumbComponent.builder(Component.Type.DIRECTORY, 2).setUuid("CDEF").setKey(PROJECT_KEY + ":src/test/java/dir").addChildren( DumbComponent.builder(Component.Type.DIRECTORY, 2).setUuid("CDEF").setKey(PROJECT_KEY + ":src/test/java/dir").addChildren(
DumbComponent.builder(Component.Type.FILE, 3).setUuid("DEFG").setKey(PROJECT_KEY + ":src/test/java/dir/FooTest.java").setUnitTest(true).build()) DumbComponent.builder(Component.Type.FILE, 3).setUuid("DEFG").setKey(PROJECT_KEY + ":src/test/java/dir/FooTest.java").setFileAttributes(new FileAttributes(true, null)).build())
.build()) .build())
.build()); .build());


sut.execute(); sut.execute();
Expand Down
Expand Up @@ -45,6 +45,7 @@
import org.sonar.server.computation.component.Component; import org.sonar.server.computation.component.Component;
import org.sonar.server.computation.component.DbIdsRepository; import org.sonar.server.computation.component.DbIdsRepository;
import org.sonar.server.computation.component.DumbComponent; import org.sonar.server.computation.component.DumbComponent;
import org.sonar.server.computation.component.FileAttributes;
import org.sonar.server.computation.period.Period; import org.sonar.server.computation.period.Period;
import org.sonar.server.computation.period.PeriodsHolderRule; import org.sonar.server.computation.period.PeriodsHolderRule;
import org.sonar.server.db.DbClient; import org.sonar.server.db.DbClient;
Expand Down Expand Up @@ -222,7 +223,7 @@ public void persist_unit_test() throws Exception {
dbClient.componentDao().insert(session, fileDto); dbClient.componentDao().insert(session, fileDto);
session.commit(); session.commit();


Component file = DumbComponent.builder(Component.Type.FILE, 3).setUuid("DEFG").setKey(PROJECT_KEY + ":src/main/java/dir/Foo.java").setUnitTest(true).build(); Component file = DumbComponent.builder(Component.Type.FILE, 3).setUuid("DEFG").setKey(PROJECT_KEY + ":src/main/java/dir/Foo.java").setFileAttributes(new FileAttributes(true, null)).build();
Component directory = DumbComponent.builder(Component.Type.DIRECTORY, 2).setUuid("CDEF").setKey(PROJECT_KEY + ":src/main/java/dir").addChildren(file).build(); Component directory = DumbComponent.builder(Component.Type.DIRECTORY, 2).setUuid("CDEF").setKey(PROJECT_KEY + ":src/main/java/dir").addChildren(file).build();
Component project = DumbComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(directory).build(); Component project = DumbComponent.builder(Component.Type.PROJECT, 1).setUuid("ABCD").setKey(PROJECT_KEY).addChildren(directory).build();
treeRootHolder.setRoot(project); treeRootHolder.setRoot(project);
Expand Down

0 comments on commit 4b42677

Please sign in to comment.