FELIX-6009 : Metatype AD empty String default value
diff --git a/metatype/src/main/java/org/apache/felix/metatype/AD.java b/metatype/src/main/java/org/apache/felix/metatype/AD.java
index 1a2ff9a..a726a20 100644
--- a/metatype/src/main/java/org/apache/felix/metatype/AD.java
+++ b/metatype/src/main/java/org/apache/felix/metatype/AD.java
@@ -224,7 +224,7 @@
* NOTE: this method is depending on the value of {@link #getCardinality()}! Make sure that the
* cardinality is properly set <b>before</b> calling this method.
* </p>
- *
+ *
* @param defaultValue the default value to set, as encoded string-value (using comma's as separator), can be <code>null</code>.
*/
public void setDefaultValue(String defaultValue)
@@ -314,8 +314,8 @@
return new String[] { "" };
}
- List strings = new ArrayList();
- StringBuffer sb = new StringBuffer();
+ List<String> strings = new ArrayList<>();
+ StringBuilder sb = new StringBuilder();
int length = listString.length();
boolean escaped = false;
@@ -383,7 +383,7 @@
// Always add the last string, as it contains everything after the last comma...
strings.add(sb.toString());
- return (String[]) strings.toArray(new String[strings.size()]);
+ return strings.toArray(new String[strings.size()]);
}
protected Comparable convertToType(final String value)
@@ -477,6 +477,7 @@
value = Boolean.valueOf(boolValue).booleanValue();
}
+ @Override
public int compareTo(Object obj)
{
ComparableBoolean cb = (ComparableBoolean) obj;
diff --git a/metatype/src/main/java/org/apache/felix/metatype/ADValidator.java b/metatype/src/main/java/org/apache/felix/metatype/ADValidator.java
index 60b35a0..a6224af 100644
--- a/metatype/src/main/java/org/apache/felix/metatype/ADValidator.java
+++ b/metatype/src/main/java/org/apache/felix/metatype/ADValidator.java
@@ -27,29 +27,29 @@
/**
* Provides various validation routines used by the {@link AD#validate(String)}
* method.
- *
+ *
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
*/
final class ADValidator
{
/**
- * Validates a given input string according to the type specified by the given attribute
+ * Validates a given input string according to the type specified by the given attribute
* definition.
* <p>
* The validation is done in the following way:
* </p>
* <ul>
- * <li>If the input is undefined (ie. <code>null</code>), and the attribute is mandatory, the
- * validation fails due to a missing value. If the attribute is optional, the input is
+ * <li>If the input is undefined (ie. <code>null</code>), and the attribute is mandatory, the
+ * validation fails due to a missing value. If the attribute is optional, the input is
* accepted;</li>
* <li>If the input represents a <em>boolean</em> value, it is tested whether it is defined (in
* case of non-zero cardinality) and represents either <tt>"true"</tt> or <tt>"false"</tt>. The
* minimum and maximum parameters are <b>not</b> used in this validation;</li>
- * <li>If the input represents a <em>character</em> value, it is tested whether it is defined
- * (in case of non-zero cardinality). The character value must be defined within the character
+ * <li>If the input represents a <em>character</em> value, it is tested whether it is defined
+ * (in case of non-zero cardinality). The character value must be defined within the character
* range specified by the minimum and maximum parameters (if defined);</li>
* <li>If the input represents a <em>numeric</em> value, it is tested whether it is defined (in
- * case of non-zero cardinality). The numeric value must be defined within the numeric range
+ * case of non-zero cardinality). The numeric value must be defined within the numeric range
* specified by the minimum and maximum parameters (if defined);</li>
* <li>If the input represents a <em>string</em> or <em>password</em>, it is tested whether it
* is defined (in case of non-zero cardinality). The length of the string value must be in the
@@ -57,9 +57,9 @@
* </ul>
* <p>
* For all types of attributes, if it defines option values, the input should be present as one
- * of the defined option values.
+ * of the defined option values.
* </p>
- *
+ *
* @param ad
* the attribute definition to use in the validation;
* @param rawInput
@@ -122,7 +122,7 @@
/**
* Searches for a given search value in a given array of options.
- *
+ *
* @param searchValue
* the value to search for;
* @param optionValues
@@ -152,7 +152,7 @@
/**
* Parses a given string value into a numeric type.
- *
+ *
* @param type
* the type to parse;
* @param value
@@ -195,7 +195,7 @@
/**
* Parses a given string value as character, allowing <code>null</code>
* -values and empty values to be given as input.
- *
+ *
* @param value
* the value to parse as character, can be <code>null</code> or
* an empty value.
@@ -212,10 +212,10 @@
}
/**
- * Parses a given string value as numeric value, allowing
- * <code>null</code>-values and invalid numeric values to be given as
+ * Parses a given string value as numeric value, allowing
+ * <code>null</code>-values and invalid numeric values to be given as
* input.
- *
+ *
* @param type the type of number, should only be a numeric type;
* @param value
* the value to parse as integer, can be <code>null</code> or a
@@ -241,7 +241,7 @@
/**
* Validates a given input string as boolean value.
- *
+ *
* @param ad
* the attribute definition to use in the validation;
* @param input
@@ -278,7 +278,7 @@
/**
* Validates a given input string as character value.
- *
+ *
* @param ad
* the attribute definition to use in the validation;
* @param input
@@ -332,7 +332,7 @@
/**
* Validates a given input string as numeric value.
- *
+ *
* @param ad
* the attribute definition to use in the validation;
* @param input
@@ -387,7 +387,7 @@
/**
* Validates a given input string as string (or password).
- *
+ *
* @param ad
* the attribute definition to use in the validation;
* @param input
@@ -406,12 +406,9 @@
for (int i = 0; i < input.length; i++)
{
String value = input[i];
- int length = (value == null) ? 0 : value.length();
-
- if (ad.isRequired() && ((value == null) || (length == 0)))
+ final int length = value == null ? 0 : value.length();
+ if (ad.isRequired() && value == null)
{
- // Possible if the cardinality != 0 and input was something like
- // "0,,1"...
return AD.VALIDATE_MISSING;
}
// Check whether the minimum length is adhered for all values...
diff --git a/metatype/src/test/java/org/apache/felix/metatype/ADTest.java b/metatype/src/test/java/org/apache/felix/metatype/ADTest.java
index c830cfe..aa82648 100644
--- a/metatype/src/test/java/org/apache/felix/metatype/ADTest.java
+++ b/metatype/src/test/java/org/apache/felix/metatype/ADTest.java
@@ -21,10 +21,10 @@
import java.util.HashMap;
import java.util.Map;
-import junit.framework.TestCase;
-
import org.osgi.service.metatype.AttributeDefinition;
+import junit.framework.TestCase;
+
/**
* The <code>ADTest</code> class tests the static helper methods of the
* {@link AD} class.
@@ -255,11 +255,9 @@
ad.setDefaultValue("");
assertArrayEquals(new String[] { "" }, ad.getDefaultValue());
- // corner case: in case of required values, an empty default makes no sense
- // for single values, hence that the empty default is coerced into null...
ad.setRequired(true);
ad.setDefaultValue("");
- assertNull(ad.getDefaultValue());
+ assertArrayEquals(new String[] { "" }, ad.getDefaultValue());
}
/**
@@ -284,11 +282,9 @@
ad.setDefaultValue("");
assertArrayEquals(new String[] { "" }, ad.getDefaultValue());
- // corner case: in case of required values, an empty default is coerced
- // into a empty array...
ad.setRequired(true);
ad.setDefaultValue("");
- assertArrayEquals(new String[0], ad.getDefaultValue());
+ assertArrayEquals(new String[] {""}, ad.getDefaultValue());
}
private static void assertArrayEquals(String[] a, String[] b)
@@ -299,4 +295,12 @@
assertEquals(a[i], b[i]);
}
}
+
+ public void testSplitList() {
+ final String[] array = AD.splitList("321, , 123");
+ assertEquals(3, array.length);
+ assertEquals("321", array[0]);
+ assertEquals("", array[1]);
+ assertEquals("123", array[2]);
+ }
}
diff --git a/metatype/src/test/java/org/apache/felix/metatype/ADValidatorTest.java b/metatype/src/test/java/org/apache/felix/metatype/ADValidatorTest.java
index 33c02c3..5b7c11d 100644
--- a/metatype/src/test/java/org/apache/felix/metatype/ADValidatorTest.java
+++ b/metatype/src/test/java/org/apache/felix/metatype/ADValidatorTest.java
@@ -27,7 +27,7 @@
/**
* Test cases for {@link ADValidator}.
- *
+ *
* @author <a href="mailto:dev@felix.apache.org">Felix Project Team</a>
*/
public class ADValidatorTest extends TestCase
@@ -150,7 +150,7 @@
// adhere maximal values
assertEquals("", ADValidator.validate(ad, "c, d, e"));
- Map options = new HashMap();
+ Map<String, String> options = new HashMap<>();
options.put("b", "B");
options.put("c", "C");
options.put("d", "D");
@@ -349,18 +349,19 @@
// optional value
assertEquals("", ADValidator.validate(ad, null));
- // 2nd value is missing; but that's ok
+ // 2nd value is empty; but that's ok
assertEquals("", ADValidator.validate(ad, "321, , 123"));
ad.setRequired(true);
ad.setMin("3");
+
// mandatory value
assertEquals(AD.VALIDATE_MISSING, ADValidator.validate(ad, null));
- // 2nd value is missing
- assertEquals(AD.VALIDATE_MISSING, ADValidator.validate(ad, "321, , 123"));
// 2nd value is too short
assertEquals(AD.VALIDATE_LESS_THAN_MINIMUM, ADValidator.validate(ad, "321,12,123"));
+ // 2nd value is empty and therefore too short
+ assertEquals(AD.VALIDATE_LESS_THAN_MINIMUM, ADValidator.validate(ad, "321, , 123"));
// adhere minimum lengths
assertEquals("", ADValidator.validate(ad, "123, 123, 123"));
// adhere maximum lengths