}
List<String> projectBranches = selectProjectBranches(dbSession, projectUuid);
- long biggestNcloc = selectProjectBiggestNcloc(dbSession, projectBranches);
- String biggestBranch = selectProjectBiggestBranch(dbSession, projectBranches, biggestNcloc);
- PreparedStatement prepareNclocByLanguageStatement = prepareNclocByLanguageStatement(dbSession, biggestBranch);
- rs = prepareNclocByLanguageStatement.executeQuery();
- if (rs.next()) {
- readMeasure(rs, measures);
+ if (!projectBranches.isEmpty()) {
+ long biggestNcloc = selectProjectBiggestNcloc(dbSession, projectBranches);
+ String biggestBranch = selectProjectBiggestBranch(dbSession, projectBranches, biggestNcloc);
+ PreparedStatement prepareNclocByLanguageStatement = prepareNclocByLanguageStatement(dbSession, biggestBranch);
+ rs = prepareNclocByLanguageStatement.executeQuery();
+
+ if (rs.next()) {
+ readMeasure(rs, measures);
+ }
}
return measures;
}
}
- private static List<String> selectProjectBranches(DbSession session, @Nullable String projectUuid) {
+ private static List<String> selectProjectBranches(DbSession session, String projectUuid) {
ResultSet rs = null;
List<String> projectBranches = new ArrayList<>();
- try (PreparedStatement stmt = session.getConnection().prepareStatement(SQL_PROJECT_BRANCHES)){
+ try (PreparedStatement stmt = session.getConnection().prepareStatement(SQL_PROJECT_BRANCHES)) {
AtomicInteger index = new AtomicInteger(1);
stmt.setString(index.getAndIncrement(), projectUuid);
}
if (NCLOC_LANGUAGE_DISTRIBUTION_KEY.equals(metricKey)) {
readTextValue(rs, measures::setNclocByLanguages);
- return;
}
}
import com.google.common.collect.Maps;
import java.util.Map;
+import java.util.function.Consumer;
import javax.annotation.Nullable;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ComponentTesting;
import org.sonar.db.component.SnapshotDto;
import org.sonar.db.measure.ProjectMeasuresIndexerIterator.ProjectMeasures;
import org.sonar.db.metric.MetricDto;
+import org.sonar.db.project.ProjectDto;
+import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Lists.newArrayList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.sonar.api.measures.Metric.ValueType.INT;
import static org.sonar.api.measures.Metric.ValueType.LEVEL;
import static org.sonar.api.measures.Metric.ValueType.STRING;
+import static org.sonar.db.component.ComponentDbTester.defaults;
+import static org.sonar.db.component.ComponentDbTester.toProjectDto;
import static org.sonar.db.component.SnapshotTesting.newAnalysis;
public class ProjectMeasuresIndexerIteratorTest {
.containsOnly(entry("<null>", 4), entry("java", 12), entry("xoo", 36));
}
+ @Test
+ public void return_language_distribution_measure_from_project_with_no_branches() {
+ ComponentDto project = insertComponentAndProject(ComponentTesting.newPublicProjectDto(), false,
+ defaults(), defaults());
+
+ Map<String, ProjectMeasures> docsById = createResultSetAndReturnDocsById();
+
+ assertThat(docsById.get(project.uuid()).getMeasures().getNclocByLanguages())
+ .isEmpty();
+ }
+
@Test
public void does_not_return_none_numeric_metrics() {
ComponentDto project = dbTester.components().insertPrivateProject();
it.close();
return docsById;
}
+
+ private ComponentDto insertComponentAndProject(ComponentDto component, @Nullable Boolean isPrivate,
+ Consumer<ComponentDto> componentDtoPopulator, Consumer<ProjectDto> projectDtoPopulator) {
+ componentDtoPopulator.accept(component);
+ checkState(isPrivate == null || component.isPrivate() == isPrivate, "Illegal modification of private flag");
+ dbClient.componentDao().insert(dbSession, component);
+
+ ProjectDto projectDto = toProjectDto(component, System2.INSTANCE.now());
+ projectDtoPopulator.accept(projectDto);
+ dbClient.projectDao().insert(dbSession, projectDto);
+
+ dbTester.commit();
+ return component;
+ }
}