View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.api.util.internal;
20  
21  import java.util.Collections;
22  import java.util.Iterator;
23  import java.util.LinkedHashMap;
24  import java.util.Map;
25  import java.util.Map.Entry;
26  import java.util.Set;
27  
28  import org.apache.maven.surefire.api.util.internal.ImmutableMap.Node;
29  import org.junit.jupiter.api.BeforeEach;
30  import org.junit.jupiter.api.Test;
31  
32  import static org.hamcrest.MatcherAssert.assertThat;
33  import static org.hamcrest.Matchers.hasItem;
34  import static org.hamcrest.Matchers.hasSize;
35  import static org.hamcrest.Matchers.is;
36  import static org.hamcrest.Matchers.not;
37  import static org.hamcrest.Matchers.nullValue;
38  import static org.junit.jupiter.api.Assertions.assertThrows;
39  
40  /**
41   * @since 2.20
42   */
43  public class ImmutableMapTest {
44      private ImmutableMap<String, String> map;
45  
46      @BeforeEach
47      public void setUp() {
48          Map<String, String> backingMap = new LinkedHashMap<>();
49          backingMap.put("a", "1");
50          backingMap.put("x", null);
51          backingMap.put("b", "2");
52          backingMap.put("c", "3");
53          backingMap.put("", "");
54          backingMap.put(null, "1");
55          map = new ImmutableMap<>(backingMap);
56      }
57  
58      @Test
59      public void testEntrySet() {
60          Set<Entry<String, String>> entries = map.entrySet();
61          assertThat(entries, hasSize(6));
62          assertThat(entries, hasItem(new Node<>("a", "1")));
63          assertThat(entries, hasItem(new Node<>("x", (String) null)));
64          assertThat(entries, hasItem(new Node<>("b", "2")));
65          assertThat(entries, hasItem(new Node<>("c", "3")));
66          assertThat(entries, hasItem(new Node<>("", "")));
67          assertThat(entries, hasItem(new Node<>((String) null, "1")));
68      }
69  
70      @Test
71      public void testGetter() {
72          assertThat(map.size(), is(6));
73          assertThat(map.get("a"), is("1"));
74          assertThat(map.get("x"), is((String) null));
75          assertThat(map.get("b"), is("2"));
76          assertThat(map.get("c"), is("3"));
77          assertThat(map.get(""), is(""));
78          assertThat(map.get(null), is("1"));
79      }
80  
81      @Test
82      public void shouldNotModifyEntries() {
83          assertThrows(UnsupportedOperationException.class, () -> map.entrySet().clear());
84      }
85  
86      @Test
87      public void shouldSafelyEnumerateEntries() {
88          Iterator<Entry<String, String>> it = map.entrySet().iterator();
89  
90          assertThat(it.hasNext(), is(true));
91          Entry<String, String> val = it.next();
92          assertThat(val.getKey(), is("a"));
93          assertThat(val.getValue(), is("1"));
94  
95          assertThat(it.hasNext(), is(true));
96          val = it.next();
97          assertThat(val.getKey(), is("x"));
98          assertThat(val.getValue(), is(nullValue()));
99  
100         assertThat(it.hasNext(), is(true));
101         val = it.next();
102         assertThat(val.getKey(), is("b"));
103         assertThat(val.getValue(), is("2"));
104 
105         assertThat(it.hasNext(), is(true));
106         val = it.next();
107         assertThat(val.getKey(), is("c"));
108         assertThat(val.getValue(), is("3"));
109 
110         assertThat(it.hasNext(), is(true));
111         val = it.next();
112         assertThat(val.getKey(), is(""));
113         assertThat(val.getValue(), is(""));
114 
115         assertThat(it.hasNext(), is(true));
116         val = it.next();
117         assertThat(val.getKey(), is(nullValue()));
118         assertThat(val.getValue(), is("1"));
119 
120         assertThat(it.hasNext(), is(false));
121     }
122 
123     @Test
124     public void shouldNotSetEntries() {
125         assertThrows(
126                 UnsupportedOperationException.class,
127                 () -> map.entrySet().iterator().next().setValue(""));
128     }
129 
130     @Test
131     public void shouldNotRemove() {
132         assertThrows(UnsupportedOperationException.class, () -> map.remove("a"));
133     }
134 
135     @Test
136     public void shouldNotRemoveNull() {
137         assertThrows(UnsupportedOperationException.class, () -> map.remove(null));
138     }
139 
140     @Test
141     public void shouldNotHaveEqualEntry() {
142         Map<String, String> map = new ImmutableMap<>(Collections.singletonMap("k", "v"));
143         Entry<String, String> e = map.entrySet().iterator().next();
144         assertThat(e, is(not((Entry<String, String>) null)));
145         assertThat(e, is(not(new Object())));
146     }
147 
148     @Test
149     public void shouldHaveEqualEntry() {
150         Map<String, String> map = new ImmutableMap<>(Collections.singletonMap("k", "v"));
151         Entry<String, String> e = map.entrySet().iterator().next();
152         assertThat(e, is(e));
153         assertThat(e, is((Entry<String, String>) new Node<>("k", "v")));
154     }
155 }