ソースを参照

SONAR-249 improve core components to load rules and metrics

tags/2.6
simonbrandhof 13年前
コミット
6ce6cc598f
34個のファイルの変更394行の追加189行の削除
  1. 7
    8
      plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/DifferentialValueDecorator.java
  2. 2
    3
      plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeRuleMeasures.java
  3. 10
    6
      plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FakeRuleFinder.java
  4. 11
    12
      plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsProfileImporterTest.java
  5. 7
    7
      plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsSensorTest.java
  6. 1
    1
      plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/SonarWayWithFindbugsProfileTest.java
  7. 7
    3
      plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileExporterTest.java
  8. 4
    1
      sonar-batch/src/main/java/org/sonar/batch/Batch.java
  9. 21
    4
      sonar-batch/src/main/java/org/sonar/batch/DefaultTimeMachine.java
  10. 1
    3
      sonar-batch/src/main/java/org/sonar/batch/ProjectBatch.java
  11. 1
    1
      sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java
  12. 14
    2
      sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java
  13. 6
    23
      sonar-batch/src/main/java/org/sonar/batch/index/ViolationPersister.java
  14. 2
    1
      sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java
  15. 2
    1
      sonar-batch/src/test/java/org/sonar/batch/index/ViolationPersisterTest.java
  16. 19
    13
      sonar-core/src/main/java/org/sonar/core/components/CacheMetricFinder.java
  17. 74
    0
      sonar-core/src/main/java/org/sonar/core/components/CacheRuleFinder.java
  18. 1
    1
      sonar-core/src/main/java/org/sonar/core/components/DefaultModelFinder.java
  19. 15
    3
      sonar-core/src/main/java/org/sonar/core/components/DefaultRuleFinder.java
  20. 65
    0
      sonar-core/src/test/java/org/sonar/core/components/CacheMetricFinderTest.java
  21. 61
    0
      sonar-core/src/test/java/org/sonar/core/components/CacheRuleFinderTest.java
  22. 2
    1
      sonar-core/src/test/java/org/sonar/core/components/DefaultModelFinderTest.java
  23. 33
    19
      sonar-core/src/test/java/org/sonar/core/components/DefaultRuleFinderTest.java
  24. 2
    5
      sonar-core/src/test/java/org/sonar/core/components/ModelTest.java
  25. 12
    0
      sonar-core/src/test/resources/org/sonar/core/components/CacheMetricFinderTest/shared.xml
  26. 0
    0
      sonar-core/src/test/resources/org/sonar/core/components/DefaultModelFinderTest/shared.xml
  27. 0
    0
      sonar-core/src/test/resources/org/sonar/core/components/DefaultRuleFinderTest/shared.xml
  28. 0
    0
      sonar-core/src/test/resources/org/sonar/core/components/ModelTest/saveModelAndCharacteristics.xml
  29. 0
    0
      sonar-core/src/test/resources/org/sonar/core/components/ModelTest/testGraphOfCharacteristics.xml
  30. 0
    0
      sonar-core/src/test/resources/org/sonar/core/components/ModelTest/testTreeOfCharacteristics.xml
  31. 8
    68
      sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureModel.java
  32. 3
    1
      sonar-plugin-api/src/main/java/org/sonar/api/measures/MetricFinder.java
  33. 1
    0
      sonar-plugin-api/src/main/java/org/sonar/api/rules/RuleFinder.java
  34. 2
    2
      sonar-server/src/main/java/org/sonar/server/platform/Platform.java

+ 7
- 8
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/DifferentialValueDecorator.java ファイルの表示

@@ -31,7 +31,6 @@ import org.sonar.api.qualitymodel.Characteristic;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.resources.ResourceUtils;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RulePriority;

import java.util.Collection;
@@ -142,7 +141,7 @@ public class DifferentialValueDecorator implements Decorator {
// improvements : keep query in cache ? select only some columns ?
// TODO support measure on rules and characteristics
String hql = "select m from " + MeasureModel.class.getSimpleName() + " m, " + Snapshot.class.getSimpleName() + " s " +
"where m.snapshotId=s.id and m.metricId in (:metricIds) and m.rule=null and m.rulePriority=null and m.rulesCategoryId=null and m.characteristic=null "
"where m.snapshotId=s.id and m.metricId in (:metricIds) and m.ruleId=null and m.rulePriority=null and m.rulesCategoryId=null and m.characteristic=null "
+ "and (s.rootId=:rootSnapshotId or s.id=:rootSnapshotId) and s.resourceId=:resourceId and s.status=:status";
return session.createQuery(hql)
.setParameter("metricIds", metricByIds.keySet())
@@ -159,14 +158,14 @@ public class DifferentialValueDecorator implements Decorator {

static class MeasureKey {
Integer metricId;
Rule rule;
Integer ruleId;
Integer categoryId;
RulePriority priority;
Characteristic characteristic;

MeasureKey(MeasureModel model) {
this.metricId = model.getMetricId();
rule = model.getRule();
metricId = model.getMetricId();
ruleId = model.getRuleId();
categoryId = model.getRulesCategoryId();
priority = model.getRulePriority();
characteristic = model.getCharacteristic();
@@ -179,7 +178,7 @@ public class DifferentialValueDecorator implements Decorator {
if (measure instanceof RuleMeasure) {
RuleMeasure rm = (RuleMeasure) measure;
categoryId = rm.getRuleCategory();
rule = rm.getRule();
ruleId = (rm.getRule()==null ? null : rm.getRule().getId());
priority = rm.getRulePriority();
}
}
@@ -196,7 +195,7 @@ public class DifferentialValueDecorator implements Decorator {
return false;
if (!metricId.equals(that.metricId)) return false;
if (priority != that.priority) return false;
if (rule != null ? !rule.equals(that.rule) : that.rule != null) return false;
if (ruleId != null ? !ruleId.equals(that.ruleId) : that.ruleId != null) return false;

return true;
}
@@ -204,7 +203,7 @@ public class DifferentialValueDecorator implements Decorator {
@Override
public int hashCode() {
int result = metricId.hashCode();
result = 31 * result + (rule != null ? rule.hashCode() : 0);
result = 31 * result + (ruleId != null ? ruleId.hashCode() : 0);
result = 31 * result + (categoryId != null ? categoryId.hashCode() : 0);
result = 31 * result + (priority != null ? priority.hashCode() : 0);
result = 31 * result + (characteristic != null ? characteristic.hashCode() : 0);

+ 2
- 3
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeRuleMeasures.java ファイルの表示

@@ -26,9 +26,8 @@ import org.sonar.plugins.dbcleaner.api.Purge;
import org.sonar.plugins.dbcleaner.api.PurgeContext;
import org.sonar.plugins.dbcleaner.api.PurgeUtils;

import java.util.List;

import javax.persistence.Query;
import java.util.List;

/**
* see SONAR-522
@@ -49,7 +48,7 @@ public final class PurgeRuleMeasures extends Purge {

private void purge(Integer sid) {
Query query = getSession().createQuery("SELECT m.id FROM " + MeasureModel.class.getSimpleName() + " m, " + Snapshot.class.getSimpleName() + " s WHERE s.id = m.snapshotId and " +
"(s.rootId=:rootSid OR s.id=:rootSid) and (m.rule is not null or m.rulesCategoryId is not null or m.rulePriority is not null)");
"(s.rootId=:rootSid OR s.id=:rootSid) and (m.ruleId is not null or m.rulesCategoryId is not null or m.rulePriority is not null)");
query.setParameter("rootSid", sid);
List<Integer> measureIds = query.getResultList();
PurgeUtils.deleteMeasuresById(getSession(), measureIds);

plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleFinder.java → plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FakeRuleFinder.java ファイルの表示

@@ -19,26 +19,30 @@
*/
package org.sonar.plugins.findbugs;

import java.util.Collection;
import java.util.List;

import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
import org.sonar.api.rules.RuleQuery;
import org.sonar.api.rules.XMLRuleParser;

public class FindbugsRuleFinder implements RuleFinder {
import java.util.Collection;
import java.util.List;

public class FakeRuleFinder implements RuleFinder {

private final List<Rule> findbugsRules;

public FindbugsRuleFinder() {
public FakeRuleFinder() {
FindbugsRuleRepository repo = new FindbugsRuleRepository(new XMLRuleParser());
findbugsRules = repo.createRules();
for(Rule rule : findbugsRules){
for (Rule rule : findbugsRules) {
rule.setRepositoryKey(FindbugsConstants.REPOSITORY_KEY);
}
}

public Rule findById(int ruleId) {
throw new UnsupportedOperationException();
}

public Rule findByKey(String repositoryKey, String key) {
for (Rule rule : findbugsRules) {
if (rule.getKey().equals(key)) {

+ 11
- 12
plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsProfileImporterTest.java ファイルの表示

@@ -19,16 +19,7 @@
*/
package org.sonar.plugins.findbugs;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.List;

import com.thoughtworks.xstream.XStream;
import org.apache.commons.io.IOUtils;
import org.junit.Test;
import org.sonar.api.profiles.RulesProfile;
@@ -38,11 +29,19 @@ import org.sonar.plugins.findbugs.xml.FindBugsFilter;
import org.sonar.plugins.findbugs.xml.Match;
import org.sonar.test.TestUtils;

import com.thoughtworks.xstream.XStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.List;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

public class FindbugsProfileImporterTest {

private FindbugsProfileImporter importer = new FindbugsProfileImporter(new FindbugsRuleFinder());
private FindbugsProfileImporter importer = new FindbugsProfileImporter(new FakeRuleFinder());

@Test
public void shouldImportPatterns() throws IOException {

+ 7
- 7
plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsSensorTest.java ファイルの表示

@@ -48,14 +48,14 @@ public class FindbugsSensorTest extends FindbugsTests {

@Test
public void shouldExecuteWhenSomeRulesAreActive() throws Exception {
FindbugsSensor sensor = new FindbugsSensor(createRulesProfileWithActiveRules(), new FindbugsRuleFinder(), null);
FindbugsSensor sensor = new FindbugsSensor(createRulesProfileWithActiveRules(), new FakeRuleFinder(), null);
Project project = createProject();
assertTrue(sensor.shouldExecuteOnProject(project));
}

@Test
public void shouldExecuteWhenReuseExistingRulesConfig() throws Exception {
FindbugsSensor analyser = new FindbugsSensor(RulesProfile.create(), new FindbugsRuleFinder(), null);
FindbugsSensor analyser = new FindbugsSensor(RulesProfile.create(), new FakeRuleFinder(), null);
Project pom = createProject();
when(pom.getReuseExistingRulesConfig()).thenReturn(true);
assertTrue(analyser.shouldExecuteOnProject(pom));
@@ -63,7 +63,7 @@ public class FindbugsSensorTest extends FindbugsTests {

@Test
public void shouldNotExecuteWhenNoRulesAreActive() throws Exception {
FindbugsSensor analyser = new FindbugsSensor(RulesProfile.create(), new FindbugsRuleFinder(), null);
FindbugsSensor analyser = new FindbugsSensor(RulesProfile.create(), new FakeRuleFinder(), null);
Project pom = createProject();
assertFalse(analyser.shouldExecuteOnProject(pom));
}
@@ -72,7 +72,7 @@ public class FindbugsSensorTest extends FindbugsTests {
public void shouldNotExecuteOnEar() {
Project project = createProject();
when(project.getPom().getPackaging()).thenReturn("ear");
FindbugsSensor analyser = new FindbugsSensor(createRulesProfileWithActiveRules(), new FindbugsRuleFinder(), null);
FindbugsSensor analyser = new FindbugsSensor(createRulesProfileWithActiveRules(), new FakeRuleFinder(), null);
assertFalse(analyser.shouldExecuteOnProject(project));
}

@@ -88,7 +88,7 @@ public class FindbugsSensorTest extends FindbugsTests {
when(executor.execute()).thenReturn(xmlFile);
when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.MyClass"));

FindbugsSensor analyser = new FindbugsSensor(createRulesProfileWithActiveRules(), new FindbugsRuleFinder(), executor);
FindbugsSensor analyser = new FindbugsSensor(createRulesProfileWithActiveRules(), new FakeRuleFinder(), executor);
analyser.analyse(project, context);

verify(executor).execute();
@@ -116,7 +116,7 @@ public class FindbugsSensorTest extends FindbugsTests {
when(project.getConfiguration()).thenReturn(conf);
when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.MyClass"));

FindbugsSensor analyser = new FindbugsSensor(createRulesProfileWithActiveRules(), new FindbugsRuleFinder(), executor);
FindbugsSensor analyser = new FindbugsSensor(createRulesProfileWithActiveRules(), new FakeRuleFinder(), executor);
analyser.analyse(project, context);

verify(executor, never()).execute();
@@ -144,7 +144,7 @@ public class FindbugsSensorTest extends FindbugsTests {
when(project.getConfiguration()).thenReturn(conf);
when(context.getResource(any(Resource.class))).thenReturn(new JavaFile("org.sonar.MyClass"));

FindbugsSensor analyser = new FindbugsSensor(createRulesProfileWithActiveRules(), new FindbugsRuleFinder(), executor);
FindbugsSensor analyser = new FindbugsSensor(createRulesProfileWithActiveRules(), new FakeRuleFinder(), executor);
analyser.analyse(project, context);

verify(context, never()).saveViolation(any(Violation.class));

+ 1
- 1
plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/SonarWayWithFindbugsProfileTest.java ファイルの表示

@@ -31,7 +31,7 @@ public class SonarWayWithFindbugsProfileTest {

@Test
public void shouldCreateProfile() {
FindbugsProfileImporter importer = new FindbugsProfileImporter(new FindbugsRuleFinder());
FindbugsProfileImporter importer = new FindbugsProfileImporter(new FakeRuleFinder());
SonarWayWithFindbugsProfile sonarWayWithFindbugs = new SonarWayWithFindbugsProfile(importer);
ValidationMessages validation = ValidationMessages.create();
RulesProfile profile = sonarWayWithFindbugs.createProfile(validation);

+ 7
- 3
plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileExporterTest.java ファイルの表示

@@ -35,7 +35,7 @@ public class PmdProfileExporterTest {
PmdRuleRepository repository = new PmdRuleRepository(fileSystem, new XMLRuleParser());
List<Rule> rules = repository.createRules();

RuleFinder ruleFinder = new PmdRuleFinder(rules);
RuleFinder ruleFinder = new FakeRuleFinder(rules);
PmdProfileImporter importer = new PmdProfileImporter(ruleFinder);
Reader reader = new StringReader(TestUtils.getResourceContent("/org/sonar/plugins/pmd/simple.xml"));
RulesProfile rulesProfile = importer.importProfile(reader, ValidationMessages.create());
@@ -103,14 +103,18 @@ public class PmdProfileExporterTest {
assertThat(rule.getProperty(PmdConstants.XPATH_EXPRESSION_PARAM).getValue(), is(xpathExpression));
}

private static class PmdRuleFinder implements RuleFinder {
private static class FakeRuleFinder implements RuleFinder {

private List<Rule> rules;

public PmdRuleFinder(List<Rule> rules) {
public FakeRuleFinder(List<Rule> rules) {
this.rules = rules;
}

public Rule findById(int ruleId) {
throw new UnsupportedOperationException();
}

public Rule findByKey(String repositoryKey, String key) {
throw new UnsupportedOperationException();
}

+ 4
- 1
sonar-batch/src/main/java/org/sonar/batch/Batch.java ファイルの表示

@@ -30,6 +30,8 @@ import org.sonar.api.utils.HttpDownloader;
import org.sonar.api.utils.IocContainer;
import org.sonar.api.utils.ServerHttpClient;
import org.sonar.batch.index.*;
import org.sonar.core.components.CacheMetricFinder;
import org.sonar.core.components.CacheRuleFinder;
import org.sonar.core.plugin.JpaPluginDao;
import org.sonar.jpa.dao.MeasuresDao;
import org.sonar.jpa.session.DatabaseSessionProvider;
@@ -71,7 +73,6 @@ public class Batch {
batchContainer.as(Characteristics.CACHE).addComponent(ServerMetadata.class);
batchContainer.as(Characteristics.CACHE).addComponent(ProjectTree.class);
batchContainer.as(Characteristics.CACHE).addComponent(DefaultResourceCreationLock.class);
batchContainer.as(Characteristics.CACHE).addComponent(DefaultMetricFinder.class);
batchContainer.as(Characteristics.CACHE).addComponent(DefaultIndex.class);
batchContainer.as(Characteristics.CACHE).addComponent(DefaultPersistenceManager.class);
batchContainer.as(Characteristics.CACHE).addComponent(DependencyPersister.class);
@@ -87,6 +88,8 @@ public class Batch {
batchContainer.as(Characteristics.CACHE).addComponent(ServerHttpClient.class);
batchContainer.as(Characteristics.CACHE).addComponent(HttpDownloader.class);
batchContainer.as(Characteristics.CACHE).addComponent(MeasuresDao.class);
batchContainer.as(Characteristics.CACHE).addComponent(CacheRuleFinder.class);
batchContainer.as(Characteristics.CACHE).addComponent(CacheMetricFinder.class);
batchContainer.start();

ProjectTree projectTree = batchContainer.getComponent(ProjectTree.class);

+ 21
- 4
sonar-batch/src/main/java/org/sonar/batch/DefaultTimeMachine.java ファイルの表示

@@ -31,10 +31,9 @@ import org.sonar.api.measures.Metric;
import org.sonar.api.measures.MetricFinder;
import org.sonar.api.resources.Resource;
import org.sonar.batch.index.DefaultIndex;
import org.sonar.jpa.dao.MeasuresDao;

import java.util.*;
import javax.persistence.Query;
import java.util.*;

public class DefaultTimeMachine implements TimeMachine {

@@ -56,7 +55,7 @@ public class DefaultTimeMachine implements TimeMachine {

for (Object[] object : objects) {
MeasureModel model = (MeasureModel) object[0];
Measure measure = model.toMeasure(metricById.get(model.getMetricId()));
Measure measure = toMeasure(model, metricById.get(model.getMetricId()));
measure.setDate((Date) object[1]);
result.add(measure);
}
@@ -93,7 +92,7 @@ public class DefaultTimeMachine implements TimeMachine {
params.put("resourceId", resource.getId());
params.put("status", Snapshot.STATUS_PROCESSED);

sb.append(" AND m.rule IS NULL AND m.rulePriority IS NULL AND m.rulesCategoryId IS NULL ");
sb.append(" AND m.ruleId IS NULL AND m.rulePriority IS NULL AND m.rulesCategoryId IS NULL ");
if (!metricIds.isEmpty()) {
sb.append(" AND m.metricId IN (:metricIds) ");
params.put("metricIds", metricIds);
@@ -136,4 +135,22 @@ public class DefaultTimeMachine implements TimeMachine {
}
return result;
}

static Measure toMeasure(MeasureModel model, Metric metric) {
// NOTE: measures on rule are not supported
Measure measure = new Measure(metric);
measure.setId(model.getId());
measure.setDescription(model.getDescription());
measure.setValue(model.getValue());
measure.setData(model.getData(metric));
measure.setAlertStatus(model.getAlertStatus());
measure.setAlertText(model.getAlertText());
measure.setTendency(model.getTendency());
measure.setDiffValue1(model.getDiffValue1());
measure.setDiffValue2(model.getDiffValue2());
measure.setDiffValue3(model.getDiffValue3());
measure.setUrl(model.getUrl());
measure.setCharacteristic(model.getCharacteristic());
return measure;
}
}

+ 1
- 3
sonar-batch/src/main/java/org/sonar/batch/ProjectBatch.java ファイルの表示

@@ -38,8 +38,7 @@ import org.sonar.api.utils.SonarException;
import org.sonar.batch.index.DefaultIndex;
import org.sonar.batch.index.DefaultResourcePersister;
import org.sonar.batch.phases.Phases;
import org.sonar.core.qualitymodel.DefaultModelFinder;
import org.sonar.core.rule.DefaultRuleFinder;
import org.sonar.core.components.DefaultModelFinder;
import org.sonar.jpa.dao.*;

import java.util.List;
@@ -93,7 +92,6 @@ public class ProjectBatch {
batchContainer.as(Characteristics.CACHE).addComponent(ViolationFilters.class);
batchContainer.as(Characteristics.CACHE).addComponent(ResourceFilters.class);
batchContainer.as(Characteristics.CACHE).addComponent(DefaultModelFinder.class);
batchContainer.as(Characteristics.CACHE).addComponent(DefaultRuleFinder.class);
batchContainer.addAdapter(new ProfileProvider());
batchContainer.addAdapter(new CheckProfileProvider());
loadCoreComponents(batchContainer);

+ 1
- 1
sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java ファイルの表示

@@ -238,7 +238,7 @@ public final class DefaultIndex extends SonarIndex {
public Measure addMeasure(Resource resource, Measure measure) {
Bucket bucket = getOrAddBucket(resource);
if (!bucket.isExcluded()) {
Metric metric = metricFinder.find(measure.getMetricKey());
Metric metric = metricFinder.findByKey(measure.getMetricKey());
if (metric == null) {
throw new SonarException("Unknown metric: " + measure.getMetricKey());
}

+ 14
- 2
sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java ファイルの表示

@@ -32,6 +32,9 @@ import org.sonar.api.measures.RuleMeasure;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.resources.ResourceUtils;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
import org.sonar.api.utils.SonarException;

import java.util.Collection;
import java.util.Map;
@@ -42,10 +45,12 @@ public final class MeasurePersister {
private SetMultimap<Resource, Measure> unsavedMeasuresByResource = LinkedHashMultimap.create();
private DatabaseSession session;
private ResourcePersister resourcePersister;
private RuleFinder ruleFinder;

public MeasurePersister(DatabaseSession session, ResourcePersister resourcePersister) {
public MeasurePersister(DatabaseSession session, ResourcePersister resourcePersister, RuleFinder ruleFinder) {
this.session = session;
this.resourcePersister = resourcePersister;
this.ruleFinder = ruleFinder;
}

public void setDelayedMode(boolean delayedMode) {
@@ -136,7 +141,14 @@ public final class MeasurePersister {
RuleMeasure ruleMeasure = (RuleMeasure) measure;
merge.setRulesCategoryId(ruleMeasure.getRuleCategory());
merge.setRulePriority(ruleMeasure.getRulePriority());
merge.setRule(ruleMeasure.getRule());
if (ruleMeasure.getRule()!=null) {
Rule ruleWithId = ruleFinder.findByKey(ruleMeasure.getRule().getRepositoryKey(), ruleMeasure.getRule().getKey());
if (ruleWithId!=null) {
merge.setRuleId(ruleWithId.getId());
} else {
throw new SonarException("Can not save a measure with unknown rule " + ruleMeasure);
}
}
}
return merge;
}

+ 6
- 23
sonar-batch/src/main/java/org/sonar/batch/index/ViolationPersister.java ファイルの表示

@@ -19,29 +19,28 @@
*/
package org.sonar.batch.index;

import com.google.common.collect.Maps;
import org.sonar.api.database.DatabaseSession;
import org.sonar.api.database.model.RuleFailureModel;
import org.sonar.api.database.model.Snapshot;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
import org.sonar.api.rules.Violation;
import org.sonar.api.utils.SonarException;

import java.util.Collections;
import java.util.List;
import java.util.Map;

public final class ViolationPersister {

private DatabaseSession session;
private ResourcePersister resourcePersister;
private Map<Rule, Integer> ruleIds = Maps.newHashMap();
private RuleFinder ruleFinder;

public ViolationPersister(DatabaseSession session, ResourcePersister resourcePersister) {
public ViolationPersister(DatabaseSession session, ResourcePersister resourcePersister, RuleFinder ruleFinder) {
this.session = session;
this.resourcePersister = resourcePersister;
this.ruleFinder = ruleFinder;
}

public List<RuleFailureModel> getPreviousViolations(Resource resource) {
@@ -76,28 +75,12 @@ public final class ViolationPersister {
}

private RuleFailureModel mergeModel(Violation violation, RuleFailureModel merge) {
merge.setRuleId(getRuleId(violation.getRule()));
Rule rule = ruleFinder.findByKey(violation.getRule().getRepositoryKey(), violation.getRule().getKey());
merge.setRuleId(rule.getId());
merge.setPriority(violation.getPriority());
merge.setLine(violation.getLineId());
merge.setMessage(violation.getMessage());
merge.setCost(violation.getCost());
return merge;
}

private Integer getRuleId(Rule rule) {
Integer ruleId = ruleIds.get(rule);
if (ruleId == null) {
ruleId = rule.getId();
if (ruleId == null) {
Rule persistedRule = session.getSingleResult(Rule.class,
"pluginName", rule.getRepositoryKey(), "key", rule.getKey(), "enabled", true);
if (persistedRule == null) {
throw new SonarException("Rule not found: " + rule);
}
ruleId = persistedRule.getId();
}
ruleIds.put(rule, ruleId);
}
return ruleId;
}
}

+ 2
- 1
sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java ファイルの表示

@@ -31,6 +31,7 @@ import org.sonar.api.measures.PersistenceMode;
import org.sonar.api.resources.JavaFile;
import org.sonar.api.resources.JavaPackage;
import org.sonar.api.resources.Project;
import org.sonar.core.components.DefaultRuleFinder;
import org.sonar.jpa.test.AbstractDbUnitTestCase;

import java.util.List;
@@ -63,7 +64,7 @@ public class MeasurePersisterTest extends AbstractDbUnitTestCase {
when(resourcePersister.saveResource((Project) anyObject(), eq(aPackage))).thenReturn(packageSnapshot);
when(resourcePersister.getSnapshot(project)).thenReturn(projectSnapshot);
when(resourcePersister.getSnapshot(aPackage)).thenReturn(packageSnapshot);
measurePersister = new MeasurePersister(getSession(), resourcePersister);
measurePersister = new MeasurePersister(getSession(), resourcePersister, new DefaultRuleFinder(getSessionFactory()));
}

@Test

+ 2
- 1
sonar-batch/src/test/java/org/sonar/batch/index/ViolationPersisterTest.java ファイルの表示

@@ -28,6 +28,7 @@ import org.sonar.api.resources.Project;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RulePriority;
import org.sonar.api.rules.Violation;
import org.sonar.core.components.DefaultRuleFinder;
import org.sonar.jpa.test.AbstractDbUnitTestCase;

import java.util.List;
@@ -55,7 +56,7 @@ public class ViolationPersisterTest extends AbstractDbUnitTestCase {
when(resourcePersister.saveResource((Project) anyObject(), eq(javaFile))).thenReturn(snapshot);
when(resourcePersister.getPreviousLastSnapshot(snapshot)).thenReturn(snapshot);
when(resourcePersister.getSnapshot(javaFile)).thenReturn(snapshot);
violationPersister = new ViolationPersister(getSession(), resourcePersister);
violationPersister = new ViolationPersister(getSession(), resourcePersister, new DefaultRuleFinder(getSessionFactory()));
}

@Test

sonar-batch/src/main/java/org/sonar/batch/DefaultMetricFinder.java → sonar-core/src/main/java/org/sonar/core/components/CacheMetricFinder.java ファイルの表示

@@ -17,42 +17,48 @@
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.batch;
package org.sonar.core.components;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import org.sonar.api.database.DatabaseSession;
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 DefaultMetricFinder implements MetricFinder {
public final class CacheMetricFinder implements MetricFinder {

private DatabaseSession session;
private Map<String, Metric> metrics = Maps.newHashMap();
private DatabaseSessionFactory sessionFactory;
private Map<String, Metric> metricsByKey = Maps.newLinkedHashMap();
private Map<Integer, Metric> metricsById = Maps.newLinkedHashMap();

public DefaultMetricFinder(DatabaseSession session) {
this.session = session;
public CacheMetricFinder(DatabaseSessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}

public void start() {
List<Metric> list = session.getResults(Metric.class, "enabled", true);
List<Metric> list = sessionFactory.getSession().getResults(Metric.class, "enabled", true);
for (Metric metric : list) {
metrics.put(metric.getKey(), metric);
metricsByKey.put(metric.getKey(), metric);
metricsById.put(metric.getId(), metric);
}
}

public Metric find(String key) {
return metrics.get(key);
public Metric findById(int metricId) {
return metricsById.get(metricId);
}

public Metric findByKey(String key) {
return metricsByKey.get(key);
}

public Collection<Metric> findAll(List<String> metricKeys) {
List<Metric> result = Lists.newLinkedList();
for (String metricKey : metricKeys) {
Metric metric = find(metricKey);
Metric metric = findByKey(metricKey);
if (metric != null) {
result.add(metric);
}
@@ -61,6 +67,6 @@ public final class DefaultMetricFinder implements MetricFinder {
}

public Collection<Metric> findAll() {
return metrics.values();
return metricsByKey.values();
}
}

+ 74
- 0
sonar-core/src/main/java/org/sonar/core/components/CacheRuleFinder.java ファイルの表示

@@ -0,0 +1,74 @@
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2009 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* Sonar 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.
*
* Sonar 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 Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.core.components;

import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.Maps;
import org.sonar.api.rules.Rule;
import org.sonar.jpa.session.DatabaseSessionFactory;

import java.util.Map;

public final class CacheRuleFinder extends DefaultRuleFinder {

private BiMap<Integer, Rule> rulesById = HashBiMap.create();
private Map<String, Map<String, Rule>> rulesByKey = Maps.newHashMap();

public CacheRuleFinder(DatabaseSessionFactory sessionFactory) {
super(sessionFactory);
}

@Override
public Rule findById(int ruleId) {
Rule rule = rulesById.get(ruleId);
if (rule==null) {
rule = doFindById(ruleId);
addToCache(rule);
}
return rule;
}

@Override
public Rule findByKey(String repositoryKey, String key) {
Map<String,Rule> repoRules = rulesByKey.get(repositoryKey);
Rule rule = null;
if (repoRules!=null) {
rule = repoRules.get(key);
}
if (rule == null) {
rule = doFindByKey(repositoryKey, key);
addToCache(rule);
}
return rule;
}

private void addToCache(Rule rule) {
if (rule != null) {
rulesById.put(rule.getId(), rule);
Map<String, Rule> repoRules = rulesByKey.get(rule.getKey());
if (repoRules==null) {
repoRules = Maps.newHashMap();
rulesByKey.put(rule.getRepositoryKey(), repoRules);
}
repoRules.put(rule.getKey(), rule);
}
}
}

sonar-core/src/main/java/org/sonar/core/qualitymodel/DefaultModelFinder.java → sonar-core/src/main/java/org/sonar/core/components/DefaultModelFinder.java ファイルの表示

@@ -17,7 +17,7 @@
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.core.qualitymodel;
package org.sonar.core.components;

import org.sonar.api.database.DatabaseSession;
import org.sonar.api.qualitymodel.Model;

sonar-core/src/main/java/org/sonar/core/rule/DefaultRuleFinder.java → sonar-core/src/main/java/org/sonar/core/components/DefaultRuleFinder.java ファイルの表示

@@ -17,7 +17,7 @@
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.core.rule;
package org.sonar.core.components;

import org.apache.commons.lang.StringUtils;
import org.sonar.api.database.DatabaseSession;
@@ -39,17 +39,29 @@ public class DefaultRuleFinder implements RuleFinder {
this.sessionFactory = sessionFactory;
}

public Rule findById(int ruleId) {
return doFindById(ruleId);
}

protected final Rule doFindById(int ruleId) {
return sessionFactory.getSession().getSingleResult(Rule.class, "id", ruleId, "enabled", true);
}

public Rule findByKey(String repositoryKey, String key) {
return doFindByKey(repositoryKey, key);
}

protected final Rule doFindByKey(String repositoryKey, String key) {
return sessionFactory.getSession().getSingleResult(Rule.class, "pluginName", repositoryKey, "key", key, "enabled", true);
}

public Rule find(RuleQuery query) {
public final Rule find(RuleQuery query) {
DatabaseSession session = sessionFactory.getSession();
return (Rule)session.getSingleResult(createHqlQuery(session, query), null);

}

public Collection<Rule> findAll(RuleQuery query) {
public final Collection<Rule> findAll(RuleQuery query) {
DatabaseSession session = sessionFactory.getSession();
return createHqlQuery(session, query).getResultList();
}

+ 65
- 0
sonar-core/src/test/java/org/sonar/core/components/CacheMetricFinderTest.java ファイルの表示

@@ -0,0 +1,65 @@
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2009 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* Sonar 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.
*
* Sonar 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 Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
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 CacheMetricFinderTest extends AbstractDbUnitTestCase {

private CacheMetricFinder finder;

@Before
public void initFinder() {
setupData("shared");
finder = new CacheMetricFinder(getSessionFactory());
finder.start();
}

@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());
}
}

+ 61
- 0
sonar-core/src/test/java/org/sonar/core/components/CacheRuleFinderTest.java ファイルの表示

@@ -0,0 +1,61 @@
/*
* Sonar, open source software quality management tool.
* Copyright (C) 2009 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* Sonar 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.
*
* Sonar 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 Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.core.components;

import org.junit.Test;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
import org.sonar.jpa.test.AbstractDbUnitTestCase;

import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.junit.Assert.assertThat;

public class CacheRuleFinderTest extends AbstractDbUnitTestCase {

@Test
public void shouldCacheFindById() {
setupData("shared");
RuleFinder finder = new CacheRuleFinder(getSessionFactory());
assertThat(finder.findById(3).getConfigKey(), is("Checker/Treewalker/AnnotationUseStyleCheck"));

deleteRules();

assertThat(finder.findById(3), notNullValue());
}

@Test
public void shouldCacheFindByKey() {
setupData("shared");
RuleFinder finder = new CacheRuleFinder(getSessionFactory());
Rule rule = finder.findByKey("checkstyle", "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck");
assertThat(rule.getConfigKey(), is("Checker/Treewalker/AnnotationUseStyleCheck"));

deleteRules();

rule = finder.findByKey("checkstyle", "com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck");
assertThat(rule, notNullValue());
}

private void deleteRules() {
getSession().createQuery("delete " + Rule.class.getSimpleName()).executeUpdate();
getSession().commit();
}
}

sonar-core/src/test/java/org/sonar/core/qualitymodel/DefaultModelFinderTest.java → sonar-core/src/test/java/org/sonar/core/components/DefaultModelFinderTest.java ファイルの表示

@@ -17,10 +17,11 @@
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.core.qualitymodel;
package org.sonar.core.components;

import org.junit.Test;
import org.sonar.api.qualitymodel.Model;
import org.sonar.core.components.DefaultModelFinder;
import org.sonar.jpa.test.AbstractDbUnitTestCase;

import static org.junit.Assert.assertNotNull;

sonar-core/src/test/java/org/sonar/core/rule/DefaultRuleFinderTest.java → sonar-core/src/test/java/org/sonar/core/components/DefaultRuleFinderTest.java ファイルの表示

@@ -17,10 +17,11 @@
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.core.rule;
package org.sonar.core.components;

import org.junit.Test;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RuleFinder;
import org.sonar.api.rules.RuleQuery;
import org.sonar.jpa.test.AbstractDbUnitTestCase;

@@ -28,35 +29,48 @@ import java.util.Collection;

import static org.hamcrest.CoreMatchers.anyOf;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.hamcrest.core.IsNull.nullValue;
import static org.junit.Assert.*;

public class DefaultRuleFinderTest extends AbstractDbUnitTestCase {

@Test
public void findByKey() {
public void shouldFindById() {
setupData("shared");
DefaultRuleFinder provider = new DefaultRuleFinder(getSessionFactory());
Rule rule = provider.findByKey("checkstyle", "com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck");
RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
assertThat(finder.findById(3).getConfigKey(), is("Checker/Treewalker/AnnotationUseStyleCheck"));
}

@Test
public void shouldNotFindDisabledRuleById() {
setupData("shared");
RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
assertThat(finder.findById(2), nullValue());
}

@Test
public void shouldFindByKey() {
setupData("shared");
RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
Rule rule = finder.findByKey("checkstyle", "com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck");
assertNotNull(rule);
assertThat(rule.getKey(), is("com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"));
assertThat(rule.isEnabled(), is(true));
}

@Test
public void findReturnsNullIfNoResults() {
public void findShouldReturnNullIfNoResults() {
setupData("shared");
DefaultRuleFinder provider = new DefaultRuleFinder(getSessionFactory());
assertNull(provider.findByKey("checkstyle", "unknown"));
assertNull(provider.find(RuleQuery.create().withRepositoryKey("checkstyle").withConfigKey("unknown")));
RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
assertNull(finder.findByKey("checkstyle", "unknown"));
assertNull(finder.find(RuleQuery.create().withRepositoryKey("checkstyle").withConfigKey("unknown")));
}

@Test
public void findRepositoryRules() {
setupData("shared");
DefaultRuleFinder provider = new DefaultRuleFinder(getSessionFactory());
Collection<Rule> rules = provider.findAll(RuleQuery.create().withRepositoryKey("checkstyle"));
RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
Collection<Rule> rules = finder.findAll(RuleQuery.create().withRepositoryKey("checkstyle"));
assertNotNull(rules);
assertThat(rules.size(), is(2)); // only enabled checkstyle rules
}
@@ -64,8 +78,8 @@ public class DefaultRuleFinderTest extends AbstractDbUnitTestCase {
@Test
public void findAllEnabled() {
setupData("shared");
DefaultRuleFinder provider = new DefaultRuleFinder(getSessionFactory());
Collection<Rule> rules = provider.findAll(RuleQuery.create());
RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
Collection<Rule> rules = finder.findAll(RuleQuery.create());
assertNotNull(rules);
assertThat(rules.size(), is(3)); // only enabled checkstyle+pmd rules
for (Rule rule : rules) {
@@ -76,16 +90,16 @@ public class DefaultRuleFinderTest extends AbstractDbUnitTestCase {
@Test
public void doNotFindDisabledRules() {
setupData("shared");
DefaultRuleFinder provider = new DefaultRuleFinder(getSessionFactory());
Rule rule = provider.findByKey("checkstyle", "DisabledCheck");
RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
Rule rule = finder.findByKey("checkstyle", "DisabledCheck");
assertNull(rule);
}

@Test
public void doNotFindUnknownRules() {
setupData("shared");
DefaultRuleFinder provider = new DefaultRuleFinder(getSessionFactory());
Collection<Rule> rules = provider.findAll(RuleQuery.create().withRepositoryKey("unknown_repository"));
RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
Collection<Rule> rules = finder.findAll(RuleQuery.create().withRepositoryKey("unknown_repository"));
assertThat(rules.size(), is(0));
}
}

sonar-core/src/test/java/org/sonar/core/qualitymodel/ModelTest.java → sonar-core/src/test/java/org/sonar/core/components/ModelTest.java ファイルの表示

@@ -17,18 +17,15 @@
* License along with Sonar; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.core.qualitymodel;
package org.sonar.core.components;

import org.junit.Test;
import org.sonar.api.qualitymodel.Characteristic;
import org.sonar.api.qualitymodel.Model;
import org.sonar.jpa.test.AbstractDbUnitTestCase;

import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.*;
import static org.junit.internal.matchers.IsCollectionContaining.hasItems;

public class ModelTest extends AbstractDbUnitTestCase {

+ 12
- 0
sonar-core/src/test/resources/org/sonar/core/components/CacheMetricFinderTest/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>

sonar-core/src/test/resources/org/sonar/core/qualitymodel/DefaultModelFinderTest/shared.xml → sonar-core/src/test/resources/org/sonar/core/components/DefaultModelFinderTest/shared.xml ファイルの表示


sonar-core/src/test/resources/org/sonar/core/rule/DefaultRuleFinderTest/shared.xml → sonar-core/src/test/resources/org/sonar/core/components/DefaultRuleFinderTest/shared.xml ファイルの表示


sonar-core/src/test/resources/org/sonar/core/qualitymodel/ModelTest/saveModelAndCharacteristics.xml → sonar-core/src/test/resources/org/sonar/core/components/ModelTest/saveModelAndCharacteristics.xml ファイルの表示


sonar-core/src/test/resources/org/sonar/core/qualitymodel/ModelTest/testGraphOfCharacteristics.xml → sonar-core/src/test/resources/org/sonar/core/components/ModelTest/testGraphOfCharacteristics.xml ファイルの表示


sonar-core/src/test/resources/org/sonar/core/qualitymodel/ModelTest/testTreeOfCharacteristics.xml → sonar-core/src/test/resources/org/sonar/core/components/ModelTest/testTreeOfCharacteristics.xml ファイルの表示


+ 8
- 68
sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureModel.java ファイルの表示

@@ -20,14 +20,9 @@
package org.sonar.api.database.model;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.sonar.api.database.DatabaseSession;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.Metric;
import org.sonar.api.measures.RuleMeasure;
import org.sonar.api.qualitymodel.Characteristic;
import org.sonar.api.rules.Rule;
import org.sonar.api.rules.RulePriority;

import javax.persistence.*;
@@ -74,10 +69,8 @@ public class MeasureModel implements Cloneable {
@Column(name = "measure_date", updatable = true, nullable = true)
private Date measureDate;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "rule_id")
@Cache(usage = CacheConcurrencyStrategy.READ_ONLY)
private Rule rule;
@Column(name = "rule_id", updatable = true, nullable = true)
private Integer ruleId;

@Column(name = "rules_category_id")
private Integer rulesCategoryId;
@@ -223,7 +216,7 @@ public class MeasureModel implements Cloneable {
* @return whether the measure is about rule
*/
public boolean isRuleMeasure() {
return rule != null || rulePriority != null || rulesCategoryId != null;
return ruleId != null || rulePriority != null || rulesCategoryId != null;
}

/**
@@ -261,11 +254,8 @@ public class MeasureModel implements Cloneable {
return this;
}

/**
* @return the rule
*/
public Rule getRule() {
return rule;
public Integer getRuleId() {
return ruleId;
}

/**
@@ -273,8 +263,8 @@ public class MeasureModel implements Cloneable {
*
* @return the current object
*/
public MeasureModel setRule(Rule rule) {
this.rule = rule;
public MeasureModel setRuleId(Integer ruleId) {
this.ruleId = ruleId;
return this;
}

@@ -453,16 +443,6 @@ public class MeasureModel implements Cloneable {
toString();
}

/**
* @return the rule id of the measure
*/
public Integer getRuleId() {
if (getRule() != null) {
return getRule().getId();
}
return null;
}

/**
* @return diffValue1
*/
@@ -548,51 +528,11 @@ public class MeasureModel implements Cloneable {
clone.setValue(getValue());
clone.setRulesCategoryId(getRulesCategoryId());
clone.setRulePriority(getRulePriority());
clone.setRule(getRule());
clone.setRuleId(getRuleId());
clone.setSnapshotId(getSnapshotId());
clone.setMeasureDate(getMeasureDate());
clone.setUrl(getUrl());
clone.setCharacteristic(getCharacteristic());
return clone;
}

/**
* True if other fields than 'value' are set.
*/
public boolean hasOptionalData() {
return getAlertStatus()!=null ||
getAlertText()!=null ||
getDescription()!=null ||
getDiffValue1()!=null ||
getDiffValue2()!=null ||
getDiffValue3()!=null ||
getMeasureData()!=null ||
getTendency()!=null ||
getUrl()!=null;
}

/**
* @return a measure from the current object
*/
public Measure toMeasure(Metric metric) {
Measure measure;
if (isRuleMeasure()) {
measure = new RuleMeasure(metric, getRule(), getRulePriority(), getRulesCategoryId());
} else {
measure = new Measure(metric);
}
measure.setId(getId());
measure.setDescription(getDescription());
measure.setValue(getValue());
measure.setData(getData(metric));
measure.setAlertStatus(getAlertStatus());
measure.setAlertText(getAlertText());
measure.setTendency(getTendency());
measure.setDiffValue1(getDiffValue1());
measure.setDiffValue2(getDiffValue2());
measure.setDiffValue3(getDiffValue3());
measure.setUrl(getUrl());
measure.setCharacteristic(getCharacteristic());
return measure;
}
}

+ 3
- 1
sonar-plugin-api/src/main/java/org/sonar/api/measures/MetricFinder.java ファイルの表示

@@ -31,7 +31,9 @@ import java.util.List;
*/
public interface MetricFinder extends BatchComponent {

Metric find(String key);
Metric findById(int id);

Metric findByKey(String key);

Collection<Metric> findAll(List<String> metricKeys);


+ 1
- 0
sonar-plugin-api/src/main/java/org/sonar/api/rules/RuleFinder.java ファイルの表示

@@ -29,6 +29,7 @@ import java.util.Collection;
*/
public interface RuleFinder extends BatchComponent, ServerComponent {

Rule findById(int ruleId);
Rule findByKey(String repositoryKey, String key);
Rule find(RuleQuery query);
Collection<Rule> findAll(RuleQuery query);

+ 2
- 2
sonar-server/src/main/java/org/sonar/server/platform/Platform.java ファイルの表示

@@ -38,9 +38,9 @@ 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.DefaultModelFinder;
import org.sonar.core.components.DefaultRuleFinder;
import org.sonar.core.plugin.JpaPluginDao;
import org.sonar.core.qualitymodel.DefaultModelFinder;
import org.sonar.core.rule.DefaultRuleFinder;
import org.sonar.jpa.dao.*;
import org.sonar.jpa.session.DatabaseSessionFactory;
import org.sonar.jpa.session.DatabaseSessionProvider;

読み込み中…
キャンセル
保存