View Javadoc
1   package emissary.util;
2   
3   import emissary.test.core.junit5.UnitTest;
4   
5   import org.junit.jupiter.api.Test;
6   
7   import java.util.HashMap;
8   import java.util.Map;
9   
10  import static org.junit.jupiter.api.Assertions.assertEquals;
11  import static org.junit.jupiter.api.Assertions.assertNotNull;
12  import static org.junit.jupiter.api.Assertions.assertTrue;
13  
14  class CaseInsensitiveMapTest extends UnitTest {
15  
16      @Test
17      void testMapExtract() {
18          Map<String, String> m = new CaseInsensitiveMap<>(10, 0.75F);
19          m.put("Content-Type", "The Value");
20          String s = m.get("Content-type");
21          assertNotNull(s, "Insenstive extract from map");
22          assertEquals("The Value", s, "Insensitive extract value from map");
23      }
24  
25      @Test
26      void testMapInsert() {
27          Map<String, String> m = new CaseInsensitiveMap<>();
28          m.put("Content-Type", "The Value 1");
29          m.put("Content-type", "The Value 2");
30          assertEquals(1, m.size(), "Insenstive insert");
31          assertEquals("The Value 2", m.get("CONTENT-TYPE"), "Insenstive last val overwrites");
32      }
33  
34      @Test
35      void testPutAll() {
36          Map<String, String> m = new CaseInsensitiveMap<>();
37          Map<String, String> p = new HashMap<>();
38          p.put("Foo", "Bar");
39          p.put("Bar", "Foo");
40          m.putAll(p);
41          assertEquals(2, m.size(), "Putall adds all elements");
42      }
43  
44      @Test
45      void testCopyConstructor() {
46          Map<String, String> p = new HashMap<>();
47          p.put("Foo", "Bar");
48          p.put("Bar", "Foo");
49          Map<String, String> m = new CaseInsensitiveMap<>(p);
50          assertEquals(2, m.size(), "Copy constructor adds all elements");
51      }
52  
53      @Test
54      void testUsingNonStringKeys() {
55          Map<Object, Object> m = new CaseInsensitiveMap<>(10);
56          m.put("Foo", "Bar");
57          assertTrue(m.containsKey("foo"), "ContainsKey must work on case-insensitivity");
58          assertEquals("Bar", m.get("foo"), "Object map must use case-insensitivity for String keys");
59          Pair pair = new Pair("Foo", "Bar");
60          m.put(pair, "Baz");
61          assertTrue(m.containsKey(pair), "Object map must hold non-String keys");
62          assertEquals("Baz", m.get(pair), "Object map must still hold non-String keys");
63      }
64  }