aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2010-10-02 16:36:36 +0000
committersimonbrandhof <simon.brandhof@gmail.com>2010-10-02 16:36:36 +0000
commit477c266f521aa6058b6ec9489116fd12dcdde3cf (patch)
tree36cd3b7b0b064290be3db0c0cb5277573aac55bd
parent3cf4bf1b3c8c2a10f5c0d8c2d894de34469264b5 (diff)
downloadsonarqube-477c266f521aa6058b6ec9489116fd12dcdde3cf.tar.gz
sonarqube-477c266f521aa6058b6ec9489116fd12dcdde3cf.zip
add some methods to quality model API
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/Characteristic.java7
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/Model.java15
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/CharacteristicTest.java18
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/ModelTest.java19
4 files changed, 57 insertions, 2 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/Characteristic.java b/sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/Characteristic.java
index e6cb3b78ce6..c93be01d7fe 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/Characteristic.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/Characteristic.java
@@ -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, ' ', '_');
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/Model.java b/sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/Model.java
index 2cd4f1f86bf..923767f577e 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/Model.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/Model.java
@@ -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;
@@ -137,6 +138,20 @@ public final class Model implements Comparable<Model> {
}
/**
+ * 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.
*/
public List<Characteristic> getCharacteristicsByDepth(int depth) {
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/CharacteristicTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/CharacteristicTest.java
index 1a665b6601d..26ddfb5bb99 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/CharacteristicTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/CharacteristicTest.java
@@ -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());
+ }
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/ModelTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/ModelTest.java
index 20b6a402f2d..6b6e5cba1a2 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/ModelTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/ModelTest.java
@@ -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());
+ }
}