Skip to content

Commit

Permalink
validating the date format, add test case, since NumberFormatExceptio… (
Browse files Browse the repository at this point in the history
#2538)

* validating the date format, add test case, since NumberFormatException extends IllegalArgumentException, it is only necessary to write IllegalArgumentException.

* Apply code formatting with Spotless

---------

Co-authored-by: Carpe-Wang <wangcarpe@126.com>
  • Loading branch information
Carpe-Wang and Carpe-Wang authored Nov 18, 2023
1 parent bf57258 commit 335ff0f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
10 changes: 9 additions & 1 deletion gson/src/main/java/com/google/gson/GsonBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import com.google.gson.stream.JsonWriter;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -598,7 +599,14 @@ public GsonBuilder disableHtmlEscaping() {
*/
@CanIgnoreReturnValue
public GsonBuilder setDateFormat(String pattern) {
// TODO(Joel): Make this fail fast if it is an invalid date format
if (pattern != null) {
try {
new SimpleDateFormat(pattern);
} catch (IllegalArgumentException e) {
// Throw exception if it is an invalid date format
throw new IllegalArgumentException("The date pattern '" + pattern + "' is not valid", e);
}
}
this.datePattern = pattern;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,7 @@ public static Date parse(String date, ParsePosition pos) throws ParseException {
return calendar.getTime();
// If we get a ParseException it'll already have the right message/offset.
// Other exception types can convert here.
} catch (IndexOutOfBoundsException e) {
fail = e;
} catch (NumberFormatException e) {
fail = e;
} catch (IllegalArgumentException e) {
} catch (IndexOutOfBoundsException | IllegalArgumentException e) {
fail = e;
}
String input = (date == null) ? null : ('"' + date + '"');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.google.gson.functional;

import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;

import com.google.gson.Gson;
Expand Down Expand Up @@ -737,6 +738,24 @@ public void testStringBufferDeserialization() {
assertThat(sb.toString()).isEqualTo("abc");
}

@Test
public void testSetDateFormatWithInvalidPattern() {
GsonBuilder builder = new GsonBuilder();
String invalidPattern = "This is a invalid Pattern";
assertThrows(
IllegalArgumentException.class,
() -> {
builder.setDateFormat(invalidPattern);
});
}

@Test
public void testSetDateFormatWithValidPattern() {
GsonBuilder builder = new GsonBuilder();
String validPattern = "yyyy-MM-dd";
builder.setDateFormat(validPattern);
}

private static class MyClassTypeAdapter extends TypeAdapter<Class<?>> {
@Override
public void write(JsonWriter out, Class<?> value) throws IOException {
Expand Down

0 comments on commit 335ff0f

Please sign in to comment.