From 81b79922b8ed6cbdf1e4d03d4fc3d4615e53d88d Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Fri, 27 Jun 2014 16:53:37 +0200 Subject: [PATCH] Deprecating DefaultRuleFinder in core. --- .../org/sonar/core/rule/CacheRuleFinder.java | 77 +++++++++- .../sonar/core/rule/DefaultRuleFinder.java | 126 ---------------- .../core/rule/DefaultRuleFinderTest.java | 114 --------------- .../rule/DefaultRuleFinderTest/shared.xml | 18 --- sonar-server/pom.xml | 2 - .../server/issue/DefaultIssueFinder.java | 2 +- .../server/platform/ServerComponents.java | 2 +- .../sonar/server/rule/DefaultRuleFinder.java | 120 ++++++++++++++++ .../sonar/server/rule/index/RuleIndex.java | 34 +++++ .../server/issue/DefaultIssueFinderTest.java | 2 +- .../rule/DefaultRuleFinderMediumTest.java | 134 ++++++++++++++++++ 11 files changed, 366 insertions(+), 265 deletions(-) delete mode 100644 sonar-core/src/main/java/org/sonar/core/rule/DefaultRuleFinder.java delete mode 100644 sonar-core/src/test/java/org/sonar/core/rule/DefaultRuleFinderTest.java delete mode 100644 sonar-core/src/test/resources/org/sonar/core/rule/DefaultRuleFinderTest/shared.xml create mode 100644 sonar-server/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java create mode 100644 sonar-server/src/test/java/org/sonar/server/rule/DefaultRuleFinderMediumTest.java 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 rulesById = HashBiMap.create(); private Map> 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 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 params = new HashMap(); + 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 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 findByIds(Collection 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 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 params = new HashMap(); - 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 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 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 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 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 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 @@ - - - - - - - - - - - - - - diff --git a/sonar-server/pom.xml b/sonar-server/pom.xml index 67877dc5a99..f42a71c7e4d 100644 --- a/sonar-server/pom.xml +++ b/sonar-server/pom.xml @@ -450,10 +450,8 @@ false ${project.build.directory}/dev - development 3 ${sonar.jdbc.url} - development 127.0.0.1 9200 true diff --git a/sonar-server/src/main/java/org/sonar/server/issue/DefaultIssueFinder.java b/sonar-server/src/main/java/org/sonar/server/issue/DefaultIssueFinder.java index cc9230326d7..41364c818af 100644 --- a/sonar-server/src/main/java/org/sonar/server/issue/DefaultIssueFinder.java +++ b/sonar-server/src/main/java/org/sonar/server/issue/DefaultIssueFinder.java @@ -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; diff --git a/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java b/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java index 1bb53f1316c..f93e5a76890 100644 --- a/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java +++ b/sonar-server/src/main/java/org/sonar/server/platform/ServerComponents.java @@ -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 index 00000000000..34c07556590 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java @@ -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 findByIds(Collection ruleIds) { + List 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 findAll(org.sonar.api.rules.RuleQuery query) { + List 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 params = new HashMap(); +// 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 entry : params.entrySet()) { +// hqlQuery.setParameter(entry.getKey(), entry.getValue()); +// } +// return hqlQuery; +// } +} diff --git a/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java b/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java index c40326a63ea..ee6d9a00b66 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java @@ -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 { } 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 getByIds(Collection ids) { + SearchResponse response = getClient().prepareSearch(this.getIndexName()) + .setTypes(this.getIndexType()) + .setQuery(QueryBuilders.termQuery(RuleNormalizer.RuleField.ID.field(),ids)) + .setSize(1) + .get(); + List rules = Lists.newArrayList(); + for (SearchHit hit : response.getHits()) { + rules.add(toDoc(hit.getSource())); + } + return rules; + } } diff --git a/sonar-server/src/test/java/org/sonar/server/issue/DefaultIssueFinderTest.java b/sonar-server/src/test/java/org/sonar/server/issue/DefaultIssueFinderTest.java index 2ec0c9bb183..663e57a5420 100644 --- a/sonar-server/src/test/java/org/sonar/server/issue/DefaultIssueFinderTest.java +++ b/sonar-server/src/test/java/org/sonar/server/issue/DefaultIssueFinderTest.java @@ -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 index 00000000000..9083130358e --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/rule/DefaultRuleFinderMediumTest.java @@ -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.emptyList())).isEmpty(); + } +} -- 2.39.5