ICU-13227 Throwing NullPointerException on string methods in DecimalFormat

X-SVN-Rev: 40170
This commit is contained in:
Shane Carr 2017-06-15 01:45:12 +00:00
parent 7d05feb7a6
commit 8b95db7157
2 changed files with 64 additions and 3 deletions

View file

@ -888,6 +888,9 @@ public class DecimalFormat extends NumberFormat {
* @stable ICU 2.0
*/
public synchronized void setPositivePrefix(String prefix) {
if (prefix == null) {
throw new NullPointerException();
}
properties.setPositivePrefix(prefix);
refreshFormatter();
}
@ -918,12 +921,15 @@ public class DecimalFormat extends NumberFormat {
* <p>Using this method overrides the affix specified via the pattern, and unlike the pattern, the
* string given to this method will be interpreted literally WITHOUT locale symbol substitutions.
*
* @param suffix The literal string to prepend to negative numbers.
* @param prefix The literal string to prepend to negative numbers.
* @category Affixes
* @stable ICU 2.0
*/
public synchronized void setNegativePrefix(String suffix) {
properties.setNegativePrefix(suffix);
public synchronized void setNegativePrefix(String prefix) {
if (prefix == null) {
throw new NullPointerException();
}
properties.setNegativePrefix(prefix);
refreshFormatter();
}
@ -958,6 +964,9 @@ public class DecimalFormat extends NumberFormat {
* @stable ICU 2.0
*/
public synchronized void setPositiveSuffix(String suffix) {
if (suffix == null) {
throw new NullPointerException();
}
properties.setPositiveSuffix(suffix);
refreshFormatter();
}
@ -993,6 +1002,9 @@ public class DecimalFormat extends NumberFormat {
* @stable ICU 2.0
*/
public synchronized void setNegativeSuffix(String suffix) {
if (suffix == null) {
throw new NullPointerException();
}
properties.setNegativeSuffix(suffix);
refreshFormatter();
}
@ -2473,6 +2485,9 @@ public class DecimalFormat extends NumberFormat {
* @see PatternString#parseToExistingProperties
*/
void setPropertiesFromPattern(String pattern, int ignoreRounding) {
if (pattern == null) {
throw new NullPointerException();
}
PatternString.parseToExistingProperties(pattern, properties, ignoreRounding);
}

View file

@ -19,6 +19,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationTargetException;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.text.AttributedCharacterIterator;
@ -5312,6 +5313,51 @@ public class NumberFormatTest extends TestFmwk {
}
}
@Test
public void testStringMethodsNPE() {
String[] npeMethods = {
"applyLocalizedPattern",
"applyPattern",
"setNegativePrefix",
"setNegativeSuffix",
"setPositivePrefix",
"setPositiveSuffix"
};
for (String npeMethod : npeMethods) {
DecimalFormat df = new DecimalFormat();
try {
DecimalFormat.class.getDeclaredMethod(npeMethod, String.class).invoke(df, (String) null);
fail("NullPointerException not thrown in method " + npeMethod);
} catch (InvocationTargetException e) {
assertTrue("Exception should be NullPointerException in method " + npeMethod,
e.getCause() instanceof NullPointerException);
} catch (Exception e) {
// Other reflection exceptions
throw new AssertionError("Reflection error in method " + npeMethod, e);
}
}
// Also test the constructors
try {
new DecimalFormat(null);
fail("NullPointerException not thrown in 1-parameter constructor");
} catch (NullPointerException e) {
// Expected
}
try {
new DecimalFormat(null, new DecimalFormatSymbols());
fail("NullPointerException not thrown in 2-parameter constructor");
} catch (NullPointerException e) {
// Expected
}
try {
new DecimalFormat(null, new DecimalFormatSymbols(), CurrencyPluralInfo.getInstance(), 0);
fail("NullPointerException not thrown in 4-parameter constructor");
} catch (NullPointerException e) {
// Expected
}
}
@Test
public void testParseGroupingMode() {
ULocale[] locales = { // GROUPING DECIMAL