-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
…3158) Add improved validation in StringMatchFilter for null/empty text Co-authored-by: Jeff Thomas <[email protected]>
- Loading branch information
1 parent
70f058d
commit f592d29
Showing
5 changed files
with
146 additions
and
3 deletions.
There are no files selected for viewing
88 changes: 88 additions & 0 deletions
88
...j-core-test/src/test/java/org/apache/logging/log4j/core/filter/StringMatchFilterTest.java
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 |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* 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 test for {@link StringMatchFilter}. | ||
*/ | ||
public class StringMatchFilterTest { | ||
|
||
/** | ||
* Test that if no match-string is set on the builder, the '{@link StringMatchFilter.Builder#build()}' returns | ||
* {@code null}. | ||
*/ | ||
@Test | ||
public void testFilterBuilderFailsWithNullText() { | ||
Assertions.assertNull(StringMatchFilter.newBuilder().build()); | ||
} | ||
|
||
/** | ||
* Test that if a {@code null} string is set as a match-pattern, an {@code IllegalArgumentExeption} is thrown. | ||
*/ | ||
@Test | ||
void testFilterBuilderFailsWithExceptionOnNullText() { | ||
Assertions.assertThrows(IllegalArgumentException.class, () -> StringMatchFilter.newBuilder() | ||
.setMatchString(null)); | ||
} | ||
|
||
/** | ||
* Test that if an empty ({@code ""}) string is set as a match-pattern, an {@code IllegalArgumentException} is thrown. | ||
*/ | ||
@Test | ||
void testFilterBuilderFailsWithExceptionOnEmptyText() { | ||
Assertions.assertThrows(IllegalArgumentException.class, () -> StringMatchFilter.newBuilder() | ||
.setMatchString("")); | ||
} | ||
|
||
/** | ||
* 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", filter.toString()); | ||
} | ||
|
||
/** | ||
* 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."); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
log4j-core-test/src/test/resources/log4j2-stringmatchfilter-3153-nok.xml
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ 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. | ||
--> | ||
<Configuration status="warn"> | ||
<StringMatchFfilter/> | ||
</Configuration> |
20 changes: 20 additions & 0 deletions
20
log4j-core-test/src/test/resources/log4j2-stringmatchfilter-3153-ok.xml
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 |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ 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. | ||
--> | ||
<Configuration status="warn"> | ||
<StringMatchFilter text="FooBar"/> | ||
</Configuration> |
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns="https://logging.apache.org/xml/ns" | ||
xsi:schemaLocation="https://logging.apache.org/xml/ns https://logging.apache.org/xml/ns/log4j-changelog-0.xsd" | ||
type="changed"> | ||
<issue id="3153" link="https://github.com/apache/logging-log4j2/issues/3153"/> | ||
<description format="asciidoc">Add improved validation to StringMatchFilter for null/empty text. GitHub issue #3153.</description> | ||
</entry> |