mirror of
https://github.com/lightbend/config.git
synced 2025-03-29 21:51:10 +08:00
throw exception if you getInt() and the int is out of range
This commit is contained in:
parent
13362f46b4
commit
4f623d9dd5
@ -3,6 +3,7 @@
|
|||||||
*/
|
*/
|
||||||
package com.typesafe.config.impl;
|
package com.typesafe.config.impl;
|
||||||
|
|
||||||
|
import com.typesafe.config.ConfigException;
|
||||||
import com.typesafe.config.ConfigOrigin;
|
import com.typesafe.config.ConfigOrigin;
|
||||||
|
|
||||||
abstract class ConfigNumber extends AbstractConfigValue {
|
abstract class ConfigNumber extends AbstractConfigValue {
|
||||||
@ -17,11 +18,23 @@ abstract class ConfigNumber extends AbstractConfigValue {
|
|||||||
this.originalText = originalText;
|
this.originalText = originalText;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public abstract Number unwrapped();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
String transformToString() {
|
String transformToString() {
|
||||||
return originalText;
|
return originalText;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int intValueRangeChecked(String path) {
|
||||||
|
long l = longValue();
|
||||||
|
if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
|
||||||
|
throw new ConfigException.WrongType(origin(), path, "32-bit integer",
|
||||||
|
"out-of-range value " + l);
|
||||||
|
}
|
||||||
|
return (int) l;
|
||||||
|
}
|
||||||
|
|
||||||
protected abstract long longValue();
|
protected abstract long longValue();
|
||||||
|
|
||||||
protected abstract double doubleValue();
|
protected abstract double doubleValue();
|
||||||
|
@ -139,15 +139,20 @@ class SimpleConfig implements Config {
|
|||||||
return (Boolean) v.unwrapped();
|
return (Boolean) v.unwrapped();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private ConfigNumber getConfigNumber(String path) {
|
||||||
|
ConfigValue v = find(path, ConfigValueType.NUMBER, path);
|
||||||
|
return (ConfigNumber) v;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Number getNumber(String path) {
|
public Number getNumber(String path) {
|
||||||
ConfigValue v = find(path, ConfigValueType.NUMBER, path);
|
return getConfigNumber(path).unwrapped();
|
||||||
return (Number) v.unwrapped();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public int getInt(String path) {
|
public int getInt(String path) {
|
||||||
return getNumber(path).intValue();
|
ConfigNumber n = getConfigNumber(path);
|
||||||
|
return n.intValueRangeChecked(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -255,9 +260,9 @@ class SimpleConfig implements Config {
|
|||||||
@Override
|
@Override
|
||||||
public List<Integer> getIntList(String path) {
|
public List<Integer> getIntList(String path) {
|
||||||
List<Integer> l = new ArrayList<Integer>();
|
List<Integer> l = new ArrayList<Integer>();
|
||||||
List<Number> numbers = getNumberList(path);
|
List<AbstractConfigValue> numbers = getHomogeneousWrappedList(path, ConfigValueType.NUMBER);
|
||||||
for (Number n : numbers) {
|
for (AbstractConfigValue v : numbers) {
|
||||||
l.add(n.intValue());
|
l.add(((ConfigNumber) v).intValueRangeChecked(path));
|
||||||
}
|
}
|
||||||
return l;
|
return l;
|
||||||
}
|
}
|
||||||
|
@ -504,6 +504,20 @@ class ConfigTest extends TestUtils {
|
|||||||
testIgnoredMergesDoNothing(root)
|
testIgnoredMergesDoNothing(root)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
def integerRangeChecks() {
|
||||||
|
val conf = parseConfig("{ tooNegative: " + (Integer.MIN_VALUE - 1L) + ", tooPositive: " + (Integer.MAX_VALUE + 1L) + "}")
|
||||||
|
val en = intercept[ConfigException.WrongType] {
|
||||||
|
conf.getInt("tooNegative")
|
||||||
|
}
|
||||||
|
assertTrue(en.getMessage.contains("range"))
|
||||||
|
|
||||||
|
val ep = intercept[ConfigException.WrongType] {
|
||||||
|
conf.getInt("tooPositive")
|
||||||
|
}
|
||||||
|
assertTrue(ep.getMessage.contains("range"))
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
def test01Getting() {
|
def test01Getting() {
|
||||||
val conf = ConfigFactory.load("test01")
|
val conf = ConfigFactory.load("test01")
|
||||||
|
Loading…
Reference in New Issue
Block a user