From aeadc1f9129274949daaa57738c7c4550bdfbc7b Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Mon, 6 Sep 2010 14:08:06 +0000 Subject: SONAR-236 remove deprecated code from checkstyle plugin + display default value of rule parameters in Q profile console --- .../java/org/sonar/api/batch/AbstractPurge.java | 35 +++ .../org/sonar/api/database/daos/MeasuresDao.java | 75 +++++++ .../java/org/sonar/api/database/daos/RulesDao.java | 97 ++++++++ .../org/sonar/api/rules/DefaultRulesManager.java | 247 +++++++++++++++++++++ .../sonar/api/rules/DefaultRulesManagerTest.java | 90 ++++++++ 5 files changed, 544 insertions(+) create mode 100644 sonar-deprecated/src/main/java/org/sonar/api/batch/AbstractPurge.java create mode 100644 sonar-deprecated/src/main/java/org/sonar/api/database/daos/MeasuresDao.java create mode 100644 sonar-deprecated/src/main/java/org/sonar/api/database/daos/RulesDao.java create mode 100644 sonar-deprecated/src/main/java/org/sonar/api/rules/DefaultRulesManager.java create mode 100644 sonar-deprecated/src/test/java/org/sonar/api/rules/DefaultRulesManagerTest.java (limited to 'sonar-deprecated/src') diff --git a/sonar-deprecated/src/main/java/org/sonar/api/batch/AbstractPurge.java b/sonar-deprecated/src/main/java/org/sonar/api/batch/AbstractPurge.java new file mode 100644 index 00000000000..f4218432165 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/batch/AbstractPurge.java @@ -0,0 +1,35 @@ +/* + * 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.api.batch; + +import org.sonar.api.database.DatabaseSession; + +/** + * @since 1.10 + * @deprecated database components have been moved to sonar-database. Please extend org.sonar.core.purge.AbstractPurge. + */ +@Deprecated +public abstract class AbstractPurge extends org.sonar.core.purge.AbstractPurge { + + public AbstractPurge(DatabaseSession session) { + super(session); + } + +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/database/daos/MeasuresDao.java b/sonar-deprecated/src/main/java/org/sonar/api/database/daos/MeasuresDao.java new file mode 100644 index 00000000000..383864d37c8 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/database/daos/MeasuresDao.java @@ -0,0 +1,75 @@ +/* + * 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.api.database.daos; + +import org.sonar.api.measures.Metric; + +import java.util.Collection; +import java.util.List; + +@Deprecated +public class MeasuresDao { + + private org.sonar.jpa.dao.MeasuresDao target; + + public MeasuresDao(org.sonar.jpa.dao.MeasuresDao target) { + this.target = target; + } + public Metric getMetric(Metric metric) { + return target.getMetric(metric); + } + + public List getMetrics(List metrics) { + return target.getMetrics(metrics); + } + + public Metric getMetric(String metricName) { + return target.getMetric(metricName); + } + + public Collection getMetrics() { + return target.getMetrics(); + } + + public Collection getEnabledMetrics() { + return target.getEnabledMetrics(); + } + + public Collection getUserDefinedMetrics() { + return target.getUserDefinedMetrics(); + } + + public void disableAutomaticMetrics() { + target.disableAutomaticMetrics(); + } + + public void registerMetrics(Collection metrics) { + target.registerMetrics(metrics); + } + + public void persistMetric(Metric metric) { + target.persistMetric(metric); + } + + public void disabledMetrics(Collection metrics) { + target.disabledMetrics(metrics); + } + +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/database/daos/RulesDao.java b/sonar-deprecated/src/main/java/org/sonar/api/database/daos/RulesDao.java new file mode 100644 index 00000000000..1d5d5fb90bc --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/database/daos/RulesDao.java @@ -0,0 +1,97 @@ +/* + * 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.api.database.daos; + +import org.sonar.api.database.model.RuleFailureModel; +import org.sonar.api.database.model.Snapshot; +import org.sonar.api.profiles.RulesProfile; +import org.sonar.api.rules.ActiveRule; +import org.sonar.api.rules.Rule; +import org.sonar.api.rules.RuleParam; +import org.sonar.api.rules.RulesCategory; + +import java.util.List; + +@Deprecated +public class RulesDao { + + private org.sonar.jpa.dao.RulesDao target; + + public RulesDao(org.sonar.jpa.dao.RulesDao target) { + this.target = target; + } + + public List getRules() { + return target.getRules(); + } + + public List getRulesByPlugin(String pluginKey) { + return target.getRulesByPlugin(pluginKey); + } + + public List getRulesByCategory(RulesCategory categ) { + return target.getRulesByCategory(categ); + } + + public Rule getRuleByKey(String pluginKey, String ruleKey) { + return target.getRuleByKey(pluginKey, ruleKey); + } + + public Long countRules(List plugins, String categoryName) { + return target.countRules(plugins, categoryName); + } + + public List getCategories() { + return target.getCategories(); + } + + public RulesCategory getCategory(String key) { + return target.getCategory(key); + } + + + public List getRuleParams() { + return target.getRuleParams(); + } + + public RuleParam getRuleParam(Rule rule, String paramKey) { + return target.getRuleParam(rule, paramKey); + } + + public void addActiveRulesToProfile(List activeRules, int profileId, String pluginKey) { + target.addActiveRulesToProfile(activeRules, profileId, pluginKey); + } + + public List getViolations(Snapshot snapshot) { + return target.getViolations(snapshot); + } + + public void synchronizeRuleOfActiveRule(ActiveRule activeRule, String pluginKey) { + target.synchronizeRuleOfActiveRule(activeRule, pluginKey); + } + + public boolean isRuleParamEqual(RuleParam ruleParam, RuleParam ruleParamFromDatabase, String ruleKey, String pluginKey) { + return target.isRuleParamEqual(ruleParam, ruleParamFromDatabase, ruleKey, pluginKey); + } + + public RulesProfile getProfileById(int profileId) { + return target.getProfileById(profileId); + } +} diff --git a/sonar-deprecated/src/main/java/org/sonar/api/rules/DefaultRulesManager.java b/sonar-deprecated/src/main/java/org/sonar/api/rules/DefaultRulesManager.java new file mode 100644 index 00000000000..dce362a38a6 --- /dev/null +++ b/sonar-deprecated/src/main/java/org/sonar/api/rules/DefaultRulesManager.java @@ -0,0 +1,247 @@ +/* + * 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.api.rules; + +import org.apache.commons.collections.CollectionUtils; +import org.sonar.api.Plugin; +import org.sonar.api.Plugins; +import org.sonar.jpa.dao.RulesDao; +import org.sonar.api.resources.Language; + +import java.util.*; + +/** + * A class to manage and access rules defined in Sonar. + * + * @deprecated UGLY CLASS - WILL BE COMPLETELY REFACTORED IN SONAR 2.3 + */ +@Deprecated +public class DefaultRulesManager extends RulesManager { + + private final Set languages; + private final RulesRepository[] repositories; + private final Map>> rulesByLanguage; + private final Map> pluginsByLanguage; + private final Map> rulesByPluginAndKey = new HashMap>(); + private final RulesDao rulesDao; + private final Plugins plugins; + + /** + * Creates a RuleManager + * @param plugins the plugins dictionnary + * @param repositories the repositories of rules + * @param dao the dao object + */ + public DefaultRulesManager(Plugins plugins, RulesRepository[] repositories, RulesDao dao) { + this.plugins = plugins; + this.rulesDao = dao; + + languages = new HashSet(); + rulesByLanguage = new HashMap>>(); + pluginsByLanguage = new HashMap>(); + this.repositories = repositories; + + for (RulesRepository repository : repositories) { + languages.add(repository.getLanguage()); + + List> list = rulesByLanguage.get(repository.getLanguage()); + if (list == null) { + list = new ArrayList>(); + rulesByLanguage.put(repository.getLanguage(), list); + } + list.add(repository); + + List languagePlugins = pluginsByLanguage.get(repository.getLanguage()); + if (languagePlugins == null) { + languagePlugins = new ArrayList(); + pluginsByLanguage.put(repository.getLanguage(), languagePlugins); + } + languagePlugins.add(plugins.getPluginByExtension(repository)); + } + } + + /** + * Constructor for tests only + * + * @param dao the dao + */ + protected DefaultRulesManager(RulesDao dao, Plugins plugins) { + this.rulesDao = dao; + this.plugins = plugins; + languages = new HashSet(); + rulesByLanguage = new HashMap>>(); + pluginsByLanguage = new HashMap>(); + repositories = null; + + } + + /** + * Returns the list of languages for which there is a rule repository + * + * @return a Set of languages + */ + public Set getLanguages() { + return languages; + } + + /** + * Gets the list of Rules Repositories available for a language + * + * @param language the language + * @return the list of rules repositories + */ + public List> getRulesRepositories(Language language) { + List> rulesRepositories = rulesByLanguage.get(language); + if (CollectionUtils.isNotEmpty(rulesRepositories)) { + return rulesRepositories; + } + return Collections.emptyList(); + } + + /** + * Gets the complete list of Rules Repositories in the Sonar instance + * + * @return the list of rules repositories + */ + public List> getRulesRepositories() { + return Arrays.asList(repositories); + } + + /** + * Gets the list of rules plugins for a given language + * @param language the language + * @return the list of plugins + */ + public List getPlugins(Language language) { + List result = pluginsByLanguage.get(language); + if (!CollectionUtils.isEmpty(result)) { + return result; + } + return Collections.emptyList(); + } + + /** + * Gets count of rules by categories defined for a given language + * + * @param language the language + * @return a Map with the category as key and the count as value + */ + public Map countRulesByCategory(Language language) { + return countRulesByCategory(language, rulesDao); + } + + protected Map countRulesByCategory(Language language, RulesDao rulesDao) { + Map countByCategory = new HashMap(); + List result = getPlugins(language); + if (!CollectionUtils.isEmpty(result)) { + List keys = getPluginKeys(getPlugins(language)); + for (RulesCategory rulesCategory : rulesDao.getCategories()) { + Long rulesCount = rulesDao.countRules(keys, rulesCategory.getName()); + countByCategory.put(rulesCategory.getName(), rulesCount); + } + } + return countByCategory; + } + + private List getPluginKeys(List plugins) { + ArrayList keys = new ArrayList(); + for (Plugin plugin : plugins) { + keys.add(plugin.getKey()); + } + return keys; + } + + /** + * Get the list of rules plugin that implement a mechanism of export for a given language + * + * @param language the language + * @return the list of plugins + */ + public List getExportablePlugins(Language language) { + List targets = new ArrayList(); + List> rulesRepositories = getRulesRepositories(language); + for (RulesRepository repository : rulesRepositories) { + if (repository instanceof ConfigurationExportable) { + targets.add(plugins.getPluginByExtension(repository)); + } + } + return targets; + } + + /** + * Get the list of rules plugin that implement a mechanism of import for a given language + * + * @param language the language + * @return the list of plugins + */ + public List getImportablePlugins(Language language) { + List targets = new ArrayList(); + for (RulesRepository repository : getRulesRepositories(language)) { + if (repository instanceof ConfigurationImportable) { + targets.add(plugins.getPluginByExtension(repository)); + } + } + return targets; + } + + /** + * Gets a list of rules indexed by their key for a given plugin + * @param pluginKey the plugin key + * @return a Map with the rule key and the rule + */ + public Map getPluginRulesIndexedByKey(String pluginKey) { + Map rulesByKey = rulesByPluginAndKey.get(pluginKey); + if (rulesByKey == null) { + rulesByKey = new HashMap(); + List rules = rulesDao.getRulesByPlugin(pluginKey); + if (rules != null) { + for (Rule rule : rules) { + rulesByKey.put(rule.getKey(), rule); + } + } + rulesByPluginAndKey.put(pluginKey, rulesByKey); + } + return rulesByKey; + } + + /** + * Gets a collection of rules belonging to a plugin + * + * @param pluginKey the plugin key + * @return the collection of rules + */ + public Collection getPluginRules(String pluginKey) { + Map rulesByKey = getPluginRulesIndexedByKey(pluginKey); + return rulesByKey.values(); + } + + /** + * Gets a rule belonging to a defined plugin based on its key + * + * @param pluginKey the plugin key + * @param ruleKey the rule key + * @return the rule + */ + public Rule getPluginRule(String pluginKey, String ruleKey) { + Map rulesByKey = getPluginRulesIndexedByKey(pluginKey); + return rulesByKey.get(ruleKey); + } + +} diff --git a/sonar-deprecated/src/test/java/org/sonar/api/rules/DefaultRulesManagerTest.java b/sonar-deprecated/src/test/java/org/sonar/api/rules/DefaultRulesManagerTest.java new file mode 100644 index 00000000000..62aa2a3a077 --- /dev/null +++ b/sonar-deprecated/src/test/java/org/sonar/api/rules/DefaultRulesManagerTest.java @@ -0,0 +1,90 @@ +/* + * 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.api.rules; + +import org.junit.Test; +import org.sonar.api.Plugin; +import org.sonar.api.Plugins; +import org.sonar.jpa.dao.RulesDao; +import org.sonar.api.resources.Language; +import org.sonar.jpa.dao.DaoFacade; + +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class DefaultRulesManagerTest { + + @Test + public void shouldReturnsZeroRulesByCategoryWhenNoPluginsForALanguage() { + DefaultRulesManager rulesManager = createRulesManagerForAlanguageWithNoPlugins(); + + Language language = mock(Language.class); + Map result = rulesManager.countRulesByCategory(language); + assertThat(result.size(), is(0)); + } + + @Test + public void shouldReturnsZeroExportablePluginsWhenLanguageHasNoRulesPlugin() { + DefaultRulesManager rulesManager = createRulesManagerForAlanguageWithNoPlugins(); + + Language language = mock(Language.class); + List result = rulesManager.getExportablePlugins(language); + assertThat(result.size(), is(0)); + } + + @Test + public void shouldReturnsZeroImportablePluginsWhenLanguageHasNoRulesPlugin() { + DefaultRulesManager rulesManager = createRulesManagerForAlanguageWithNoPlugins(); + + Language language = mock(Language.class); + List result = rulesManager.getImportablePlugins(language); + assertThat(result.size(), is(0)); + } + + @Test + public void shouldReturnZeroPluginWhenLanguageHasNoRulesPlugin() { + DefaultRulesManager rulesManager = createRulesManagerForAlanguageWithNoPlugins(); + Language language = mock(Language.class); + List result = rulesManager.getPlugins(language); + assertThat(result.size(), is(0)); + } + + @Test + public void shouldReturnZeroRulesRepositoryWhenLanguageHasNoRulesRepository() { + DefaultRulesManager rulesManager = createRulesManagerForAlanguageWithNoPlugins(); + Language language = mock(Language.class); + List> result = rulesManager.getRulesRepositories(language); + assertThat(result.size(), is(0)); + } + + private DefaultRulesManager createRulesManagerForAlanguageWithNoPlugins() { + DaoFacade dao = mock(DaoFacade.class); + RulesDao rulesDao = mock(RulesDao.class); + when(rulesDao.getCategories()).thenReturn(Collections.emptyList()); + DefaultRulesManager rulesManager = new DefaultRulesManager(rulesDao, new Plugins(null)); + return rulesManager; + } +} -- cgit v1.2.3