|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to you under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.logging.log4j.core.filter; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 23 | + |
| 24 | +import org.apache.logging.log4j.core.Filter; |
| 25 | +import org.apache.logging.log4j.core.config.Configuration; |
| 26 | +import org.apache.logging.log4j.core.test.junit.LoggerContextSource; |
| 27 | +import org.junit.jupiter.api.Assertions; |
| 28 | +import org.junit.jupiter.api.Test; |
| 29 | + |
| 30 | +/** |
| 31 | + * Unit tests for {@link StringMatchFilter}. |
| 32 | + */ |
| 33 | +class StringMatchFilterTest { |
| 34 | + |
| 35 | + /** |
| 36 | + * Test the normal valid programmatic instantiation of a {@link StringMatchFilter} via its builder. |
| 37 | + */ |
| 38 | + @Test |
| 39 | + void testFilterBuilderOK() { |
| 40 | + StringMatchFilter.Builder stringMatchFilterBuilder = StringMatchFilter.newBuilder(); |
| 41 | + stringMatchFilterBuilder.setText("foo"); |
| 42 | + StringMatchFilter stringMatchFilter = stringMatchFilterBuilder.build(); |
| 43 | + assertNotNull(stringMatchFilter, "The filter should not be null."); |
| 44 | + assertEquals("foo", stringMatchFilter.getText()); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * Test that if no match-string is set on the builder, the '{@link StringMatchFilter.Builder#build()}' returns |
| 49 | + * {@code null}. |
| 50 | + */ |
| 51 | + @Test |
| 52 | + void testFilterBuilderFailsWithNullText() { |
| 53 | + StringMatchFilter.Builder stringMatchFilterBuilder = StringMatchFilter.newBuilder(); |
| 54 | + Assertions.assertNull(stringMatchFilterBuilder.build()); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Test that if a {@code null} string is set as a match-pattern, an {@code IllegalArgumentExeption} is thrown. |
| 59 | + */ |
| 60 | + @Test |
| 61 | + @SuppressWarnings({"DataFlowIssue" // invalid null parameter explicitly being tested |
| 62 | + }) |
| 63 | + void testFilterBuilderFailsWithExceptionOnNullText() { |
| 64 | + StringMatchFilter.Builder stringMatchFilterBuilder = StringMatchFilter.newBuilder(); |
| 65 | + Assertions.assertThrows(IllegalArgumentException.class, () -> stringMatchFilterBuilder.setText(null)); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Test that if an empty ({@code ""}) string is set as a match-pattern, an {@code IllegalArgumentException} is thrown. |
| 70 | + */ |
| 71 | + @Test |
| 72 | + void testFilterBuilderFailsWithExceptionOnEmptyText() { |
| 73 | + StringMatchFilter.Builder stringMatchFilterBuilder = StringMatchFilter.newBuilder(); |
| 74 | + Assertions.assertThrows(IllegalArgumentException.class, () -> stringMatchFilterBuilder.setText("")); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Test that if a {@link StringMatchFilter} is specified with a 'text' attribute it is correctly instantiated. |
| 79 | + * |
| 80 | + * @param configuration the configuration |
| 81 | + */ |
| 82 | + @Test |
| 83 | + @LoggerContextSource("log4j2-stringmatchfilter-3153-ok.xml") |
| 84 | + void testConfigurationWithTextPOS(final Configuration configuration) { |
| 85 | + final Filter filter = configuration.getFilter(); |
| 86 | + assertNotNull(filter, "The filter should not be null."); |
| 87 | + assertInstanceOf( |
| 88 | + StringMatchFilter.class, filter, "Expected a StringMatchFilter, but got: " + filter.getClass()); |
| 89 | + assertEquals("FooBar", ((StringMatchFilter) filter).getText()); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Test that if a {@link StringMatchFilter} is specified without a 'text' attribute it is not instantiated. |
| 94 | + * |
| 95 | + * @param configuration the configuration |
| 96 | + */ |
| 97 | + @Test |
| 98 | + @LoggerContextSource("log4j2-stringmatchfilter-3153-nok.xml") |
| 99 | + void testConfigurationWithTextNEG(final Configuration configuration) { |
| 100 | + final Filter filter = configuration.getFilter(); |
| 101 | + assertNull(filter, "The filter should be null."); |
| 102 | + } |
| 103 | +} |
0 commit comments