Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark JdkMapAdapterStringMap as frozen if map is immutable #2101

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.apache.logging.log4j.core.impl;

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;
Expand All @@ -24,11 +25,16 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

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.
Expand Down Expand Up @@ -812,4 +818,18 @@ public void testForEachTriConsumer() throws Exception {
original.forEach(COUNTER, state);
assertEquals(state.count, original.size());
}

static Stream<Arguments> testImmutability() {
return Stream.of(
Arguments.of(new HashMap<>(), false),
Arguments.of(Map.of(), true),
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public JdkMapAdapterStringMap() {

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;
}
vy marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -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>