aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src
diff options
context:
space:
mode:
authorsimonbrandhof <simon.brandhof@gmail.com>2010-10-04 22:01:29 +0000
committersimonbrandhof <simon.brandhof@gmail.com>2010-10-04 22:01:29 +0000
commit4393d073128d84efce75f4b85792c19506c3811d (patch)
treeea3f8eac0c2fef9b511fec45f9197698e31fed2c /sonar-plugin-api/src
parenta8f6e5c63afe3d64ffede32cb72ee707ffcf91dc (diff)
downloadsonarqube-4393d073128d84efce75f4b85792c19506c3811d.tar.gz
sonarqube-4393d073128d84efce75f4b85792c19506c3811d.zip
quality model API: simplify difference between property numeric value and text value
Diffstat (limited to 'sonar-plugin-api/src')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/Characteristic.java24
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/CharacteristicProperty.java24
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/CharacteristicPropertyTest.java33
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/CharacteristicTest.java33
4 files changed, 39 insertions, 75 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 fb1bac76cc4..27b6bce4402 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
@@ -20,7 +20,6 @@
package org.sonar.api.qualitymodel;
import com.google.common.collect.Lists;
-import com.google.common.collect.Maps;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
@@ -31,7 +30,6 @@ import org.sonar.api.rules.Rule;
import javax.persistence.*;
import java.util.Collections;
import java.util.List;
-import java.util.Map;
/**
* @since 2.3
@@ -254,10 +252,10 @@ public final class Characteristic implements Comparable<Characteristic> {
}
public CharacteristicProperty setProperty(String key, String value) {
- return addProperty(CharacteristicProperty.create(key).setValue(value));
+ return addProperty(CharacteristicProperty.create(key).setTextValue(value));
}
- public CharacteristicProperty setProperty(String key, double value) {
+ public CharacteristicProperty setProperty(String key, Double value) {
return addProperty(CharacteristicProperty.create(key).setValue(value));
}
@@ -276,22 +274,20 @@ public final class Characteristic implements Comparable<Characteristic> {
return null;
}
- public String getPropertyValueAsString(String key) {
+ public String getPropertyTextValue(String key, String defaultValue) {
CharacteristicProperty property = getProperty(key);
- return property != null ? property.getValue() : null;
+ String value = property != null ? property.getTextValue() : null;
+ return StringUtils.defaultIfEmpty(value, defaultValue);
}
- public Double getPropertyValueAsDouble(String key) {
+ public Double getPropertyValue(String key, Double defaultValue) {
CharacteristicProperty property = getProperty(key);
- return property != null ? property.getValueAsDouble() : null;
+ Double value = (property != null ? property.getValue() : null);
+ return value==null ? defaultValue : value;
}
- public Map<String,String> getProperties() {
- Map<String,String> map = Maps.newTreeMap();
- for (CharacteristicProperty property : properties) {
- map.put(property.getKey(), property.getValue());
- }
- return map;
+ public List<CharacteristicProperty> getProperties() {
+ return properties;
}
@Override
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/CharacteristicProperty.java b/sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/CharacteristicProperty.java
index 287dddc22e1..911041553e4 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/CharacteristicProperty.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/qualitymodel/CharacteristicProperty.java
@@ -74,11 +74,11 @@ public final class CharacteristicProperty {
return this;
}
- public String getValue() {
+ public String getTextValue() {
return textValue;
}
- public Double getValueAsDouble() {
+ public Double getValue() {
return value;
}
@@ -89,29 +89,11 @@ public final class CharacteristicProperty {
return null;
}
- public Boolean getValueAsBoolean() {
- if (textValue!=null) {
- return Boolean.parseBoolean(textValue);
- }
- return null;
- }
-
- public CharacteristicProperty setValue(String s) {
+ public CharacteristicProperty setTextValue(String s) {
this.textValue = s;
return this;
}
- public CharacteristicProperty setValue(Boolean b) {
- this.textValue = (b==null ? null : String.valueOf(b));
- return this;
- }
-
- public CharacteristicProperty setValue(Long l) {
- this.textValue = (l==null ? null : String.valueOf(l));
- return this;
- }
-
-
public CharacteristicProperty setValue(Double d) {
this.value = d;
return this;
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/CharacteristicPropertyTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/CharacteristicPropertyTest.java
index a7b4d7365de..77062d8a372 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/CharacteristicPropertyTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/qualitymodel/CharacteristicPropertyTest.java
@@ -28,45 +28,28 @@ import static org.junit.Assert.assertThat;
public class CharacteristicPropertyTest {
@Test
- public void testNullValue() {
+ public void testNullValues() {
CharacteristicProperty property = CharacteristicProperty.create("foo");
+ assertThat(property.getTextValue(), nullValue());
assertThat(property.getValue(), nullValue());
- assertThat(property.getValueAsDouble(), nullValue());
- assertThat(property.getValueAsBoolean(), nullValue());
assertThat(property.getValueAsLong(), nullValue());
}
@Test
- public void testBooleanValue() {
- CharacteristicProperty property = CharacteristicProperty.create("foo");
- property.setValue(true);
- assertThat(property.getValue(), is("true"));//stored in the text_value column
- assertThat(property.getValueAsDouble(), nullValue());
- assertThat(property.getValueAsBoolean(), is(true));
- assertThat(property.getValueAsLong(), nullValue());
-
- property.setValue(false);
- assertThat(property.getValue(), is("false"));
- assertThat(property.getValueAsBoolean(), is(false));
- }
-
- @Test
public void testNumericValue() {
CharacteristicProperty property = CharacteristicProperty.create("foo");
property.setValue(3.14);
- assertThat(property.getValueAsDouble(), is(3.14));//stored in the value column
+ assertThat(property.getValue(), is(3.14));//stored in the value column
assertThat(property.getValueAsLong(), is(3L));
- assertThat(property.getValue(), nullValue());
- assertThat(property.getValueAsBoolean(), nullValue());
+ assertThat(property.getTextValue(), nullValue());
}
@Test
- public void testStringValue() {
+ public void testTextValue() {
CharacteristicProperty property = CharacteristicProperty.create("foo");
- property.setValue("bar");
- assertThat(property.getValue(), is("bar"));
- assertThat(property.getValueAsDouble(), nullValue());
- assertThat(property.getValueAsBoolean(), is(false));
+ property.setTextValue("bar");
+ assertThat(property.getTextValue(), is("bar"));
+ assertThat(property.getValue(), nullValue());
assertThat(property.getValueAsLong(), nullValue());
}
}
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 26ddfb5bb99..6d6a23ce5ea 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
@@ -31,30 +31,30 @@ public class CharacteristicTest {
@Test
public void testStringProperties() {
- Characteristic characteristic = new Characteristic();
+ Characteristic characteristic = Characteristic.create();
characteristic.setProperty("foo", "bar");
assertThat(characteristic.getProperty("foo"), notNullValue());
- assertThat(characteristic.getPropertyValueAsString("foo"), is("bar"));
- assertThat(characteristic.getPropertyValueAsDouble("foo"), nullValue());
+ assertThat(characteristic.getPropertyTextValue("foo", null), is("bar"));
+ assertThat(characteristic.getPropertyValue("foo", null), nullValue());
assertThat(characteristic.getProperty("unknown"), nullValue());
- assertThat(characteristic.getPropertyValueAsString("unknown"), nullValue());
+ assertThat(characteristic.getPropertyTextValue("unknown", null), nullValue());
}
@Test
public void testDoubleProperties() {
- Characteristic characteristic = new Characteristic();
+ Characteristic characteristic = Characteristic.create();
characteristic.setProperty("foo", 3.1);
assertThat(characteristic.getProperty("foo"), notNullValue());
- assertThat(characteristic.getPropertyValueAsDouble("foo"), is(3.1));
- assertThat(characteristic.getPropertyValueAsString("foo"), nullValue());
+ assertThat(characteristic.getPropertyValue("foo", null), is(3.1));
+ assertThat(characteristic.getPropertyTextValue("foo", null), nullValue());
}
@Test
public void addProperty() {
- Characteristic characteristic = new Characteristic();
+ Characteristic characteristic = Characteristic.create();
characteristic.addProperty(CharacteristicProperty.create("foo"));
CharacteristicProperty property = characteristic.getProperty("foo");
@@ -70,13 +70,16 @@ public class CharacteristicTest {
}
@Test
- public void shouldSetNameAsKey() {
- Characteristic characteristic = new Characteristic().setName("Foo", true);
- assertThat(characteristic.getKey(), is("FOO"));
- assertThat(characteristic.getName(), is("Foo"));
+ public void shouldReturnDefaultValues() {
+ Characteristic characteristic = Characteristic.create();
+ characteristic.setProperty("foo", (String)null);
+ characteristic.setProperty("bar", (Double)null);
- characteristic = new Characteristic().setName(null, true);
- assertThat(characteristic.getKey(), nullValue());
- assertThat(characteristic.getName(), nullValue());
+ assertThat(characteristic.getPropertyTextValue("foo", "foodef"), is("foodef"));
+ assertThat(characteristic.getPropertyTextValue("other", "otherdef"), is("otherdef"));
+ assertThat(characteristic.getPropertyValue("bar", 3.14), is(3.14));
+ assertThat(characteristic.getPropertyValue("other", 3.14), is(3.14));
}
+
+
}