import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Maps;
-import com.google.common.collect.Sets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.collect.Lists.newArrayList;
+import static com.google.common.collect.Sets.intersection;
/**
* Register rules at server startup
private final System2 system2;
public RegisterRules(RuleDefinitionsLoader defLoader, RuleActivator ruleActivator, DbClient dbClient, RuleIndexer ruleIndexer,
- ActiveRuleIndexer activeRuleIndexer, Languages languages, System2 system2) {
+ ActiveRuleIndexer activeRuleIndexer, Languages languages, System2 system2) {
this.defLoader = defLoader;
this.ruleActivator = ruleActivator;
this.dbClient = dbClient;
if (mergeDescription(def, dto)) {
changed = true;
}
- if (!dto.getSystemTags().containsAll(def.tags())) {
- dto.setSystemTags(def.tags());
- changed = true;
- }
if (!StringUtils.equals(dto.getConfigKey(), def.internalKey())) {
dto.setConfigKey(def.internalKey());
changed = true;
if (RuleStatus.REMOVED == ruleDef.status()) {
dto.setSystemTags(Collections.<String>emptySet());
changed = true;
- } else if (!dto.getSystemTags().containsAll(ruleDef.tags())
- || !Sets.intersection(dto.getTags(), ruleDef.tags()).isEmpty()) {
+ } else if (dto.getSystemTags().size() != ruleDef.tags().size() ||
+ !dto.getSystemTags().containsAll(ruleDef.tags())
+ || !intersection(dto.getTags(), ruleDef.tags()).isEmpty()) {
dto.setSystemTags(ruleDef.tags());
// remove end-user tags that are now declared as system
RuleTagHelper.applyTags(dto, ImmutableSet.copyOf(dto.getTags()));
*/
package org.sonar.server.rule;
-import com.google.common.collect.Sets;
import java.util.Date;
import java.util.List;
import org.junit.Before;
import org.sonar.server.rule.index.RuleIndexer;
import org.sonar.server.rule.index.RuleQuery;
+import static com.google.common.collect.Sets.newHashSet;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
// user adds tags and sets markdown note
RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY1);
- rule1.setTags(Sets.newHashSet("usertag1", "usertag2"));
+ rule1.setTags(newHashSet("usertag1", "usertag2"));
rule1.setNoteData("user *note*");
rule1.setNoteUserLogin("marius");
dbClient.ruleDao().update(dbTester.getSession(), rule1);
assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(RULE_KEY1, RULE_KEY3);
}
+ @Test
+ public void add_new_tag() {
+ execute(new RulesDefinition() {
+ @Override
+ public void define(RulesDefinition.Context context) {
+ RulesDefinition.NewRepository repo = context.createRepository("fake", "java");
+ repo.createRule("rule1")
+ .setName("Rule One")
+ .setHtmlDescription("Description of Rule One")
+ .setTags("tag1");
+ repo.done();
+ }
+ });
+
+ RuleDto rule = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY1);
+ assertThat(rule.getSystemTags()).containsOnly("tag1");
+
+ execute(new RulesDefinition() {
+ @Override
+ public void define(RulesDefinition.Context context) {
+ RulesDefinition.NewRepository repo = context.createRepository("fake", "java");
+ repo.createRule("rule1")
+ .setName("Rule One")
+ .setHtmlDescription("Description of Rule One")
+ .setTags("tag1", "tag2");
+ repo.done();
+ }
+ });
+
+ rule = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY1);
+ assertThat(rule.getSystemTags()).containsOnly("tag1", "tag2");
+ }
+
@Test
public void update_only_rule_name() throws Exception {
when(system.now()).thenReturn(DATE1.getTime());
}
}
+ @Test
+ public void remove_system_tags_when_plugin_does_not_provide_any() {
+ // Rule already exists in DB, with some system tags
+ dbClient.ruleDao().insert(dbTester.getSession(), new RuleDto()
+ .setRuleKey("rule1")
+ .setRepositoryKey("findbugs")
+ .setName("Rule One")
+ .setDescription("Rule one description")
+ .setDescriptionFormat(RuleDto.Format.HTML)
+ .setSystemTags(newHashSet("tag1", "tag2"))
+ );
+ dbTester.getSession().commit();
+
+ // Synchronize rule without tag
+ execute(new FindbugsRepository());
+
+ List<RuleDto> rules = dbClient.ruleDao().selectAll(dbTester.getSession());
+ assertThat(rules).hasSize(1);
+ RuleDto result = rules.get(0);
+ assertThat(result.getKey()).isEqualTo(RuleKey.of("findbugs", "rule1"));
+ assertThat(result.getSystemTags()).isEmpty();
+ }
+
private void execute(RulesDefinition... defs) {
RuleDefinitionsLoader loader = new RuleDefinitionsLoader(mock(DeprecatedRulesDefinitionLoader.class), new RuleRepositories(), mock(CommonRuleDefinitionsImpl.class), defs);
Languages languages = mock(Languages.class);