ppkarwasz commented on code in PR #3510:
URL: https://github.com/apache/logging-log4j2/pull/3510#discussion_r1977280688


##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/filter/StringMatchFilterTest.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.logging.log4j.core.filter;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+import org.apache.logging.log4j.core.Filter;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Unit tests for {@link StringMatchFilter}.
+ */
+class StringMatchFilterTest {
+
+    /**
+     * Test the normal valid programmatic instantiation of a {@link 
StringMatchFilter} via its builder.
+     */
+    @Test
+    void testFilterBuilderOK() {
+        StringMatchFilter.Builder stringMatchFilterBuilder = 
StringMatchFilter.newBuilder();
+        stringMatchFilterBuilder.setText("foo");
+        StringMatchFilter stringMatchFilter = stringMatchFilterBuilder.build();
+        assertNotNull(stringMatchFilter, "The filter should not be null.");
+        assertEquals("foo", stringMatchFilter.getText());
+    }
+
+    /**
+     * Test that if no match-string is set on the builder, the '{@link 
StringMatchFilter.Builder#build()}' returns
+     * {@code null}.
+     */
+    @Test
+    void testFilterBuilderFailsWithNullText() {
+        StringMatchFilter.Builder stringMatchFilterBuilder = 
StringMatchFilter.newBuilder();
+        Assertions.assertNull(stringMatchFilterBuilder.build());
+    }
+
+    /**
+     * Test that if a {@code null} string is set as a match-pattern, an {@code 
IllegalArgumentExeption} is thrown.
+     */
+    @Test
+    @SuppressWarnings({"DataFlowIssue" // invalid null parameter explicitly 
being tested
+    })
+    void testFilterBuilderFailsWithExceptionOnNullText() {
+        StringMatchFilter.Builder stringMatchFilterBuilder = 
StringMatchFilter.newBuilder();
+        Assertions.assertThrows(IllegalArgumentException.class, () -> 
stringMatchFilterBuilder.setText(null));

Review Comment:
   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.



##########
log4j-core-test/src/test/java/org/apache/logging/log4j/core/filter/StringMatchFilterTest.java:
##########
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to you under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.logging.log4j.core.filter;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+import org.apache.logging.log4j.core.Filter;
+import org.apache.logging.log4j.core.config.Configuration;
+import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Unit tests for {@link StringMatchFilter}.
+ */
+class StringMatchFilterTest {
+
+    /**
+     * Test the normal valid programmatic instantiation of a {@link 
StringMatchFilter} via its builder.
+     */
+    @Test
+    void testFilterBuilderOK() {
+        StringMatchFilter.Builder stringMatchFilterBuilder = 
StringMatchFilter.newBuilder();
+        stringMatchFilterBuilder.setText("foo");
+        StringMatchFilter stringMatchFilter = stringMatchFilterBuilder.build();
+        assertNotNull(stringMatchFilter, "The filter should not be null.");
+        assertEquals("foo", stringMatchFilter.getText());
+    }
+
+    /**
+     * Test that if no match-string is set on the builder, the '{@link 
StringMatchFilter.Builder#build()}' returns
+     * {@code null}.
+     */
+    @Test
+    void testFilterBuilderFailsWithNullText() {
+        StringMatchFilter.Builder stringMatchFilterBuilder = 
StringMatchFilter.newBuilder();
+        Assertions.assertNull(stringMatchFilterBuilder.build());
+    }
+
+    /**
+     * Test that if a {@code null} string is set as a match-pattern, an {@code 
IllegalArgumentExeption} is thrown.
+     */
+    @Test
+    @SuppressWarnings({"DataFlowIssue" // invalid null parameter explicitly 
being tested
+    })
+    void testFilterBuilderFailsWithExceptionOnNullText() {
+        StringMatchFilter.Builder stringMatchFilterBuilder = 
StringMatchFilter.newBuilder();
+        Assertions.assertThrows(IllegalArgumentException.class, () -> 
stringMatchFilterBuilder.setText(null));
+    }
+
+    /**
+     * Test that if an empty ({@code ""}) string is set as a match-pattern, an 
{@code IllegalArgumentException} is thrown.
+     */
+    @Test
+    void testFilterBuilderFailsWithExceptionOnEmptyText() {
+        StringMatchFilter.Builder stringMatchFilterBuilder = 
StringMatchFilter.newBuilder();
+        Assertions.assertThrows(IllegalArgumentException.class, () -> 
stringMatchFilterBuilder.setText(""));
+    }
+
+    /**
+     * Test that if a {@link StringMatchFilter} is specified with a 'text' 
attribute it is correctly instantiated.
+     *
+     * @param configuration the configuration
+     */
+    @Test
+    @LoggerContextSource("log4j2-stringmatchfilter-3153-ok.xml")
+    void testConfigurationWithTextPOS(final Configuration configuration) {
+        final Filter filter = configuration.getFilter();
+        assertNotNull(filter, "The filter should not be null.");
+        assertInstanceOf(
+                StringMatchFilter.class, filter, "Expected a 
StringMatchFilter, but got: " + filter.getClass());
+        assertEquals("FooBar", ((StringMatchFilter) filter).getText());
+    }
+
+    /**
+     * Test that if a {@link StringMatchFilter} is specified without a 'text' 
attribute it is not instantiated.
+     *
+     * @param configuration the configuration
+     */
+    @Test
+    @LoggerContextSource("log4j2-stringmatchfilter-3153-nok.xml")
+    void testConfigurationWithTextNEG(final Configuration configuration) {
+        final Filter filter = configuration.getFilter();
+        assertNull(filter, "The filter should be null.");
+    }

Review Comment:
   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.



##########
log4j-core/src/main/java/org/apache/logging/log4j/core/filter/StringMatchFilter.java:
##########
@@ -26,232 +27,551 @@
 import org.apache.logging.log4j.plugins.Plugin;
 import org.apache.logging.log4j.plugins.PluginBuilderAttribute;
 import org.apache.logging.log4j.plugins.PluginFactory;
+import org.apache.logging.log4j.plugins.util.Assert;
+import org.apache.logging.log4j.plugins.validation.constraints.Required;
 import org.apache.logging.log4j.util.PerformanceSensitive;
+import org.apache.logging.log4j.util.Strings;
+import org.jspecify.annotations.NullMarked;
+import org.jspecify.annotations.Nullable;
 
 /**
- * This filter returns the onMatch result if the message in the event matches 
the specified text
- * exactly.
+ * This filter returns the {@code onMatch} result if the formatted message 
contains the
+ * configured "{@code text}" value; otherwise, it returns the {@code 
onMismatch} result.
+ * <p>
+ *   The text comparison is case-sensitive.
+ * </p>
  */
 @Configurable(elementType = Filter.ELEMENT_TYPE, printObject = true)
+@NullMarked
 @Plugin
 @PerformanceSensitive("allocation")
 public final class StringMatchFilter extends AbstractFilter {
 
-    public static final String ATTR_MATCH = "match";
+    /** The string match text. */
     private final String text;
 
-    private StringMatchFilter(final String text, final Result onMatch, final 
Result onMismatch) {
-        super(onMatch, onMismatch);
-        this.text = text;
+    /**
+     * Constructs a new string-match filter instance.
+     *
+     * @param builder the builder implementation
+     * @throws IllegalArgumentException if the {@code text} argument is {@code 
null} or blank
+     */
+    private StringMatchFilter(final Builder builder) {
+
+        super(builder);
+
+        if (Strings.isNotEmpty(builder.text)) {
+            this.text = builder.text;
+        } else {
+            throw new IllegalArgumentException("The 'text' argument must not 
be null or empty.");
+        }
     }
 
-    @Override
-    public Result filter(
-            final Logger logger, final Level level, final Marker marker, final 
String msg, final Object... params) {
-        return filter(logger.getMessageFactory().newMessage(msg, 
params).getFormattedMessage());
+    /**
+     * Returns the string-filter match text
+     * @return the match text
+     */
+    public String getText() {
+        return this.text;
     }
 
+    /**
+     * {@inheritDoc}
+     * <p>
+     *   This implementation performs the filter evaluation on the given 
event's formatted message.
+     * </p>
+     *
+     * @throws NullPointerException if the given {@code event} is {@code null}
+     */
     @Override
-    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());

Review Comment:
   Please don't sort methods. It makes it much harder to review the code.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@logging.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to