aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-server
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2015-10-09 11:16:45 +0200
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2015-10-14 11:50:47 +0200
commit1488cd4d3952b65aae551a48814c6feeaad21548 (patch)
tree77a4c0e34d79c69f816f4b319baaeca6ae76c823 /server/sonar-server
parent921920d153fd8f9176e5fffebd5efd5764702cb5 (diff)
downloadsonarqube-1488cd4d3952b65aae551a48814c6feeaad21548.tar.gz
sonarqube-1488cd4d3952b65aae551a48814c6feeaad21548.zip
SONAR-6397 ScmInfoRepository optimisation for non FILE components
when Component is not FILE, no need to neither check the report nor the DB
Diffstat (limited to 'server/sonar-server')
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/computation/scm/ScmInfoRepository.java7
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/computation/scm/ScmInfoRepositoryImpl.java6
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/computation/scm/ScmInfoRepositoryImplTest.java45
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/computation/scm/ScmInfoRepositoryRule.java5
4 files changed, 54 insertions, 9 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/scm/ScmInfoRepository.java b/server/sonar-server/src/main/java/org/sonar/server/computation/scm/ScmInfoRepository.java
index defd8a9712b..002f903759e 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/computation/scm/ScmInfoRepository.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/computation/scm/ScmInfoRepository.java
@@ -31,7 +31,12 @@ public interface ScmInfoRepository {
/**
* Returns Scm info for the specified component if there is any, first looking into the report, then into the database
- * It there's nothing in the report and in the db (on first analysis for instance), then it return a {@link Optional#absent()}.
+ * <p>
+ * If there's nothing in the report and in the db (on first analysis for instance), then it return a {@link Optional#absent()}.
+ * </p>
+ * <p>
+ * This method will always return {@link Optional#absent()} if the specified component's type is not {@link Component.Type#FILE}.
+ * </p>
*
* @throws NullPointerException if argument is {@code null}
*/
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/scm/ScmInfoRepositoryImpl.java b/server/sonar-server/src/main/java/org/sonar/server/computation/scm/ScmInfoRepositoryImpl.java
index b39fc819f20..5733265a838 100644
--- a/server/sonar-server/src/main/java/org/sonar/server/computation/scm/ScmInfoRepositoryImpl.java
+++ b/server/sonar-server/src/main/java/org/sonar/server/computation/scm/ScmInfoRepositoryImpl.java
@@ -65,10 +65,14 @@ public class ScmInfoRepositoryImpl implements ScmInfoRepository {
return;
}
Optional<ScmInfo> scmInfoOptional = getScmInfoForComponent(component);
- scmInfoCache.put(component, scmInfoOptional.isPresent() ? scmInfoOptional.get() : null);
+ scmInfoCache.put(component, scmInfoOptional.orNull());
}
private Optional<ScmInfo> getScmInfoForComponent(Component component) {
+ if (component.getType() != Component.Type.FILE) {
+ return Optional.absent();
+ }
+
BatchReport.Changesets changesets = batchReportReader.readChangesets(component.getReportAttributes().getRef());
if (changesets == null) {
return getScmInfoFromDb(component);
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/scm/ScmInfoRepositoryImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/scm/ScmInfoRepositoryImplTest.java
index 6380d274b2c..e6f10b19dd1 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/computation/scm/ScmInfoRepositoryImplTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/computation/scm/ScmInfoRepositoryImplTest.java
@@ -20,9 +20,14 @@
package org.sonar.server.computation.scm;
+import com.tngtech.java.junit.dataprovider.DataProvider;
+import com.tngtech.java.junit.dataprovider.DataProviderRunner;
+import com.tngtech.java.junit.dataprovider.UseDataProvider;
+import java.util.EnumSet;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
+import org.junit.runner.RunWith;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.log.LogTester;
import org.sonar.batch.protocol.output.BatchReport;
@@ -30,16 +35,23 @@ import org.sonar.db.DbClient;
import org.sonar.db.DbTester;
import org.sonar.db.protobuf.DbFileSources;
import org.sonar.db.source.FileSourceDto;
+import org.sonar.server.computation.analysis.AnalysisMetadataHolder;
import org.sonar.server.computation.analysis.MutableAnalysisMetadataHolderRule;
+import org.sonar.server.computation.batch.BatchReportReader;
import org.sonar.server.computation.batch.BatchReportReaderRule;
import org.sonar.server.computation.component.Component;
+import org.sonar.server.computation.component.ReportComponent;
+import org.sonar.server.computation.component.ViewsComponent;
import org.sonar.server.source.SourceService;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.guava.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.sonar.api.utils.log.LoggerLevel.TRACE;
import static org.sonar.server.computation.component.ReportComponent.builder;
+@RunWith(DataProviderRunner.class)
public class ScmInfoRepositoryImplTest {
static final int FILE_REF = 1;
@@ -48,16 +60,12 @@ public class ScmInfoRepositoryImplTest {
@Rule
public ExpectedException thrown = ExpectedException.none();
-
@Rule
public LogTester logTester = new LogTester();
-
@Rule
public BatchReportReaderRule reportReader = new BatchReportReaderRule();
-
@Rule
public MutableAnalysisMetadataHolderRule analysisMetadataHolder = new MutableAnalysisMetadataHolderRule();
-
@Rule
public DbTester dbTester = DbTester.create(System2.INSTANCE);
@@ -132,6 +140,35 @@ public class ScmInfoRepositoryImplTest {
underTest.getScmInfo(null);
}
+ @DataProvider
+ public static Object[][] allTypeComponentButFile() {
+ Object[][] res = new Object[Component.Type.values().length - 1][1];
+ int i = 0;
+ for (Component.Type type : EnumSet.complementOf(EnumSet.of(Component.Type.FILE))) {
+ if (type.isReportType()) {
+ res[i][0] = ReportComponent.builder(type, i).build();
+ } else {
+ res[i][0] = ViewsComponent.builder(type, i).build();
+ }
+ i++;
+ }
+ return res;
+ }
+
+ @Test
+ @UseDataProvider("allTypeComponentButFile")
+ public void do_not_query_db_nor_report_if_component_type_is_not_FILE(Component component) {
+ BatchReportReader batchReportReader = mock(BatchReportReader.class);
+ AnalysisMetadataHolder analysisMetadataHolder = mock(AnalysisMetadataHolder.class);
+ DbClient dbClient = mock(DbClient.class);
+ SourceService sourceService = mock(SourceService.class);
+ ScmInfoRepositoryImpl underTest = new ScmInfoRepositoryImpl(batchReportReader, analysisMetadataHolder, dbClient, sourceService);
+
+ assertThat(underTest.getScmInfo(component)).isAbsent();
+
+ verifyNoMoreInteractions(batchReportReader, analysisMetadataHolder, dbClient, sourceService);
+ }
+
@Test
public void load_scm_info_from_cache_when_already_read() throws Exception {
analysisMetadataHolder.setIsFirstAnalysis(false);
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/scm/ScmInfoRepositoryRule.java b/server/sonar-server/src/test/java/org/sonar/server/computation/scm/ScmInfoRepositoryRule.java
index 678a3602304..73bb0f1949c 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/computation/scm/ScmInfoRepositoryRule.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/computation/scm/ScmInfoRepositoryRule.java
@@ -21,12 +21,11 @@
package org.sonar.server.computation.scm;
import com.google.common.base.Optional;
-import org.junit.rules.ExternalResource;
-import org.sonar.server.computation.component.Component;
-
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
+import org.junit.rules.ExternalResource;
+import org.sonar.server.computation.component.Component;
import static com.google.common.base.Preconditions.checkNotNull;