1 package org.djutils.multikeymap;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.assertNull;
5 import static org.junit.jupiter.api.Assertions.assertTrue;
6 import static org.junit.jupiter.api.Assertions.fail;
7
8 import java.util.Set;
9 import java.util.function.Supplier;
10
11 import org.junit.jupiter.api.Test;
12
13
14
15
16
17
18
19
20
21 public class TestMultiKeyMap
22 {
23
24
25
26
27 @Test
28 public void testMultiKeyMap()
29 {
30 try
31 {
32 new MultiKeyMap<>();
33 fail("empty key list should have thrown an exception");
34 }
35 catch (IllegalArgumentException iae)
36 {
37
38 }
39
40 MultiKeyMap<String> mkm = new MultiKeyMap<>(String.class);
41 try
42 {
43 mkm.get(123.456);
44 fail("key of wrong type should have thrown an exception");
45 }
46 catch (IllegalArgumentException iae)
47 {
48
49 }
50 assertNull(mkm.get("abc"), "non existent key return null");
51 mkm.put("value1", "key1");
52 assertEquals("value1", mkm.get("key1"), "existing key return value for that key");
53 mkm.put("value2", "key2");
54 assertEquals("value1", mkm.get("key1"), "existing key return value for that key");
55 assertEquals("value2", mkm.get("key2"), "existing key return value for that key");
56 mkm.clear("value1");
57 assertNull(mkm.get("value1"), "no longer existent key return null");
58 assertEquals("value2", mkm.get("key2"), "existing key return value for that key");
59 String result = mkm.get(new Supplier<String>()
60 {
61 @Override
62 public String get()
63 {
64 return "newValue3";
65 }
66 }, "key3");
67 assertEquals("newValue3", result, "result is new value");
68 assertEquals("newValue3", mkm.get("key3"), "existing key return value for that key");
69 String oldValue = mkm.put("newValue3", "key3");
70 assertEquals("newValue3", mkm.get("key3"), "existing key returns new value for that key");
71 assertEquals("newValue3", oldValue, "put has returned old value");
72 result = mkm.get(new Supplier<String>()
73 {
74 @Override
75 public String get()
76 {
77 fail("get method in Supplier should not have been called");
78 return "newNewalue3";
79 }
80 }, "key3");
81 assertEquals("newValue3", result, "result is unchanged");
82
83 try
84 {
85 mkm.get("k", "l", "m");
86 fail("Wrong number of keys should have thrown an exeption");
87 }
88 catch (IllegalArgumentException iae)
89 {
90
91 }
92
93 try
94 {
95 mkm.get();
96 fail("Wrong number of keys should have thrown an exeption");
97 }
98 catch (IllegalArgumentException iae)
99 {
100
101 }
102
103 mkm = new MultiKeyMap<>(String.class, Double.class);
104 result = mkm.get("k", 123.456);
105 assertNull(result, "result should be null");
106 mkm.put("dummy", "1", 123.456);
107 assertEquals("dummy", mkm.get("1", 123.456), "two step key works");
108 assertNull(mkm.get("1", 123.457), "two step key works");
109 try
110 {
111 mkm.get("1", "2");
112 fail("Wrong type of last key should have thrown an exception");
113 }
114 catch (IllegalArgumentException iae)
115 {
116
117 }
118
119 Set<Object> keySet = mkm.getKeys();
120 assertEquals(1, keySet.size(), "there is one key at level 0");
121 assertEquals(String.class, keySet.iterator().next().getClass(), "type of key is String");
122 assertEquals("1", keySet.iterator().next(), "object is string with value \"1\"");
123 keySet = mkm.getKeys("1");
124 assertEquals(1, keySet.size(), "there is one key at level 1");
125 assertEquals(Double.class, keySet.iterator().next().getClass(), "type of key is Double");
126 assertEquals(123.456, keySet.iterator().next(), "object is Double with value 123.456");
127 try
128 {
129 mkm.getKeys("1", 123.456, "3");
130 fail("too many keys should have thrown an exception");
131 }
132 catch (IllegalArgumentException iae)
133 {
134
135 }
136 assertNull(mkm.clear("2", 123.4), "Clearing non-existant sub map returns null");
137 Object o = mkm.clear("1", 123.456);
138 assertEquals("dummy", o, "result of clear is removed object");
139 assertNull(mkm.get("1", 123.456), "dummy is no longer in the map");
140 mkm.put("dummy", "1", 123.456);
141 assertEquals("dummy", mkm.get("1", 123.456), "dummy is back");
142 mkm.put("dummy2", "2", 23.456);
143 MultiKeyMap<String> subMap = mkm.getSubMap();
144 assertEquals(subMap, mkm, "Top level sub map ");
145 subMap = mkm.getSubMap("1");
146 assertEquals("dummy", subMap.get(123.456), "level one sub map contains dummy");
147 try
148 {
149 mkm.getSubMap("1", 123.456);
150 fail("Too many arguments should have thrown an exception");
151 }
152 catch (IllegalArgumentException iae)
153 {
154
155 }
156 mkm.clear("1");
157 assertNull(mkm.get("1", 123.456), "dummy was removed");
158 assertEquals("dummy2", mkm.get("2", 23.456), "dummy2 is still there");
159 mkm.clear();
160 assertEquals(0, mkm.getKeys().size(), "result of clear at top level clears the entire map");
161
162 assertTrue(mkm.toString().startsWith("MultiKeyMap ["), "toString returns something descriptive");
163 }
164 }