]> source.dussan.org Git - sonarqube.git/commitdiff
Deprecating DefaultRuleFinder in core.
authorStephane Gamard <stephane.gamard@searchbox.com>
Fri, 27 Jun 2014 14:53:37 +0000 (16:53 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Mon, 30 Jun 2014 12:46:43 +0000 (14:46 +0200)
sonar-core/src/main/java/org/sonar/core/rule/CacheRuleFinder.java
sonar-core/src/main/java/org/sonar/core/rule/DefaultRuleFinder.java [deleted file]
sonar-core/src/test/java/org/sonar/core/rule/DefaultRuleFinderTest.java [deleted file]
sonar-core/src/test/resources/org/sonar/core/rule/DefaultRuleFinderTest/shared.xml [deleted file]
sonar-server/pom.xml
sonar-server/src/main/java/org/sonar/server/issue/DefaultIssueFinder.java
sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java
sonar-server/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java [new file with mode: 0644]
sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java
sonar-server/src/test/java/org/sonar/server/issue/DefaultIssueFinderTest.java
sonar-server/src/test/java/org/sonar/server/rule/DefaultRuleFinderMediumTest.java [new file with mode: 0644]

index 3ff1505ce2fc5e573631fd1ef00143830d8392ae..8747055a4ee7522555c67ceb138f0c9271a88672 100644 (file)
@@ -22,19 +22,29 @@ package org.sonar.core.rule;
 import com.google.common.collect.BiMap;
 import com.google.common.collect.HashBiMap;
 import com.google.common.collect.Maps;
+import org.apache.commons.lang.StringUtils;
+import org.sonar.api.database.DatabaseSession;
+import org.sonar.api.rule.RuleKey;
 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.annotation.CheckForNull;
+import javax.persistence.Query;
+import java.util.Collection;
+import java.util.HashMap;
 import java.util.Map;
 
-public final class CacheRuleFinder extends DefaultRuleFinder {
+public final class CacheRuleFinder implements RuleFinder {
 
   private BiMap<Integer, Rule> rulesById = HashBiMap.create();
   private Map<String, Map<String, Rule>> rulesByKey = Maps.newHashMap();
 
+  private DatabaseSessionFactory sessionFactory;
+
   public CacheRuleFinder(DatabaseSessionFactory sessionFactory) {
-    super(sessionFactory);
+    this.sessionFactory = sessionFactory;
   }
 
   @Override
@@ -68,4 +78,67 @@ public final class CacheRuleFinder extends DefaultRuleFinder {
     }
     return repository;
   }
+
+  protected final Rule doFindById(int ruleId) {
+    DatabaseSession session = sessionFactory.getSession();
+    return session.getSingleResult(
+      session.createQuery("FROM " + Rule.class.getSimpleName() + " r WHERE r.id=:id and r.status<>:status")
+        .setParameter("id", ruleId)
+        .setParameter("status", Rule.STATUS_REMOVED
+        ),
+      null);
+  }
+
+
+  @CheckForNull
+  public Rule findByKey(RuleKey key) {
+    return findByKey(key.repository(), key.rule());
+  }
+
+
+  @CheckForNull
+  protected final Rule doFindByKey(String repositoryKey, String key) {
+    DatabaseSession session = sessionFactory.getSession();
+    return session.getSingleResult(
+      session.createQuery("FROM " + Rule.class.getSimpleName() + " r WHERE r.key=:key and r.pluginName=:pluginName and r.status<>:status")
+        .setParameter("key", key)
+        .setParameter("pluginName", repositoryKey)
+        .setParameter("status", Rule.STATUS_REMOVED
+        ),
+      null);
+  }
+
+  public final Rule find(RuleQuery query) {
+    DatabaseSession session = sessionFactory.getSession();
+    return 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 status<>:status ");
+    Map<String, Object> params = new HashMap<String, Object>();
+    params.put("status", Rule.STATUS_REMOVED);
+    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/rule/DefaultRuleFinder.java b/sonar-core/src/main/java/org/sonar/core/rule/DefaultRuleFinder.java
deleted file mode 100644 (file)
index 7cbbdd2..0000000
+++ /dev/null
@@ -1,126 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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 this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-
-package org.sonar.core.rule;
-
-import org.apache.commons.lang.StringUtils;
-import org.sonar.api.database.DatabaseSession;
-import org.sonar.api.rule.RuleKey;
-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.annotation.CheckForNull;
-import javax.persistence.Query;
-
-import java.util.Collection;
-import java.util.Collections;
-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) {
-    DatabaseSession session = sessionFactory.getSession();
-    return session.getSingleResult(
-        session.createQuery("FROM " + Rule.class.getSimpleName() + " r WHERE r.id=:id and r.status<>:status")
-            .setParameter("id", ruleId)
-            .setParameter("status", Rule.STATUS_REMOVED
-        ),
-        null);
-  }
-
-  public Collection<Rule> findByIds(Collection<Integer> ruleIds) {
-    if (ruleIds.isEmpty()) {
-      return Collections.emptyList();
-    }
-    DatabaseSession session = sessionFactory.getSession();
-    StringBuilder hql = new StringBuilder().append("from ").append(Rule.class.getSimpleName()).append(" r where r.id in (:ids)");
-    Query hqlQuery = session.createQuery(hql.toString()).setParameter("ids", ruleIds);
-    return hqlQuery.getResultList();
-  }
-
-  @CheckForNull
-  public Rule findByKey(RuleKey key) {
-    return findByKey(key.repository(), key.rule());
-  }
-
-  @CheckForNull
-  public Rule findByKey(String repositoryKey, String key) {
-    return doFindByKey(repositoryKey, key);
-  }
-
-  @CheckForNull
-  protected final Rule doFindByKey(String repositoryKey, String key) {
-    DatabaseSession session = sessionFactory.getSession();
-    return session.getSingleResult(
-        session.createQuery("FROM " + Rule.class.getSimpleName() + " r WHERE r.key=:key and r.pluginName=:pluginName and r.status<>:status")
-            .setParameter("key", key)
-            .setParameter("pluginName", repositoryKey)
-            .setParameter("status", Rule.STATUS_REMOVED
-        ),
-        null);
-  }
-
-  public final Rule find(RuleQuery query) {
-    DatabaseSession session = sessionFactory.getSession();
-    return 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 status<>:status ");
-    Map<String, Object> params = new HashMap<String, Object>();
-    params.put("status", Rule.STATUS_REMOVED);
-    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/rule/DefaultRuleFinderTest.java b/sonar-core/src/test/java/org/sonar/core/rule/DefaultRuleFinderTest.java
deleted file mode 100644 (file)
index d754734..0000000
+++ /dev/null
@@ -1,114 +0,0 @@
-/*
- * SonarQube, open source software quality management tool.
- * Copyright (C) 2008-2014 SonarSource
- * mailto:contact AT sonarsource DOT com
- *
- * SonarQube 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.
- *
- * SonarQube 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 this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.core.rule;
-
-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 com.google.common.collect.Lists.newArrayList;
-import static org.fest.assertions.Assertions.assertThat;
-
-public class DefaultRuleFinderTest extends AbstractDbUnitTestCase {
-
-  @Test
-  public void should_find_by_id() {
-    setupData("shared");
-    RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
-    assertThat(finder.findById(3).getConfigKey()).isEqualTo("Checker/Treewalker/AnnotationUseStyleCheck");
-  }
-
-  @Test
-  public void should_not_find_disabled_rule_by_id() {
-    setupData("shared");
-    RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
-    assertThat(finder.findById(2)).isNull();
-  }
-
-  @Test
-  public void should_find_by_ids() {
-    setupData("shared");
-    DefaultRuleFinder finder = new DefaultRuleFinder(getSessionFactory());
-    // 2 is returned even its status is REMOVED
-    assertThat(finder.findByIds(newArrayList(2, 3))).hasSize(2);
-  }
-
-  @Test
-  public void should_find_by_ids_empty() {
-    Collection<Integer> newArrayList = newArrayList();
-    assertThat(new DefaultRuleFinder(getSessionFactory()).findByIds(newArrayList)).isEmpty();
-  }
-
-  @Test
-  public void should_find_by_key() {
-    setupData("shared");
-    RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
-    Rule rule = finder.findByKey("checkstyle", "com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck");
-    assertThat(rule).isNotNull();
-    assertThat(rule.getKey()).isEqualTo(("com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"));
-    assertThat(rule.isEnabled()).isTrue();
-  }
-
-  @Test
-  public void find_should_return_null_if_no_results() {
-    setupData("shared");
-    RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
-    assertThat(finder.findByKey("checkstyle", "unknown")).isNull();
-    assertThat(finder.find(RuleQuery.create().withRepositoryKey("checkstyle").withConfigKey("unknown"))).isNull();
-  }
-
-  @Test
-  public void find_repository_rules() {
-    setupData("shared");
-    RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
-    Collection<Rule> rules = finder.findAll(RuleQuery.create().withRepositoryKey("checkstyle"));
-
-    assertThat(rules).hasSize(2);
-  }
-
-  @Test
-  public void find_all_enabled() {
-    setupData("shared");
-    RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
-    Collection<Rule> rules = finder.findAll(RuleQuery.create());
-
-    assertThat(rules).onProperty("id").containsOnly(1, 3, 4);
-  }
-
-  @Test
-  public void do_not_find_disabled_rules() {
-    setupData("shared");
-    RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
-    Rule rule = finder.findByKey("checkstyle", "DisabledCheck");
-    assertThat(rule).isNull();
-  }
-
-  @Test
-  public void do_not_find_unknown_rules() {
-    setupData("shared");
-    RuleFinder finder = new DefaultRuleFinder(getSessionFactory());
-    Collection<Rule> rules = finder.findAll(RuleQuery.create().withRepositoryKey("unknown_repository"));
-    assertThat(rules).isEmpty();
-  }
-}
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 f01f4cf..0000000
+++ /dev/null
@@ -1,18 +0,0 @@
-<dataset>
-  <!-- CHECKSTYLE -->
-
-  <rules tags="[null]" system_tags="[null]" id="1" name="Check Header" plugin_rule_key="com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"
-         plugin_config_key="Checker/Treewalker/HeaderCheck" plugin_name="checkstyle" description="[null]" priority="4" status="READY"  is_template="[false]" template_id="[null]"/>
-
-  <!-- disabled rule -->
-  <rules tags="[null]" system_tags="[null]" id="2" name="Disabled checked" plugin_rule_key="DisabledCheck"
-         plugin_config_key="Checker/Treewalker/DisabledCheck" plugin_name="checkstyle" description="[null]" priority="4" status="REMOVED" is_template="[false]" template_id="[null]" />
-
-  <rules tags="[null]" system_tags="[null]" id="3" name="Check Annotation" plugin_rule_key="com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck"
-         plugin_config_key="Checker/Treewalker/AnnotationUseStyleCheck" plugin_name="checkstyle" description="[null]" priority="4" status="READY" is_template="[false]" template_id="[null]" />
-
-
-  <!-- PMD -->
-  <rules tags="[null]" system_tags="[null]" id="4" name="Call Super First" plugin_rule_key="CallSuperFirst"
-         plugin_config_key="rulesets/android.xml/CallSuperFirst" plugin_name="pmd" description="[null]" priority="2" status="READY" is_template="[false]" template_id="[null]" />
-</dataset>
index 67877dc5a995e8f3d500a138c13e3767b4bd2222..f42a71c7e4d323a76d9916d899e4c129214b854d 100644 (file)
               <useNaming>false</useNaming>
               <systemProperties>
                 <SONAR_HOME>${project.build.directory}/dev</SONAR_HOME>
-                <rails.env>development</rails.env>
                 <jruby.max.runtimes>3</jruby.max.runtimes>
                 <sonar.jdbc.url>${sonar.jdbc.url}</sonar.jdbc.url>
-                <sonar.runtime.mode>development</sonar.runtime.mode>
                 <sonar.es.http.host>127.0.0.1</sonar.es.http.host>
                 <sonar.es.http.port>9200</sonar.es.http.port>
                 <sonar.log.console>true</sonar.log.console>
index cc9230326d7d65303521a36322b2d8240b76e68f..41364c818af5f9ee4243cbb231033671d161959d 100644 (file)
@@ -41,7 +41,7 @@ import org.sonar.core.issue.db.IssueDao;
 import org.sonar.core.issue.db.IssueDto;
 import org.sonar.core.persistence.MyBatis;
 import org.sonar.core.resource.ResourceDao;
-import org.sonar.core.rule.DefaultRuleFinder;
+import org.sonar.server.rule.DefaultRuleFinder;
 import org.sonar.server.issue.actionplan.ActionPlanService;
 import org.sonar.server.user.UserSession;
 
index 1bb53f1316c166665cd596bec61431e3861e65a5..f93e5a768906915df79a830fb0dcd33dfa6727ff 100644 (file)
@@ -60,7 +60,7 @@ import org.sonar.core.qualitygate.db.ProjectQgateAssociationDao;
 import org.sonar.core.qualitygate.db.QualityGateConditionDao;
 import org.sonar.core.qualitygate.db.QualityGateDao;
 import org.sonar.core.resource.DefaultResourcePermissions;
-import org.sonar.core.rule.DefaultRuleFinder;
+import org.sonar.server.rule.DefaultRuleFinder;
 import org.sonar.core.test.TestPlanPerspectiveLoader;
 import org.sonar.core.test.TestablePerspectiveLoader;
 import org.sonar.core.timemachine.Periods;
diff --git a/sonar-server/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java b/sonar-server/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java
new file mode 100644 (file)
index 0000000..34c0755
--- /dev/null
@@ -0,0 +1,120 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+package org.sonar.server.rule;
+
+import com.google.common.collect.Lists;
+import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rules.RuleFinder;
+import org.sonar.server.rule.index.RuleIndex;
+import org.sonar.server.rule.index.RuleQuery;
+import org.sonar.server.search.IndexClient;
+import org.sonar.server.search.QueryOptions;
+
+import javax.annotation.CheckForNull;
+import java.util.Collection;
+import java.util.List;
+
+@Deprecated
+/**
+ * Will be removed in the future. Please use {@link org.sonar.server.rule.RuleService}
+ */
+public class DefaultRuleFinder implements RuleFinder {
+
+  private final RuleIndex index;
+
+  public DefaultRuleFinder(IndexClient indexes) {
+    this.index = indexes.get(RuleIndex.class);
+  }
+
+  @CheckForNull
+  public org.sonar.api.rules.Rule findById(int ruleId) {
+    return toRule(index.getById(ruleId));
+  }
+
+  @CheckForNull
+  public Collection<org.sonar.api.rules.Rule> findByIds(Collection<Integer> ruleIds) {
+    List<org.sonar.api.rules.Rule> rules = Lists.newArrayList();
+    if (ruleIds.isEmpty()) {
+      return rules;
+    }
+    for (Rule rule : index.getByIds(ruleIds)) {
+      rules.add(toRule(rule));
+    }
+    return rules;
+  }
+
+  @CheckForNull
+  public org.sonar.api.rules.Rule findByKey(RuleKey key) {
+    return toRule(index.getByKey(key));
+  }
+
+  @CheckForNull
+  public org.sonar.api.rules.Rule findByKey(String repositoryKey, String key) {
+    return findByKey(RuleKey.of(repositoryKey, key));
+  }
+
+  public final org.sonar.api.rules.Rule find(org.sonar.api.rules.RuleQuery query) {
+    return toRule(index.search(toQuery(query), new QueryOptions()).getHits().get(0));
+  }
+
+  public final Collection<org.sonar.api.rules.Rule> findAll(org.sonar.api.rules.RuleQuery query) {
+    List<org.sonar.api.rules.Rule> rules = Lists.newArrayList();
+    for(Rule rule:index.search(toQuery(query), new QueryOptions()).getHits()){
+      rules.add(toRule(rule));
+    }
+    return rules;
+  }
+
+  private org.sonar.api.rules.Rule toRule(Rule rule) {
+    org.sonar.api.rules.Rule apiRule = new org.sonar.api.rules.Rule();
+    apiRule.setCharacteristicId(rule.)
+    System.out.println("rule = " + rule);
+    return null;
+  }
+
+  private RuleQuery toQuery(org.sonar.api.rules.RuleQuery query) {
+    return null;
+  }
+//
+//  private Query createHqlQuery(DatabaseSession session, org.sonar.api.rules.RuleQuery query) {
+//    StringBuilder hql = new StringBuilder().append("from ").append(Rule.class.getSimpleName()).append(" where status<>:status ");
+//    Map<String, Object> params = new HashMap<String, Object>();
+//    params.put("status", Rule.STATUS_REMOVED);
+//    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;
+//  }
+}
index c40326a63ea440e59bf63f12c14c433fd5288a5a..ee6d9a00b660d518df348a5653e8fc1d733fd28f 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.server.rule.index;
 
 import com.google.common.base.Preconditions;
+import com.google.common.collect.Lists;
 import org.elasticsearch.action.search.SearchRequestBuilder;
 import org.elasticsearch.action.search.SearchResponse;
 import org.elasticsearch.action.search.SearchType;
@@ -35,6 +36,7 @@ import org.elasticsearch.index.query.MatchQueryBuilder;
 import org.elasticsearch.index.query.QueryBuilder;
 import org.elasticsearch.index.query.QueryBuilders;
 import org.elasticsearch.index.query.QueryStringQueryBuilder;
+import org.elasticsearch.search.SearchHit;
 import org.elasticsearch.search.aggregations.AggregationBuilders;
 import org.elasticsearch.search.aggregations.bucket.terms.Terms;
 import org.elasticsearch.search.sort.FieldSortBuilder;
@@ -56,12 +58,14 @@ import org.sonar.server.search.IndexField;
 import org.sonar.server.search.QueryOptions;
 import org.sonar.server.search.Result;
 
+import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -393,4 +397,34 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
     }
     return tags;
   }
+
+  @Deprecated
+  @CheckForNull
+  public Rule getById(int id) {
+    SearchResponse response = getClient().prepareSearch(this.getIndexName())
+      .setTypes(this.getIndexType())
+      .setQuery(QueryBuilders.termQuery(RuleNormalizer.RuleField.ID.field(),id))
+      .setSize(1)
+      .get();
+    SearchHit hit = response.getHits().getAt(0);
+    if(hit == null){
+      return null;
+    } else {
+      return toDoc(hit.getSource());
+    }
+  }
+
+  @Deprecated
+  public List<Rule> getByIds(Collection<Integer> ids) {
+    SearchResponse response = getClient().prepareSearch(this.getIndexName())
+      .setTypes(this.getIndexType())
+      .setQuery(QueryBuilders.termQuery(RuleNormalizer.RuleField.ID.field(),ids))
+      .setSize(1)
+      .get();
+    List<Rule> rules = Lists.newArrayList();
+    for (SearchHit hit : response.getHits()) {
+      rules.add(toDoc(hit.getSource()));
+    }
+    return rules;
+  }
 }
index 2ec0c9bb18345e86ff7d4b67d92c450b0aacadac..663e57a542049310bc9a1954a2e4e81a7b4eb5f1 100644 (file)
@@ -41,7 +41,7 @@ import org.sonar.core.issue.db.IssueDao;
 import org.sonar.core.issue.db.IssueDto;
 import org.sonar.core.persistence.MyBatis;
 import org.sonar.core.resource.ResourceDao;
-import org.sonar.core.rule.DefaultRuleFinder;
+import org.sonar.server.rule.DefaultRuleFinder;
 import org.sonar.core.user.DefaultUser;
 import org.sonar.server.issue.actionplan.ActionPlanService;
 
diff --git a/sonar-server/src/test/java/org/sonar/server/rule/DefaultRuleFinderMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule/DefaultRuleFinderMediumTest.java
new file mode 100644 (file)
index 0000000..9083130
--- /dev/null
@@ -0,0 +1,134 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.rule;
+
+import org.fest.assertions.Assertions;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.sonar.api.rule.RuleStatus;
+import org.sonar.api.rules.Rule;
+import org.sonar.api.rules.RuleQuery;
+import org.sonar.core.persistence.DbSession;
+import org.sonar.core.rule.RuleDto;
+import org.sonar.server.db.DbClient;
+import org.sonar.server.rule.db.RuleDao;
+import org.sonar.server.tester.ServerTester;
+
+import java.util.Collections;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.fest.assertions.Assertions.assertThat;
+
+@Deprecated
+public class DefaultRuleFinderMediumTest {
+
+  @ClassRule
+  public static ServerTester tester = new ServerTester();
+
+  private DefaultRuleFinder finder;
+  private DbSession session;
+
+  @Before
+  public void setup() {
+    finder = tester.get(DefaultRuleFinder.class);
+
+    session = tester.get(DbClient.class).openSession(false);
+
+    tester.get(RuleDao.class).insert(session,
+      new RuleDto()
+        .setName("Check Header")
+        .setConfigKey("Checker/Treewalker/HeaderCheck")
+        .setRuleKey("com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck")
+        .setRepositoryKey("checkstyle")
+        .setSeverity(4)
+        .setStatus(RuleStatus.READY),
+      new RuleDto()
+        .setName("Disabled checked")
+        .setConfigKey("Checker/Treewalker/DisabledCheck")
+        .setRuleKey("DisabledCheck")
+        .setRepositoryKey("checkstyle")
+        .setSeverity(4)
+        .setStatus(RuleStatus.REMOVED),
+      new RuleDto()
+        .setName("Check Annotation")
+        .setConfigKey("Checker/Treewalker/AnnotationUseStyleCheck")
+        .setRuleKey("com.puppycrawl.tools.checkstyle.checks.annotation.AnnotationUseStyleCheck")
+        .setRepositoryKey("checkstyle")
+        .setSeverity(4)
+        .setStatus(RuleStatus.READY),
+      new RuleDto()
+        .setName("Call Super First")
+        .setConfigKey("rulesets/android.xml/CallSuperFirst")
+        .setRuleKey("CallSuperFirst")
+        .setRepositoryKey("pmd")
+        .setSeverity(2)
+        .setStatus(RuleStatus.READY)
+    );
+    session.commit();
+  }
+
+  @After
+  public void after() {
+    session.close();
+  }
+
+
+  @Test
+  public void should_success_finder_wrap() {
+
+    // should_find_by_id
+    Assertions.assertThat(finder.findById(3).getConfigKey()).isEqualTo("Checker/Treewalker/AnnotationUseStyleCheck");
+
+    // should_not_find_disabled_rule_by_id
+    Assertions.assertThat(finder.findById(2)).isNull();
+
+    // should_find_by_ids
+    // 2 is returned even its status is REMOVED
+    assertThat(finder.findByIds(newArrayList(2, 3))).hasSize(2);
+
+    // should_find_by_key
+    Rule rule = finder.findByKey("checkstyle", "com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck");
+    Assertions.assertThat(rule).isNotNull();
+    Assertions.assertThat(rule.getKey()).isEqualTo(("com.puppycrawl.tools.checkstyle.checks.header.HeaderCheck"));
+    Assertions.assertThat(rule.isEnabled()).isTrue();
+
+    // find_should_return_null_if_no_results
+    Assertions.assertThat(finder.findByKey("checkstyle", "unknown")).isNull();
+    Assertions.assertThat(finder.find(RuleQuery.create().withRepositoryKey("checkstyle").withConfigKey("unknown"))).isNull();
+
+    // find_repository_rules
+    Assertions.assertThat(finder.findAll(RuleQuery.create().withRepositoryKey("checkstyle"))).hasSize(2);
+
+    // find_all_enabled
+    Assertions.assertThat(finder.findAll(RuleQuery.create())).onProperty("id").containsOnly(1, 3, 4);
+
+    // do_not_find_disabled_rules
+    Assertions.assertThat(finder.findByKey("checkstyle", "DisabledCheck")).isNull();
+
+    // do_not_find_unknown_rules
+    Assertions.assertThat(finder.findAll(RuleQuery.create().withRepositoryKey("unknown_repository"))).isEmpty();
+
+    // should_find_by_ids_empty
+    tester.clearDbAndIndexes();
+    assertThat(finder.findByIds(Collections.<Integer>emptyList())).isEmpty();
+  }
+}