-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Update StringMatchFilter builder API (#3509) #3510
base: main
Are you sure you want to change the base?
Conversation
* Guard against NPEs * added private constructor to builder * added getText() accessor * add JVerify nullability annotations * added detailed javadoc with implementation details * optimized AbstractLifeCycle and AbstractFilter "equalsImpl" implementations * added changelog
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.
My 2 cents:
}) | ||
void testFilterBuilderFailsWithExceptionOnNullText() { | ||
StringMatchFilter.Builder stringMatchFilterBuilder = StringMatchFilter.newBuilder(); | ||
Assertions.assertThrows(IllegalArgumentException.class, () -> stringMatchFilterBuilder.setText(null)); |
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.
This is certainly debatable, but I would rather expect NPE to be thrown here.
The way I see it, there are two levels of validation:
- The parameter is annotated as
@NonNull
and we expect the programmer to check for that. In this case NPE seems appropriate. - There is a more involved validation process that requires a non-empty string. In this case
IllegalArgumentException
seems appropriate.
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.
this is only because I used the Log4j Assert which throws IllegalArgumentException.
public Builder setText(final String text) {
this.text = Assert.requireNonEmpty(text, "The 'text' argument must not be null or empty.");
return this;
}
I could of course add an extra Objects.requiresNonNull if you think that is better?
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.
Maybe we can modify Assert.requireNonEmpty
? What do you think?
It is mostly a question of convention (and we have none), but I think that it is worth differentiating the null
and empty cases, since there are plenty of tools that do static analysis on null
s and the caller could use one.
void testConfigurationWithTextNEG(final Configuration configuration) { | ||
final Filter filter = configuration.getFilter(); | ||
assertNull(filter, "The filter should be null."); | ||
} |
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.
This would rather belong in a unit test for PluginBuilder
or similar.
The fact that the user misspells StringMatchFilter
or another name, doesn't seem relevant.
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.
this simply tests that a misconfigured (or not at all) fails on build and returns null I think.
A "" invalid name would throw in an entirely different locattion I think - unresolved plugin type.
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.
Unless I am mistaken this tests that a misspelled plugin name (StringMatchFfilter
) will be ignored.
Since the plugin name is misspelled PluginBuilder
will not find the plugin and return null
. No code in StringMatchFilter.java
will be executed.
public Result filter( | ||
final Logger logger, final Level level, final Marker marker, final Object msg, final Throwable t) { | ||
return filter(logger.getMessageFactory().newMessage(msg).getFormattedMessage()); | ||
public Result filter(final LogEvent event) { | ||
Objects.requireNonNull(event, "The 'event' argument must not be null."); | ||
return filter(event.getMessage().getFormattedMessage()); |
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 don't sort methods. It makes it much harder to review the code.
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.
Agreed with @ppkarwasz. Please strive to avoid cosmetic changes on the existing code in PRs. Feel free to submit a follow-up PR/commit for these kind of changes.
#3509
Fix StringMatchFilter Validation