|
| 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.config; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 20 | + |
| 21 | +import org.apache.logging.log4j.core.LoggerContext; |
| 22 | +import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory; |
| 23 | +import org.assertj.core.api.Assertions; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +/** |
| 27 | + * Tests the change for Log4j issue #3431. |
| 28 | + * <p> |
| 29 | + * The configuration name should not be set to a default if a name already exists. |
| 30 | + * </p> |
| 31 | + * |
| 32 | + * @see <a href="https://github.com/apache/logging-log4j2/issues/3431"/> |
| 33 | + */ |
| 34 | +@SuppressWarnings("NewClassNamingConvention") |
| 35 | +class Log4j_3431_Test { |
| 36 | + |
| 37 | + /** |
| 38 | + * Tests that the name of a configurations with no defined loggers is <strong>not</strong> reset when |
| 39 | + * the configuration is started. |
| 40 | + */ |
| 41 | + @Test |
| 42 | + void testConfigurationDefaults_WithName() { |
| 43 | + |
| 44 | + try (final LoggerContext ctx = new LoggerContext("Log4j_3431_Test")) { |
| 45 | + |
| 46 | + final String name = "Log4j_3431_Configuration"; |
| 47 | + |
| 48 | + Configuration config = ConfigurationBuilderFactory.newConfigurationBuilder() |
| 49 | + .setConfigurationName(name) |
| 50 | + .setConfigurationSource(ConfigurationSource.NULL_SOURCE) |
| 51 | + .build(false); |
| 52 | + |
| 53 | + // a configuration with no defined loggers should trigger AbstractConfiguration 'setToDefault()' |
| 54 | + // from 'doConfigure()' |
| 55 | + |
| 56 | + ctx.start(config); |
| 57 | + |
| 58 | + assertEquals(name, config.getName(), "The name of the configuration should be '" + name + "'"); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Tests that the name of a configurations with no defined loggers is set to a default when |
| 64 | + * the configuration is started. |
| 65 | + */ |
| 66 | + @Test |
| 67 | + void testConfigurationDefaults_WithNoName() { |
| 68 | + |
| 69 | + try (final LoggerContext ctx = new LoggerContext("Log4j_3431_Test")) { |
| 70 | + |
| 71 | + final String name = "Log4j_3431_Configuration"; |
| 72 | + |
| 73 | + Configuration config = ConfigurationBuilderFactory.newConfigurationBuilder() |
| 74 | + .setConfigurationSource(ConfigurationSource.NULL_SOURCE) |
| 75 | + .build(false); |
| 76 | + |
| 77 | + // a configuration with no defined loggers should trigger AbstractConfiguration 'setToDefault()' |
| 78 | + // from 'doConfigure()' |
| 79 | + |
| 80 | + ctx.start(config); |
| 81 | + |
| 82 | + final String expectedPrefix = DefaultConfiguration.DEFAULT_NAME + "@"; |
| 83 | + Assertions.assertThatCharSequence(config.getName()) |
| 84 | + .withFailMessage("The name of the configuration should start with '" + expectedPrefix + "'.") |
| 85 | + .isNotBlank() |
| 86 | + .startsWith(expectedPrefix); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments