]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6620 replace MetricCache by MetricRepository everywhere
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 12 Jun 2015 14:16:34 +0000 (16:16 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 15 Jun 2015 11:07:53 +0000 (13:07 +0200)
and remove MetricCache

server/sonar-server/src/main/java/org/sonar/server/computation/container/ComputeEngineContainerImpl.java
server/sonar-server/src/main/java/org/sonar/server/computation/measure/MetricCache.java [deleted file]
server/sonar-server/src/main/java/org/sonar/server/computation/step/PersistNumberOfDaysSinceLastCommitStep.java
server/sonar-server/src/test/java/org/sonar/server/computation/measure/MetricCacheTest.java [deleted file]
server/sonar-server/src/test/java/org/sonar/server/computation/step/PersistNumberOfDaysSinceLastCommitStepTest.java

index a3f6dc9c0608dd94845086d26456af4c160a7d36..909f2c4aa84a8c0a59513b80d9ccb5e81c6692dc 100644 (file)
@@ -51,7 +51,6 @@ import org.sonar.server.computation.issue.ScmAccountCacheLoader;
 import org.sonar.server.computation.issue.SourceLinesCache;
 import org.sonar.server.computation.language.PlatformLanguageRepository;
 import org.sonar.server.computation.measure.MeasureRepositoryImpl;
-import org.sonar.server.computation.measure.MetricCache;
 import org.sonar.server.computation.metric.MetricRepositoryImpl;
 import org.sonar.server.computation.period.PeriodsHolderImpl;
 import org.sonar.server.computation.qualitygate.QualityGateHolderImpl;
@@ -154,7 +153,6 @@ public class ComputeEngineContainerImpl extends ComponentContainer implements Co
       RuleCache.class,
       RuleCacheLoader.class,
       IssueCache.class,
-      MetricCache.class,
       UpdateConflictResolver.class,
 
       // views
diff --git a/server/sonar-server/src/main/java/org/sonar/server/computation/measure/MetricCache.java b/server/sonar-server/src/main/java/org/sonar/server/computation/measure/MetricCache.java
deleted file mode 100644 (file)
index ac130fd..0000000
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * 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.measure;
-
-import com.google.common.base.Function;
-import com.google.common.collect.Maps;
-import org.sonar.core.metric.db.MetricDto;
-import org.sonar.core.persistence.DbSession;
-import org.sonar.core.persistence.MyBatis;
-import org.sonar.server.db.DbClient;
-import org.sonar.server.exceptions.NotFoundException;
-
-import java.util.List;
-import java.util.Map;
-
-public class MetricCache {
-  private final Map<String, MetricDto> metrics;
-
-  public MetricCache(DbClient dbClient) {
-    DbSession dbSession = dbClient.openSession(false);
-    try {
-      List<MetricDto> metricList = dbClient.metricDao().selectEnabled(dbSession);
-      this.metrics = Maps.uniqueIndex(metricList, new Function<MetricDto, String>() {
-        @Override
-        public String apply(MetricDto metric) {
-          return metric.getKey();
-        }
-      });
-    } finally {
-      MyBatis.closeQuietly(dbSession);
-    }
-  }
-
-  public MetricDto get(String key) {
-    MetricDto metric = metrics.get(key);
-    if (metric == null) {
-      throw new NotFoundException(String.format("Not found: '%s'", key));
-    }
-
-    return metric;
-  }
-}
index bf122a3714cf489110214a21e961f8c7bf26fbe4..2b3866ce74256bfbe53de58d1c077ef83d67f1df 100644 (file)
@@ -34,7 +34,7 @@ import org.sonar.server.computation.component.Component;
 import org.sonar.server.computation.component.DbIdsRepository;
 import org.sonar.server.computation.component.DepthTraversalTypeAwareVisitor;
 import org.sonar.server.computation.component.TreeRootHolder;
-import org.sonar.server.computation.measure.MetricCache;
+import org.sonar.server.computation.metric.MetricRepository;
 import org.sonar.server.db.DbClient;
 import org.sonar.server.source.index.SourceLineIndex;
 
@@ -47,17 +47,17 @@ public class PersistNumberOfDaysSinceLastCommitStep implements ComputationStep {
 
   private final DbClient dbClient;
   private final SourceLineIndex sourceLineIndex;
-  private final MetricCache metricCache;
+  private final MetricRepository metricRepository;
   private final System2 system;
   private final TreeRootHolder treeRootHolder;
   private final BatchReportReader reportReader;
   private final DbIdsRepository dbIdsRepository;
 
-  public PersistNumberOfDaysSinceLastCommitStep(System2 system, DbClient dbClient, SourceLineIndex sourceLineIndex, MetricCache metricCache,
+  public PersistNumberOfDaysSinceLastCommitStep(System2 system, DbClient dbClient, SourceLineIndex sourceLineIndex, MetricRepository metricRepository,
     TreeRootHolder treeRootHolder, BatchReportReader reportReader, DbIdsRepository dbIdsRepository) {
     this.dbClient = dbClient;
     this.sourceLineIndex = sourceLineIndex;
-    this.metricCache = metricCache;
+    this.metricRepository = metricRepository;
     this.system = system;
     this.treeRootHolder = treeRootHolder;
     this.reportReader = reportReader;
@@ -98,7 +98,7 @@ public class PersistNumberOfDaysSinceLastCommitStep implements ComputationStep {
     try {
       dbClient.measureDao().insert(dbSession, new MeasureDto()
         .setValue((double) numberOfDaysSinceLastCommit)
-        .setMetricId(metricCache.get(CoreMetrics.DAYS_SINCE_LAST_COMMIT_KEY).getId())
+        .setMetricId(metricRepository.getByKey(CoreMetrics.DAYS_SINCE_LAST_COMMIT_KEY).getId())
         .setSnapshotId(projectSnapshotId));
       dbSession.commit();
     } finally {
diff --git a/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MetricCacheTest.java b/server/sonar-server/src/test/java/org/sonar/server/computation/measure/MetricCacheTest.java
deleted file mode 100644 (file)
index 8670bb1..0000000
+++ /dev/null
@@ -1,56 +0,0 @@
-/*
- * 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.measure;
-
-import org.junit.Before;
-import org.junit.ClassRule;
-import org.junit.Test;
-import org.sonar.core.persistence.DbTester;
-import org.sonar.server.db.DbClient;
-import org.sonar.server.exceptions.NotFoundException;
-import org.sonar.server.metric.persistence.MetricDao;
-
-import static org.assertj.core.api.Assertions.assertThat;
-
-public class MetricCacheTest {
-
-  @ClassRule
-  public static DbTester db = new DbTester();
-
-  MetricCache sut;
-
-  @Before
-  public void setUp() {
-    db.prepareDbUnit(getClass(), "metrics.xml");
-    sut = new MetricCache(new DbClient(db.database(), db.myBatis(), new MetricDao()));
-  }
-
-  @Test
-  public void cache_give_access_to_enabled_metrics() {
-    assertThat(sut.get("ncloc").getId()).isEqualTo(1);
-    assertThat(sut.get("coverage").getId()).isEqualTo(2);
-  }
-
-  @Test(expected = NotFoundException.class)
-  public void fail_when_metric_not_found() {
-    sut.get("complexity");
-  }
-}
index a7a7e2fc9df17d03c26853b68f55ddf94e181012..169e0fb9576e126f5b5e30466ee0ff5a835d374f 100644 (file)
@@ -29,7 +29,6 @@ import org.sonar.api.config.Settings;
 import org.sonar.api.utils.DateUtils;
 import org.sonar.api.utils.System2;
 import org.sonar.batch.protocol.output.BatchReport;
-import org.sonar.core.metric.db.MetricDto;
 import org.sonar.core.persistence.DbTester;
 import org.sonar.server.computation.batch.BatchReportReaderRule;
 import org.sonar.server.computation.batch.TreeRootHolderRule;
@@ -37,7 +36,9 @@ import org.sonar.server.computation.component.Component;
 import org.sonar.server.computation.component.DbIdsRepository;
 import org.sonar.server.computation.component.DumbComponent;
 import org.sonar.server.computation.language.LanguageRepository;
-import org.sonar.server.computation.measure.MetricCache;
+import org.sonar.server.computation.metric.Metric;
+import org.sonar.server.computation.metric.MetricImpl;
+import org.sonar.server.computation.metric.MetricRepository;
 import org.sonar.server.db.DbClient;
 import org.sonar.server.measure.persistence.MeasureDao;
 import org.sonar.server.source.index.SourceLineIndex;
@@ -63,7 +64,7 @@ public class PersistNumberOfDaysSinceLastCommitStepTest extends BaseStepTest {
 
   DbClient dbClient;
   SourceLineIndex sourceLineIndex;
-  MetricCache metricCache;
+  MetricRepository metricRepository;
   Settings projectSettings;
   LanguageRepository languageRepository;
 
@@ -72,12 +73,12 @@ public class PersistNumberOfDaysSinceLastCommitStepTest extends BaseStepTest {
     db.truncateTables();
     dbClient = new DbClient(db.database(), db.myBatis(), new MeasureDao());
     sourceLineIndex = mock(SourceLineIndex.class);
-    metricCache = mock(MetricCache.class);
+    metricRepository = mock(MetricRepository.class);
     projectSettings = new Settings();
     languageRepository = mock(LanguageRepository.class);
-    when(metricCache.get(anyString())).thenReturn(new MetricDto().setId(10));
+    when(metricRepository.getByKey(anyString())).thenReturn(new MetricImpl(10, "key", "name", Metric.MetricType.STRING));
 
-    sut = new PersistNumberOfDaysSinceLastCommitStep(System2.INSTANCE, dbClient, sourceLineIndex, metricCache, treeRootHolder, reportReader, dbIdsRepository);
+    sut = new PersistNumberOfDaysSinceLastCommitStep(System2.INSTANCE, dbClient, sourceLineIndex, metricRepository, treeRootHolder, reportReader, dbIdsRepository);
   }
 
   @Override