|
16 | 16 | */ |
17 | 17 | package org.apache.logging.log4j.spi; |
18 | 18 |
|
| 19 | +import static org.assertj.core.api.Assertions.assertThat; |
19 | 20 | import static org.junit.jupiter.api.Assertions.assertEquals; |
20 | 21 | import static org.junit.jupiter.api.Assertions.assertFalse; |
| 22 | +import static org.junit.jupiter.api.Assertions.assertNotSame; |
21 | 23 | import static org.junit.jupiter.api.Assertions.assertTrue; |
22 | 24 |
|
23 | 25 | import java.util.HashMap; |
@@ -130,4 +132,124 @@ void threadLocalNotInheritableByDefault() { |
130 | 132 | void threadLocalInheritableIfConfigured() { |
131 | 133 | threadLocalInheritableIfConfigured(createInheritableThreadContextMap()); |
132 | 134 | } |
| 135 | + |
| 136 | + @Test |
| 137 | + void testGetCopyWithEmptyMap() { |
| 138 | + final DefaultThreadContextMap contextMap = new DefaultThreadContextMap(); |
| 139 | + |
| 140 | + // Verify map is empty |
| 141 | + assertTrue(contextMap.isEmpty()); |
| 142 | + assertEquals(0, contextMap.size()); |
| 143 | + |
| 144 | + // Get copy of empty map |
| 145 | + final Map<String, String> copy = contextMap.getCopy(); |
| 146 | + |
| 147 | + // Verify copy is empty HashMap |
| 148 | + assertThat(copy).isInstanceOf(HashMap.class); |
| 149 | + assertTrue(copy.isEmpty()); |
| 150 | + assertEquals(0, copy.size()); |
| 151 | + |
| 152 | + // Verify copy is independent |
| 153 | + copy.put("test", "value"); |
| 154 | + assertTrue(contextMap.isEmpty()); |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + void testGetCopyWithSingleElement() { |
| 159 | + final DefaultThreadContextMap contextMap = new DefaultThreadContextMap(); |
| 160 | + |
| 161 | + // Add single element |
| 162 | + contextMap.put("key1", "value1"); |
| 163 | + assertEquals(1, contextMap.size()); |
| 164 | + assertEquals("value1", contextMap.get("key1")); |
| 165 | + |
| 166 | + // Get copy |
| 167 | + final Map<String, String> copy = contextMap.getCopy(); |
| 168 | + |
| 169 | + // Verify copy contains identical data |
| 170 | + assertThat(copy).isInstanceOf(HashMap.class); |
| 171 | + assertEquals(1, copy.size()); |
| 172 | + assertEquals("value1", copy.get("key1")); |
| 173 | + assertTrue(copy.containsKey("key1")); |
| 174 | + |
| 175 | + // Verify copy is independent |
| 176 | + assertNotSame(copy, contextMap.toMap()); |
| 177 | + copy.put("key2", "value2"); |
| 178 | + assertEquals(1, contextMap.size()); |
| 179 | + assertFalse(contextMap.containsKey("key2")); |
| 180 | + } |
| 181 | + |
| 182 | + @Test |
| 183 | + void testGetCopyWithMultipleElements() { |
| 184 | + final DefaultThreadContextMap contextMap = new DefaultThreadContextMap(); |
| 185 | + |
| 186 | + // Add multiple elements |
| 187 | + final Map<String, String> testData = new HashMap<>(); |
| 188 | + testData.put("key1", "value1"); |
| 189 | + testData.put("key2", "value2"); |
| 190 | + testData.put("key3", "value3"); |
| 191 | + testData.put("key4", "value4"); |
| 192 | + testData.put("key5", "value5"); |
| 193 | + |
| 194 | + contextMap.putAll(testData); |
| 195 | + assertEquals(5, contextMap.size()); |
| 196 | + |
| 197 | + // Get copy |
| 198 | + final Map<String, String> copy = contextMap.getCopy(); |
| 199 | + |
| 200 | + // Verify copy contains identical data |
| 201 | + assertThat(copy).isInstanceOf(HashMap.class); |
| 202 | + assertEquals(5, copy.size()); |
| 203 | + |
| 204 | + // Verify all entries match |
| 205 | + assertEquals(testData, copy); |
| 206 | + |
| 207 | + // Verify copy is independent |
| 208 | + copy.clear(); |
| 209 | + assertEquals(5, contextMap.size()); |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + void testGetCopyReturnsHashMap() { |
| 214 | + final DefaultThreadContextMap contextMap = new DefaultThreadContextMap(); |
| 215 | + |
| 216 | + // Test with empty map |
| 217 | + Map<String, String> copy = contextMap.getCopy(); |
| 218 | + assertThat(copy).isInstanceOf(HashMap.class); |
| 219 | + |
| 220 | + // Test with populated map |
| 221 | + contextMap.put("key", "value"); |
| 222 | + copy = contextMap.getCopy(); |
| 223 | + assertThat(copy).isInstanceOf(HashMap.class); |
| 224 | + } |
| 225 | + |
| 226 | + @Test |
| 227 | + void testGetCopyIndependence() { |
| 228 | + final DefaultThreadContextMap contextMap = new DefaultThreadContextMap(); |
| 229 | + |
| 230 | + // Setup initial data |
| 231 | + contextMap.put("key1", "value1"); |
| 232 | + contextMap.put("key2", "value2"); |
| 233 | + |
| 234 | + final Map<String, String> copy1 = contextMap.getCopy(); |
| 235 | + final Map<String, String> copy2 = contextMap.getCopy(); |
| 236 | + |
| 237 | + // Verify copies are independent of each other |
| 238 | + assertNotSame(copy1, copy2); |
| 239 | + assertEquals(copy1, copy2); |
| 240 | + |
| 241 | + // Modify first copy |
| 242 | + copy1.put("key3", "value3"); |
| 243 | + copy1.remove("key1"); |
| 244 | + |
| 245 | + // Verify second copy is unaffected |
| 246 | + assertEquals(2, copy2.size()); |
| 247 | + assertTrue(copy2.containsKey("key1")); |
| 248 | + assertFalse(copy2.containsKey("key3")); |
| 249 | + |
| 250 | + // Verify original map is unaffected |
| 251 | + assertEquals(2, contextMap.size()); |
| 252 | + assertTrue(contextMap.containsKey("key1")); |
| 253 | + assertFalse(contextMap.containsKey("key3")); |
| 254 | + } |
133 | 255 | } |
0 commit comments