Skip to content

Commit 06fd4ef

Browse files
committed
Fix #191 - default size units to bytes if not present
1 parent dce36b4 commit 06fd4ef

File tree

2 files changed

+10
-8
lines changed

2 files changed

+10
-8
lines changed

src/main/java/cd/go/contrib/elasticagent/utils/Size.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import static java.util.Objects.requireNonNull;
2828

2929
public class Size implements Comparable<Size> {
30-
private static final Pattern SIZE_PATTERN = Pattern.compile("(\\d+)\\s*(\\S+)");
30+
private static final Pattern SIZE_PATTERN = Pattern.compile("(\\d+)\\s*(\\S*)");
3131

3232
private static final Map<String, SizeUnit> SUFFIXES;
3333

@@ -101,10 +101,11 @@ public static Size parse(String size) {
101101
}
102102

103103
final double count = Double.parseDouble(matcher.group(1));
104-
final SizeUnit unit = SUFFIXES.get(matcher.group(2));
105-
if (unit == null) {
104+
String suffix = matcher.group(2);
105+
if (!suffix.isBlank() && !SUFFIXES.containsKey(suffix)) {
106106
throw new IllegalArgumentException("Invalid size: " + size + ". Wrong size unit");
107107
}
108+
final SizeUnit unit = SUFFIXES.getOrDefault(suffix, SizeUnit.BYTES);
108109

109110
return new Size(count, unit);
110111
}

src/test/java/cd/go/contrib/elasticagent/utils/SizeTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ public void shouldThrowExceptionIfTheGivenSizeHasDifferentSuffix() {
3737
assertThrows(IllegalArgumentException.class, () -> Size.parse("24000kig"));
3838
}
3939

40-
@Test
41-
public void shouldThrowExceptionIfTheGivenSizeHasNoUint() {
42-
assertThrows(IllegalArgumentException.class, () -> Size.parse("24000"));
43-
}
44-
4540
@Test
4641
public void shouldThrowExceptionIfTheGivenSizeIsNull() {
4742
assertThrows(IllegalArgumentException.class, () -> Size.parse(null));
@@ -57,6 +52,12 @@ public void fromQuantity() {
5752
assertEquals("58 GB", Size.fromQuantity(Quantity.parse("58Gi")).readableSize());
5853
}
5954

55+
@Test
56+
public void defaultSizeUnitIsBytes() {
57+
Size oneKB = Size.parse("1024");
58+
assertEquals(oneKB.toBytes(), 1024, 0);
59+
}
60+
6061
@Test
6162
public void toBytes() {
6263
Size oneKB = Size.parse("1Ki");

0 commit comments

Comments
 (0)