This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch feature/log4j-sdk
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/feature/log4j-sdk by this push:
new 7c09141a56 Fix environment variable names
7c09141a56 is described below
commit 7c09141a56f8d8ff670747e995ba57f4bf954470
Author: Piotr P. Karwasz <[email protected]>
AuthorDate: Tue Mar 5 12:51:08 2024 +0100
Fix environment variable names
We add tests to ensure that the equivalent of issue #2347 does not
affect the new `PropertyEnvironment`.
---
log4j-kit/pom.xml | 20 +++++++++
.../ContextualEnvironmentPropertySource.java | 23 +++++++---
.../ContextualJavaPropsPropertySource.java | 7 +--
.../env/internal/AbstractPropertyNamesTest.java | 42 ++++++++++++++++++
.../ContextualEnvironmentPropertySourceTest.java | 34 +++++++++++++++
.../ContextualJavaPropsPropertySourceTest.java | 35 +++++++++++++++
.../PropertiesUtilPropertyEnvironmentTest.java | 51 ++++++++++++++++++++++
7 files changed, 199 insertions(+), 13 deletions(-)
diff --git a/log4j-kit/pom.xml b/log4j-kit/pom.xml
index f9470106ab..af5a95371e 100644
--- a/log4j-kit/pom.xml
+++ b/log4j-kit/pom.xml
@@ -54,6 +54,12 @@
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.junit-pioneer</groupId>
+ <artifactId>junit-pioneer</artifactId>
+ <scope>test</scope>
+ </dependency>
+
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
@@ -72,5 +78,19 @@
<version>3.29.2-GA</version>
<scope>test</scope>
</dependency>
+
</dependencies>
+
+ <build>
+ <plugins>
+ <plugin>
+ <groupId>org.apache.maven.plugins</groupId>
+ <artifactId>maven-surefire-plugin</artifactId>
+ <configuration>
+ <argLine>--add-opens java.base/java.lang=ALL-UNNAMED
+ --add-opens java.base/java.util=ALL-UNNAMED</argLine>
+ </configuration>
+ </plugin>
+ </plugins>
+ </build>
</project>
diff --git
a/log4j-kit/src/main/java/org/apache/logging/log4j/kit/env/internal/ContextualEnvironmentPropertySource.java
b/log4j-kit/src/main/java/org/apache/logging/log4j/kit/env/internal/ContextualEnvironmentPropertySource.java
index 54a4cdede6..12a3067095 100644
---
a/log4j-kit/src/main/java/org/apache/logging/log4j/kit/env/internal/ContextualEnvironmentPropertySource.java
+++
b/log4j-kit/src/main/java/org/apache/logging/log4j/kit/env/internal/ContextualEnvironmentPropertySource.java
@@ -17,8 +17,10 @@
package org.apache.logging.log4j.kit.env.internal;
import java.util.Locale;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+import org.apache.logging.log4j.kit.env.PropertySource;
import org.apache.logging.log4j.status.StatusLogger;
-import org.apache.logging.log4j.util.PropertySource;
import org.jspecify.annotations.Nullable;
/**
@@ -29,6 +31,7 @@ import org.jspecify.annotations.Nullable;
*/
public class ContextualEnvironmentPropertySource implements PropertySource {
+ private static final Pattern PROPERTY_TOKENIZER =
Pattern.compile("([A-Z]?[a-z0-9]+|[A-Z0-9]+)\\.?");
private static final int DEFAULT_PRIORITY = 0;
private final String prefix;
@@ -39,7 +42,7 @@ public class ContextualEnvironmentPropertySource implements
PropertySource {
}
public ContextualEnvironmentPropertySource(final String contextName, final
int priority) {
- this.prefix = "log4j2." + contextName + ".";
+ this.prefix = "LOG4J_CONTEXTS_" + contextName.toUpperCase(Locale.ROOT);
this.priority = priority;
}
@@ -50,9 +53,9 @@ public class ContextualEnvironmentPropertySource implements
PropertySource {
@Override
public @Nullable String getProperty(final String key) {
- final String actualKey = key.replace('.',
'_').toUpperCase(Locale.ROOT);
+ final String actualKey = getNormalForm(key);
try {
- return System.getenv(prefix + actualKey);
+ return System.getenv(actualKey);
} catch (final SecurityException e) {
StatusLogger.getLogger()
.warn(
@@ -64,8 +67,14 @@ public class ContextualEnvironmentPropertySource implements
PropertySource {
return null;
}
- @Override
- public boolean containsProperty(final String key) {
- return getProperty(key) != null;
+ private String getNormalForm(final CharSequence key) {
+ final StringBuilder sb = new StringBuilder(prefix);
+ final Matcher matcher = PROPERTY_TOKENIZER.matcher(key);
+ int start = 0;
+ while (matcher.find(start)) {
+ start = matcher.end();
+ sb.append('_').append(matcher.group(1).toUpperCase(Locale.ROOT));
+ }
+ return sb.toString();
}
}
diff --git
a/log4j-kit/src/main/java/org/apache/logging/log4j/kit/env/internal/ContextualJavaPropsPropertySource.java
b/log4j-kit/src/main/java/org/apache/logging/log4j/kit/env/internal/ContextualJavaPropsPropertySource.java
index 16edc814b4..c0861c919d 100644
---
a/log4j-kit/src/main/java/org/apache/logging/log4j/kit/env/internal/ContextualJavaPropsPropertySource.java
+++
b/log4j-kit/src/main/java/org/apache/logging/log4j/kit/env/internal/ContextualJavaPropsPropertySource.java
@@ -16,8 +16,8 @@
*/
package org.apache.logging.log4j.kit.env.internal;
+import org.apache.logging.log4j.kit.env.PropertySource;
import org.apache.logging.log4j.status.StatusLogger;
-import org.apache.logging.log4j.util.PropertySource;
import org.jspecify.annotations.Nullable;
/**
@@ -62,9 +62,4 @@ public class ContextualJavaPropsPropertySource implements
PropertySource {
}
return null;
}
-
- @Override
- public boolean containsProperty(final String key) {
- return getProperty(key) != null;
- }
}
diff --git
a/log4j-kit/src/test/java/org/apache/logging/log4j/kit/env/internal/AbstractPropertyNamesTest.java
b/log4j-kit/src/test/java/org/apache/logging/log4j/kit/env/internal/AbstractPropertyNamesTest.java
new file mode 100644
index 0000000000..0361b18b7e
--- /dev/null
+++
b/log4j-kit/src/test/java/org/apache/logging/log4j/kit/env/internal/AbstractPropertyNamesTest.java
@@ -0,0 +1,42 @@
+/*
+ * 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.kit.env.internal;
+
+import java.util.stream.Stream;
+import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.kit.env.PropertyEnvironment;
+import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.util.PropertiesUtil;
+import org.assertj.core.api.Assertions;
+
+public abstract class AbstractPropertyNamesTest {
+
+ // Prevents values in tests cases from being cached
+ protected static final PropertiesUtil PROPERTIES_UTIL =
PropertiesUtil.getProperties();
+ protected static final ClassLoader LOADER =
AbstractPropertyNamesTest.class.getClassLoader();
+ protected static final Logger LOGGER = StatusLogger.getLogger();
+
+ protected Stream<String> testPropertyNames() {
+ return Stream.of("Message.factory", "TransportSecurity.keyStore.path");
+ }
+
+ protected void assertPropertiesAreSet(final String expected, final
PropertyEnvironment environment) {
+ Assertions.assertThat(testPropertyNames())
+ .extracting(environment::getStringProperty)
+ .isSubsetOf(expected);
+ }
+}
diff --git
a/log4j-kit/src/test/java/org/apache/logging/log4j/kit/env/internal/ContextualEnvironmentPropertySourceTest.java
b/log4j-kit/src/test/java/org/apache/logging/log4j/kit/env/internal/ContextualEnvironmentPropertySourceTest.java
new file mode 100644
index 0000000000..d14ebd3ab3
--- /dev/null
+++
b/log4j-kit/src/test/java/org/apache/logging/log4j/kit/env/internal/ContextualEnvironmentPropertySourceTest.java
@@ -0,0 +1,34 @@
+/*
+ * 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.kit.env.internal;
+
+import java.util.Collections;
+import org.apache.logging.log4j.kit.env.PropertyEnvironment;
+import org.apache.logging.log4j.kit.env.support.CompositePropertyEnvironment;
+import org.junit.jupiter.api.Test;
+import org.junitpioneer.jupiter.SetEnvironmentVariable;
+
+public class ContextualEnvironmentPropertySourceTest extends
AbstractPropertyNamesTest {
+ @Test
+ @SetEnvironmentVariable(key = "LOG4J_CONTEXTS_FOO_MESSAGE_FACTORY", value
= "env")
+ @SetEnvironmentVariable(key =
"LOG4J_CONTEXTS_FOO_TRANSPORT_SECURITY_KEY_STORE_PATH", value = "env")
+ void environment_variables_are_recognized() {
+ final PropertyEnvironment environment = new
CompositePropertyEnvironment(
+ null, Collections.singleton(new
ContextualEnvironmentPropertySource("foo", 0)), LOADER, LOGGER);
+ assertPropertiesAreSet("env", environment);
+ }
+}
diff --git
a/log4j-kit/src/test/java/org/apache/logging/log4j/kit/env/internal/ContextualJavaPropsPropertySourceTest.java
b/log4j-kit/src/test/java/org/apache/logging/log4j/kit/env/internal/ContextualJavaPropsPropertySourceTest.java
new file mode 100644
index 0000000000..db101951fc
--- /dev/null
+++
b/log4j-kit/src/test/java/org/apache/logging/log4j/kit/env/internal/ContextualJavaPropsPropertySourceTest.java
@@ -0,0 +1,35 @@
+/*
+ * 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.kit.env.internal;
+
+import java.util.Collections;
+import org.apache.logging.log4j.kit.env.PropertyEnvironment;
+import org.apache.logging.log4j.kit.env.support.CompositePropertyEnvironment;
+import org.junit.jupiter.api.Test;
+import org.junitpioneer.jupiter.SetSystemProperty;
+
+class ContextualJavaPropsPropertySourceTest extends AbstractPropertyNamesTest {
+
+ @Test
+ @SetSystemProperty(key = "log4j.contexts.foo.Message.factory", value =
"3.x")
+ @SetSystemProperty(key =
"log4j.contexts.foo.TransportSecurity.keyStore.path", value = "3.x")
+ void properties_3_x_are_recognized() {
+ final PropertyEnvironment environment = new
CompositePropertyEnvironment(
+ null, Collections.singleton(new
ContextualJavaPropsPropertySource("foo", 0)), LOADER, LOGGER);
+ assertPropertiesAreSet("3.x", environment);
+ }
+}
diff --git
a/log4j-kit/src/test/java/org/apache/logging/log4j/kit/env/internal/PropertiesUtilPropertyEnvironmentTest.java
b/log4j-kit/src/test/java/org/apache/logging/log4j/kit/env/internal/PropertiesUtilPropertyEnvironmentTest.java
new file mode 100644
index 0000000000..2c9b8b9c1d
--- /dev/null
+++
b/log4j-kit/src/test/java/org/apache/logging/log4j/kit/env/internal/PropertiesUtilPropertyEnvironmentTest.java
@@ -0,0 +1,51 @@
+/*
+ * 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.kit.env.internal;
+
+import org.apache.logging.log4j.kit.env.PropertyEnvironment;
+import org.junit.jupiter.api.Test;
+import org.junitpioneer.jupiter.DisabledUntil;
+import org.junitpioneer.jupiter.SetEnvironmentVariable;
+import org.junitpioneer.jupiter.SetSystemProperty;
+
+@DisabledUntil(date = "2024-04-01", reason = "Disable until `log4j-api` 3.x is
removed")
+class PropertiesUtilPropertyEnvironmentTest extends AbstractPropertyNamesTest {
+
+ @Test
+ @SetEnvironmentVariable(key = "LOG4J_MESSAGE_FACTORY", value = "env")
+ @SetEnvironmentVariable(key = "LOG4J_TRANSPORT_SECURITY_KEY_STORE_PATH",
value = "env")
+ void environment_variables_are_recognized() {
+ final PropertyEnvironment environment = new
PropertiesUtilPropertyEnvironment(PROPERTIES_UTIL, LOGGER);
+ assertPropertiesAreSet("env", environment);
+ }
+
+ @Test
+ @SetSystemProperty(key = "log4j2.messageFactory", value = "2.x")
+ @SetSystemProperty(key = "log4j2.transportSecurityKeystorePath", value =
"2.x")
+ void properties_2_x_are_recognized() {
+ final PropertyEnvironment environment = new
PropertiesUtilPropertyEnvironment(PROPERTIES_UTIL, LOGGER);
+ assertPropertiesAreSet("2.x", environment);
+ }
+
+ @Test
+ @SetSystemProperty(key = "log4j.Message.factory", value = "3.x")
+ @SetSystemProperty(key = "log4j.TransportSecurity.keyStore.path", value =
"3.x")
+ void properties_3_x_are_recognized() {
+ final PropertyEnvironment environment = new
PropertiesUtilPropertyEnvironment(PROPERTIES_UTIL, LOGGER);
+ assertPropertiesAreSet("3.x", environment);
+ }
+}