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

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

Closed
wants to merge 8 commits into from

Conversation

Carpe-Wang
Copy link
Contributor

@Carpe-Wang Carpe-Wang commented Nov 6, 2023

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

Purpose

Checklist

  • New code follows the Google Java Style Guide
  • If necessary, new public API validates arguments, for example rejects null
  • New public API has Javadoc
    • Javadoc uses @since $next-version$
      ($next-version$ is a special placeholder which is automatically replaced during release)
  • If necessary, new unit tests have been added
    • Assertions in unit tests use Truth, see existing tests
    • No JUnit 3 features are used (such as extending class TestCase)
    • If this pull request fixes a bug, a new test was added for a situation which failed previously and is now fixed
  • mvn clean verify javadoc:jar passes without errors

…NumberFormatException extends IllegalArgumentException, it is only necessary to write IllegalArgumentException.
Copy link

google-cla bot commented Nov 6, 2023

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

Copy link
Member

@eamonnmcmanus eamonnmcmanus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great, thanks! Could you add a test, for example in DefaultTypeAdaptersTest, that fails with the current code and passes with this change? You could use assertThrows: with the current code the test will fail because no exception will be thrown.

builder.setDateFormat(invalidPattern);
});
}
@Test
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just one tiny thing: could you add a blank line before each of these new test methods?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, I am sorry about that.

@Carpe-Wang
Copy link
Contributor Author

This looks great, thanks! Could you add a test, for example in DefaultTypeAdaptersTest, that fails with the current code and passes with this change? You could use assertThrows: with the current code the test will fail because no exception will be thrown.

Please check again to ensure it meets the requirements.

Copy link
Member

@eamonnmcmanus eamonnmcmanus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I am just checking this against Google's internal tests to see if they show up anything unexpected. If not, I'll go ahead and merge this.

@Marcono1234
Copy link
Collaborator

Should the new tests maybe be moved to GsonBuilderTest instead since they seem to only test GsonBuilder functionality?

Also, could you please change the title of this PR, there is an "Edit" button at the top right of the GitHub UI1. The main change here seems to be about validating the date format, and issue 2472 might not be that closely related that it is worth mentioning it in the final commit message after merge. What do you think about something like this as title (based on what you wrote in the description)?

Throw exception if GsonBuilder.setDateFormat input pattern is invalid

Footnotes

  1. Or alternatively, @eamonnmcmanus could you please adjust the message when merging?

@@ -595,7 +596,12 @@ public GsonBuilder disableHtmlEscaping() {
*/
@CanIgnoreReturnValue
public GsonBuilder setDateFormat(String pattern) {
// TODO(Joel): Make this fail fast if it is an invalid date format
try {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears null might actually be a valid pattern value to clear any custom pattern previously set for the builder. The changes here would not support that anymore.

@eamonnmcmanus, should null be supported?

Apparently no unit test is covering that yet, might be good to add one (or I can add one in a separate PR if desired).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if pattern is null, here will throw null point exception.

@Carpe-Wang Carpe-Wang changed the title issue 2472 throw exception if it is an invalid date format and since … validating the date format, add test case, since NumberFormatException extends IllegalArgumentException, it is only necessary to write IllegalArgumentException. Nov 7, 2023
@Carpe-Wang
Copy link
Contributor Author

Should the new tests maybe be moved to GsonBuilderTest instead since they seem to only test GsonBuilder functionality?

Also, could you please change the title of this PR, there is an "Edit" button at the top right of the GitHub UI1. The main change here seems to be about validating the date format, and issue 2472 might not be that closely related that it is worth mentioning it in the final commit message after merge. What do you think about something like this as title (based on what you wrote in the description)?

Throw exception if GsonBuilder.setDateFormat input pattern is invalid

Footnotes

  1. Or alternatively, @eamonnmcmanus could you please adjust the message when merging?

I had change the title. Please check again to ensure it meets the requirements. In addition I'm not sure if I need to move the test cases to GsonBuilderTest, and I see a conflict with a small note that says "Only those with write access to this repository can merge pull requests." Do I need to resolve this conflict by myself?

@eamonnmcmanus
Copy link
Member

I see a conflict with a small note that says "Only those with write access to this repository can merge pull requests." Do I need to resolve this conflict by myself?

Yes, you should resolve the conflict. I will merge the PR when it is ready.

I don't have a strong opinion on where the new tests should be. We may move them later. On the other hand, I agree with @Marcono1234 that we probably shouldn't change the behaviour for setDateFormat(null). So the call to new SimpleDateFormat should be guarded by a != null check.

offset += 1;
}
// second and milliseconds can be optional
if (date.length() > offset) {

Check warning

Code scanning / CodeQL

Dereferenced variable may be null Warning

Variable
date
may be null at this access as suggested by
this
null guard.

if (hasT) {
if (!hasT && (date.length() <= offset)) {

Check warning

Code scanning / CodeQL

Dereferenced variable may be null Warning

Variable
date
may be null at this access as suggested by
this
null guard.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am trying to solve that

@Carpe-Wang Carpe-Wang closed this Nov 7, 2023
@Carpe-Wang
Copy link
Contributor Author

I close the pr, because I make error when I solve conflict. I make a new pr 2538. Sorry for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants