*/
package org.sonar.server.qualityprofile;
+import com.google.common.base.Objects;
import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.sonar.core.activity.ActivityLog;
private ActiveRule.Inheritance inheritance = null;
private Map<String, String> parameters = Maps.newHashMap();
- private long start;
-
private ActiveRuleChange(Type type, ActiveRuleKey key) {
this.type = type;
this.key = key;
- this.start = System.currentTimeMillis();
}
public ActiveRuleKey getKey() {
public static ActiveRuleChange createFor(Type type, ActiveRuleKey key) {
return new ActiveRuleChange(type, key);
}
+
+ @Override
+ public String toString() {
+ return Objects.toStringHelper(this)
+ .add("type", type)
+ .add("key", key)
+ .add("severity", severity)
+ .add("inheritance", inheritance)
+ .add("parameters", parameters)
+ .toString();
+ }
}
stopPropagation = true;
} else {
applySeverityAndParamToChange(activation, context, change);
- if (!activation.isCascade() && context.parentProfile() != null) {
+ if (!activation.isCascade() && context.parentActiveRule() != null) {
// override rule which is already declared on parents
change.setInheritance(context.isSameAsParent(activation) ? ActiveRule.Inheritance.INHERITED : ActiveRule.Inheritance.OVERRIDES);
}
}
+ if (context.isSame(change)) {
+ change = null;
+ }
}
- changes.add(change);
- persist(change, context, dbSession);
+ if (change != null) {
+ changes.add(change);
+ persist(change, context, dbSession);
+ }
if (!stopPropagation) {
changes.addAll(cascadeActivation(dbSession, activation));
} catch (BadRequestException e) {
// other exceptions stop the bulk activation
result.incrementFailed();
- // TODO result.addMessage
+ result.getErrors().add(e.errors());
}
}
dbSession.commit();
}
@CheckForNull
- QualityProfileDto parentProfile() {
- return parentProfile;
+ ActiveRuleDto parentActiveRule() {
+ return parentActiveRule;
}
RuleActivatorContext setParentProfile(@Nullable QualityProfileDto p) {
if (parentActiveRule == null) {
return false;
}
- if (activation.isReset()) {
+ if (activation.useDefaults()) {
return true;
}
if (StringUtils.equals(activation.getSeverity(), parentActiveRule.getSeverityString())) {
return false;
}
+ boolean isSame(ActiveRuleChange change) {
+ if (change.getInheritance()!=null && !change.getInheritance().name().equals(activeRule.getInheritance())) {
+ return false;
+ }
+ if (change.getSeverity()!=null && !change.getSeverity().equals(activeRule.getSeverityString())) {
+ return false;
+ }
+ for (Map.Entry<String, String> changeParam : change.getParameters().entrySet()) {
+ ActiveRuleParamDto param = activeRuleParams.get(changeParam.getKey());
+ if (param != null && !StringUtils.equals(changeParam.getValue(), param.getValue())) {
+ return false;
+ }
+ }
+ return true;
+ }
+
void verifyForActivation() {
if (RuleStatus.REMOVED == rule.getStatus()) {
throw new BadRequestException("Rule was removed: " + rule.getKey());
verifyHasActiveRule(activeRuleKey, Severity.CRITICAL, null, ImmutableMap.of("max", "42"));
}
+ @Test
+ public void ignore_activation_without_changes() throws Exception {
+ // initial activation
+ ActiveRuleKey activeRuleKey = ActiveRuleKey.of(XOO_PROFILE_KEY, XOO_RULE_1);
+ RuleActivation activation = new RuleActivation(activeRuleKey);
+ activation.setSeverity(Severity.BLOCKER);
+ ruleActivator.activate(activation);
+ dbSession.clearCache();
+
+ // update with exactly the same severity and params
+ RuleActivation update = new RuleActivation(ActiveRuleKey.of(XOO_PROFILE_KEY, XOO_RULE_1));
+ update.setSeverity(Severity.BLOCKER);
+ List<ActiveRuleChange> changes = ruleActivator.activate(update);
+ assertThat(changes).isEmpty();
+ }
+
@Test
public void revert_activation_to_default_severity_and_parameters() throws Exception {
// initial activation
// INHERITANCE OF PROFILES
@Test
- public void activate_on_child_profile() throws Exception {
+ public void activate_on_child_profile_but_not_on_parent() throws Exception {
createChildProfiles();
// activate on child profile, but not on root
verifyZeroActiveRules(XOO_PROFILE_KEY);
verifyOneActiveRule(XOO_CHILD_PROFILE_KEY, XOO_RULE_1, Severity.BLOCKER, null, ImmutableMap.of("max", "7"));
verifyOneActiveRule(XOO_GRAND_CHILD_PROFILE_KEY, XOO_RULE_1, Severity.BLOCKER, ActiveRuleDto.INHERITED, ImmutableMap.of("max", "7"));
+
+ // update severity on child
+ activation = new RuleActivation(ActiveRuleKey.of(XOO_CHILD_PROFILE_KEY, XOO_RULE_1));
+ activation.setSeverity(Severity.MINOR);
+ activation.setParameter("max", "77");
+ ruleActivator.activate(activation);
+ verifyZeroActiveRules(XOO_PROFILE_KEY);
+ verifyOneActiveRule(XOO_CHILD_PROFILE_KEY, XOO_RULE_1, Severity.MINOR, null, ImmutableMap.of("max", "77"));
+ verifyOneActiveRule(XOO_GRAND_CHILD_PROFILE_KEY, XOO_RULE_1, Severity.MINOR, ActiveRuleDto.INHERITED, ImmutableMap.of("max", "77"));
}
@Test
RuleActivation activation = new RuleActivation(ActiveRuleKey.of(XOO_PROFILE_KEY, XOO_RULE_1));
activation.setSeverity(Severity.BLOCKER);
activation.setParameter("max", "7");
- ruleActivator.activate(activation);
+ List<ActiveRuleChange> changes = ruleActivator.activate(activation);
+ assertThat(changes).hasSize(3);
verifyOneActiveRule(XOO_PROFILE_KEY, XOO_RULE_1, Severity.BLOCKER, null, ImmutableMap.of("max", "7"));
verifyOneActiveRule(XOO_CHILD_PROFILE_KEY, XOO_RULE_1, Severity.BLOCKER, ActiveRuleDto.INHERITED, ImmutableMap.of("max", "7"));
verifyOneActiveRule(XOO_GRAND_CHILD_PROFILE_KEY, XOO_RULE_1, Severity.BLOCKER, ActiveRuleDto.INHERITED, ImmutableMap.of("max", "7"));