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;
// 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())
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();
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();
}
}
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;
}
@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);
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
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);
--- /dev/null
+/*
+ * 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.plugins.findbugs;
+
+import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.RuleFinder;
+import org.sonar.api.rules.RuleQuery;
+import org.sonar.api.rules.XMLRuleParser;
+
+import java.util.Collection;
+import java.util.List;
+
+public class FakeRuleFinder implements RuleFinder {
+
+ private final List<Rule> findbugsRules;
+
+ public FakeRuleFinder() {
+ FindbugsRuleRepository repo = new FindbugsRuleRepository(new XMLRuleParser());
+ findbugsRules = repo.createRules();
+ 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)) {
+ return rule;
+ }
+ }
+ return null;
+ }
+
+ public Rule find(RuleQuery query) {
+ throw new UnsupportedOperationException();
+ }
+
+ public Collection<Rule> findAll(RuleQuery query) {
+ return findbugsRules;
+ }
+}
\ No newline at end of file
*/
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;
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 {
+++ /dev/null
-/*
- * 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.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 {
-
- private final List<Rule> findbugsRules;
-
- public FindbugsRuleFinder() {
- FindbugsRuleRepository repo = new FindbugsRuleRepository(new XMLRuleParser());
- findbugsRules = repo.createRules();
- for(Rule rule : findbugsRules){
- rule.setRepositoryKey(FindbugsConstants.REPOSITORY_KEY);
- }
- }
-
- public Rule findByKey(String repositoryKey, String key) {
- for (Rule rule : findbugsRules) {
- if (rule.getKey().equals(key)) {
- return rule;
- }
- }
- return null;
- }
-
- public Rule find(RuleQuery query) {
- throw new UnsupportedOperationException();
- }
-
- public Collection<Rule> findAll(RuleQuery query) {
- return findbugsRules;
- }
-}
\ No newline at end of file
@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));
@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));
}
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));
}
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();
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();
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));
@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);
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());
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();
}
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;
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);
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);
+++ /dev/null
-/*
- * 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.batch;
-
-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 java.util.Collection;
-import java.util.List;
-import java.util.Map;
-
-public final class DefaultMetricFinder implements MetricFinder {
-
- private DatabaseSession session;
- private Map<String, Metric> metrics = Maps.newHashMap();
-
- public DefaultMetricFinder(DatabaseSession session) {
- this.session = session;
- }
-
- public void start() {
- List<Metric> list = session.getResults(Metric.class, "enabled", true);
- for (Metric metric : list) {
- metrics.put(metric.getKey(), metric);
- }
- }
-
- public Metric find(String key) {
- return metrics.get(key);
- }
-
- public Collection<Metric> findAll(List<String> metricKeys) {
- List<Metric> result = Lists.newLinkedList();
- for (String metricKey : metricKeys) {
- Metric metric = find(metricKey);
- if (metric != null) {
- result.add(metric);
- }
- }
- return result;
- }
-
- public Collection<Metric> findAll() {
- return metrics.values();
- }
-}
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 {
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);
}
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);
}
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;
+ }
}
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;
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);
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());
}
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;
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) {
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;
}
*/
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) {
}
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;
- }
}
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;
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
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;
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
--- /dev/null
+/*
+ * 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.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 {
+
+ private DatabaseSessionFactory sessionFactory;
+ private Map<String, Metric> metricsByKey = Maps.newLinkedHashMap();
+ private Map<Integer, Metric> metricsById = Maps.newLinkedHashMap();
+
+ public CacheMetricFinder(DatabaseSessionFactory sessionFactory) {
+ this.sessionFactory = sessionFactory;
+ }
+
+ public void start() {
+ List<Metric> list = sessionFactory.getSession().getResults(Metric.class, "enabled", true);
+ for (Metric metric : list) {
+ metricsByKey.put(metric.getKey(), metric);
+ metricsById.put(metric.getId(), metric);
+ }
+ }
+
+ 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 = findByKey(metricKey);
+ if (metric != null) {
+ result.add(metric);
+ }
+ }
+ return result;
+ }
+
+ public Collection<Metric> findAll() {
+ return metricsByKey.values();
+ }
+}
--- /dev/null
+/*
+ * 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);
+ }
+ }
+}
--- /dev/null
+/*
+ * 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.sonar.api.database.DatabaseSession;
+import org.sonar.api.qualitymodel.Model;
+import org.sonar.api.qualitymodel.ModelFinder;
+import org.sonar.jpa.session.DatabaseSessionFactory;
+
+public class DefaultModelFinder implements ModelFinder {
+
+ private DatabaseSessionFactory sessionFactory;
+
+ public DefaultModelFinder(DatabaseSessionFactory sessionFactory) {
+ this.sessionFactory = sessionFactory;
+ }
+
+ public Model findByName(String name) {
+ DatabaseSession session = sessionFactory.getSession();
+ return session.getSingleResult(Model.class, "name", name);
+ }
+
+}
--- /dev/null
+/*
+ * 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.apache.commons.lang.StringUtils;
+import org.sonar.api.database.DatabaseSession;
+import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.RuleFinder;
+import org.sonar.api.rules.RuleQuery;
+import org.sonar.jpa.session.DatabaseSessionFactory;
+
+import javax.persistence.Query;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.Map;
+
+public class DefaultRuleFinder implements RuleFinder {
+
+ private DatabaseSessionFactory sessionFactory;
+
+ public DefaultRuleFinder(DatabaseSessionFactory sessionFactory) {
+ 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 final Rule find(RuleQuery query) {
+ DatabaseSession session = sessionFactory.getSession();
+ return (Rule)session.getSingleResult(createHqlQuery(session, query), null);
+
+ }
+
+ public final Collection<Rule> findAll(RuleQuery query) {
+ DatabaseSession session = sessionFactory.getSession();
+ return createHqlQuery(session, query).getResultList();
+ }
+
+ private Query createHqlQuery(DatabaseSession session, RuleQuery query) {
+ StringBuilder hql = new StringBuilder().append("from ").append(Rule.class.getSimpleName()).append(" where enabled=true ");
+ Map<String,Object> params = new HashMap<String,Object>();
+ if (StringUtils.isNotBlank(query.getRepositoryKey())) {
+ hql.append("AND pluginName=:repositoryKey ");
+ params.put("repositoryKey", query.getRepositoryKey());
+ }
+ if (StringUtils.isNotBlank(query.getKey())) {
+ hql.append("AND key=:key ");
+ params.put("key", query.getKey());
+ }
+ if (StringUtils.isNotBlank(query.getConfigKey())) {
+ hql.append("AND configKey=:configKey ");
+ params.put("configKey", query.getConfigKey());
+ }
+
+ Query hqlQuery = session.createQuery(hql.toString());
+ for (Map.Entry<String, Object> entry : params.entrySet()) {
+ hqlQuery.setParameter(entry.getKey(), entry.getValue());
+ }
+ return hqlQuery;
+ }
+}
+++ /dev/null
-/*
- * 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.qualitymodel;
-
-import org.sonar.api.database.DatabaseSession;
-import org.sonar.api.qualitymodel.Model;
-import org.sonar.api.qualitymodel.ModelFinder;
-import org.sonar.jpa.session.DatabaseSessionFactory;
-
-public class DefaultModelFinder implements ModelFinder {
-
- private DatabaseSessionFactory sessionFactory;
-
- public DefaultModelFinder(DatabaseSessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
-
- public Model findByName(String name) {
- DatabaseSession session = sessionFactory.getSession();
- return session.getSingleResult(Model.class, "name", name);
- }
-
-}
+++ /dev/null
-/*
- * 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.rule;
-
-import org.apache.commons.lang.StringUtils;
-import org.sonar.api.database.DatabaseSession;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RuleFinder;
-import org.sonar.api.rules.RuleQuery;
-import org.sonar.jpa.session.DatabaseSessionFactory;
-
-import javax.persistence.Query;
-import java.util.Collection;
-import java.util.HashMap;
-import java.util.Map;
-
-public class DefaultRuleFinder implements RuleFinder {
-
- private DatabaseSessionFactory sessionFactory;
-
- public DefaultRuleFinder(DatabaseSessionFactory sessionFactory) {
- this.sessionFactory = sessionFactory;
- }
-
- public Rule findByKey(String repositoryKey, String key) {
- return sessionFactory.getSession().getSingleResult(Rule.class, "pluginName", repositoryKey, "key", key, "enabled", true);
- }
-
- public Rule find(RuleQuery query) {
- DatabaseSession session = sessionFactory.getSession();
- return (Rule)session.getSingleResult(createHqlQuery(session, query), null);
-
- }
-
- public Collection<Rule> findAll(RuleQuery query) {
- DatabaseSession session = sessionFactory.getSession();
- return createHqlQuery(session, query).getResultList();
- }
-
- private Query createHqlQuery(DatabaseSession session, RuleQuery query) {
- StringBuilder hql = new StringBuilder().append("from ").append(Rule.class.getSimpleName()).append(" where enabled=true ");
- Map<String,Object> params = new HashMap<String,Object>();
- if (StringUtils.isNotBlank(query.getRepositoryKey())) {
- hql.append("AND pluginName=:repositoryKey ");
- params.put("repositoryKey", query.getRepositoryKey());
- }
- if (StringUtils.isNotBlank(query.getKey())) {
- hql.append("AND key=:key ");
- params.put("key", query.getKey());
- }
- if (StringUtils.isNotBlank(query.getConfigKey())) {
- hql.append("AND configKey=:configKey ");
- params.put("configKey", query.getConfigKey());
- }
-
- Query hqlQuery = session.createQuery(hql.toString());
- for (Map.Entry<String, Object> entry : params.entrySet()) {
- hqlQuery.setParameter(entry.getKey(), entry.getValue());
- }
- return hqlQuery;
- }
-}
--- /dev/null
+/*
+ * 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());
+ }
+}
--- /dev/null
+/*
+ * 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();
+ }
+}
--- /dev/null
+/*
+ * 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.qualitymodel.Model;
+import org.sonar.core.components.DefaultModelFinder;
+import org.sonar.jpa.test.AbstractDbUnitTestCase;
+
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+
+public class DefaultModelFinderTest extends AbstractDbUnitTestCase {
+
+ @Test
+ public void findByName() {
+ setupData("shared");
+ DefaultModelFinder provider = new DefaultModelFinder(getSessionFactory());
+ Model model = provider.findByName("M1");
+ assertNotNull(model);
+ assertNotNull(model.getCharacteristicByName("M1C1"));
+ }
+
+ @Test
+ public void findByNameNotFound() {
+ setupData("shared");
+ DefaultModelFinder provider = new DefaultModelFinder(getSessionFactory());
+ assertNull(provider.findByName("UNKNOWN"));
+ }
+}
\ No newline at end of file
--- /dev/null
+/*
+ * 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.api.rules.RuleQuery;
+import org.sonar.jpa.test.AbstractDbUnitTestCase;
+
+import java.util.Collection;
+
+import static org.hamcrest.CoreMatchers.anyOf;
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNull.nullValue;
+import static org.junit.Assert.*;
+
+public class DefaultRuleFinderTest extends AbstractDbUnitTestCase {
+
+ @Test
+ public void shouldFindById() {
+ setupData("shared");
+ 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 findShouldReturnNullIfNoResults() {
+ setupData("shared");
+ 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");
+ 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
+ }
+
+ @Test
+ public void findAllEnabled() {
+ setupData("shared");
+ 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) {
+ assertThat(rule.getId(), anyOf(is(1), is(3), is(4)));
+ }
+ }
+
+ @Test
+ public void doNotFindDisabledRules() {
+ setupData("shared");
+ RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
+ Rule rule = finder.findByKey("checkstyle", "DisabledCheck");
+ assertNull(rule);
+ }
+
+ @Test
+ public void doNotFindUnknownRules() {
+ setupData("shared");
+ RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
+ Collection<Rule> rules = finder.findAll(RuleQuery.create().withRepositoryKey("unknown_repository"));
+ assertThat(rules.size(), is(0));
+ }
+}
--- /dev/null
+/*
+ * 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.qualitymodel.Characteristic;
+import org.sonar.api.qualitymodel.Model;
+import org.sonar.jpa.test.AbstractDbUnitTestCase;
+
+import static org.hamcrest.Matchers.is;
+import static org.junit.Assert.*;
+import static org.junit.internal.matchers.IsCollectionContaining.hasItems;
+
+public class ModelTest extends AbstractDbUnitTestCase {
+
+ @Test
+ public void saveModelAndCharacteristics() {
+ setupData("saveModelAndCharacteristics");
+ Model model = Model.createByName("fake");
+ model.createCharacteristicByName("Efficiency");
+ model.createCharacteristicByName("Usability");
+ getSession().save(model);
+ getSession().commit();
+
+ model = getSession().getSingleResult(Model.class, "name", "fake");
+ assertThat(model.getName(), is("fake"));
+ assertThat(model.getCharacteristics().size(), is(2));
+ assertThat(model.getRootCharacteristics().size(), is(2));
+ assertNotNull(model.getCharacteristicByName("Efficiency"));
+ }
+
+ /**
+ * max one parent by characteristic
+ */
+ @Test
+ public void saveTreeOfCharacteristics() {
+ setupData("testTreeOfCharacteristics");
+ Model model = Model.createByName("fake");
+
+ Characteristic efficiency = model.createCharacteristicByName("Efficiency");
+ Characteristic usability = model.createCharacteristicByName("Usability");
+ Characteristic cpuEfficiency = model.createCharacteristicByName("CPU Efficiency");
+ Characteristic ramEfficiency = model.createCharacteristicByName("RAM Efficiency");
+
+ efficiency.addChildren(cpuEfficiency, ramEfficiency);
+
+ getSession().save(model);
+ getSession().commit();
+
+ model = getSession().getSingleResult(Model.class, "name", "fake");
+ assertThat(model.getCharacteristics().size(), is(4));
+ assertThat(model.getCharacteristics(), hasItems(efficiency, usability, ramEfficiency, cpuEfficiency));
+ assertThat(efficiency.getChildren(), hasItems(ramEfficiency, cpuEfficiency));
+ assertTrue(ramEfficiency.getChildren().isEmpty());
+ assertThat(ramEfficiency.getParents(), hasItems(efficiency));
+ }
+
+ /**
+ * many-to-many relation between characteristics
+ */
+ @Test
+ public void testGraphOfCharacteristics() {
+ setupData("testGraphOfCharacteristics");
+ Model model = Model.createByName("fake");
+
+ Characteristic level1a = model.createCharacteristicByName("level1a");
+ Characteristic level1b = model.createCharacteristicByName("level1b");
+ Characteristic level2a = model.createCharacteristicByName("level2a");
+ Characteristic level2b = model.createCharacteristicByName("level2b");
+
+ level1a.addChildren(level2a, level2b);
+ level1b.addChildren(level2a, level2b);
+
+ getSession().save(model);
+ getSession().commit();
+
+ Model persistedModel = getSession().getSingleResult(Model.class, "name", "fake");
+ assertThat(persistedModel.getCharacteristics().size(), is(4));
+ assertThat(persistedModel.getRootCharacteristics().size(), is(2));
+
+ assertThat(persistedModel.getCharacteristicByName("level1a").getChildren().size(), is(2));
+ assertThat(persistedModel.getCharacteristicByName("level1b").getChildren().size(), is(2));
+
+ assertThat(persistedModel.getCharacteristicByName("level2a").getParents().size(), is(2));
+ assertThat(persistedModel.getCharacteristicByName("level2b").getParents().size(), is(2));
+ }
+}
+++ /dev/null
-/*
- * 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.qualitymodel;
-
-import org.junit.Test;
-import org.sonar.api.qualitymodel.Model;
-import org.sonar.jpa.test.AbstractDbUnitTestCase;
-
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
-
-public class DefaultModelFinderTest extends AbstractDbUnitTestCase {
-
- @Test
- public void findByName() {
- setupData("shared");
- DefaultModelFinder provider = new DefaultModelFinder(getSessionFactory());
- Model model = provider.findByName("M1");
- assertNotNull(model);
- assertNotNull(model.getCharacteristicByName("M1C1"));
- }
-
- @Test
- public void findByNameNotFound() {
- setupData("shared");
- DefaultModelFinder provider = new DefaultModelFinder(getSessionFactory());
- assertNull(provider.findByName("UNKNOWN"));
- }
-}
\ No newline at end of file
+++ /dev/null
-/*
- * 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.qualitymodel;
-
-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.internal.matchers.IsCollectionContaining.hasItems;
-
-public class ModelTest extends AbstractDbUnitTestCase {
-
- @Test
- public void saveModelAndCharacteristics() {
- setupData("saveModelAndCharacteristics");
- Model model = Model.createByName("fake");
- model.createCharacteristicByName("Efficiency");
- model.createCharacteristicByName("Usability");
- getSession().save(model);
- getSession().commit();
-
- model = getSession().getSingleResult(Model.class, "name", "fake");
- assertThat(model.getName(), is("fake"));
- assertThat(model.getCharacteristics().size(), is(2));
- assertThat(model.getRootCharacteristics().size(), is(2));
- assertNotNull(model.getCharacteristicByName("Efficiency"));
- }
-
- /**
- * max one parent by characteristic
- */
- @Test
- public void saveTreeOfCharacteristics() {
- setupData("testTreeOfCharacteristics");
- Model model = Model.createByName("fake");
-
- Characteristic efficiency = model.createCharacteristicByName("Efficiency");
- Characteristic usability = model.createCharacteristicByName("Usability");
- Characteristic cpuEfficiency = model.createCharacteristicByName("CPU Efficiency");
- Characteristic ramEfficiency = model.createCharacteristicByName("RAM Efficiency");
-
- efficiency.addChildren(cpuEfficiency, ramEfficiency);
-
- getSession().save(model);
- getSession().commit();
-
- model = getSession().getSingleResult(Model.class, "name", "fake");
- assertThat(model.getCharacteristics().size(), is(4));
- assertThat(model.getCharacteristics(), hasItems(efficiency, usability, ramEfficiency, cpuEfficiency));
- assertThat(efficiency.getChildren(), hasItems(ramEfficiency, cpuEfficiency));
- assertTrue(ramEfficiency.getChildren().isEmpty());
- assertThat(ramEfficiency.getParents(), hasItems(efficiency));
- }
-
- /**
- * many-to-many relation between characteristics
- */
- @Test
- public void testGraphOfCharacteristics() {
- setupData("testGraphOfCharacteristics");
- Model model = Model.createByName("fake");
-
- Characteristic level1a = model.createCharacteristicByName("level1a");
- Characteristic level1b = model.createCharacteristicByName("level1b");
- Characteristic level2a = model.createCharacteristicByName("level2a");
- Characteristic level2b = model.createCharacteristicByName("level2b");
-
- level1a.addChildren(level2a, level2b);
- level1b.addChildren(level2a, level2b);
-
- getSession().save(model);
- getSession().commit();
-
- Model persistedModel = getSession().getSingleResult(Model.class, "name", "fake");
- assertThat(persistedModel.getCharacteristics().size(), is(4));
- assertThat(persistedModel.getRootCharacteristics().size(), is(2));
-
- assertThat(persistedModel.getCharacteristicByName("level1a").getChildren().size(), is(2));
- assertThat(persistedModel.getCharacteristicByName("level1b").getChildren().size(), is(2));
-
- assertThat(persistedModel.getCharacteristicByName("level2a").getParents().size(), is(2));
- assertThat(persistedModel.getCharacteristicByName("level2b").getParents().size(), is(2));
- }
-}
+++ /dev/null
-/*
- * 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.rule;
-
-import org.junit.Test;
-import org.sonar.api.rules.Rule;
-import org.sonar.api.rules.RuleQuery;
-import org.sonar.jpa.test.AbstractDbUnitTestCase;
-
-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;
-
-public class DefaultRuleFinderTest extends AbstractDbUnitTestCase {
-
- @Test
- public void findByKey() {
- setupData("shared");
- DefaultRuleFinder provider = new DefaultRuleFinder(getSessionFactory());
- Rule rule = provider.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() {
- setupData("shared");
- DefaultRuleFinder provider = new DefaultRuleFinder(getSessionFactory());
- assertNull(provider.findByKey("checkstyle", "unknown"));
- assertNull(provider.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"));
- assertNotNull(rules);
- assertThat(rules.size(), is(2)); // only enabled checkstyle rules
- }
-
- @Test
- public void findAllEnabled() {
- setupData("shared");
- DefaultRuleFinder provider = new DefaultRuleFinder(getSessionFactory());
- Collection<Rule> rules = provider.findAll(RuleQuery.create());
- assertNotNull(rules);
- assertThat(rules.size(), is(3)); // only enabled checkstyle+pmd rules
- for (Rule rule : rules) {
- assertThat(rule.getId(), anyOf(is(1), is(3), is(4)));
- }
- }
-
- @Test
- public void doNotFindDisabledRules() {
- setupData("shared");
- DefaultRuleFinder provider = new DefaultRuleFinder(getSessionFactory());
- Rule rule = provider.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"));
- assertThat(rules.size(), is(0));
- }
-}
--- /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
--- /dev/null
+<dataset>
+ <quality_models id="1" name="M1" />
+ <quality_models id="2" name="M2" />
+
+ <characteristics id="1" kee="M1C1" name="M1C1" quality_model_id="1" rule_id="[null]" characteristic_order="1" depth="1" description="[null]" enabled="true" />
+ <characteristics id="2" kee="M1C2" name="M1C2" quality_model_id="1" rule_id="[null]" characteristic_order="1" depth="2" description="[null]" enabled="true" />
+ <characteristics id="3" kee="M2C1" name="M2C1" quality_model_id="2" rule_id="[null]" characteristic_order="1" depth="1" description="[null]" enabled="true"/>
+
+ <characteristic_edges child_id="2" parent_id="1"/>
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <rules_categories id="6" name="Efficiency" description="[null]" />
+
+ <!-- CHECKSTYLE -->
+
+ <rules id="1" name="Check Header" rules_category_id="6" plugin_rule_key="com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"
+ plugin_config_key="Checker/Treewalker/HeaderCheck" plugin_name="checkstyle" description="[null]" priority="4" enabled="true" cardinality="SINGLE" parent_id="[null]"/>
+
+ <!-- disabled rule -->
+ <rules id="2" name="Disabled checked" rules_category_id="6" plugin_rule_key="DisabledCheck"
+ plugin_config_key="Checker/Treewalker/DisabledCheck" plugin_name="checkstyle" description="[null]" priority="4" enabled="false" cardinality="SINGLE" parent_id="[null]" />
+
+ <rules id="3" name="Check Annotation" rules_category_id="6" plugin_rule_key="com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck"
+ plugin_config_key="Checker/Treewalker/AnnotationUseStyleCheck" plugin_name="checkstyle" description="[null]" priority="4" enabled="true" cardinality="SINGLE" parent_id="[null]" />
+
+
+ <!-- PMD -->
+ <rules id="4" name="Call Super First" rules_category_id="6" plugin_rule_key="CallSuperFirst"
+ plugin_config_key="rulesets/android.xml/CallSuperFirst" plugin_name="pmd" description="[null]" priority="2" enabled="true" cardinality="SINGLE" parent_id="[null]" />
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <quality_models id="1" name="initial" />
+ <characteristics id="1" kee="FAKE" name="fake" quality_model_id="1" rule_id="[null]" characteristic_order="1" depth="1" description="[null]" enabled="true" />
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <quality_models id="1" name="initial" />
+
+ <characteristics id="1" kee="FAKE1" name="fake1" quality_model_id="1" rule_id="[null]" characteristic_order="1" depth="1" description="[null]" enabled="true"/>
+ <characteristics id="2" kee="FAKE2" name="fake2" quality_model_id="1" rule_id="[null]" characteristic_order="2" depth="1" description="[null]" enabled="true" />
+
+ <characteristic_edges child_id="2" parent_id="1"/>
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <quality_models id="1" name="initial" />
+ <characteristics id="1" kee="FAKE" name="fake" quality_model_id="1" rule_id="[null]" characteristic_order="1" depth="1" description="[null]" enabled="true" />
+ <characteristic_edges child_id="1" parent_id="1"/>
+</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <quality_models id="1" name="M1" />
- <quality_models id="2" name="M2" />
-
- <characteristics id="1" kee="M1C1" name="M1C1" quality_model_id="1" rule_id="[null]" characteristic_order="1" depth="1" description="[null]" enabled="true" />
- <characteristics id="2" kee="M1C2" name="M1C2" quality_model_id="1" rule_id="[null]" characteristic_order="1" depth="2" description="[null]" enabled="true" />
- <characteristics id="3" kee="M2C1" name="M2C1" quality_model_id="2" rule_id="[null]" characteristic_order="1" depth="1" description="[null]" enabled="true"/>
-
- <characteristic_edges child_id="2" parent_id="1"/>
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <quality_models id="1" name="initial" />
- <characteristics id="1" kee="FAKE" name="fake" quality_model_id="1" rule_id="[null]" characteristic_order="1" depth="1" description="[null]" enabled="true" />
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <quality_models id="1" name="initial" />
-
- <characteristics id="1" kee="FAKE1" name="fake1" quality_model_id="1" rule_id="[null]" characteristic_order="1" depth="1" description="[null]" enabled="true"/>
- <characteristics id="2" kee="FAKE2" name="fake2" quality_model_id="1" rule_id="[null]" characteristic_order="2" depth="1" description="[null]" enabled="true" />
-
- <characteristic_edges child_id="2" parent_id="1"/>
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <quality_models id="1" name="initial" />
- <characteristics id="1" kee="FAKE" name="fake" quality_model_id="1" rule_id="[null]" characteristic_order="1" depth="1" description="[null]" enabled="true" />
- <characteristic_edges child_id="1" parent_id="1"/>
-</dataset>
\ No newline at end of file
+++ /dev/null
-<dataset>
- <rules_categories id="6" name="Efficiency" description="[null]" />
-
- <!-- CHECKSTYLE -->
-
- <rules id="1" name="Check Header" rules_category_id="6" plugin_rule_key="com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"
- plugin_config_key="Checker/Treewalker/HeaderCheck" plugin_name="checkstyle" description="[null]" priority="4" enabled="true" cardinality="SINGLE" parent_id="[null]"/>
-
- <!-- disabled rule -->
- <rules id="2" name="Disabled checked" rules_category_id="6" plugin_rule_key="DisabledCheck"
- plugin_config_key="Checker/Treewalker/DisabledCheck" plugin_name="checkstyle" description="[null]" priority="4" enabled="false" cardinality="SINGLE" parent_id="[null]" />
-
- <rules id="3" name="Check Annotation" rules_category_id="6" plugin_rule_key="com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck"
- plugin_config_key="Checker/Treewalker/AnnotationUseStyleCheck" plugin_name="checkstyle" description="[null]" priority="4" enabled="true" cardinality="SINGLE" parent_id="[null]" />
-
-
- <!-- PMD -->
- <rules id="4" name="Call Super First" rules_category_id="6" plugin_rule_key="CallSuperFirst"
- plugin_config_key="rulesets/android.xml/CallSuperFirst" plugin_name="pmd" description="[null]" priority="2" enabled="true" cardinality="SINGLE" parent_id="[null]" />
-</dataset>
\ No newline at end of file
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.*;
@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;
* @return whether the measure is about rule
*/
public boolean isRuleMeasure() {
- return rule != null || rulePriority != null || rulesCategoryId != null;
+ return ruleId != null || rulePriority != null || rulesCategoryId != null;
}
/**
return this;
}
- /**
- * @return the rule
- */
- public Rule getRule() {
- return rule;
+ public Integer getRuleId() {
+ return ruleId;
}
/**
*
* @return the current object
*/
- public MeasureModel setRule(Rule rule) {
- this.rule = rule;
+ public MeasureModel setRuleId(Integer ruleId) {
+ this.ruleId = ruleId;
return this;
}
toString();
}
- /**
- * @return the rule id of the measure
- */
- public Integer getRuleId() {
- if (getRule() != null) {
- return getRule().getId();
- }
- return null;
- }
-
/**
* @return diffValue1
*/
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;
- }
}
*/
public interface MetricFinder extends BatchComponent {
- Metric find(String key);
+ Metric findById(int id);
+
+ Metric findByKey(String key);
Collection<Metric> findAll(List<String> metricKeys);
*/
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);
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;