Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LongInterval does not catch NumberFormatException #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/main/java/com/greplin/interval/LongInterval.java
Expand Up @@ -105,9 +105,13 @@ public boolean intersects(final LongInterval other) {
public static LongInterval valueOf(final String value) {
String trimmed = value.trim();
int middle = trimmed.indexOf('-', 1);
long start = Long.parseLong(trimmed.substring(0, middle).trim());
long end = Long.parseLong(trimmed.substring(middle + 1).trim());
return new LongInterval(start, end);
try {
long start = Long.parseLong(trimmed.substring(0, middle).trim());
long end = Long.parseLong(trimmed.substring(middle + 1).trim());
return new LongInterval(start, end);
} catch(NumberFormatException e) {
throw new NumberFormatException("Passed value does not contain two parsable long values");
}
}


Expand Down
5 changes: 5 additions & 0 deletions src/test/java/com/greplin/interval/LongIntervalTest.java
Expand Up @@ -21,6 +21,11 @@ public void testNegativeNumbers() throws Exception {
assertParse("-2--1", -2, -1);
}

@Test(expected=NumberFormatException.class)
public void testEmptyNumbers() throws Exception {
LongInterval result = LongInterval.valueOf("null-null");
}

@Test
public void testEquals() throws Exception {
Assert.assertEquals(new LongInterval(1, 100), new LongInterval(1, 100));
Expand Down