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) {
return result;
}
+ @Override
public Collection<Metric> findAll() {
return metricsByKey.values();
}
--- /dev/null
+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);
+ }
+
+}
--- /dev/null
+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());
+ }
+}
--- /dev/null
+<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
/**
* Extension point to purge data.
- * <p/>
- * <p>It is executed when the batch finishes.
- *
+ * <p>
+ * It is executed when the batch finishes.
+ * </p>
+ *
* @since 1.10
* @deprecated since 2.5. The DBCleaner plugin implements all required purge taks.
*/
/**
* Snapshots include the current snapshot (flagged as last) and optionally the penultimate one.
- *
+ *
* @snapshots never null.
*/
void purge(PurgeContext context);
import java.util.Collection;
import java.util.List;
+/**
+ * @deprecated since 2.3
+ */
@Deprecated
public class MeasuresDao {
public MeasuresDao(org.sonar.jpa.dao.MeasuresDao target) {
this.target = target;
}
+
public Metric getMetric(Metric metric) {
return target.getMetric(metric);
}
import java.util.Collections;
import java.util.List;
+/**
+ * @deprecated since 2.3
+ */
@Deprecated
public class RulesDao {
return null;
}
-
public List<RuleParam> getRuleParams() {
return target.getRuleParams();
}
import org.sonar.api.design.DependencyDto;
import org.sonar.api.utils.TimeProfiler;
-import javax.persistence.Query;
import java.util.List;
+import javax.persistence.Query;
+
+/**
+ * @deprecated since 2.5. The DBCleaner plugin implements all required purge taks, but you can extend org.sonar.plugins.dbcleaner.api.Purge
+ */
+@Deprecated
public abstract class AbstractPurge implements Purge {
private static final int MAX_IN_ELEMENTS = 950;
}
protected void deleteDependencies(List<Integer> snapshotIds) {
- executeQuery("delete dependencies", snapshotIds, "delete from " + DependencyDto.class.getSimpleName() + " d where d.fromSnapshotId in (:ids)");
- executeQuery("delete dependencies", snapshotIds, "delete from " + DependencyDto.class.getSimpleName() + " d where d.toSnapshotId in (:ids)");
+ executeQuery("delete dependencies", snapshotIds, "delete from " + DependencyDto.class.getSimpleName()
+ + " d where d.fromSnapshotId in (:ids)");
+ executeQuery("delete dependencies", snapshotIds, "delete from " + DependencyDto.class.getSimpleName()
+ + " d where d.toSnapshotId in (:ids)");
}
/**
* Delete all measures, including MEASURE_DATA
*/
protected void deleteMeasuresBySnapshotId(List<Integer> snapshotIds) {
- executeQuery("delete measures by snapshot id", snapshotIds, "delete from " + MeasureData.class.getSimpleName() + " m where m.snapshotId in (:ids)");
- executeQuery("delete measures by snapshot id", snapshotIds, "delete from " + MeasureModel.class.getSimpleName() + " m where m.snapshotId in (:ids)");
+ executeQuery("delete measures by snapshot id", snapshotIds, "delete from " + MeasureData.class.getSimpleName()
+ + " m where m.snapshotId in (:ids)");
+ executeQuery("delete measures by snapshot id", snapshotIds, "delete from " + MeasureModel.class.getSimpleName()
+ + " m where m.snapshotId in (:ids)");
}
/**
* Delete all measures, including MEASURE_DATA
*/
protected void deleteMeasuresById(List<Integer> measureIds) {
- executeQuery("delete measures by id", measureIds, "delete from " + MeasureData.class.getSimpleName() + " m where m.measure.id in (:ids)");
+ executeQuery("delete measures by id", measureIds, "delete from " + MeasureData.class.getSimpleName()
+ + " m where m.measure.id in (:ids)");
executeQuery("delete measures by id", measureIds, "delete from " + MeasureModel.class.getSimpleName() + " m where m.id in (:ids)");
}
* Delete violations (RULE_FAILURES table)
*/
protected void deleteViolations(List<Integer> snapshotIds) {
- executeQuery("delete violations", snapshotIds, "delete from " + RuleFailureModel.class.getSimpleName() + " e where e.snapshotId in (:ids)");
+ executeQuery("delete violations", snapshotIds, "delete from " + RuleFailureModel.class.getSimpleName()
+ + " e where e.snapshotId in (:ids)");
}
/**
package org.sonar.api.measures;
import org.sonar.api.BatchComponent;
+import org.sonar.api.ServerComponent;
import java.util.Collection;
import java.util.List;
/**
- * This component is currently available only on batch-side
- *
* @since 2.5
*/
-public interface MetricFinder extends BatchComponent {
+public interface MetricFinder extends BatchComponent, ServerComponent {
Metric findById(int id);
if (metric == null) {
messages.addWarningText("Metric '" + metricKey + "' does not exist");
} else {
- Alert alert = new Alert();
- alert.setMetric(metric);
- alert.setOperator(operator);
- alert.setValueError(valueError);
- alert.setValueWarning(valueWarning);
+ Alert alert = new Alert(profile, metric, operator, valueError, valueWarning);
profile.getAlerts().add(alert);
}
}
RulesProfile profile = RulesProfile.create("sonar way", "java");
new XMLProfileSerializer().write(profile, writer);
- assertSimilarXml("/org/sonar/api/profiles/XMLProfileSerializerTest/exportEmptyProfile.xml", writer.toString());
+ assertSimilarXml("exportEmptyProfile.xml", writer.toString());
}
@Test
profile.activateRule(Rule.create("checkstyle", "IllegalRegexp", "illegal regexp"), RulePriority.BLOCKER);
new XMLProfileSerializer().write(profile, writer);
- assertSimilarXml("/org/sonar/api/profiles/XMLProfileSerializerTest/exportProfile.xml", writer.toString());
+ assertSimilarXml("exportProfile.xml", writer.toString());
}
@Test
// the tokens parameter is not set
new XMLProfileSerializer().write(profile, writer);
- assertSimilarXml("/org/sonar/api/profiles/XMLProfileSerializerTest/exportRuleParameters.xml", writer.toString());
+ assertSimilarXml("exportRuleParameters.xml", writer.toString());
}
@Test
profile.getAlerts().add(alert);
new XMLProfileSerializer().write(profile, writer);
- assertSimilarXml("/org/sonar/api/profiles/XMLProfileSerializerTest/exportAlerts.xml", writer.toString());
+ assertSimilarXml("exportAlerts.xml", writer.toString());
}
- public static void assertSimilarXml(String pathToExpectedXml, String xml) throws IOException, SAXException {
+ public static void assertSimilarXml(String fileWithExpectedXml, String xml) throws IOException, SAXException {
+ String pathToExpectedXml = "/org/sonar/api/profiles/XMLProfileSerializerTest/" + fileWithExpectedXml;
InputStream stream = XMLProfileSerializerTest.class.getResourceAsStream(pathToExpectedXml);
try {
Diff diff = isSimilarXml(IOUtils.toString(stream), xml);
import org.sonar.api.utils.HttpDownloader;
import org.sonar.api.utils.IocContainer;
import org.sonar.api.utils.TimeProfiler;
+import org.sonar.core.components.DefaultMetricFinder;
import org.sonar.core.components.DefaultModelFinder;
import org.sonar.core.components.DefaultRuleFinder;
import org.sonar.core.plugin.JpaPluginDao;
private static final Platform INSTANCE = new Platform();
- private MutablePicoContainer rootContainer;//level 1 : only database connectors
- private MutablePicoContainer coreContainer;//level 2 : level 1 + core components
- private MutablePicoContainer servicesContainer;//level 3 : level 2 + plugin extensions + core components that depend on plugin extensions
+ private MutablePicoContainer rootContainer;// level 1 : only database connectors
+ private MutablePicoContainer coreContainer;// level 2 : level 1 + core components
+ private MutablePicoContainer servicesContainer;// level 3 : level 2 + plugin extensions + core components that depend on plugin extensions
private boolean connected = false;
private boolean started = false;
}
}
-
private void startDatabaseConnectors(Configuration configuration) {
rootContainer = IocContainer.buildPicoContainer();
ConfigurationLogger.log(configuration);
servicesContainer.as(Characteristics.CACHE).addComponent(AnnotationRuleParser.class);
servicesContainer.as(Characteristics.CACHE).addComponent(XMLRuleParser.class);
servicesContainer.as(Characteristics.CACHE).addComponent(DefaultRuleFinder.class);
+ servicesContainer.as(Characteristics.CACHE).addComponent(DefaultMetricFinder.class);
servicesContainer.as(Characteristics.CACHE).addComponent(DeprecatedRuleRepositories.class);
servicesContainer.as(Characteristics.CACHE).addComponent(DeprecatedProfiles.class);
servicesContainer.as(Characteristics.CACHE).addComponent(DeprecatedProfileExporters.class);