|
| 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.status; |
| 18 | + |
| 19 | +import static org.apache.logging.log4j.status.StatusLogger.PropertiesUtilsDouble.readAllAvailableProperties; |
| 20 | +import static org.apache.logging.log4j.status.StatusLogger.PropertiesUtilsDouble.readProperty; |
| 21 | +import static org.assertj.core.api.Assertions.assertThat; |
| 22 | +import static uk.org.webcompere.systemstubs.SystemStubs.restoreSystemProperties; |
| 23 | +import static uk.org.webcompere.systemstubs.SystemStubs.withEnvironmentVariable; |
| 24 | + |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.Map; |
| 27 | +import java.util.stream.Stream; |
| 28 | +import org.junit.jupiter.api.parallel.ResourceAccessMode; |
| 29 | +import org.junit.jupiter.api.parallel.ResourceLock; |
| 30 | +import org.junit.jupiter.api.parallel.Resources; |
| 31 | +import org.junit.jupiter.params.ParameterizedTest; |
| 32 | +import org.junit.jupiter.params.provider.MethodSource; |
| 33 | + |
| 34 | +class StatusLoggerPropertiesUtilDoubleTest { |
| 35 | + |
| 36 | + private static final String[] MATCHING_PROPERTY_NAMES = new String[] { |
| 37 | + // System properties for version range `[, 2.10)` |
| 38 | + "log4j2.StatusLogger.DateFormat", |
| 39 | + // System properties for version range `[2.10, 3)` |
| 40 | + "log4j2.statusLoggerDateFormat", |
| 41 | + // System properties for version range `[3,)` |
| 42 | + "log4j2.StatusLogger.dateFormat", |
| 43 | + // Environment variables |
| 44 | + "LOG4J_STATUS_LOGGER_DATE_FORMAT" |
| 45 | + }; |
| 46 | + |
| 47 | + private static final String[] NOT_MATCHING_PROPERTY_NAMES = |
| 48 | + new String[] {"log4j2.StatusLogger$DateFormat", "log4j2.StàtusLögger.DateFormat"}; |
| 49 | + |
| 50 | + private static final class TestCase { |
| 51 | + |
| 52 | + private final boolean matching; |
| 53 | + |
| 54 | + private final String propertyName; |
| 55 | + |
| 56 | + private final String userProvidedPropertyName; |
| 57 | + |
| 58 | + private TestCase(final boolean matching, final String propertyName, final String userProvidedPropertyName) { |
| 59 | + this.matching = matching; |
| 60 | + this.propertyName = propertyName; |
| 61 | + this.userProvidedPropertyName = userProvidedPropertyName; |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public String toString() { |
| 66 | + return String.format("`%s` %s `%s`", propertyName, matching ? "==" : "!=", userProvidedPropertyName); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + static Stream<TestCase> testCases() { |
| 71 | + return Stream.concat( |
| 72 | + testCases(true, MATCHING_PROPERTY_NAMES, MATCHING_PROPERTY_NAMES), |
| 73 | + testCases(false, MATCHING_PROPERTY_NAMES, NOT_MATCHING_PROPERTY_NAMES)); |
| 74 | + } |
| 75 | + |
| 76 | + private static Stream<TestCase> testCases( |
| 77 | + final boolean matching, final String[] propertyNames, final String[] userProvidedPropertyNames) { |
| 78 | + return Arrays.stream(propertyNames).flatMap(propertyName -> Arrays.stream(userProvidedPropertyNames) |
| 79 | + .map(userProvidedPropertyName -> new TestCase(matching, propertyName, userProvidedPropertyName))); |
| 80 | + } |
| 81 | + |
| 82 | + @ParameterizedTest |
| 83 | + @MethodSource("testCases") |
| 84 | + @ResourceLock(value = Resources.SYSTEM_PROPERTIES, mode = ResourceAccessMode.READ_WRITE) |
| 85 | + void system_properties_should_work(final TestCase testCase) throws Exception { |
| 86 | + restoreSystemProperties(() -> { |
| 87 | + final String expectedValue = "foo"; |
| 88 | + System.setProperty(testCase.propertyName, expectedValue); |
| 89 | + verifyProperty(testCase, expectedValue); |
| 90 | + }); |
| 91 | + } |
| 92 | + |
| 93 | + @ParameterizedTest |
| 94 | + @MethodSource("testCases") |
| 95 | + @ResourceLock(value = Resources.GLOBAL, mode = ResourceAccessMode.READ_WRITE) |
| 96 | + void environment_variables_should_work(final TestCase testCase) throws Exception { |
| 97 | + final String expectedValue = "bar"; |
| 98 | + withEnvironmentVariable(testCase.propertyName, expectedValue).execute(() -> { |
| 99 | + verifyProperty(testCase, expectedValue); |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + private static void verifyProperty(final TestCase testCase, final String expectedValue) { |
| 104 | + final Map<String, Object> normalizedProperties = readAllAvailableProperties(); |
| 105 | + final String actualValue = readProperty(normalizedProperties, testCase.userProvidedPropertyName); |
| 106 | + if (testCase.matching) { |
| 107 | + assertThat(actualValue).describedAs("" + testCase).isEqualTo(expectedValue); |
| 108 | + } else { |
| 109 | + assertThat(actualValue).describedAs("" + testCase).isNull(); |
| 110 | + } |
| 111 | + } |
| 112 | +} |
0 commit comments