diff options
author | Godin <mandrikov@gmail.com> | 2010-12-08 21:30:14 +0000 |
---|---|---|
committer | Godin <mandrikov@gmail.com> | 2010-12-08 21:30:14 +0000 |
commit | 8ea838b32e45f4fb08d1ac3aa8d480a2e0cef7af (patch) | |
tree | a363634e6c11e1602261efa14bf9499fa1adc55c /sonar-core | |
parent | 85ad745aaee19225e0abbb11abd167e39a8b1e95 (diff) | |
download | sonarqube-8ea838b32e45f4fb08d1ac3aa8d480a2e0cef7af.tar.gz sonarqube-8ea838b32e45f4fb08d1ac3aa8d480a2e0cef7af.zip |
* SONAR-1809: MetricFinder should be available on server side
* Fix javadocs
Diffstat (limited to 'sonar-core')
4 files changed, 110 insertions, 6 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/components/CacheMetricFinder.java b/sonar-core/src/main/java/org/sonar/core/components/CacheMetricFinder.java index fa125dc9da1..7b42df06bb7 100644 --- a/sonar-core/src/main/java/org/sonar/core/components/CacheMetricFinder.java +++ b/sonar-core/src/main/java/org/sonar/core/components/CacheMetricFinder.java @@ -22,39 +22,40 @@ package org.sonar.core.components; import com.google.common.collect.Lists; import com.google.common.collect.Maps; import org.sonar.api.measures.Metric; -import org.sonar.api.measures.MetricFinder; import org.sonar.jpa.session.DatabaseSessionFactory; import java.util.Collection; import java.util.List; import java.util.Map; -public final class CacheMetricFinder implements MetricFinder { +public final class CacheMetricFinder extends DefaultMetricFinder { - private DatabaseSessionFactory sessionFactory; private Map<String, Metric> metricsByKey = Maps.newLinkedHashMap(); private Map<Integer, Metric> metricsById = Maps.newLinkedHashMap(); public CacheMetricFinder(DatabaseSessionFactory sessionFactory) { - this.sessionFactory = sessionFactory; + super(sessionFactory); } public void start() { - List<Metric> list = sessionFactory.getSession().getResults(Metric.class, "enabled", true); - for (Metric metric : list) { + Collection<Metric> metrics = doFindAll(); + for (Metric metric : metrics) { metricsByKey.put(metric.getKey(), metric); metricsById.put(metric.getId(), metric); } } + @Override public Metric findById(int metricId) { return metricsById.get(metricId); } + @Override public Metric findByKey(String key) { return metricsByKey.get(key); } + @Override public Collection<Metric> findAll(List<String> metricKeys) { List<Metric> result = Lists.newLinkedList(); for (String metricKey : metricKeys) { @@ -66,6 +67,7 @@ public final class CacheMetricFinder implements MetricFinder { return result; } + @Override public Collection<Metric> findAll() { return metricsByKey.values(); } diff --git a/sonar-core/src/main/java/org/sonar/core/components/DefaultMetricFinder.java b/sonar-core/src/main/java/org/sonar/core/components/DefaultMetricFinder.java new file mode 100644 index 00000000000..cf691878603 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/components/DefaultMetricFinder.java @@ -0,0 +1,46 @@ +package org.sonar.core.components; + +import com.google.common.collect.Lists; +import org.sonar.api.measures.Metric; +import org.sonar.api.measures.MetricFinder; +import org.sonar.jpa.session.DatabaseSessionFactory; + +import java.util.Collection; +import java.util.List; + +public class DefaultMetricFinder implements MetricFinder { + + private DatabaseSessionFactory sessionFactory; + + public DefaultMetricFinder(DatabaseSessionFactory sessionFactory) { + this.sessionFactory = sessionFactory; + } + + public Metric findById(int id) { + return sessionFactory.getSession().getSingleResult(Metric.class, "id", id, "enabled", true); + } + + public Metric findByKey(String key) { + return sessionFactory.getSession().getSingleResult(Metric.class, "key", key, "enabled", true); + } + + public Collection<Metric> findAll(List<String> metricKeys) { + List<Metric> result = Lists.newLinkedList(); + for (String metricKey : metricKeys) { + Metric metric = findByKey(metricKey); + if (metric != null) { + result.add(metric); + } + } + return result; + } + + public Collection<Metric> findAll() { + return doFindAll(); + } + + protected Collection<Metric> doFindAll() { + return sessionFactory.getSession().getResults(Metric.class, "enabled", true); + } + +} diff --git a/sonar-core/src/test/java/org/sonar/core/components/DefaultMetricFinderTest.java b/sonar-core/src/test/java/org/sonar/core/components/DefaultMetricFinderTest.java new file mode 100644 index 00000000000..31dc62eb0ac --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/components/DefaultMetricFinderTest.java @@ -0,0 +1,44 @@ +package org.sonar.core.components; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.jpa.test.AbstractDbUnitTestCase; + +import java.util.Arrays; + +import static org.hamcrest.core.Is.is; +import static org.hamcrest.core.IsNull.nullValue; +import static org.junit.Assert.assertThat; + +public class DefaultMetricFinderTest extends AbstractDbUnitTestCase { + + private DefaultMetricFinder finder; + + @Before + public void setUp() { + setupData("shared"); + finder = new DefaultMetricFinder(getSessionFactory()); + } + + @Test + public void shouldFindAll() { + assertThat(finder.findAll().size(), is(2)); + } + + @Test + public void shouldFindByKeys() { + assertThat(finder.findAll(Arrays.<String> asList("ncloc", "foo", "coverage")).size(), is(2)); + } + + @Test + public void shouldFindById() { + assertThat(finder.findById(1).getKey(), is("ncloc")); + assertThat(finder.findById(3), nullValue()); + } + + @Test + public void shouldFindByKey() { + assertThat(finder.findByKey("ncloc").getKey(), is("ncloc")); + assertThat(finder.findByKey("disabled"), nullValue()); + } +} diff --git a/sonar-core/src/test/resources/org/sonar/core/components/DefaultMetricFinderTest/shared.xml b/sonar-core/src/test/resources/org/sonar/core/components/DefaultMetricFinderTest/shared.xml new file mode 100644 index 00000000000..85709bcbf98 --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/components/DefaultMetricFinderTest/shared.xml @@ -0,0 +1,12 @@ +<dataset> + + <metrics id="1" name="ncloc" VAL_TYPE="INT" DESCRIPTION="[null]" domain="[null]" short_name="" + enabled="true" worst_value="[null]" optimized_best_value="[null]" best_value="[null]" direction="0" + hidden="false"/> + + <metrics id="2" name="coverage" VAL_TYPE="INT" DESCRIPTION="[null]" domain="[null]" short_name="" + enabled="true" worst_value="0" optimized_best_value="true" best_value="100" direction="1" hidden="false"/> + + <metrics id="3" name="disabled" VAL_TYPE="INT" DESCRIPTION="[null]" domain="[null]" short_name="" + enabled="false" worst_value="0" optimized_best_value="true" best_value="100" direction="1" hidden="false"/> +</dataset>
\ No newline at end of file |