Skip to content

Commit

Permalink
Guard Values against null/empty values (#1965)
Browse files Browse the repository at this point in the history
* Guard Values against null/empty values

The classes modified by this commit are `DoubleValue`, `LongValue`, and
`TimeValue`. Both `null` and empty strings provided to their
constructors fail, but they provide very different error messages
(NullPointerException and StringIndexOutOfBoundsException), which is
neither sensible nor helpful in debugging.
This commit adds a guard to throw `IllegalArgumentException` for both
cases in order to improve coherency and usefulness of the error
messages.

* fix checkstyle issues
  • Loading branch information
pingpingy1 authored Feb 14, 2024
1 parent 67e2204 commit b00322e
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/DoubleValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public DoubleValue() {
}

public DoubleValue(final String value) {
if (value == null || value.length() == 0) {
throw new IllegalArgumentException("value can neither be null nor empty.");
}
String val = value;
if (val.charAt(0) == '+') {
val = val.substring(1);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/LongValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public LongValue() {
}

public LongValue(final String value) {
if (value == null || value.length() == 0) {
throw new IllegalArgumentException("value can neither be null nor empty.");
}
String val = value;
if (val.charAt(0) == '+') {
val = val.substring(1);
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/net/sf/jsqlparser/expression/TimeValue.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public TimeValue() {
}

public TimeValue(String value) {
if (value == null || value.length() == 0) {
throw new IllegalArgumentException("value can neither be null nor empty.");
}
this.value = Time.valueOf(value.substring(1, value.length() - 1));
}

Expand Down
31 changes: 31 additions & 0 deletions src/test/java/net/sf/jsqlparser/expression/DoubleValueTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.expression;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;

public class DoubleValueTest {

@Test
public void testNullValue() {
assertThrows(IllegalArgumentException.class, () -> {
new DoubleValue(null);
});
}

@Test
public void testEmptyValue() {
assertThrows(IllegalArgumentException.class, () -> {
new DoubleValue("");
});
}
}
17 changes: 16 additions & 1 deletion src/test/java/net/sf/jsqlparser/expression/LongValueTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.math.BigInteger;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.fail;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -39,8 +40,22 @@ public void testLargeNumber() {
value.getValue();
fail("should not work");
} catch (Exception e) {
//expected to fail
// expected to fail
}
assertEquals(new BigInteger(largeNumber), value.getBigIntegerValue());
}

@Test
public void testNullStringValue() {
assertThrows(IllegalArgumentException.class, () -> {
new LongValue((String) null);
});
}

@Test
public void testEmptyStringValue() {
assertThrows(IllegalArgumentException.class, () -> {
new LongValue("");
});
}
}
31 changes: 31 additions & 0 deletions src/test/java/net/sf/jsqlparser/expression/TimeValueTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*-
* #%L
* JSQLParser library
* %%
* Copyright (C) 2004 - 2019 JSQLParser
* %%
* Dual licensed under GNU LGPL 2.1 or Apache License 2.0
* #L%
*/
package net.sf.jsqlparser.expression;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;

public class TimeValueTest {

@Test
public void testNullValue() {
assertThrows(IllegalArgumentException.class, () -> {
new TimeValue(null);
});
}

@Test
public void testEmptyValue() {
assertThrows(IllegalArgumentException.class, () -> {
new TimeValue("");
});
}
}

0 comments on commit b00322e

Please sign in to comment.