]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-249 improve core components to load rules and metrics
authorsimonbrandhof <simon.brandhof@gmail.com>
Mon, 29 Nov 2010 17:00:54 +0000 (17:00 +0000)
committersimonbrandhof <simon.brandhof@gmail.com>
Mon, 29 Nov 2010 17:00:54 +0000 (17:00 +0000)
46 files changed:
plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/DifferentialValueDecorator.java
plugins/sonar-dbcleaner-plugin/src/main/java/org/sonar/plugins/dbcleaner/purges/PurgeRuleMeasures.java
plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FakeRuleFinder.java [new file with mode: 0644]
plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsProfileImporterTest.java
plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleFinder.java [deleted file]
plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsSensorTest.java
plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/SonarWayWithFindbugsProfileTest.java
plugins/sonar-pmd-plugin/src/test/java/org/sonar/plugins/pmd/PmdProfileExporterTest.java
sonar-batch/src/main/java/org/sonar/batch/Batch.java
sonar-batch/src/main/java/org/sonar/batch/DefaultMetricFinder.java [deleted file]
sonar-batch/src/main/java/org/sonar/batch/DefaultTimeMachine.java
sonar-batch/src/main/java/org/sonar/batch/ProjectBatch.java
sonar-batch/src/main/java/org/sonar/batch/index/DefaultIndex.java
sonar-batch/src/main/java/org/sonar/batch/index/MeasurePersister.java
sonar-batch/src/main/java/org/sonar/batch/index/ViolationPersister.java
sonar-batch/src/test/java/org/sonar/batch/index/MeasurePersisterTest.java
sonar-batch/src/test/java/org/sonar/batch/index/ViolationPersisterTest.java
sonar-core/src/main/java/org/sonar/core/components/CacheMetricFinder.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/components/CacheRuleFinder.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/components/DefaultModelFinder.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/components/DefaultRuleFinder.java [new file with mode: 0644]
sonar-core/src/main/java/org/sonar/core/qualitymodel/DefaultModelFinder.java [deleted file]
sonar-core/src/main/java/org/sonar/core/rule/DefaultRuleFinder.java [deleted file]
sonar-core/src/test/java/org/sonar/core/components/CacheMetricFinderTest.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/core/components/CacheRuleFinderTest.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/core/components/DefaultModelFinderTest.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/core/components/DefaultRuleFinderTest.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/core/components/ModelTest.java [new file with mode: 0644]
sonar-core/src/test/java/org/sonar/core/qualitymodel/DefaultModelFinderTest.java [deleted file]
sonar-core/src/test/java/org/sonar/core/qualitymodel/ModelTest.java [deleted file]
sonar-core/src/test/java/org/sonar/core/rule/DefaultRuleFinderTest.java [deleted file]
sonar-core/src/test/resources/org/sonar/core/components/CacheMetricFinderTest/shared.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/components/DefaultModelFinderTest/shared.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/components/DefaultRuleFinderTest/shared.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/components/ModelTest/saveModelAndCharacteristics.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/components/ModelTest/testGraphOfCharacteristics.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/components/ModelTest/testTreeOfCharacteristics.xml [new file with mode: 0644]
sonar-core/src/test/resources/org/sonar/core/qualitymodel/DefaultModelFinderTest/shared.xml [deleted file]
sonar-core/src/test/resources/org/sonar/core/qualitymodel/ModelTest/saveModelAndCharacteristics.xml [deleted file]
sonar-core/src/test/resources/org/sonar/core/qualitymodel/ModelTest/testGraphOfCharacteristics.xml [deleted file]
sonar-core/src/test/resources/org/sonar/core/qualitymodel/ModelTest/testTreeOfCharacteristics.xml [deleted file]
sonar-core/src/test/resources/org/sonar/core/rule/DefaultRuleFinderTest/shared.xml [deleted file]
sonar-plugin-api/src/main/java/org/sonar/api/database/model/MeasureModel.java
sonar-plugin-api/src/main/java/org/sonar/api/measures/MetricFinder.java
sonar-plugin-api/src/main/java/org/sonar/api/rules/RuleFinder.java
sonar-server/src/main/java/org/sonar/server/platform/Platform.java

index 8ba04b4e621fa0005e454acad49fd8fc854ae543..2ece3f89ff1838204b7b5782a5330ed9854bd145 100644 (file)
@@ -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);
index d61ad8124991854aeaff14e0cd532bcedf7fc4f6..c0eb8f3620720daa3a0558a5db54b431e26dfb47 100644 (file)
@@ -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);
diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FakeRuleFinder.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FakeRuleFinder.java
new file mode 100644 (file)
index 0000000..eae8230
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * 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
index 9296f03613e3675ecba65823824066a72254fb6e..206ccdfc07dc923049c977d13855324e48e2428e 100644 (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;
@@ -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 {
diff --git a/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleFinder.java b/plugins/sonar-findbugs-plugin/src/test/java/org/sonar/plugins/findbugs/FindbugsRuleFinder.java
deleted file mode 100644 (file)
index ab3af9d..0000000
+++ /dev/null
@@ -1,58 +0,0 @@
-/*
- * 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
index d5124bf0f1a131069d691d0f63db4a0c977524d2..9cd16af6c9828b477814e887d02aef1f2b5f1e21 100644 (file)
@@ -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));
index 881fbf9a36b9fadac2cf7e069456a56ec47c6fc7..a21e3ccaeccbfb0045d23c601a8ab66b38bf07c7 100644 (file)
@@ -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);
index c35e9440f45026cd0587b03ee72414eff3777844..a0a458758bffe8ac47f8fc8a42d181c03eaf56c5 100644 (file)
@@ -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();
     }
index 303d4d099b5bd145c6bcd104b93b723c7da91dec..d6c5c6a8bd668fce1465850bde6ae98c825288ec 100644 (file)
@@ -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);
diff --git a/sonar-batch/src/main/java/org/sonar/batch/DefaultMetricFinder.java b/sonar-batch/src/main/java/org/sonar/batch/DefaultMetricFinder.java
deleted file mode 100644 (file)
index b0c7f01..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * 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();
-  }
-}
index 86fe3f38b58644c59c77dee7ef2b4be705fa4539..8aedf065a5ef1cfbfe74fb0584daab6a0e44cb8c 100644 (file)
@@ -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;
+  }
 }
index d6843c900dd1f9158b3b8fc0d4c5c16cc0b3b522..104f7eb0660d06f7f3b827403715f56ecc7818e5 100644 (file)
@@ -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);
index 55dbfa90c6426df9b3c52ae0774d2d80eef9c3f8..fca1ee8f04b131e173fa1dde66309343ca29030d 100644 (file)
@@ -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());
       }
index c4e34052b4e67a03e2e2fabad61f0c372e61a184..4b7c47b21fc82014dd3420e5c33b1778d098c55d 100644 (file)
@@ -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;
   }
index f5d2e9661c8b36ffca070732d941b6616030b184..934aaadb7bdef2ade21fa63aef07bb8caa381634 100644 (file)
  */
 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;
-  }
 }
index 8d847fe0c73ebdb74dac41b936762f814dc2a756..5c556b8a53a2509f0c157564231023a3b9ada7ce 100644 (file)
@@ -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
index 0c6b898813df3c389979e0272bed38473bbf4189..f4b3164345f8766e3cb921a7d294821ddeaf6617 100644 (file)
@@ -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
diff --git a/sonar-core/src/main/java/org/sonar/core/components/CacheMetricFinder.java b/sonar-core/src/main/java/org/sonar/core/components/CacheMetricFinder.java
new file mode 100644 (file)
index 0000000..fa125dc
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * 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();
+  }
+}
diff --git a/sonar-core/src/main/java/org/sonar/core/components/CacheRuleFinder.java b/sonar-core/src/main/java/org/sonar/core/components/CacheRuleFinder.java
new file mode 100644 (file)
index 0000000..1e0b747
--- /dev/null
@@ -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);
+    }
+  }
+}
diff --git a/sonar-core/src/main/java/org/sonar/core/components/DefaultModelFinder.java b/sonar-core/src/main/java/org/sonar/core/components/DefaultModelFinder.java
new file mode 100644 (file)
index 0000000..a7a04c7
--- /dev/null
@@ -0,0 +1,40 @@
+/*
+ * 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);
+  }
+
+}
diff --git a/sonar-core/src/main/java/org/sonar/core/components/DefaultRuleFinder.java b/sonar-core/src/main/java/org/sonar/core/components/DefaultRuleFinder.java
new file mode 100644 (file)
index 0000000..3c0d50e
--- /dev/null
@@ -0,0 +1,91 @@
+/*
+ * 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;
+  }
+}
diff --git a/sonar-core/src/main/java/org/sonar/core/qualitymodel/DefaultModelFinder.java b/sonar-core/src/main/java/org/sonar/core/qualitymodel/DefaultModelFinder.java
deleted file mode 100644 (file)
index 5606e5e..0000000
+++ /dev/null
@@ -1,40 +0,0 @@
-/*
- * 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);
-  }
-
-}
diff --git a/sonar-core/src/main/java/org/sonar/core/rule/DefaultRuleFinder.java b/sonar-core/src/main/java/org/sonar/core/rule/DefaultRuleFinder.java
deleted file mode 100644 (file)
index 6a167b1..0000000
+++ /dev/null
@@ -1,79 +0,0 @@
-/*
- * 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;
-  }
-}
diff --git a/sonar-core/src/test/java/org/sonar/core/components/CacheMetricFinderTest.java b/sonar-core/src/test/java/org/sonar/core/components/CacheMetricFinderTest.java
new file mode 100644 (file)
index 0000000..a67fd0c
--- /dev/null
@@ -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());
+  }
+}
diff --git a/sonar-core/src/test/java/org/sonar/core/components/CacheRuleFinderTest.java b/sonar-core/src/test/java/org/sonar/core/components/CacheRuleFinderTest.java
new file mode 100644 (file)
index 0000000..0eb9d23
--- /dev/null
@@ -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();
+  }
+}
diff --git a/sonar-core/src/test/java/org/sonar/core/components/DefaultModelFinderTest.java b/sonar-core/src/test/java/org/sonar/core/components/DefaultModelFinderTest.java
new file mode 100644 (file)
index 0000000..222bf40
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * 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
diff --git a/sonar-core/src/test/java/org/sonar/core/components/DefaultRuleFinderTest.java b/sonar-core/src/test/java/org/sonar/core/components/DefaultRuleFinderTest.java
new file mode 100644 (file)
index 0000000..7d2c2b8
--- /dev/null
@@ -0,0 +1,105 @@
+/*
+ * 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));
+  }
+}
diff --git a/sonar-core/src/test/java/org/sonar/core/components/ModelTest.java b/sonar-core/src/test/java/org/sonar/core/components/ModelTest.java
new file mode 100644 (file)
index 0000000..b614dfb
--- /dev/null
@@ -0,0 +1,104 @@
+/*
+ * 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));
+  }
+}
diff --git a/sonar-core/src/test/java/org/sonar/core/qualitymodel/DefaultModelFinderTest.java b/sonar-core/src/test/java/org/sonar/core/qualitymodel/DefaultModelFinderTest.java
deleted file mode 100644 (file)
index 9069fab..0000000
+++ /dev/null
@@ -1,46 +0,0 @@
-/*
- * 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
diff --git a/sonar-core/src/test/java/org/sonar/core/qualitymodel/ModelTest.java b/sonar-core/src/test/java/org/sonar/core/qualitymodel/ModelTest.java
deleted file mode 100644 (file)
index 0c15154..0000000
+++ /dev/null
@@ -1,107 +0,0 @@
-/*
- * 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));
-  }
-}
diff --git a/sonar-core/src/test/java/org/sonar/core/rule/DefaultRuleFinderTest.java b/sonar-core/src/test/java/org/sonar/core/rule/DefaultRuleFinderTest.java
deleted file mode 100644 (file)
index c80573b..0000000
+++ /dev/null
@@ -1,91 +0,0 @@
-/*
- * 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));
-  }
-}
diff --git a/sonar-core/src/test/resources/org/sonar/core/components/CacheMetricFinderTest/shared.xml b/sonar-core/src/test/resources/org/sonar/core/components/CacheMetricFinderTest/shared.xml
new file mode 100644 (file)
index 0000000..85709bc
--- /dev/null
@@ -0,0 +1,12 @@
+<dataset>
+
+  <metrics id="1" name="ncloc" VAL_TYPE="INT" DESCRIPTION="[null]" domain="[null]" short_name=""
+           enabled="true" worst_value="[null]" optimized_best_value="[null]" best_value="[null]" direction="0"
+           hidden="false"/>
+
+  <metrics id="2" name="coverage" VAL_TYPE="INT" DESCRIPTION="[null]" domain="[null]" short_name=""
+           enabled="true" worst_value="0" optimized_best_value="true" best_value="100" direction="1" hidden="false"/>
+
+  <metrics id="3" name="disabled" VAL_TYPE="INT" DESCRIPTION="[null]" domain="[null]" short_name=""
+           enabled="false" worst_value="0" optimized_best_value="true" best_value="100" direction="1" hidden="false"/>
+</dataset>
\ No newline at end of file
diff --git a/sonar-core/src/test/resources/org/sonar/core/components/DefaultModelFinderTest/shared.xml b/sonar-core/src/test/resources/org/sonar/core/components/DefaultModelFinderTest/shared.xml
new file mode 100644 (file)
index 0000000..c26de53
--- /dev/null
@@ -0,0 +1,10 @@
+<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
diff --git a/sonar-core/src/test/resources/org/sonar/core/components/DefaultRuleFinderTest/shared.xml b/sonar-core/src/test/resources/org/sonar/core/components/DefaultRuleFinderTest/shared.xml
new file mode 100644 (file)
index 0000000..f6adafe
--- /dev/null
@@ -0,0 +1,20 @@
+<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
diff --git a/sonar-core/src/test/resources/org/sonar/core/components/ModelTest/saveModelAndCharacteristics.xml b/sonar-core/src/test/resources/org/sonar/core/components/ModelTest/saveModelAndCharacteristics.xml
new file mode 100644 (file)
index 0000000..cd07057
--- /dev/null
@@ -0,0 +1,4 @@
+<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
diff --git a/sonar-core/src/test/resources/org/sonar/core/components/ModelTest/testGraphOfCharacteristics.xml b/sonar-core/src/test/resources/org/sonar/core/components/ModelTest/testGraphOfCharacteristics.xml
new file mode 100644 (file)
index 0000000..ee6e959
--- /dev/null
@@ -0,0 +1,8 @@
+<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
diff --git a/sonar-core/src/test/resources/org/sonar/core/components/ModelTest/testTreeOfCharacteristics.xml b/sonar-core/src/test/resources/org/sonar/core/components/ModelTest/testTreeOfCharacteristics.xml
new file mode 100644 (file)
index 0000000..ef3d9a5
--- /dev/null
@@ -0,0 +1,5 @@
+<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
diff --git a/sonar-core/src/test/resources/org/sonar/core/qualitymodel/DefaultModelFinderTest/shared.xml b/sonar-core/src/test/resources/org/sonar/core/qualitymodel/DefaultModelFinderTest/shared.xml
deleted file mode 100644 (file)
index c26de53..0000000
+++ /dev/null
@@ -1,10 +0,0 @@
-<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
diff --git a/sonar-core/src/test/resources/org/sonar/core/qualitymodel/ModelTest/saveModelAndCharacteristics.xml b/sonar-core/src/test/resources/org/sonar/core/qualitymodel/ModelTest/saveModelAndCharacteristics.xml
deleted file mode 100644 (file)
index cd07057..0000000
+++ /dev/null
@@ -1,4 +0,0 @@
-<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
diff --git a/sonar-core/src/test/resources/org/sonar/core/qualitymodel/ModelTest/testGraphOfCharacteristics.xml b/sonar-core/src/test/resources/org/sonar/core/qualitymodel/ModelTest/testGraphOfCharacteristics.xml
deleted file mode 100644 (file)
index ee6e959..0000000
+++ /dev/null
@@ -1,8 +0,0 @@
-<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
diff --git a/sonar-core/src/test/resources/org/sonar/core/qualitymodel/ModelTest/testTreeOfCharacteristics.xml b/sonar-core/src/test/resources/org/sonar/core/qualitymodel/ModelTest/testTreeOfCharacteristics.xml
deleted file mode 100644 (file)
index ef3d9a5..0000000
+++ /dev/null
@@ -1,5 +0,0 @@
-<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
diff --git a/sonar-core/src/test/resources/org/sonar/core/rule/DefaultRuleFinderTest/shared.xml b/sonar-core/src/test/resources/org/sonar/core/rule/DefaultRuleFinderTest/shared.xml
deleted file mode 100644 (file)
index f6adafe..0000000
+++ /dev/null
@@ -1,20 +0,0 @@
-<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
index 3a9c1a4bca0825833edceab360300c97a17bc37c..e829e6e786e6867017c9145071c276f82bb6bd86 100644 (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.*;
@@ -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;
-  }
 }
index acd85a01e510deb40224df549048aa09bd50e2f0..d05609c607a600fed3ebf7d5b76de9d7974438f3 100644 (file)
@@ -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);
 
index 8358128090890bd7f27f3c53f1e56a6f7ffef483..b96249622e1a587b209c60d02299f4140c420c5f 100644 (file)
@@ -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);
index 9cec2e85564b242b6ecc3fff7e41ec8f0343cb99..e17b9e1bac0e07e3a82da2cb38803a8e579e4fa9 100644 (file)
@@ -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;