Skip to content

Commit b36f533

Browse files
committed
Added StringMatchFilterTest with test XMLs
1 parent cd95879 commit b36f533

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.apache.logging.log4j.core.filter;
2+
3+
import org.apache.logging.log4j.core.Filter;
4+
import org.apache.logging.log4j.core.config.Configuration;
5+
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
6+
import org.junit.jupiter.api.Assertions;
7+
import org.junit.jupiter.api.Test;
8+
9+
import static org.junit.jupiter.api.Assertions.assertEquals;
10+
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
11+
import static org.junit.jupiter.api.Assertions.assertNotNull;
12+
import static org.junit.jupiter.api.Assertions.assertNull;
13+
14+
/**
15+
* Unit test for {@link StringMatchFilter}.
16+
*/
17+
public class StringMatchFilterTest {
18+
19+
/**
20+
* Test that if no match-string is set on the builder, the '{@link StringMatchFilter.Builder#build()}' returns
21+
* {@code null}.
22+
*/
23+
@Test
24+
public void testFilterBuilderFailsWithNullText() {
25+
Assertions.assertNull(StringMatchFilter.newBuilder().build());
26+
}
27+
28+
/**
29+
* Test that if a {@code null} string is set as a match-pattern, an {@code IllegalArgumentExeption} is thrown.
30+
*/
31+
@Test
32+
void testFilterBuilderFailsWithExceptionOnNullText() {
33+
Assertions.assertThrows(IllegalArgumentException.class, () -> StringMatchFilter.newBuilder().setMatchString(null));
34+
}
35+
36+
/**
37+
* Test that if an empty ({@code ""}) string is set as a match-pattern, an {@code IllegalArgumentException} is thrown.
38+
*/
39+
@Test
40+
void testFilterBuilderFailsWithExceptionOnEmptyText() {
41+
Assertions.assertThrows(IllegalArgumentException.class, () -> StringMatchFilter.newBuilder().setMatchString(""));
42+
}
43+
44+
/**
45+
* Test that if a {@link StringMatchFilter} is specified with a 'text' attribute it is correctly instantiated.
46+
*
47+
* @param configuration the configuration
48+
*/
49+
@Test
50+
@LoggerContextSource("log4j2-stringmatchfilter-3153-ok.xml")
51+
void testConfigurationWithTextPOS(final Configuration configuration) {
52+
final Filter filter = configuration.getFilter();
53+
assertNotNull(filter, "The filter should not be null.");
54+
assertInstanceOf(StringMatchFilter.class, filter, "Expected a StringMatchFilter, but got: " + filter.getClass());
55+
assertEquals("FooBar", filter.toString());
56+
}
57+
58+
/**
59+
* Test that if a {@link StringMatchFilter} is specified without a 'text' attribute it is not instantiated.
60+
*
61+
* @param configuration the configuration
62+
*/
63+
@Test
64+
@LoggerContextSource("log4j2-stringmatchfilter-3153-nok.xml")
65+
void testConfigurationWithTextNEG(final Configuration configuration) {
66+
final Filter filter = configuration.getFilter();
67+
assertNull(filter, "The filter should be null.");
68+
}
69+
70+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<Configuration status="warn">
19+
<StringMatchFfilter/>
20+
</Configuration>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<Configuration status="warn">
19+
<StringMatchFilter text="FooBar"/>
20+
</Configuration>

0 commit comments

Comments
 (0)