1414 * See the License for the specific language governing permissions and
1515 * limitations under the License.
1616 */
17- package org .apache .logging .log4j .spi ;
17+ package org .apache .logging .log4j .internal . map ;
1818
19- import static org .junit .Assert .assertNull ;
2019import static org .junit .jupiter .api .Assertions .assertEquals ;
2120import static org .junit .jupiter .api .Assertions .assertFalse ;
21+ import static org .junit .jupiter .api .Assertions .assertNull ;
2222import static org .junit .jupiter .api .Assertions .assertThrows ;
2323import static org .junit .jupiter .api .Assertions .assertTrue ;
2424
2525import java .util .HashMap ;
26+ import java .util .HashSet ;
2627import java .util .Map ;
27- import org .apache .logging .log4j .test .junit .InitializesThreadContext ;
28- import org .apache .logging .log4j .test .junit .SetTestProperty ;
28+ import java .util .Set ;
2929import org .apache .logging .log4j .test .junit .UsingThreadContextMap ;
30+ import org .apache .logging .log4j .util .TriConsumer ;
3031import org .junit .jupiter .api .Test ;
31- import org .junitpioneer .jupiter .ClearSystemProperty ;
3232
3333/**
3434 * Tests the {@code StringArrayThreadContextMap} class.
@@ -56,27 +56,20 @@ public void testHashCodeVsSameKind() {
5656 @ Test
5757 public void testGet () {
5858 final StringArrayThreadContextMap map1 = createMap ();
59- assertEquals ( null , map1 .get ("test" ));
59+ assertNull ( map1 .get ("test" ));
6060 map1 .put ("test" , "test" );
6161 assertEquals ("test" , map1 .get ("test" ));
62- assertEquals (null , map1 .get ("not_present" ));
63- }
64-
65- @ Test
66- public void testDoesNothingIfConstructedWithUseMapIsFalse () {
67- final StringArrayThreadContextMap map = new StringArrayThreadContextMap (false );
68- assertTrue (map .isEmpty ());
69- assertFalse (map .containsKey ("key" ));
70- map .put ("key" , "value" );
62+ assertNull (map1 .get ("not_present" ));
63+ assertEquals ("test" , map1 .getValue ("test" ));
64+ assertNull (map1 .getValue ("not_present" ));
7165
72- assertTrue (map .isEmpty ());
73- assertFalse (map .containsKey ("key" ));
74- assertNull (map .get ("key" ));
66+ map1 .clear ();
67+ assertNull (map1 .get ("not_present" ));
7568 }
7669
7770 @ Test
7871 public void testPut () {
79- final StringArrayThreadContextMap map = new StringArrayThreadContextMap (true );
72+ final StringArrayThreadContextMap map = new StringArrayThreadContextMap ();
8073 assertTrue (map .isEmpty ());
8174 assertFalse (map .containsKey ("key" ));
8275 map .put ("key" , "value" );
@@ -88,7 +81,7 @@ public void testPut() {
8881
8982 @ Test
9083 public void testPutAll () {
91- final StringArrayThreadContextMap map = new StringArrayThreadContextMap (true );
84+ final StringArrayThreadContextMap map = new StringArrayThreadContextMap ();
9285 assertTrue (map .isEmpty ());
9386 assertFalse (map .containsKey ("key" ));
9487 final int mapSize = 10 ;
@@ -106,7 +99,7 @@ public void testPutAll() {
10699
107100 /**
108101 * Test method for
109- * {@link org.apache.logging.log4j.spi .StringArrayThreadContextMap#remove(java.lang.String)}
102+ * {@link org.apache.logging.log4j.internal.map .StringArrayThreadContextMap#remove(java.lang.String)}
110103 * .
111104 */
112105 @ Test
@@ -118,6 +111,9 @@ public void testRemove() {
118111 map .remove ("key" );
119112 assertFalse (map .containsKey ("key" ));
120113 assertEquals ("value2" , map .get ("key2" ));
114+
115+ map .clear ();
116+ map .remove ("test" );
121117 }
122118
123119 @ Test
@@ -132,6 +128,9 @@ public void testRemoveAll() {
132128 map .removeAll (newValues .keySet ());
133129
134130 map .put ("3" , "value3" );
131+
132+ map .clear ();
133+ map .removeAll (newValues .keySet ());
135134 }
136135
137136 @ Test
@@ -148,7 +147,7 @@ public void testClear() {
148147 * @return
149148 */
150149 private StringArrayThreadContextMap createMap () {
151- final StringArrayThreadContextMap map = new StringArrayThreadContextMap (true );
150+ final StringArrayThreadContextMap map = new StringArrayThreadContextMap ();
152151 assertTrue (map .isEmpty ());
153152 map .put ("key" , "value" );
154153 map .put ("key2" , "value2" );
@@ -159,7 +158,7 @@ private StringArrayThreadContextMap createMap() {
159158
160159 @ Test
161160 public void testGetCopyReturnsMutableMap () {
162- final StringArrayThreadContextMap map = new StringArrayThreadContextMap (true );
161+ final StringArrayThreadContextMap map = new StringArrayThreadContextMap ();
163162 assertTrue (map .isEmpty ());
164163 final Map <String , String > copy = map .getCopy ();
165164 assertTrue (copy .isEmpty ());
@@ -173,7 +172,7 @@ public void testGetCopyReturnsMutableMap() {
173172
174173 @ Test
175174 public void testGetCopyReturnsMutableCopy () {
176- final StringArrayThreadContextMap map = new StringArrayThreadContextMap (true );
175+ final StringArrayThreadContextMap map = new StringArrayThreadContextMap ();
177176 map .put ("key1" , "value1" );
178177 assertFalse (map .isEmpty ());
179178 final Map <String , String > copy = map .getCopy ();
@@ -194,14 +193,14 @@ public void testGetCopyReturnsMutableCopy() {
194193
195194 @ Test
196195 public void testGetImmutableMapReturnsNullIfEmpty () {
197- final StringArrayThreadContextMap map = new StringArrayThreadContextMap (true );
196+ final StringArrayThreadContextMap map = new StringArrayThreadContextMap ();
198197 assertTrue (map .isEmpty ());
199198 assertNull (map .getImmutableMapOrNull ());
200199 }
201200
202201 @ Test
203202 public void testGetImmutableMapReturnsImmutableMapIfNonEmpty () {
204- final StringArrayThreadContextMap map = new StringArrayThreadContextMap (true );
203+ final StringArrayThreadContextMap map = new StringArrayThreadContextMap ();
205204 map .put ("key1" , "value1" );
206205 assertFalse (map .isEmpty ());
207206
@@ -214,7 +213,7 @@ public void testGetImmutableMapReturnsImmutableMapIfNonEmpty() {
214213
215214 @ Test
216215 public void testGetImmutableMapCopyNotAffectdByContextMapChanges () {
217- final StringArrayThreadContextMap map = new StringArrayThreadContextMap (true );
216+ final StringArrayThreadContextMap map = new StringArrayThreadContextMap ();
218217 map .put ("key1" , "value1" );
219218 assertFalse (map .isEmpty ());
220219
@@ -230,7 +229,7 @@ public void testGetImmutableMapCopyNotAffectdByContextMapChanges() {
230229
231230 @ Test
232231 public void testToStringShowsMapContext () {
233- final StringArrayThreadContextMap map = new StringArrayThreadContextMap (true );
232+ final StringArrayThreadContextMap map = new StringArrayThreadContextMap ();
234233 assertEquals ("{}" , map .toString ());
235234
236235 map .put ("key1" , "value1" );
@@ -242,24 +241,47 @@ public void testToStringShowsMapContext() {
242241 }
243242
244243 @ Test
245- @ ClearSystemProperty (key = StringArrayThreadContextMap .INHERITABLE_MAP )
246- @ InitializesThreadContext
247- public void testThreadLocalNotInheritableByDefault () {
248- ThreadContextMapFactory .init ();
249- final ThreadLocal <Object []> threadLocal = StringArrayThreadContextMap .createThreadLocalMap (true );
250- assertFalse (threadLocal instanceof InheritableThreadLocal <?>);
244+ public void testEmptyMap () {
245+ assertNull (UnmodifiableArrayBackedMap .EMPTY_MAP .get ("test" ));
251246 }
252247
253248 @ Test
254- @ SetTestProperty (key = StringArrayThreadContextMap .INHERITABLE_MAP , value = "true" )
255- @ InitializesThreadContext
256- public void testThreadLocalInheritableIfConfigured () {
257- ThreadContextMapFactory .init ();
258- try {
259- final ThreadLocal <Object []> threadLocal = StringArrayThreadContextMap .createThreadLocalMap (true );
260- assertTrue (threadLocal instanceof InheritableThreadLocal <?>);
261- } finally {
262- System .clearProperty (StringArrayThreadContextMap .INHERITABLE_MAP );
263- }
249+ public void testForEachBiConsumer_Log4jUtil () {
250+ StringArrayThreadContextMap map = createMap ();
251+ Set <String > keys = new HashSet <>();
252+ org .apache .logging .log4j .util .BiConsumer <String , String > log4j_util_action =
253+ new org .apache .logging .log4j .util .BiConsumer <String , String >() {
254+ @ Override
255+ public void accept (String key , String value ) {
256+ keys .add (key );
257+ }
258+ };
259+ map .forEach (log4j_util_action );
260+ assertEquals (map .toMap ().keySet (), keys );
261+
262+ map .clear ();
263+ keys .clear ();
264+ map .forEach (log4j_util_action );
265+ assertTrue (keys .isEmpty ());
266+ }
267+
268+ @ Test
269+ public void testForEachTriConsumer () {
270+ StringArrayThreadContextMap map = createMap ();
271+ HashMap <String , String > iterationResultMap = new HashMap <>();
272+ TriConsumer <String , String , Map <String , String >> triConsumer =
273+ new TriConsumer <String , String , Map <String , String >>() {
274+ @ Override
275+ public void accept (String k , String v , Map <String , String > s ) {
276+ s .put (k , v );
277+ }
278+ };
279+ map .forEach (triConsumer , iterationResultMap );
280+ assertEquals (map .toMap (), iterationResultMap );
281+
282+ map .clear ();
283+ iterationResultMap .clear ();
284+ map .forEach (triConsumer , iterationResultMap );
285+ assertTrue (iterationResultMap .isEmpty ());
264286 }
265287}
0 commit comments