diff options
author | Stephane Gamard <stephane.gamard@searchbox.com> | 2014-06-27 16:53:37 +0200 |
---|---|---|
committer | Stephane Gamard <stephane.gamard@searchbox.com> | 2014-06-30 14:46:43 +0200 |
commit | 81b79922b8ed6cbdf1e4d03d4fc3d4615e53d88d (patch) | |
tree | 9c3af7fe64ce816caabd8a23b2ce4112916b46b3 /sonar-core | |
parent | ea918c95cd59fff1d04b44a530c81599df131e1a (diff) | |
download | sonarqube-81b79922b8ed6cbdf1e4d03d4fc3d4615e53d88d.tar.gz sonarqube-81b79922b8ed6cbdf1e4d03d4fc3d4615e53d88d.zip |
Deprecating DefaultRuleFinder in core.
Diffstat (limited to 'sonar-core')
4 files changed, 75 insertions, 260 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/rule/CacheRuleFinder.java b/sonar-core/src/main/java/org/sonar/core/rule/CacheRuleFinder.java index 3ff1505ce2f..8747055a4ee 100644 --- a/sonar-core/src/main/java/org/sonar/core/rule/CacheRuleFinder.java +++ b/sonar-core/src/main/java/org/sonar/core/rule/CacheRuleFinder.java @@ -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 index 7cbbdd24098..00000000000 --- a/sonar-core/src/main/java/org/sonar/core/rule/DefaultRuleFinder.java +++ /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 index d754734b98a..00000000000 --- a/sonar-core/src/test/java/org/sonar/core/rule/DefaultRuleFinderTest.java +++ /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 index f01f4cf5fc0..00000000000 --- a/sonar-core/src/test/resources/org/sonar/core/rule/DefaultRuleFinderTest/shared.xml +++ /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> |