This is an automated email from the ASF dual-hosted git repository.

pkarwasz pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit a6c1921493048428261190f005f173742e175008
Author: Piotr P. Karwasz <[email protected]>
AuthorDate: Fri Dec 15 23:39:42 2023 +0100

    Mark `JdkMapAdapterStringMap` as frozen if map is immutable
    
    This PR marks `StringMap`s generated from Java `Map`s as frozen if the
    wrapped map is immutable.
    
    Fixes #2098
---
 .../core/impl/JdkMapAdapterStringMapTest.java      | 27 +++++++++++++++++++++-
 .../log4j/core/impl/JdkMapAdapterStringMap.java    |  5 ++++
 ..._JdkMapAdapterStringMap_detect_immutability.xml | 10 ++++++++
 3 files changed, 41 insertions(+), 1 deletion(-)

diff --git 
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/impl/JdkMapAdapterStringMapTest.java
 
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/impl/JdkMapAdapterStringMapTest.java
index e53fe61b84..9f387c1388 100644
--- 
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/impl/JdkMapAdapterStringMapTest.java
+++ 
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/impl/JdkMapAdapterStringMapTest.java
@@ -16,18 +16,30 @@
  */
 package org.apache.logging.log4j.core.impl;
 
-import static org.junit.jupiter.api.Assertions.*;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.stream.Stream;
 import org.apache.logging.log4j.util.BiConsumer;
 import org.apache.logging.log4j.util.TriConsumer;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
 
 /**
  * Tests the JdkMapAdapterStringMap class.
@@ -837,4 +849,17 @@ public class JdkMapAdapterStringMapTest {
         original.forEach(COUNTER, state);
         assertEquals(state.count, original.size());
     }
+
+    static Stream<Arguments> testImmutability() {
+        return Stream.of(
+                Arguments.of(new HashMap<>(), false),
+                Arguments.of(Collections.emptyMap(), true),
+                Arguments.of(Collections.unmodifiableMap(new HashMap<>()), 
true));
+    }
+
+    @ParameterizedTest
+    @MethodSource
+    void testImmutability(final Map<String, String> map, final boolean frozen) 
{
+        assertThat(new 
JdkMapAdapterStringMap(map).isFrozen()).as("Frozen").isEqualTo(frozen);
+    }
 }
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/JdkMapAdapterStringMap.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/JdkMapAdapterStringMap.java
index e4d62acf0c..e8426e55dc 100644
--- 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/JdkMapAdapterStringMap.java
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/JdkMapAdapterStringMap.java
@@ -53,6 +53,11 @@ public class JdkMapAdapterStringMap implements StringMap {
 
     public JdkMapAdapterStringMap(final Map<String, String> map) {
         this.map = Objects.requireNonNull(map, "map");
+        try {
+            map.replace(Strings.EMPTY, Strings.EMPTY, Strings.EMPTY);
+        } catch (final UnsupportedOperationException ignored) {
+            immutable = true;
+        }
     }
 
     @Override
diff --git 
a/src/changelog/.2.x.x/2098_JdkMapAdapterStringMap_detect_immutability.xml 
b/src/changelog/.2.x.x/2098_JdkMapAdapterStringMap_detect_immutability.xml
new file mode 100644
index 0000000000..84409b272e
--- /dev/null
+++ b/src/changelog/.2.x.x/2098_JdkMapAdapterStringMap_detect_immutability.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<entry xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
+       xmlns="http://logging.apache.org/log4j/changelog";
+       xsi:schemaLocation="http://logging.apache.org/log4j/changelog 
https://logging.apache.org/log4j/changelog-0.1.2.xsd";
+       type="fixed">
+  <issue id="2098" 
link="https://github.com/apache/logging-log4j2/issues/2098"/>
+  <description format="asciidoc">
+    Mark `JdkMapAdapterStringMap` as frozen if map is immutable.
+  </description>
+</entry>

Reply via email to