support terabytes when parsing a memory size

This commit is contained in:
Havoc Pennington 2011-11-15 16:31:37 -05:00
parent 6ca952e516
commit 598c5d045f
2 changed files with 13 additions and 3 deletions
src
main/java/com/typesafe/config
test/scala/com/typesafe/config/impl

View File

@ -235,7 +235,7 @@ public final class Config {
private static enum MemoryUnit {
BYTES(1), KILOBYTES(1024), MEGABYTES(1024 * 1024), GIGABYTES(
1024 * 1024 * 1024);
1024 * 1024 * 1024), TERABYTES(1024 * 1024 * 1024 * 1024);
int bytes;
MemoryUnit(int bytes) {
@ -293,10 +293,12 @@ public final class Config {
units = MemoryUnit.MEGABYTES;
} else if (unitStringLower.equals("g") || unitString.equals("gigabyte")) {
units = MemoryUnit.GIGABYTES;
} else if (unitStringLower.equals("t") || unitString.equals("terabyte")) {
units = MemoryUnit.TERABYTES;
} else {
throw new ConfigException.BadValue(originForException,
pathForException, "Could not parse size unit '"
+ unitStringMaybePlural + "' (try b, k, m, g)");
+ unitStringMaybePlural + "' (try b, k, m, g, t)");
}
try {

View File

@ -46,11 +46,19 @@ class UnitParserTest extends TestUtils {
"1024k", "1024K", "1024 kilobytes", "1024 kilobyte",
"1m", "1M", "1 M", "1 megabytes", "1 megabyte",
"0.0009765625g", "0.0009765625G", "0.0009765625 gigabytes", "0.0009765625 gigabyte")
def parseMem(s: String) = Config.parseMemorySize(s, fakeOrigin(), "test")
for (s <- oneMegs) {
val result = Config.parseMemorySize(s, fakeOrigin(), "test")
val result = parseMem(s)
assertEquals(1024 * 1024, result)
}
assertEquals(1024 * 1024 * 1024 * 1024, parseMem("1t"))
assertEquals(1024 * 1024 * 1024 * 1024, parseMem(" 1 T "))
assertEquals(1024 * 1024 * 1024 * 1024, parseMem("1 terabyte"))
assertEquals(1024 * 1024 * 1024 * 1024, parseMem(" 1 terabytes "))
// bad units
val e = intercept[ConfigException.BadValue] {
Config.parseMemorySize("100 dollars", fakeOrigin(), "test")