| 1 |
package ezvcard.util; |
| 2 |
|
| 3 |
import static ezvcard.util.TestUtils.assertCollectionContains; |
| 4 |
import static ezvcard.util.TestUtils.assertEqualsAndHash; |
| 5 |
import static ezvcard.util.TestUtils.assertEqualsMethodEssentials; |
| 6 |
import static ezvcard.util.TestUtils.assertNotEqualsBothWays; |
| 7 |
import static org.junit.Assert.assertEquals; |
| 8 |
import static org.junit.Assert.assertFalse; |
| 9 |
import static org.junit.Assert.assertNull; |
| 10 |
import static org.junit.Assert.assertTrue; |
| 11 |
import static org.junit.Assert.fail; |
| 12 |
|
| 13 |
import java.util.ArrayList; |
| 14 |
import java.util.Arrays; |
| 15 |
import java.util.Collection; |
| 16 |
import java.util.Iterator; |
| 17 |
import java.util.List; |
| 18 |
import java.util.ListIterator; |
| 19 |
import java.util.Map; |
| 20 |
import java.util.Set; |
| 21 |
|
| 22 |
import org.junit.Test; |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public class ListMultimapTest { |
| 57 |
@Test |
| 58 |
public void first() { |
| 59 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 60 |
map.put("one", "1"); |
| 61 |
map.put("one", "11"); |
| 62 |
map.put("one", "111"); |
| 63 |
|
| 64 |
assertEquals("1", map.first("one")); |
| 65 |
assertNull(map.first("two")); |
| 66 |
} |
| 67 |
|
| 68 |
@Test |
| 69 |
public void get() { |
| 70 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 71 |
map.put("one", "1"); |
| 72 |
map.put("two", "22"); |
| 73 |
map.put("two", "2"); |
| 74 |
|
| 75 |
assertEquals(Arrays.asList("1"), map.get("one")); |
| 76 |
assertEquals(Arrays.asList("22", "2"), map.get("two")); |
| 77 |
assertTrue(map.get("three").isEmpty()); |
| 78 |
} |
| 79 |
|
| 80 |
@Test |
| 81 |
public void containsKey() { |
| 82 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 83 |
map.put("one", "1"); |
| 84 |
map.put("two", "22"); |
| 85 |
map.put("two", "2"); |
| 86 |
|
| 87 |
assertTrue(map.containsKey("one")); |
| 88 |
assertTrue(map.containsKey("two")); |
| 89 |
assertFalse(map.containsKey("three")); |
| 90 |
} |
| 91 |
|
| 92 |
@Test |
| 93 |
public void put() { |
| 94 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 95 |
map.put("one", "1"); |
| 96 |
map.put("one", "111"); |
| 97 |
map.put("one", "11"); |
| 98 |
map.put("two", "2"); |
| 99 |
|
| 100 |
assertEquals(Arrays.asList("1", "111", "11"), map.get("one")); |
| 101 |
assertEquals(Arrays.asList("2"), map.get("two")); |
| 102 |
} |
| 103 |
|
| 104 |
@Test |
| 105 |
public void putAll() { |
| 106 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 107 |
map.put("one", "1"); |
| 108 |
map.putAll("one", Arrays.asList("111", "11")); |
| 109 |
map.put("two", "2"); |
| 110 |
|
| 111 |
assertEquals(Arrays.asList("1", "111", "11"), map.get("one")); |
| 112 |
assertEquals(Arrays.asList("2"), map.get("two")); |
| 113 |
|
| 114 |
map.putAll("one", new ArrayList<>()); |
| 115 |
assertEquals(Arrays.asList("1", "111", "11"), map.get("one")); |
| 116 |
assertEquals(Arrays.asList("2"), map.get("two")); |
| 117 |
} |
| 118 |
|
| 119 |
@Test |
| 120 |
public void replace_string() { |
| 121 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 122 |
map.put("one", "1"); |
| 123 |
map.put("one", "111"); |
| 124 |
|
| 125 |
assertEquals(Arrays.asList("1", "111"), map.replace("one", "11")); |
| 126 |
assertEquals(Arrays.asList("11"), map.get("one")); |
| 127 |
} |
| 128 |
|
| 129 |
@Test |
| 130 |
public void replace_collection() { |
| 131 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 132 |
map.put("one", "1"); |
| 133 |
map.put("one", "111"); |
| 134 |
|
| 135 |
assertEquals(Arrays.asList("1", "111"), map.replace("one", Arrays.asList("11", "1111"))); |
| 136 |
assertEquals(Arrays.asList("11", "1111"), map.get("one")); |
| 137 |
} |
| 138 |
|
| 139 |
@Test |
| 140 |
public void replace_null_value_string() { |
| 141 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 142 |
map.put("one", "1"); |
| 143 |
map.put("one", "111"); |
| 144 |
|
| 145 |
assertEquals(Arrays.asList("1", "111"), map.replace("one", (String) null)); |
| 146 |
assertTrue(map.isEmpty()); |
| 147 |
} |
| 148 |
|
| 149 |
@Test |
| 150 |
public void remove() { |
| 151 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 152 |
map.put("one", "1"); |
| 153 |
map.put("two", "22"); |
| 154 |
map.put("two", "2"); |
| 155 |
|
| 156 |
assertFalse(map.remove("three", "3")); |
| 157 |
assertFalse(map.remove("two", "222")); |
| 158 |
assertTrue(map.remove("two", "2")); |
| 159 |
assertEquals(Arrays.asList("22"), map.get("two")); |
| 160 |
assertEquals(Arrays.asList("1"), map.get("one")); |
| 161 |
assertCollectionContains(map.keySet(), "one", "two"); |
| 162 |
|
| 163 |
|
| 164 |
assertTrue(map.remove("one", "1")); |
| 165 |
assertCollectionContains(map.keySet(), "two"); |
| 166 |
} |
| 167 |
|
| 168 |
@Test |
| 169 |
public void removeAll() { |
| 170 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 171 |
map.put("one", "1"); |
| 172 |
map.put("two", "22"); |
| 173 |
map.put("two", "2"); |
| 174 |
assertEquals(3, map.size()); |
| 175 |
|
| 176 |
assertTrue(map.removeAll("three").isEmpty()); |
| 177 |
assertEquals(3, map.size()); |
| 178 |
|
| 179 |
List<String> two = map.get("two"); |
| 180 |
assertEquals(Arrays.asList("22", "2"), map.removeAll("two")); |
| 181 |
assertTrue(two.isEmpty()); |
| 182 |
assertTrue(map.get("two").isEmpty()); |
| 183 |
assertEquals(1, map.size()); |
| 184 |
|
| 185 |
try { |
| 186 |
map.removeAll("one").add("2"); |
| 187 |
fail(); |
| 188 |
} catch (UnsupportedOperationException e) { |
| 189 |
|
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
@Test |
| 194 |
public void keySet() { |
| 195 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 196 |
map.put("one", "1"); |
| 197 |
map.put("one", "111"); |
| 198 |
map.put("one", "11"); |
| 199 |
map.put("two", "2"); |
| 200 |
map.put("three", "3"); |
| 201 |
|
| 202 |
Set<String> actual = map.keySet(); |
| 203 |
assertCollectionContains(actual, "one", "two", "three"); |
| 204 |
try { |
| 205 |
actual.add("four"); |
| 206 |
fail(); |
| 207 |
} catch (UnsupportedOperationException e) { |
| 208 |
|
| 209 |
} |
| 210 |
try { |
| 211 |
actual.remove("one"); |
| 212 |
fail(); |
| 213 |
} catch (UnsupportedOperationException e) { |
| 214 |
|
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
@Test |
| 219 |
public void values() { |
| 220 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 221 |
map.put("one", "1"); |
| 222 |
map.put("one", "111"); |
| 223 |
map.put("one", "11"); |
| 224 |
map.put("two", "2"); |
| 225 |
map.put("three", "3"); |
| 226 |
|
| 227 |
Collection<String> actual = map.values(); |
| 228 |
assertEquals(5, actual.size()); |
| 229 |
assertTrue(actual.contains("1")); |
| 230 |
assertTrue(actual.contains("111")); |
| 231 |
assertTrue(actual.contains("11")); |
| 232 |
assertTrue(actual.contains("2")); |
| 233 |
assertTrue(actual.contains("3")); |
| 234 |
} |
| 235 |
|
| 236 |
@Test |
| 237 |
public void isEmpty() { |
| 238 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 239 |
|
| 240 |
assertTrue(map.isEmpty()); |
| 241 |
map.put("one", "1"); |
| 242 |
assertFalse(map.isEmpty()); |
| 243 |
map.removeAll("one"); |
| 244 |
assertTrue(map.isEmpty()); |
| 245 |
} |
| 246 |
|
| 247 |
@Test |
| 248 |
public void size() { |
| 249 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 250 |
|
| 251 |
assertEquals(0, map.size()); |
| 252 |
|
| 253 |
map.put("one", "1"); |
| 254 |
map.put("one", "111"); |
| 255 |
map.put("one", "11"); |
| 256 |
map.put("two", "2"); |
| 257 |
map.put("three", "3"); |
| 258 |
|
| 259 |
assertEquals(5, map.size()); |
| 260 |
|
| 261 |
map.removeAll("one"); |
| 262 |
|
| 263 |
assertEquals(2, map.size()); |
| 264 |
} |
| 265 |
|
| 266 |
@Test |
| 267 |
public void copy_constructor() { |
| 268 |
ListMultimap<String, String> original = new ListMultimap<>(); |
| 269 |
original.put("one", "1"); |
| 270 |
original.put("one", "111"); |
| 271 |
original.put("one", "11"); |
| 272 |
original.put("two", "2"); |
| 273 |
original.put("three", "3"); |
| 274 |
|
| 275 |
|
| 276 |
ListMultimap<String, String> copy = new ListMultimap<>(original); |
| 277 |
assertEquals(Arrays.asList("1", "111", "11"), copy.get("one")); |
| 278 |
assertEquals(Arrays.asList("2"), copy.get("two")); |
| 279 |
assertEquals(Arrays.asList("3"), copy.get("three")); |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
original.removeAll("one"); |
| 284 |
assertEquals(Arrays.asList("1", "111", "11"), copy.get("one")); |
| 285 |
|
| 286 |
original.put("four", "4"); |
| 287 |
assertTrue(copy.get("four").isEmpty()); |
| 288 |
|
| 289 |
original.put("two", "22"); |
| 290 |
assertEquals(Arrays.asList("2"), copy.get("two")); |
| 291 |
|
| 292 |
copy.removeAll("two"); |
| 293 |
assertEquals(Arrays.asList("2", "22"), original.get("two")); |
| 294 |
|
| 295 |
copy.put("five", "5"); |
| 296 |
assertTrue(original.get("five").isEmpty()); |
| 297 |
|
| 298 |
copy.put("three", "33"); |
| 299 |
assertEquals(Arrays.asList("3"), original.get("three")); |
| 300 |
} |
| 301 |
|
| 302 |
@Test |
| 303 |
public void clear() { |
| 304 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 305 |
map.put("one", "1"); |
| 306 |
assertEquals(1, map.size()); |
| 307 |
map.clear(); |
| 308 |
assertEquals(0, map.size()); |
| 309 |
} |
| 310 |
|
| 311 |
@Test |
| 312 |
public void sanitizeKey() { |
| 313 |
ListMultimap<String, String> map = new ListMultimap<String, String>() { |
| 314 |
@Override |
| 315 |
protected String sanitizeKey(String key) { |
| 316 |
return key.toLowerCase(); |
| 317 |
} |
| 318 |
}; |
| 319 |
map.put("one", "1"); |
| 320 |
map.put("One", "111"); |
| 321 |
map.putAll("oNe", Arrays.asList("1111")); |
| 322 |
|
| 323 |
assertEquals("1", map.first("onE")); |
| 324 |
|
| 325 |
List<String> expected = Arrays.asList("1", "111", "1111"); |
| 326 |
assertEquals(expected, map.get("ONe")); |
| 327 |
|
| 328 |
assertTrue(map.remove("oNE", "1")); |
| 329 |
assertEquals(Arrays.asList("111", "1111"), map.removeAll("OnE")); |
| 330 |
assertTrue(map.isEmpty()); |
| 331 |
} |
| 332 |
|
| 333 |
@Test |
| 334 |
public void asMap() { |
| 335 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 336 |
map.put("foo", "1"); |
| 337 |
map.put("foo", "2"); |
| 338 |
map.put("bar", "1"); |
| 339 |
|
| 340 |
Map<String, List<String>> m = map.asMap(); |
| 341 |
assertEquals(2, m.size()); |
| 342 |
assertEquals(Arrays.asList("1", "2"), m.get("foo")); |
| 343 |
assertEquals(Arrays.asList("1"), m.get("bar")); |
| 344 |
|
| 345 |
try { |
| 346 |
m.get("foo").add("3"); |
| 347 |
fail(); |
| 348 |
} catch (UnsupportedOperationException e) { |
| 349 |
|
| 350 |
} |
| 351 |
|
| 352 |
try { |
| 353 |
m.put("foo", new ArrayList<>()); |
| 354 |
fail(); |
| 355 |
} catch (UnsupportedOperationException e) { |
| 356 |
|
| 357 |
} |
| 358 |
} |
| 359 |
|
| 360 |
@Test |
| 361 |
public void iterator() { |
| 362 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 363 |
map.put("foo", "1"); |
| 364 |
map.put("foo", "2"); |
| 365 |
map.put("bar", "1"); |
| 366 |
|
| 367 |
Iterator<Map.Entry<String, List<String>>> it = map.iterator(); |
| 368 |
|
| 369 |
Map.Entry<String, List<String>> entry = it.next(); |
| 370 |
assertEquals("foo", entry.getKey()); |
| 371 |
assertEquals(Arrays.asList("1", "2"), entry.getValue()); |
| 372 |
try { |
| 373 |
entry.getValue().add("3"); |
| 374 |
fail(); |
| 375 |
} catch (UnsupportedOperationException e) { |
| 376 |
|
| 377 |
} |
| 378 |
try { |
| 379 |
entry.setValue(new ArrayList<>()); |
| 380 |
fail(); |
| 381 |
} catch (UnsupportedOperationException e) { |
| 382 |
|
| 383 |
} |
| 384 |
try { |
| 385 |
it.remove(); |
| 386 |
fail(); |
| 387 |
} catch (UnsupportedOperationException e) { |
| 388 |
|
| 389 |
} |
| 390 |
|
| 391 |
entry = it.next(); |
| 392 |
assertEquals("bar", entry.getKey()); |
| 393 |
assertEquals(Arrays.asList("1"), entry.getValue()); |
| 394 |
try { |
| 395 |
entry.getValue().add("3"); |
| 396 |
fail(); |
| 397 |
} catch (UnsupportedOperationException e) { |
| 398 |
|
| 399 |
} |
| 400 |
try { |
| 401 |
entry.setValue(new ArrayList<>()); |
| 402 |
fail(); |
| 403 |
} catch (UnsupportedOperationException e) { |
| 404 |
|
| 405 |
} |
| 406 |
try { |
| 407 |
it.remove(); |
| 408 |
fail(); |
| 409 |
} catch (UnsupportedOperationException e) { |
| 410 |
|
| 411 |
} |
| 412 |
|
| 413 |
assertFalse(it.hasNext()); |
| 414 |
} |
| 415 |
|
| 416 |
@Test |
| 417 |
public void equals() { |
| 418 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 419 |
assertEqualsMethodEssentials(map); |
| 420 |
|
| 421 |
ListMultimap<String, String> one = new ListMultimap<>(); |
| 422 |
ListMultimap<String, String> two = new ListMultimap<>(); |
| 423 |
two.put("foo", "1"); |
| 424 |
assertNotEqualsBothWays(one, two); |
| 425 |
|
| 426 |
one.put("foo", "1"); |
| 427 |
assertEqualsAndHash(one, two); |
| 428 |
|
| 429 |
} |
| 430 |
|
| 431 |
@Test |
| 432 |
public void WrappedList_addAll() { |
| 433 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 434 |
List<String> list = map.get("foo"); |
| 435 |
|
| 436 |
assertFalse(list.addAll(new ArrayList<>())); |
| 437 |
|
| 438 |
assertTrue(list.addAll(Arrays.asList("1", "2"))); |
| 439 |
assertEquals(Arrays.asList("1", "2"), list); |
| 440 |
assertEquals(Arrays.asList("1", "2"), map.get("foo")); |
| 441 |
|
| 442 |
assertTrue(list.addAll(Arrays.asList("3", "4"))); |
| 443 |
assertEquals(Arrays.asList("1", "2", "3", "4"), list); |
| 444 |
assertEquals(Arrays.asList("1", "2", "3", "4"), map.get("foo")); |
| 445 |
} |
| 446 |
|
| 447 |
@Test |
| 448 |
public void WrappedList_addAll_index() { |
| 449 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 450 |
List<String> list = map.get("foo"); |
| 451 |
|
| 452 |
assertFalse(list.addAll(0, new ArrayList<>())); |
| 453 |
|
| 454 |
assertTrue(list.addAll(0, Arrays.asList("1", "2"))); |
| 455 |
assertEquals(Arrays.asList("1", "2"), list); |
| 456 |
assertEquals(Arrays.asList("1", "2"), map.get("foo")); |
| 457 |
|
| 458 |
assertTrue(list.addAll(0, Arrays.asList("3", "4"))); |
| 459 |
assertEquals(Arrays.asList("3", "4", "1", "2"), list); |
| 460 |
assertEquals(Arrays.asList("3", "4", "1", "2"), map.get("foo")); |
| 461 |
} |
| 462 |
|
| 463 |
@Test |
| 464 |
public void WrappedList_get() { |
| 465 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 466 |
List<String> list = map.get("foo"); |
| 467 |
|
| 468 |
map.put("foo", "1"); |
| 469 |
assertEquals("1", list.get(0)); |
| 470 |
} |
| 471 |
|
| 472 |
@Test |
| 473 |
public void WrappedList_set() { |
| 474 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 475 |
List<String> list = map.get("foo"); |
| 476 |
|
| 477 |
map.put("foo", "1"); |
| 478 |
assertEquals("1", list.set(0, "2")); |
| 479 |
assertEquals(Arrays.asList("2"), list); |
| 480 |
assertEquals(Arrays.asList("2"), map.get("foo")); |
| 481 |
} |
| 482 |
|
| 483 |
@Test |
| 484 |
public void WrappedList_add() { |
| 485 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 486 |
List<String> list = map.get("foo"); |
| 487 |
|
| 488 |
list.add("1"); |
| 489 |
assertEquals(Arrays.asList("1"), list); |
| 490 |
assertEquals(Arrays.asList("1"), map.get("foo")); |
| 491 |
} |
| 492 |
|
| 493 |
@Test |
| 494 |
public void WrappedList_add_index() { |
| 495 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 496 |
List<String> list = map.get("foo"); |
| 497 |
|
| 498 |
list.add(0, "1"); |
| 499 |
assertEquals(Arrays.asList("1"), list); |
| 500 |
assertEquals(Arrays.asList("1"), map.get("foo")); |
| 501 |
} |
| 502 |
|
| 503 |
@Test |
| 504 |
public void WrappedList_remove() { |
| 505 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 506 |
List<String> list = map.get("foo"); |
| 507 |
map.put("foo", "1"); |
| 508 |
|
| 509 |
assertFalse(list.remove("2")); |
| 510 |
assertTrue(list.remove("1")); |
| 511 |
|
| 512 |
assertEquals(Arrays.asList(), list); |
| 513 |
assertEquals(Arrays.asList(), map.get("foo")); |
| 514 |
assertCollectionContains(map.keySet()); |
| 515 |
} |
| 516 |
|
| 517 |
@Test |
| 518 |
public void WrappedList_removeAll() { |
| 519 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 520 |
List<String> list = map.get("foo"); |
| 521 |
map.put("foo", "1"); |
| 522 |
map.put("foo", "2"); |
| 523 |
map.put("foo", "3"); |
| 524 |
|
| 525 |
assertFalse(list.removeAll(Arrays.asList())); |
| 526 |
assertEquals(Arrays.asList("1", "2", "3"), list); |
| 527 |
assertCollectionContains(map.keySet(), "foo"); |
| 528 |
|
| 529 |
assertFalse(list.removeAll(Arrays.asList("4"))); |
| 530 |
assertEquals(Arrays.asList("1", "2", "3"), list); |
| 531 |
assertCollectionContains(map.keySet(), "foo"); |
| 532 |
|
| 533 |
assertTrue(list.removeAll(Arrays.asList("1", "2"))); |
| 534 |
assertEquals(Arrays.asList("3"), list); |
| 535 |
assertCollectionContains(map.keySet(), "foo"); |
| 536 |
|
| 537 |
assertTrue(list.removeAll(Arrays.asList("3"))); |
| 538 |
assertEquals(Arrays.asList(), list); |
| 539 |
assertCollectionContains(map.keySet()); |
| 540 |
} |
| 541 |
|
| 542 |
@Test |
| 543 |
public void WrappedList_retainAll() { |
| 544 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 545 |
List<String> list = map.get("foo"); |
| 546 |
map.put("foo", "1"); |
| 547 |
map.put("foo", "2"); |
| 548 |
map.put("foo", "3"); |
| 549 |
|
| 550 |
assertFalse(list.retainAll(Arrays.asList("1", "2", "3"))); |
| 551 |
assertEquals(Arrays.asList("1", "2", "3"), list); |
| 552 |
assertCollectionContains(map.keySet(), "foo"); |
| 553 |
|
| 554 |
assertTrue(list.retainAll(Arrays.asList("1", "4"))); |
| 555 |
assertEquals(Arrays.asList("1"), list); |
| 556 |
assertCollectionContains(map.keySet(), "foo"); |
| 557 |
|
| 558 |
assertTrue(list.retainAll(Arrays.asList())); |
| 559 |
assertEquals(Arrays.asList(), list); |
| 560 |
assertCollectionContains(map.keySet()); |
| 561 |
} |
| 562 |
|
| 563 |
@Test |
| 564 |
public void WrappedList_clear() { |
| 565 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 566 |
List<String> list = map.get("foo"); |
| 567 |
map.put("foo", "1"); |
| 568 |
map.put("foo", "2"); |
| 569 |
map.put("foo", "3"); |
| 570 |
|
| 571 |
list.clear(); |
| 572 |
assertTrue(list.isEmpty()); |
| 573 |
assertCollectionContains(map.keySet()); |
| 574 |
|
| 575 |
list.clear(); |
| 576 |
assertTrue(list.isEmpty()); |
| 577 |
assertCollectionContains(map.keySet()); |
| 578 |
} |
| 579 |
|
| 580 |
@Test |
| 581 |
public void WrappedList_indexOf() { |
| 582 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 583 |
List<String> list = map.get("foo"); |
| 584 |
map.put("foo", "1"); |
| 585 |
map.put("foo", "1"); |
| 586 |
|
| 587 |
assertEquals(-1, list.indexOf("2")); |
| 588 |
assertEquals(0, list.indexOf("1")); |
| 589 |
assertEquals(0, map.get("foo").indexOf("1")); |
| 590 |
} |
| 591 |
|
| 592 |
@Test |
| 593 |
public void WrappedList_lastIndexOf() { |
| 594 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 595 |
List<String> list = map.get("foo"); |
| 596 |
map.put("foo", "1"); |
| 597 |
map.put("foo", "1"); |
| 598 |
|
| 599 |
assertEquals(-1, list.lastIndexOf("2")); |
| 600 |
assertEquals(1, list.lastIndexOf("1")); |
| 601 |
assertEquals(1, map.get("foo").lastIndexOf("1")); |
| 602 |
} |
| 603 |
|
| 604 |
@Test |
| 605 |
public void WrappedList_subList() { |
| 606 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 607 |
map.putAll("foo", Arrays.asList("1", "2", "3", "4")); |
| 608 |
|
| 609 |
List<String> list = map.get("foo"); |
| 610 |
List<String> subList = list.subList(0, 2); |
| 611 |
|
| 612 |
assertEquals("1", subList.remove(0)); |
| 613 |
assertEquals(Arrays.asList("2"), subList); |
| 614 |
assertEquals(Arrays.asList("2", "3", "4"), list); |
| 615 |
assertEquals(Arrays.asList("2", "3", "4"), map.get("foo")); |
| 616 |
|
| 617 |
subList.add(0, "5"); |
| 618 |
assertEquals(Arrays.asList("5", "2"), subList); |
| 619 |
assertEquals(Arrays.asList("5", "2", "3", "4"), list); |
| 620 |
assertEquals(Arrays.asList("5", "2", "3", "4"), map.get("foo")); |
| 621 |
|
| 622 |
subList.clear(); |
| 623 |
assertEquals(Arrays.asList(), subList); |
| 624 |
assertEquals(Arrays.asList("3", "4"), list); |
| 625 |
assertEquals(Arrays.asList("3", "4"), map.get("foo")); |
| 626 |
|
| 627 |
list = map.get("bar"); |
| 628 |
subList = list.subList(0, 0); |
| 629 |
|
| 630 |
subList.add("1"); |
| 631 |
assertEquals(Arrays.asList("1"), subList); |
| 632 |
assertEquals(Arrays.asList("1"), list); |
| 633 |
assertEquals(Arrays.asList("1"), map.get("bar")); |
| 634 |
} |
| 635 |
|
| 636 |
@Test |
| 637 |
public void WrappedList_subSubList() { |
| 638 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 639 |
map.putAll("foo", Arrays.asList("1", "2", "3", "4")); |
| 640 |
|
| 641 |
List<String> list = map.get("foo"); |
| 642 |
List<String> subList = list.subList(0, 2); |
| 643 |
List<String> subSubList = subList.subList(0, 1); |
| 644 |
|
| 645 |
assertEquals("1", subSubList.remove(0)); |
| 646 |
assertEquals(Arrays.asList(), subSubList); |
| 647 |
assertEquals(Arrays.asList("2"), subList); |
| 648 |
assertEquals(Arrays.asList("2", "3", "4"), list); |
| 649 |
assertEquals(Arrays.asList("2", "3", "4"), map.get("foo")); |
| 650 |
|
| 651 |
subSubList.add(0, "5"); |
| 652 |
assertEquals(Arrays.asList("5"), subSubList); |
| 653 |
assertEquals(Arrays.asList("5", "2"), subList); |
| 654 |
assertEquals(Arrays.asList("5", "2", "3", "4"), list); |
| 655 |
assertEquals(Arrays.asList("5", "2", "3", "4"), map.get("foo")); |
| 656 |
|
| 657 |
subSubList.clear(); |
| 658 |
assertEquals(Arrays.asList(), subSubList); |
| 659 |
assertEquals(Arrays.asList("2"), subList); |
| 660 |
assertEquals(Arrays.asList("2", "3", "4"), list); |
| 661 |
assertEquals(Arrays.asList("2", "3", "4"), map.get("foo")); |
| 662 |
} |
| 663 |
|
| 664 |
@Test |
| 665 |
public void WrappedList_equals() { |
| 666 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 667 |
List<String> list = map.get("foo"); |
| 668 |
map.put("foo", "1"); |
| 669 |
|
| 670 |
assertEqualsMethodEssentials(list); |
| 671 |
assertTrue(list.equals(Arrays.asList("1"))); |
| 672 |
assertFalse(list.equals(Arrays.asList("2"))); |
| 673 |
} |
| 674 |
|
| 675 |
@Test |
| 676 |
public void WrappedList_hashCode() { |
| 677 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 678 |
List<String> list = map.get("foo"); |
| 679 |
map.put("foo", "1"); |
| 680 |
map.put("bar", "1"); |
| 681 |
|
| 682 |
assertEquals(list.hashCode(), map.get("bar").hashCode()); |
| 683 |
} |
| 684 |
|
| 685 |
@Test |
| 686 |
public void WrappedList_contains() { |
| 687 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 688 |
List<String> list = map.get("foo"); |
| 689 |
map.put("foo", "1"); |
| 690 |
|
| 691 |
assertFalse(list.contains("3")); |
| 692 |
assertTrue(list.contains("1")); |
| 693 |
} |
| 694 |
|
| 695 |
@Test |
| 696 |
public void WrappedList_containsAll() { |
| 697 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 698 |
List<String> list = map.get("foo"); |
| 699 |
map.put("foo", "1"); |
| 700 |
map.put("foo", "2"); |
| 701 |
map.put("foo", "3"); |
| 702 |
|
| 703 |
assertFalse(list.containsAll(Arrays.asList("4"))); |
| 704 |
assertTrue(list.containsAll(Arrays.asList("1"))); |
| 705 |
} |
| 706 |
|
| 707 |
@Test |
| 708 |
public void WrappedList_listIterator() { |
| 709 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 710 |
List<String> list = map.get("foo"); |
| 711 |
map.put("foo", "1"); |
| 712 |
map.put("foo", "2"); |
| 713 |
map.put("foo", "3"); |
| 714 |
map.put("foo", "4"); |
| 715 |
|
| 716 |
ListIterator<String> it = list.listIterator(); |
| 717 |
|
| 718 |
assertFalse(it.hasPrevious()); |
| 719 |
assertTrue(it.hasNext()); |
| 720 |
assertEquals(-1, it.previousIndex()); |
| 721 |
assertEquals(0, it.nextIndex()); |
| 722 |
assertEquals("1", it.next()); |
| 723 |
assertEquals(Arrays.asList("1", "2", "3", "4"), list); |
| 724 |
assertEquals(Arrays.asList("1", "2", "3", "4"), map.get("foo")); |
| 725 |
|
| 726 |
assertTrue(it.hasPrevious()); |
| 727 |
assertTrue(it.hasNext()); |
| 728 |
assertEquals(0, it.previousIndex()); |
| 729 |
assertEquals(1, it.nextIndex()); |
| 730 |
it.remove(); |
| 731 |
assertEquals(Arrays.asList("2", "3", "4"), list); |
| 732 |
assertEquals(Arrays.asList("2", "3", "4"), map.get("foo")); |
| 733 |
|
| 734 |
assertFalse(it.hasPrevious()); |
| 735 |
assertTrue(it.hasNext()); |
| 736 |
assertEquals(-1, it.previousIndex()); |
| 737 |
assertEquals(0, it.nextIndex()); |
| 738 |
assertEquals("2", it.next()); |
| 739 |
assertEquals(Arrays.asList("2", "3", "4"), list); |
| 740 |
assertEquals(Arrays.asList("2", "3", "4"), map.get("foo")); |
| 741 |
|
| 742 |
assertTrue(it.hasPrevious()); |
| 743 |
assertTrue(it.hasNext()); |
| 744 |
assertEquals(0, it.previousIndex()); |
| 745 |
assertEquals(1, it.nextIndex()); |
| 746 |
it.set("5"); |
| 747 |
assertEquals(Arrays.asList("5", "3", "4"), list); |
| 748 |
assertEquals(Arrays.asList("5", "3", "4"), map.get("foo")); |
| 749 |
|
| 750 |
assertTrue(it.hasPrevious()); |
| 751 |
assertTrue(it.hasNext()); |
| 752 |
assertEquals(0, it.previousIndex()); |
| 753 |
assertEquals(1, it.nextIndex()); |
| 754 |
assertEquals("3", it.next()); |
| 755 |
assertEquals(Arrays.asList("5", "3", "4"), list); |
| 756 |
assertEquals(Arrays.asList("5", "3", "4"), map.get("foo")); |
| 757 |
|
| 758 |
assertTrue(it.hasPrevious()); |
| 759 |
assertTrue(it.hasNext()); |
| 760 |
assertEquals(1, it.previousIndex()); |
| 761 |
assertEquals(2, it.nextIndex()); |
| 762 |
assertEquals("3", it.previous()); |
| 763 |
assertEquals(Arrays.asList("5", "3", "4"), list); |
| 764 |
assertEquals(Arrays.asList("5", "3", "4"), map.get("foo")); |
| 765 |
|
| 766 |
assertTrue(it.hasPrevious()); |
| 767 |
assertTrue(it.hasNext()); |
| 768 |
assertEquals(0, it.previousIndex()); |
| 769 |
assertEquals(1, it.nextIndex()); |
| 770 |
assertEquals("3", it.next()); |
| 771 |
assertEquals(Arrays.asList("5", "3", "4"), list); |
| 772 |
assertEquals(Arrays.asList("5", "3", "4"), map.get("foo")); |
| 773 |
|
| 774 |
assertTrue(it.hasPrevious()); |
| 775 |
assertTrue(it.hasNext()); |
| 776 |
assertEquals(1, it.previousIndex()); |
| 777 |
assertEquals(2, it.nextIndex()); |
| 778 |
assertEquals("4", it.next()); |
| 779 |
assertEquals(Arrays.asList("5", "3", "4"), list); |
| 780 |
assertEquals(Arrays.asList("5", "3", "4"), map.get("foo")); |
| 781 |
|
| 782 |
assertTrue(it.hasPrevious()); |
| 783 |
assertFalse(it.hasNext()); |
| 784 |
assertEquals(2, it.previousIndex()); |
| 785 |
assertEquals(3, it.nextIndex()); |
| 786 |
it.add("6"); |
| 787 |
assertEquals(Arrays.asList("5", "3", "4", "6"), list); |
| 788 |
assertEquals(Arrays.asList("5", "3", "4", "6"), map.get("foo")); |
| 789 |
|
| 790 |
assertTrue(it.hasPrevious()); |
| 791 |
assertFalse(it.hasNext()); |
| 792 |
assertEquals(3, it.previousIndex()); |
| 793 |
assertEquals(4, it.nextIndex()); |
| 794 |
} |
| 795 |
|
| 796 |
@Test |
| 797 |
public void WrappedList_listIterator_empty_add() { |
| 798 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 799 |
List<String> list = map.get("foo"); |
| 800 |
|
| 801 |
ListIterator<String> it = list.listIterator(); |
| 802 |
|
| 803 |
assertFalse(it.hasPrevious()); |
| 804 |
assertFalse(it.hasNext()); |
| 805 |
assertEquals(-1, it.previousIndex()); |
| 806 |
assertEquals(0, it.nextIndex()); |
| 807 |
it.add("1"); |
| 808 |
assertEquals(Arrays.asList("1"), list); |
| 809 |
assertEquals(Arrays.asList("1"), map.get("foo")); |
| 810 |
|
| 811 |
assertTrue(it.hasPrevious()); |
| 812 |
assertFalse(it.hasNext()); |
| 813 |
assertEquals(0, it.previousIndex()); |
| 814 |
assertEquals(1, it.nextIndex()); |
| 815 |
} |
| 816 |
|
| 817 |
@Test |
| 818 |
public void WrappedList_listIterator_index() { |
| 819 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 820 |
List<String> list = map.get("foo"); |
| 821 |
map.put("foo", "1"); |
| 822 |
map.put("foo", "2"); |
| 823 |
map.put("foo", "3"); |
| 824 |
map.put("foo", "4"); |
| 825 |
|
| 826 |
ListIterator<String> it = list.listIterator(1); |
| 827 |
|
| 828 |
assertTrue(it.hasPrevious()); |
| 829 |
assertTrue(it.hasNext()); |
| 830 |
assertEquals(0, it.previousIndex()); |
| 831 |
assertEquals(1, it.nextIndex()); |
| 832 |
assertEquals("2", it.next()); |
| 833 |
} |
| 834 |
|
| 835 |
@Test |
| 836 |
public void WrappedList_iterator() { |
| 837 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 838 |
List<String> list = map.get("foo"); |
| 839 |
map.put("foo", "1"); |
| 840 |
map.put("foo", "2"); |
| 841 |
map.put("foo", "3"); |
| 842 |
map.put("foo", "4"); |
| 843 |
|
| 844 |
Iterator<String> it = list.iterator(); |
| 845 |
|
| 846 |
assertTrue(it.hasNext()); |
| 847 |
assertEquals("1", it.next()); |
| 848 |
assertTrue(it.hasNext()); |
| 849 |
assertEquals("2", it.next()); |
| 850 |
assertTrue(it.hasNext()); |
| 851 |
assertEquals("3", it.next()); |
| 852 |
assertTrue(it.hasNext()); |
| 853 |
assertEquals("4", it.next()); |
| 854 |
assertFalse(it.hasNext()); |
| 855 |
} |
| 856 |
|
| 857 |
@Test |
| 858 |
public void WrappedList_two_instances() { |
| 859 |
ListMultimap<String, String> map = new ListMultimap<>(); |
| 860 |
List<String> one = map.get("foo"); |
| 861 |
List<String> two = map.get("foo"); |
| 862 |
|
| 863 |
assertTrue(one.isEmpty()); |
| 864 |
assertTrue(two.isEmpty()); |
| 865 |
assertCollectionContains(map.keySet()); |
| 866 |
|
| 867 |
one.add("1"); |
| 868 |
assertEquals(Arrays.asList("1"), one); |
| 869 |
assertEquals(Arrays.asList("1"), two); |
| 870 |
assertCollectionContains(map.keySet(), "foo"); |
| 871 |
|
| 872 |
two.add("2"); |
| 873 |
assertEquals(Arrays.asList("1", "2"), one); |
| 874 |
assertEquals(Arrays.asList("1", "2"), two); |
| 875 |
assertCollectionContains(map.keySet(), "foo"); |
| 876 |
|
| 877 |
one.addAll(Arrays.asList("3", "4")); |
| 878 |
assertEquals(Arrays.asList("1", "2", "3", "4"), one); |
| 879 |
assertEquals(Arrays.asList("1", "2", "3", "4"), two); |
| 880 |
assertCollectionContains(map.keySet(), "foo"); |
| 881 |
|
| 882 |
map.clear(); |
| 883 |
assertTrue(one.isEmpty()); |
| 884 |
assertTrue(two.isEmpty()); |
| 885 |
assertCollectionContains(map.keySet()); |
| 886 |
|
| 887 |
map.put("foo", "1"); |
| 888 |
assertEquals(Arrays.asList("1"), one); |
| 889 |
assertEquals(Arrays.asList("1"), two); |
| 890 |
assertCollectionContains(map.keySet(), "foo"); |
| 891 |
|
| 892 |
one.clear(); |
| 893 |
assertTrue(one.isEmpty()); |
| 894 |
assertTrue(two.isEmpty()); |
| 895 |
assertCollectionContains(map.keySet()); |
| 896 |
} |
| 897 |
} |