From cc0ddbfb90f0d5b44cb0fa3e087f1ef1a5f813ec Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Tue, 18 Feb 2014 11:41:58 +0100 Subject: [PATCH] SONAR-4326 support tags in deprecated rule API --- .../sonar/api/rules/AnnotationRuleParser.java | 1 + .../org/sonar/api/rules/XMLRuleParser.java | 6 +++++ .../main/java/org/sonar/api/rules/Rule.java | 27 +++++++++++++++---- .../rule/DeprecatedRuleDefinitions.java | 1 + .../rule/DeprecatedRuleDefinitionsTest.java | 3 ++- 5 files changed, 32 insertions(+), 6 deletions(-) diff --git a/sonar-deprecated/src/main/java/org/sonar/api/rules/AnnotationRuleParser.java b/sonar-deprecated/src/main/java/org/sonar/api/rules/AnnotationRuleParser.java index f800e45c045..a5ce13160d8 100644 --- a/sonar-deprecated/src/main/java/org/sonar/api/rules/AnnotationRuleParser.java +++ b/sonar-deprecated/src/main/java/org/sonar/api/rules/AnnotationRuleParser.java @@ -72,6 +72,7 @@ public final class AnnotationRuleParser implements ServerComponent { rule.setSeverity(RulePriority.fromCheckPriority(ruleAnnotation.priority())); rule.setCardinality(ruleAnnotation.cardinality()); rule.setStatus(ruleAnnotation.status()); + rule.setTags(ruleAnnotation.tags()); List fields = FieldUtils2.getFields(clazz, true); for (Field field : fields) { diff --git a/sonar-deprecated/src/main/java/org/sonar/api/rules/XMLRuleParser.java b/sonar-deprecated/src/main/java/org/sonar/api/rules/XMLRuleParser.java index 17a60afeb74..fc2b7f7a0cb 100644 --- a/sonar-deprecated/src/main/java/org/sonar/api/rules/XMLRuleParser.java +++ b/sonar-deprecated/src/main/java/org/sonar/api/rules/XMLRuleParser.java @@ -21,6 +21,7 @@ package org.sonar.api.rules; import com.google.common.annotations.VisibleForTesting; import com.google.common.base.Strings; +import com.google.common.collect.Lists; import com.google.common.collect.Maps; import com.google.common.io.Closeables; import org.apache.commons.io.FileUtils; @@ -126,6 +127,7 @@ public final class XMLRuleParser implements ServerComponent { rule.setSeverity(RulePriority.valueOf(StringUtils.trim(priorityAttribute))); } + List tags = Lists.newArrayList(); SMInputCursor cursor = ruleC.childElementCursor(); while (cursor.getNext() != null) { @@ -154,11 +156,15 @@ public final class XMLRuleParser implements ServerComponent { } else if (StringUtils.equalsIgnoreCase("param", nodeName)) { processParameter(rule, cursor); + + } else if (StringUtils.equalsIgnoreCase("tag", nodeName)) { + tags.add(StringUtils.trim(cursor.collectDescendantText(false))); } } if (Strings.isNullOrEmpty(rule.getKey())) { throw new SonarException("Node is missing in "); } + rule.setTags(tags.toArray(new String[tags.size()])); } private static void processParameter(Rule rule, SMInputCursor ruleC) throws XMLStreamException { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java index c3596856c74..b8fe7071970 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/rules/Rule.java @@ -36,10 +36,7 @@ import javax.annotation.CheckForNull; import javax.annotation.Nullable; import javax.persistence.*; -import java.util.ArrayList; -import java.util.Date; -import java.util.List; -import java.util.Set; +import java.util.*; @Entity @Table(name = "rules") @@ -72,6 +69,11 @@ public class Rule { */ private static final Set STATUS_LIST = ImmutableSet.of(STATUS_READY, STATUS_BETA, STATUS_DEPRECATED, STATUS_REMOVED); + /** + * @since 4.2 + */ + private static final String[] DEFAULT_TAGS = new String[0]; + @Id @Column(name = "id") @GeneratedValue @@ -127,12 +129,13 @@ public class Rule { @Column(name = "updated_at", updatable = true, nullable = true) private Date updatedAt; + private transient String[] tags = DEFAULT_TAGS; + /** * @deprecated since 2.3. Use the factory method {@link #create()} */ @Deprecated public Rule() { - // TODO reduce visibility to packaete } /** @@ -422,6 +425,20 @@ public class Rule { return this; } + /** + * For definition of rule only + */ + public String[] getTags() { + return tags; + } + + /** + * For definition of rule only + */ + public void setTags(String[] tags) { + this.tags = tags; + } + @Override public boolean equals(Object obj) { if (!(obj instanceof Rule)) { diff --git a/sonar-server/src/main/java/org/sonar/server/rule/DeprecatedRuleDefinitions.java b/sonar-server/src/main/java/org/sonar/server/rule/DeprecatedRuleDefinitions.java index c2e7fb43158..650aae01ff0 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/DeprecatedRuleDefinitions.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/DeprecatedRuleDefinitions.java @@ -67,6 +67,7 @@ public class DeprecatedRuleDefinitions implements RuleDefinitions { newRule.setTemplate(Cardinality.MULTIPLE.equals(rule.getCardinality())); newRule.setSeverity(rule.getSeverity().toString()); newRule.setStatus(rule.getStatus() == null ? RuleStatus.defaultStatus() : RuleStatus.valueOf(rule.getStatus())); + newRule.setTags(rule.getTags()); for (RuleParam param : rule.getParams()) { NewParam newParam = newRule.newParam(param.getKey()); newParam.setDefaultValue(param.getDefaultValue()); diff --git a/sonar-server/src/test/java/org/sonar/server/rule/DeprecatedRuleDefinitionsTest.java b/sonar-server/src/test/java/org/sonar/server/rule/DeprecatedRuleDefinitionsTest.java index e6f07bf6311..5937637d48d 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/DeprecatedRuleDefinitionsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/DeprecatedRuleDefinitionsTest.java @@ -50,6 +50,7 @@ public class DeprecatedRuleDefinitionsTest { rule.setConfigKey("Checker/TreeWalker/ConstantName"); rule.setSeverity(RulePriority.BLOCKER); rule.setStatus(Rule.STATUS_BETA); + rule.setTags(new String[]{"style", "security"}); rule.createParameter("format").setDescription("Regular expression").setDefaultValue("A-Z").setType("REGULAR_EXPRESSION"); return Arrays.asList(rule); } @@ -77,7 +78,7 @@ public class DeprecatedRuleDefinitionsTest { assertThat(rule.severity()).isEqualTo(Severity.BLOCKER); assertThat(rule.internalKey()).isEqualTo("Checker/TreeWalker/ConstantName"); assertThat(rule.status()).isEqualTo(RuleStatus.BETA); - assertThat(rule.tags()).isEmpty(); + assertThat(rule.tags()).containsOnly("style", "security"); assertThat(rule.params()).hasSize(1); RuleDefinitions.Param param = rule.param("format"); assertThat(param).isNotNull(); -- 2.39.5