Skip to content

Commit

Permalink
Mark JdkMapAdapterStringMap as frozen if map is immutable
Browse files Browse the repository at this point in the history
This PR marks `StringMap`s generated from Java `Map`s as frozen if the
wrapped map is immutable.

Fixes apache#2098
  • Loading branch information
ppkarwasz committed Dec 15, 2023
1 parent a8dcc55 commit d32a71d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
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;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ 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.
-->
<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="changed">
<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>

0 comments on commit d32a71d

Please sign in to comment.