View Javadoc
1   package org.djutils.multikeymap;
2   
3   import static org.junit.Assert.assertEquals;
4   import static org.junit.Assert.assertNull;
5   import static org.junit.Assert.assertTrue;
6   import static org.junit.Assert.fail;
7   
8   import java.util.Set;
9   import java.util.function.Supplier;
10  
11  import org.junit.Test;
12  
13  /**
14   * Test the MultiKeyMap class.
15   * <p>
16   * Copyright (c) 2013-2022 Delft University of Technology, PO Box 5, 2600 AA, Delft, the Netherlands. All rights reserved. <br>
17   * BSD-style license. See <a href="https://djutils.org/docs/current/djutils/licenses.html">DJUTILS License</a>.
18   * <p>
19   * @author <a href="http://www.tudelft.nl/pknoppers">Peter Knoppers</a>
20   */
21  public class TestMultiKeyMap
22  {
23  
24      /**
25       * Test the MultiKeyMap class.
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              // Ignore expected exception
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              // Ignore expected exception
49          }
50          assertNull("non existent key return null", mkm.get("abc"));
51          mkm.put("value1", "key1");
52          assertEquals("existing key return value for that  key", "value1", mkm.get("key1"));
53          mkm.put("value2", "key2");
54          assertEquals("existing key return value for that  key", "value1", mkm.get("key1"));
55          assertEquals("existing key return value for that  key", "value2", mkm.get("key2"));
56          mkm.clear("value1");
57          assertNull("no longer existent key return null", mkm.get("value1"));
58          assertEquals("existing key return value for that  key", "value2", mkm.get("key2"));
59          String result = mkm.get(new Supplier<String>()
60          {
61              @Override
62              public String get()
63              {
64                  return "newValue3";
65              }
66          }, "key3");
67          assertEquals("result is new value", "newValue3", result);
68          assertEquals("existing key return value for that  key", "newValue3", mkm.get("key3"));
69          String oldValue = mkm.put("newValue3", "key3");
70          assertEquals("existing key returns new value for that  key", "newValue3", mkm.get("key3"));
71          assertEquals("put has returned old value", "newValue3", oldValue);
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("result is unchanged", "newValue3", result);
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              // Ignore expected exception
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             // Ignore expected exception
101         }
102 
103         mkm = new MultiKeyMap<>(String.class, Double.class);
104         result = mkm.get("k", 123.456);
105         assertNull("result should be null", result);
106         mkm.put("dummy", "1", 123.456);
107         assertEquals("two step key works", "dummy", mkm.get("1", 123.456));
108         assertNull("two step key works", mkm.get("1", 123.457));
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             // Ignore expected exception
117         }
118 
119         Set<Object> keySet = mkm.getKeys();
120         assertEquals("there is one key at level 0", 1, keySet.size());
121         assertEquals("type of key is String", String.class, keySet.iterator().next().getClass());
122         assertEquals("object is string with value \"1\"", "1", keySet.iterator().next());
123         keySet = mkm.getKeys("1");
124         assertEquals("there is one key at level 1", 1, keySet.size());
125         assertEquals("type of key is Double", Double.class, keySet.iterator().next().getClass());
126         assertEquals("object is Double with value 123.456", 123.456, keySet.iterator().next());
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             // Ignore expected exception
135         }
136         assertNull("Clearing non-existant sub map returns null", mkm.clear("2", 123.4));
137         Object o = mkm.clear("1", 123.456);
138         assertEquals("result of clear is removed object", "dummy", o);
139         assertNull("dummy is no longer in the map", mkm.get("1", 123.456));
140         mkm.put("dummy", "1", 123.456);
141         assertEquals("dummy is back", "dummy", mkm.get("1", 123.456));
142         mkm.put("dummy2", "2", 23.456);
143         MultiKeyMap<String> subMap = mkm.getSubMap();
144         assertEquals("Top level sub map ", subMap, mkm);
145         subMap = mkm.getSubMap("1");
146         assertEquals("level one sub map contains dummy", "dummy", subMap.get(123.456));
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             // Ignore expected exception
155         }
156         mkm.clear("1");
157         assertNull("dummy was removed", mkm.get("1", 123.456));
158         assertEquals("dummy2 is still there", "dummy2", mkm.get("2", 23.456));
159         mkm.clear();
160         assertEquals("result of clear at top level clears the entire map", 0, mkm.getKeys().size());
161 
162         assertTrue("toString returns something descriptive", mkm.toString().startsWith("MultiKeyMap ["));
163     }
164 }