]> source.dussan.org Git - sonarqube.git/commitdiff
add some methods to quality model API
authorsimonbrandhof <simon.brandhof@gmail.com>
Sat, 2 Oct 2010 16:36:36 +0000 (16:36 +0000)
committersimonbrandhof <simon.brandhof@gmail.com>
Sat, 2 Oct 2010 16:36:36 +0000 (16:36 +0000)
sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/Characteristic.java
sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/Model.java
sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/CharacteristicTest.java
sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/ModelTest.java

index e6cb3b78ce6e2d73198c3caecba24374769084bb..c93be01d7fedd804817055ad3372baa6254a4159 100644 (file)
@@ -106,7 +106,7 @@ public final class Characteristic implements Comparable<Characteristic> {
   }
 
   public Characteristic setKey(String s) {
-    this.key = StringUtils.trimToEmpty(s);
+    this.key = StringUtils.trimToNull(s);
     return this;
   }
 
@@ -114,8 +114,11 @@ public final class Characteristic implements Comparable<Characteristic> {
     return name;
   }
 
+  public Characteristic setName(String s) {
+    return setName(s, false);
+  }
   public Characteristic setName(String s, boolean asKey) {
-    this.name = StringUtils.trimToEmpty(s);
+    this.name = StringUtils.trimToNull(s);
     if (asKey) {
       this.key = StringUtils.upperCase(this.name);
       this.key = StringUtils.replaceChars(this.key, ' ', '_');
index 2cd4f1f86bfe44e86ceba71c33c0a92b6b350549..923767f577ed6e5fd5f66330992599f506830fb9 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.api.qualitymodel;
 
 import com.google.common.collect.Lists;
+import org.apache.commons.lang.ObjectUtils;
 import org.apache.commons.lang.StringUtils;
 import org.apache.commons.lang.builder.ToStringBuilder;
 import org.apache.commons.lang.builder.ToStringStyle;
@@ -136,6 +137,20 @@ public final class Model implements Comparable<Model> {
     return null;
   }
 
+  /**
+   * Search for an ENABLED characteristic with the specified rule.
+   */
+  public Characteristic getCharacteristicByRule(Rule rule) {
+    if (rule != null) {
+      for (Characteristic characteristic : characteristics) {
+        if (characteristic.getEnabled() && ObjectUtils.equals(rule, characteristic.getRule())) {
+          return characteristic;
+        }
+      }
+    }
+    return null;
+  }
+
   /**
    * Search for ENABLED characteristics by their depth.
    */
index 1a665b6601d7ba61a0d42ba687580c738c4d9d51..26ddfb5bb9996a5ff959989933ec4448576d5e57 100644 (file)
@@ -61,4 +61,22 @@ public class CharacteristicTest {
     assertThat(property, notNullValue());
     assertTrue(property.getCharacteristic()==characteristic);
   }
+
+  @Test
+  public void shouldCreateByName() {
+    Characteristic characteristic = Characteristic.createByName("Foo");
+    assertThat(characteristic.getKey(), is("FOO"));
+    assertThat(characteristic.getName(), is("Foo"));
+  }
+
+  @Test
+  public void shouldSetNameAsKey() {
+    Characteristic characteristic = new Characteristic().setName("Foo", true);
+    assertThat(characteristic.getKey(), is("FOO"));
+    assertThat(characteristic.getName(), is("Foo"));
+
+    characteristic = new Characteristic().setName(null, true);
+    assertThat(characteristic.getKey(), nullValue());
+    assertThat(characteristic.getName(), nullValue());
+  }
 }
index 20b6a402f2dfd62931424a116e4b0563d9960d60..6b6e5cba1a25a2f5876d4f23db127eaa5424a14d 100644 (file)
@@ -20,6 +20,7 @@
 package org.sonar.api.qualitymodel;
 
 import org.junit.Test;
+import org.sonar.api.rules.Rule;
 
 import static org.hamcrest.CoreMatchers.nullValue;
 import static org.hamcrest.Matchers.is;
@@ -41,4 +42,22 @@ public class ModelTest {
     assertThat(model.getCharacteristics().size(), is(1));
     assertThat(model.getCharacteristics(false).size(), is(2));
   }
+
+  @Test
+  public void shouldFindCharacteristicByRule() {
+    Model model = Model.create();
+    Rule rule1 = Rule.create("checkstyle", "regexp", "Regular expression");
+    Rule rule2 = Rule.create("checkstyle", "import", "Check imports");
+
+    Characteristic efficiency = model.createCharacteristicByName("Efficiency");
+    Characteristic requirement1 = model.createCharacteristicByRule(rule1);
+    Characteristic requirement2 = model.createCharacteristicByRule(rule2);
+    efficiency.addChild(requirement1);
+    efficiency.addChild(requirement2);
+
+    assertThat(model.getCharacteristicByRule(rule1), is(requirement1));
+    assertThat(model.getCharacteristicByRule(rule2), is(requirement2));
+    assertThat(model.getCharacteristicByRule(null), nullValue());
+    assertThat(model.getCharacteristicByRule(Rule.create("foo", "bar", "Bar")), nullValue());
+  }
 }