@@ -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(); | |||
} |
@@ -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); | |||
} | |||
} |
@@ -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()); | |||
} | |||
} |
@@ -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> |
@@ -23,9 +23,10 @@ import org.sonar.api.BatchExtension; | |||
/** | |||
* 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. | |||
*/ | |||
@@ -34,7 +35,7 @@ public interface Purge extends BatchExtension { | |||
/** | |||
* Snapshots include the current snapshot (flagged as last) and optionally the penultimate one. | |||
* | |||
* | |||
* @snapshots never null. | |||
*/ | |||
void purge(PurgeContext context); |
@@ -24,6 +24,9 @@ import org.sonar.api.measures.Metric; | |||
import java.util.Collection; | |||
import java.util.List; | |||
/** | |||
* @deprecated since 2.3 | |||
*/ | |||
@Deprecated | |||
public class MeasuresDao { | |||
@@ -32,6 +35,7 @@ public class MeasuresDao { | |||
public MeasuresDao(org.sonar.jpa.dao.MeasuresDao target) { | |||
this.target = target; | |||
} | |||
public Metric getMetric(Metric metric) { | |||
return target.getMetric(metric); | |||
} |
@@ -30,6 +30,9 @@ import org.sonar.api.rules.RulesCategory; | |||
import java.util.Collections; | |||
import java.util.List; | |||
/** | |||
* @deprecated since 2.3 | |||
*/ | |||
@Deprecated | |||
public class RulesDao { | |||
@@ -67,7 +70,6 @@ public class RulesDao { | |||
return null; | |||
} | |||
public List<RuleParam> getRuleParams() { | |||
return target.getRuleParams(); | |||
} |
@@ -25,9 +25,14 @@ import org.sonar.api.database.model.*; | |||
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; | |||
@@ -56,23 +61,28 @@ public abstract class AbstractPurge implements Purge { | |||
} | |||
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)"); | |||
} | |||
@@ -87,7 +97,8 @@ public abstract class AbstractPurge implements Purge { | |||
* 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)"); | |||
} | |||
/** |
@@ -20,16 +20,15 @@ | |||
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); | |||
@@ -209,11 +209,7 @@ public final class XMLProfileParser implements ServerComponent { | |||
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); | |||
} | |||
} |
@@ -45,7 +45,7 @@ public class XMLProfileSerializerTest { | |||
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 | |||
@@ -55,7 +55,7 @@ public class XMLProfileSerializerTest { | |||
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 | |||
@@ -73,7 +73,7 @@ public class XMLProfileSerializerTest { | |||
// 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 | |||
@@ -88,10 +88,11 @@ public class XMLProfileSerializerTest { | |||
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); |
@@ -38,6 +38,7 @@ import org.sonar.api.rules.XMLRuleParser; | |||
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; | |||
@@ -68,9 +69,9 @@ public final class Platform { | |||
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; | |||
@@ -105,7 +106,6 @@ public final class Platform { | |||
} | |||
} | |||
private void startDatabaseConnectors(Configuration configuration) { | |||
rootContainer = IocContainer.buildPicoContainer(); | |||
ConfigurationLogger.log(configuration); | |||
@@ -180,6 +180,7 @@ public final class Platform { | |||
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); |