From 3cc63963cfd20176c605c27f023fb6e70db69b80 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Tue, 7 Sep 2010 23:23:23 +0000 Subject: SONAR-1549 The Sonar profile creation form doesn't allow to import checkstyle,pmd and Findbugs conf files when several langages are defined SONAR-440 add warnings to checkstyle when importing configuration --- .../java/org/sonar/core/rule/DefaultRuleProvider.java | 18 +++++++++++------- .../org/sonar/core/rule/DefaultRuleProviderTest.java | 8 ++++++++ 2 files changed, 19 insertions(+), 7 deletions(-) (limited to 'sonar-core') diff --git a/sonar-core/src/main/java/org/sonar/core/rule/DefaultRuleProvider.java b/sonar-core/src/main/java/org/sonar/core/rule/DefaultRuleProvider.java index faac5971355..0512cff60e9 100644 --- a/sonar-core/src/main/java/org/sonar/core/rule/DefaultRuleProvider.java +++ b/sonar-core/src/main/java/org/sonar/core/rule/DefaultRuleProvider.java @@ -20,6 +20,7 @@ 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.RuleProvider; import org.sonar.api.rules.RuleQuery; @@ -43,30 +44,33 @@ public class DefaultRuleProvider implements RuleProvider { } public Rule find(RuleQuery query) { - return (Rule) createHqlQuery(query).getSingleResult(); + DatabaseSession session = sessionFactory.getSession(); + return (Rule)session.getSingleResult(createHqlQuery(session, query), null); + } public Collection findAll(RuleQuery query) { - return createHqlQuery(query).getResultList(); + DatabaseSession session = sessionFactory.getSession(); + return createHqlQuery(session, query).getResultList(); } - private Query createHqlQuery(RuleQuery query) { + private Query createHqlQuery(DatabaseSession session, RuleQuery query) { StringBuilder hql = new StringBuilder().append("from ").append(Rule.class.getSimpleName()).append(" where enabled=true "); Map params = new HashMap(); if (StringUtils.isNotBlank(query.getRepositoryKey())) { - hql.append("AND pluginName=:repositoryKey"); + hql.append("AND pluginName=:repositoryKey "); params.put("repositoryKey", query.getRepositoryKey()); } if (StringUtils.isNotBlank(query.getKey())) { - hql.append("AND key=:key"); + hql.append("AND key=:key "); params.put("key", query.getKey()); } if (StringUtils.isNotBlank(query.getConfigKey())) { - hql.append("AND configKey=:configKey"); + hql.append("AND configKey=:configKey "); params.put("configKey", query.getConfigKey()); } - Query hqlQuery = sessionFactory.getSession().createQuery(hql.toString()); + Query hqlQuery = session.createQuery(hql.toString()); for (Map.Entry entry : params.entrySet()) { hqlQuery.setParameter(entry.getKey(), entry.getValue()); } diff --git a/sonar-core/src/test/java/org/sonar/core/rule/DefaultRuleProviderTest.java b/sonar-core/src/test/java/org/sonar/core/rule/DefaultRuleProviderTest.java index 10830c4256f..69287804e8c 100644 --- a/sonar-core/src/test/java/org/sonar/core/rule/DefaultRuleProviderTest.java +++ b/sonar-core/src/test/java/org/sonar/core/rule/DefaultRuleProviderTest.java @@ -44,6 +44,14 @@ public class DefaultRuleProviderTest extends AbstractDbUnitTestCase { assertThat(rule.isEnabled(), is(true)); } + @Test + public void findReturnsNullIfNoResults() { + setupData("shared"); + DefaultRuleProvider provider = new DefaultRuleProvider(getSessionFactory()); + assertNull(provider.findByKey("checkstyle", "unknown")); + assertNull(provider.find(RuleQuery.create().withRepositoryKey("checkstyle").withConfigKey("unknown"))); + } + @Test public void findRepositoryRules() { setupData("shared"); -- cgit v1.2.3