-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
google-cloud-core: Optimize Date.parseDate(String) #4920
Merged
sduskis
merged 3 commits into
googleapis:master
from
olavloite:core-optimize-parse-date
Apr 10, 2019
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
|
||
import static com.google.common.testing.SerializableTester.reserializeAndAssert; | ||
import static com.google.common.truth.Truth.assertThat; | ||
import static org.junit.Assert.fail; | ||
|
||
import com.google.common.testing.EqualsTester; | ||
import java.text.ParseException; | ||
|
@@ -34,10 +35,51 @@ public class DateTest { | |
|
||
@Test | ||
public void parseDate() { | ||
Date date = Date.parseDate("2016-09-18"); | ||
assertThat(date.getYear()).isEqualTo(2016); | ||
assertThat(date.getMonth()).isEqualTo(9); | ||
assertThat(date.getDayOfMonth()).isEqualTo(18); | ||
verifyDate("2016-09-18", 2016, 9, 18); | ||
verifyDate("2000-01-01", 2000, 1, 1); | ||
verifyDate("9999-12-31", 9999, 12, 31); | ||
verifyDate("0001-01-01", 1, 1, 1); | ||
verifyDate("2000-02-29", 2000, 2, 29); // This is a valid leap year. | ||
verifyDate("1900-02-29", 1900, 2, 29); // This is NOT a valid leap year. | ||
verifyDate("2001-02-29", 2001, 2, 29); // Also not a valid leap year. | ||
verifyDate("2000-04-31", 2000, 4, 31); // Not a valid date. | ||
} | ||
|
||
private void verifyDate(String input, int year, int month, int day) { | ||
Date date = Date.parseDate(input); | ||
assertThat(date.getYear()).isEqualTo(year); | ||
assertThat(date.getMonth()).isEqualTo(month); | ||
assertThat(date.getDayOfMonth()).isEqualTo(day); | ||
} | ||
|
||
@Test | ||
public void parseInvalidDates() { | ||
parseInvalidDate("2016/09/18"); | ||
parseInvalidDate("2016 09 18"); | ||
parseInvalidDate("2016-9-18"); | ||
parseInvalidDate("2016-09-18T10:00"); | ||
parseInvalidDate(""); | ||
parseInvalidDate("test"); | ||
parseInvalidDate("aaaa-bb-cc"); | ||
parseInvalidDate("aaaa-01-01"); | ||
parseInvalidDate("2019-bb-01"); | ||
parseInvalidDate("2019-01-cc"); | ||
parseInvalidDate("2000-13-01"); | ||
parseInvalidDate("2000-12-32"); | ||
parseInvalidDate("10000-01-01"); | ||
parseInvalidDate("0000-01-01"); | ||
parseInvalidDate("-001-01-01"); | ||
parseInvalidDate("0001--1-01"); | ||
parseInvalidDate("0001-01--1"); | ||
} | ||
|
||
private void parseInvalidDate(String input) { | ||
try { | ||
Date.parseDate(input); | ||
fail("Expected exception"); | ||
} catch (IllegalArgumentException e) { | ||
// Ignore, this is the expected exception. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The message should contain There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added try-catch for |
||
} | ||
} | ||
|
||
@Test | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add
aaaa-bb-cc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added.