|
| 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.test.spi; |
| 18 | + |
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
| 20 | +import static org.assertj.core.api.Assertions.entry; |
| 21 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 22 | + |
| 23 | +import java.time.Duration; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.concurrent.ExecutorService; |
| 26 | +import java.util.concurrent.Executors; |
| 27 | +import org.apache.logging.log4j.spi.ThreadContextMap; |
| 28 | +import org.junit.jupiter.api.parallel.Execution; |
| 29 | +import org.junit.jupiter.api.parallel.ExecutionMode; |
| 30 | + |
| 31 | +/** |
| 32 | + * Provides test cases to apply to all implementations of {@link ThreadContextMap}. |
| 33 | + * @since 2.24.0 |
| 34 | + */ |
| 35 | +@Execution(ExecutionMode.CONCURRENT) |
| 36 | +public abstract class ThreadContextMapSuite { |
| 37 | + |
| 38 | + private static final String KEY = "key"; |
| 39 | + |
| 40 | + /** |
| 41 | + * Checks if the context map does not propagate to other threads by default. |
| 42 | + */ |
| 43 | + protected static void threadLocalNotInheritableByDefault(final ThreadContextMap contextMap) { |
| 44 | + contextMap.put(KEY, "threadLocalNotInheritableByDefault"); |
| 45 | + verifyThreadContextValueFromANewThread(contextMap, null); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * Checks if the context map can be configured to propagate to other threads. |
| 50 | + */ |
| 51 | + protected static void threadLocalInheritableIfConfigured(final ThreadContextMap contextMap) { |
| 52 | + contextMap.put(KEY, "threadLocalInheritableIfConfigured"); |
| 53 | + verifyThreadContextValueFromANewThread(contextMap, "threadLocalInheritableIfConfigured"); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Checks basic put/remove pattern. |
| 58 | + */ |
| 59 | + protected static void singleValue(final ThreadContextMap contextMap) { |
| 60 | + assertThat(contextMap.isEmpty()).as("Map is empty").isTrue(); |
| 61 | + contextMap.put(KEY, "testPut"); |
| 62 | + assertThat(contextMap.isEmpty()).as("Map is not empty").isFalse(); |
| 63 | + assertThat(contextMap.containsKey(KEY)).as("Map key exists").isTrue(); |
| 64 | + assertThat(contextMap.get(KEY)).as("Map contains expected value").isEqualTo("testPut"); |
| 65 | + contextMap.remove(KEY); |
| 66 | + assertThat(contextMap.isEmpty()).as("Map is empty").isTrue(); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Checks mutable copy |
| 71 | + */ |
| 72 | + protected static void getCopyReturnsMutableCopy(final ThreadContextMap contextMap) { |
| 73 | + contextMap.put(KEY, "testGetCopyReturnsMutableCopy"); |
| 74 | + |
| 75 | + final Map<String, String> copy = contextMap.getCopy(); |
| 76 | + assertThat(copy).as("Copy contains same value").containsExactly(entry(KEY, "testGetCopyReturnsMutableCopy")); |
| 77 | + |
| 78 | + copy.put(KEY, "testGetCopyReturnsMutableCopy2"); |
| 79 | + assertThat(contextMap.get(KEY)) |
| 80 | + .as("Original map is not affected by changes in the copy") |
| 81 | + .isEqualTo("testGetCopyReturnsMutableCopy"); |
| 82 | + |
| 83 | + contextMap.clear(); |
| 84 | + assertThat(contextMap.isEmpty()).as("Original map is empty").isTrue(); |
| 85 | + assertThat(copy) |
| 86 | + .as("Copy is not affected by changes in the map.") |
| 87 | + .containsExactly(entry(KEY, "testGetCopyReturnsMutableCopy2")); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * The immutable copy must be {@code null} if the map is empty. |
| 92 | + */ |
| 93 | + protected static void getImmutableMapReturnsNullIfEmpty(final ThreadContextMap contextMap) { |
| 94 | + assertThat(contextMap.isEmpty()).as("Original map is empty").isTrue(); |
| 95 | + assertThat(contextMap.getImmutableMapOrNull()) |
| 96 | + .as("Immutable copy is null") |
| 97 | + .isNull(); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * The result of {@link ThreadContextMap#getImmutableMapOrNull()} must be immutable. |
| 102 | + */ |
| 103 | + protected static void getImmutableMapReturnsImmutableMapIfNonEmpty(final ThreadContextMap contextMap) { |
| 104 | + contextMap.put(KEY, "getImmutableMapReturnsImmutableMapIfNonEmpty"); |
| 105 | + |
| 106 | + final Map<String, String> immutable = contextMap.getImmutableMapOrNull(); |
| 107 | + assertThat(immutable) |
| 108 | + .as("Immutable copy contains same value") |
| 109 | + .containsExactly(entry(KEY, "getImmutableMapReturnsImmutableMapIfNonEmpty")); |
| 110 | + |
| 111 | + assertThrows( |
| 112 | + UnsupportedOperationException.class, () -> immutable.put(KEY, "getImmutableMapReturnsNullIfEmpty2")); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * The immutable copy is not affected by changes to the original map. |
| 117 | + */ |
| 118 | + protected static void getImmutableMapCopyNotAffectedByContextMapChanges(final ThreadContextMap contextMap) { |
| 119 | + contextMap.put(KEY, "getImmutableMapCopyNotAffectedByContextMapChanges"); |
| 120 | + |
| 121 | + final Map<String, String> immutable = contextMap.getImmutableMapOrNull(); |
| 122 | + contextMap.put(KEY, "getImmutableMapCopyNotAffectedByContextMapChanges2"); |
| 123 | + assertThat(immutable) |
| 124 | + .as("Immutable copy contains the original value") |
| 125 | + .containsExactly(entry(KEY, "getImmutableMapCopyNotAffectedByContextMapChanges")); |
| 126 | + } |
| 127 | + |
| 128 | + private static void verifyThreadContextValueFromANewThread( |
| 129 | + final ThreadContextMap contextMap, final String expected) { |
| 130 | + final ExecutorService executorService = Executors.newSingleThreadExecutor(); |
| 131 | + try { |
| 132 | + assertThat(executorService.submit(() -> contextMap.get(KEY))) |
| 133 | + .succeedsWithin(Duration.ofSeconds(1)) |
| 134 | + .isEqualTo(expected); |
| 135 | + } finally { |
| 136 | + executorService.shutdown(); |
| 137 | + } |
| 138 | + } |
| 139 | +} |
0 commit comments