1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.ini4j;
17
18 import static org.junit.Assert.assertArrayEquals;
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertFalse;
21 import static org.junit.Assert.assertNotNull;
22 import static org.junit.Assert.assertNull;
23 import static org.junit.Assert.assertTrue;
24
25 import org.junit.Before;
26 import org.junit.Test;
27
28 import java.util.Arrays;
29 import java.util.HashMap;
30 import java.util.Map;
31 import java.util.Map.Entry;
32 import java.util.Set;
33
34 public class BasicMultiMapTest extends Ini4jCase
35 {
36 private static final String KEY1 = "key1";
37 private static final String KEY2 = "key2";
38 private static final String KEY3 = "key3";
39 private static final String VALUE1 = "value1";
40 private static final String VALUE2 = "value2";
41 private static final String VALUE3 = "value3";
42 private static final String[] VALUES = { VALUE1, VALUE2, VALUE3 };
43 private MultiMap<String, String> _map;
44
45 @Before @Override public void setUp() throws Exception
46 {
47 super.setUp();
48 _map = new BasicMultiMap<String, String>();
49 }
50
51 @Test public void testAdd()
52 {
53 _map.add(KEY1, VALUE1);
54 _map.add(KEY1, VALUE2);
55 _map.add(KEY1, VALUE3);
56 assertEquals(3, _map.length(KEY1));
57 _map.add(KEY1, VALUE3, 0);
58 assertEquals(4, _map.length(KEY1));
59 assertEquals(VALUE3, _map.get(KEY1, 0));
60 assertEquals(VALUE3, _map.get(KEY1, 3));
61 _map.clear();
62 assertTrue(_map.isEmpty());
63 }
64
65 @Test public void testAll()
66 {
67 _map.putAll(KEY1, Arrays.asList(VALUES));
68 assertEquals(VALUES.length, _map.length(KEY1));
69 String[] values = _map.getAll(KEY1).toArray(new String[] {});
70
71 assertArrayEquals(VALUES, values);
72 }
73
74 @Test public void testContainsValue()
75 {
76 _map.putAll(KEY1, Arrays.asList(VALUES));
77 assertTrue(_map.containsValue(VALUE1));
78 assertTrue(_map.containsValue(VALUE2));
79 assertTrue(_map.containsValue(VALUE3));
80 _map.clear();
81 _map.put(KEY2, VALUE1);
82 assertFalse(_map.containsValue(VALUE3));
83 }
84
85 @Test public void testEntrySet()
86 {
87 _map.putAll(KEY1, Arrays.asList(VALUES));
88 _map.put(KEY2, VALUE2);
89 _map.put(KEY3, VALUE3);
90 Set<Entry<String, String>> set = _map.entrySet();
91
92 assertNotNull(set);
93 assertEquals(3, set.size());
94 for (Entry<String, String> e : set)
95 {
96 if (e.getKey().equals(KEY1))
97 {
98 assertEquals(VALUES[2], e.getValue());
99 e.setValue(VALUES[1]);
100 }
101 else if (e.getKey().equals(KEY2))
102 {
103 assertEquals(VALUE2, e.getValue());
104 e.setValue(VALUE3);
105 }
106 else if (e.getKey().equals(KEY3))
107 {
108 assertEquals(VALUE3, e.getValue());
109 e.setValue(VALUE2);
110 }
111 }
112
113 assertEquals(VALUES[1], _map.get(KEY1));
114 assertEquals(VALUES.length, _map.length(KEY1));
115 assertEquals(VALUE3, _map.get(KEY2));
116 assertEquals(VALUE2, _map.get(KEY3));
117 }
118
119 @Test public void testGetEmpty()
120 {
121 assertNull(_map.get(KEY1));
122 assertNull(_map.get(KEY1, 1));
123 }
124
125 @Test public void testPut()
126 {
127 _map.put(KEY1, VALUE1);
128 _map.add(KEY1, VALUE2);
129 assertEquals(VALUE2, _map.get(KEY1, 1));
130 _map.put(KEY1, VALUE3, 1);
131 assertEquals(VALUE3, _map.get(KEY1, 1));
132 assertEquals(VALUE3, _map.get(KEY1));
133 }
134
135 @Test public void testPutAll()
136 {
137 _map.put(KEY1, VALUE1);
138 _map.put(KEY2, VALUE1);
139 _map.add(KEY2, VALUE2);
140 MultiMap<String, String> other = new BasicMultiMap<String, String>();
141
142 other.putAll(_map);
143 assertEquals(2, other.size());
144 assertEquals(2, other.length(KEY2));
145 assertEquals(1, other.length(KEY1));
146 assertEquals(VALUE1, _map.get(KEY1));
147 assertEquals(VALUE1, _map.get(KEY2, 0));
148 assertEquals(VALUE2, _map.get(KEY2, 1));
149 Map<String, String> regular = new HashMap<String, String>(_map);
150
151 _map.clear();
152 _map.putAll(regular);
153 assertEquals(regular.keySet(), _map.keySet());
154 }
155
156 @Test public void testRemove()
157 {
158 _map.add(KEY1, VALUE1);
159 _map.add(KEY2, VALUE1);
160 _map.add(KEY2, VALUE2);
161 _map.add(KEY3, VALUE1);
162 _map.add(KEY3, VALUE2);
163 _map.add(KEY3, VALUE3);
164 assertEquals(VALUE2, _map.get(KEY3, 1));
165 _map.remove(KEY3, 1);
166 assertEquals(VALUE3, _map.get(KEY3, 1));
167 _map.remove(KEY3, 1);
168 assertEquals(VALUE1, _map.get(KEY3));
169 _map.remove(KEY3, 0);
170 assertEquals(0, _map.length(KEY3));
171 assertFalse(_map.containsKey(KEY3));
172 _map.remove(KEY2);
173 assertFalse(_map.containsKey(KEY2));
174 _map.remove(KEY1);
175 assertFalse(_map.containsKey(KEY1));
176 assertEquals(0, _map.size());
177 assertTrue(_map.isEmpty());
178 assertNull(_map.remove(KEY1));
179 assertNull(_map.remove(KEY1, 1));
180 }
181
182 @Test public void testValues()
183 {
184 _map.put(KEY1, VALUE1);
185 _map.put(KEY2, VALUE2);
186 _map.add(KEY2, VALUE3);
187 String[] values = _map.values().toArray(new String[] {});
188
189 Arrays.sort(values);
190 assertArrayEquals(values, VALUES);
191 }
192 }