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.Before;
30  import org.junit.Test;
31  
32  import static org.hamcrest.Matchers.hasItem;
33  import static org.hamcrest.Matchers.hasSize;
34  import static org.hamcrest.Matchers.is;
35  import static org.hamcrest.Matchers.not;
36  import static org.hamcrest.Matchers.nullValue;
37  import static org.junit.Assert.assertThat;
38  
39  /**
40   * @since 2.20
41   */
42  public class ImmutableMapTest {
43      private ImmutableMap<String, String> map;
44  
45      @Before
46      public void setUp() {
47          Map<String, String> backingMap = new LinkedHashMap<>();
48          backingMap.put("a", "1");
49          backingMap.put("x", null);
50          backingMap.put("b", "2");
51          backingMap.put("c", "3");
52          backingMap.put("", "");
53          backingMap.put(null, "1");
54          map = new ImmutableMap<>(backingMap);
55      }
56  
57      @Test
58      public void testEntrySet() {
59          Set<Entry<String, String>> entries = map.entrySet();
60          assertThat(entries, hasSize(6));
61          assertThat(entries, hasItem(new Node<>("a", "1")));
62          assertThat(entries, hasItem(new Node<>("x", (String) null)));
63          assertThat(entries, hasItem(new Node<>("b", "2")));
64          assertThat(entries, hasItem(new Node<>("c", "3")));
65          assertThat(entries, hasItem(new Node<>("", "")));
66          assertThat(entries, hasItem(new Node<>((String) null, "1")));
67      }
68  
69      @Test
70      public void testGetter() {
71          assertThat(map.size(), is(6));
72          assertThat(map.get("a"), is("1"));
73          assertThat(map.get("x"), is((String) null));
74          assertThat(map.get("b"), is("2"));
75          assertThat(map.get("c"), is("3"));
76          assertThat(map.get(""), is(""));
77          assertThat(map.get(null), is("1"));
78      }
79  
80      @Test(expected = UnsupportedOperationException.class)
81      public void shouldNotModifyEntries() {
82          map.entrySet().clear();
83      }
84  
85      @Test
86      public void shouldSafelyEnumerateEntries() {
87          Iterator<Entry<String, String>> it = map.entrySet().iterator();
88  
89          assertThat(it.hasNext(), is(true));
90          Entry<String, String> val = it.next();
91          assertThat(val.getKey(), is("a"));
92          assertThat(val.getValue(), is("1"));
93  
94          assertThat(it.hasNext(), is(true));
95          val = it.next();
96          assertThat(val.getKey(), is("x"));
97          assertThat(val.getValue(), is(nullValue()));
98  
99          assertThat(it.hasNext(), is(true));
100         val = it.next();
101         assertThat(val.getKey(), is("b"));
102         assertThat(val.getValue(), is("2"));
103 
104         assertThat(it.hasNext(), is(true));
105         val = it.next();
106         assertThat(val.getKey(), is("c"));
107         assertThat(val.getValue(), is("3"));
108 
109         assertThat(it.hasNext(), is(true));
110         val = it.next();
111         assertThat(val.getKey(), is(""));
112         assertThat(val.getValue(), is(""));
113 
114         assertThat(it.hasNext(), is(true));
115         val = it.next();
116         assertThat(val.getKey(), is(nullValue()));
117         assertThat(val.getValue(), is("1"));
118 
119         assertThat(it.hasNext(), is(false));
120     }
121 
122     @Test(expected = UnsupportedOperationException.class)
123     public void shouldNotSetEntries() {
124         map.entrySet().iterator().next().setValue("");
125     }
126 
127     @Test(expected = UnsupportedOperationException.class)
128     public void shouldNotRemove() {
129         map.remove("a");
130     }
131 
132     @Test(expected = UnsupportedOperationException.class)
133     public void shouldNotRemoveNull() {
134         map.remove(null);
135     }
136 
137     @Test
138     public void shouldNotHaveEqualEntry() {
139         Map<String, String> map = new ImmutableMap<>(Collections.singletonMap("k", "v"));
140         Entry<String, String> e = map.entrySet().iterator().next();
141         assertThat(e, is(not((Entry<String, String>) null)));
142         assertThat(e, is(not(new Object())));
143     }
144 
145     @Test
146     public void shouldHaveEqualEntry() {
147         Map<String, String> map = new ImmutableMap<>(Collections.singletonMap("k", "v"));
148         Entry<String, String> e = map.entrySet().iterator().next();
149         assertThat(e, is(e));
150         assertThat(e, is((Entry<String, String>) new Node<>("k", "v")));
151     }
152 }