diff options
Diffstat (limited to 'guava-tests/test/com/google/common/collect/MultimapsTest.java')
-rw-r--r-- | guava-tests/test/com/google/common/collect/MultimapsTest.java | 151 |
1 files changed, 63 insertions, 88 deletions
diff --git a/guava-tests/test/com/google/common/collect/MultimapsTest.java b/guava-tests/test/com/google/common/collect/MultimapsTest.java index 5047842..5ba5180 100644 --- a/guava-tests/test/com/google/common/collect/MultimapsTest.java +++ b/guava-tests/test/com/google/common/collect/MultimapsTest.java @@ -16,13 +16,12 @@ package com.google.common.collect; -import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.collect.Maps.immutableEntry; import static com.google.common.collect.Sets.newHashSet; import static com.google.common.collect.testing.Helpers.nefariousMapEntry; import static com.google.common.collect.testing.IteratorFeature.MODIFIABLE; import static java.util.Arrays.asList; -import static org.truth0.Truth.ASSERT; +import static org.junit.contrib.truth.Truth.ASSERT; import com.google.common.annotations.GwtCompatible; import com.google.common.annotations.GwtIncompatible; @@ -62,7 +61,6 @@ import javax.annotation.Nullable; */ @GwtCompatible(emulated = true) public class MultimapsTest extends AbstractMultimapTest { - private static final Comparator<Integer> INT_COMPARATOR = Ordering.<Integer>natural().reverse().nullsFirst(); @@ -80,7 +78,7 @@ public class MultimapsTest extends AbstractMultimapTest { } @SuppressWarnings("deprecation") - public void testUnmodifiableListMultimapShortCircuit() { + public void testUnmodifiableListMultimapShortCircuit(){ ListMultimap<String, Integer> mod = ArrayListMultimap.create(); ListMultimap<String, Integer> unmod = Multimaps.unmodifiableListMultimap(mod); assertNotSame(mod, unmod); @@ -93,7 +91,7 @@ public class MultimapsTest extends AbstractMultimapTest { } @SuppressWarnings("deprecation") - public void testUnmodifiableSetMultimapShortCircuit() { + public void testUnmodifiableSetMultimapShortCircuit(){ SetMultimap<String, Integer> mod = HashMultimap.create(); SetMultimap<String, Integer> unmod = Multimaps.unmodifiableSetMultimap(mod); assertNotSame(mod, unmod); @@ -106,7 +104,7 @@ public class MultimapsTest extends AbstractMultimapTest { } @SuppressWarnings("deprecation") - public void testUnmodifiableMultimapShortCircuit() { + public void testUnmodifiableMultimapShortCircuit(){ Multimap<String, Integer> mod = HashMultimap.create(); Multimap<String, Integer> unmod = Multimaps.unmodifiableMultimap(mod); assertNotSame(mod, unmod); @@ -232,7 +230,7 @@ public class MultimapsTest extends AbstractMultimapTest { assertTrue(unmod.containsEntry("foo", 1)); assertEquals(mod, unmod); } - + @SuppressWarnings("unchecked") public void testUnmodifiableMultimapEntries() { Multimap<String, Integer> mod = HashMultimap.create(); @@ -298,7 +296,7 @@ public class MultimapsTest extends AbstractMultimapTest { assertEquals(multimap.hashCode(), unmodifiable.hashCode()); assertEquals(multimap, unmodifiable); - ASSERT.that(unmodifiable.asMap().get("bar")).has().allOf(5, -1); + ASSERT.that(unmodifiable.asMap().get("bar")).hasContentsAnyOrder(5, -1); assertNull(unmodifiable.asMap().get("missing")); assertFalse(unmodifiable.entries() instanceof Serializable); @@ -430,11 +428,11 @@ public class MultimapsTest extends AbstractMultimapTest { assertFalse(map.containsKey("bar")); assertEquals(map.keySet(), multimapView.keySet()); assertEquals(map.keySet(), multimapView.keys().elementSet()); - ASSERT.that(multimapView.keys()).has().item("foo"); - ASSERT.that(multimapView.values()).has().item(1); - ASSERT.that(multimapView.entries()).has().item( + ASSERT.that(multimapView.keys()).hasContentsAnyOrder("foo"); + ASSERT.that(multimapView.values()).hasContentsAnyOrder(1); + ASSERT.that(multimapView.entries()).hasContentsAnyOrder( Maps.immutableEntry("foo", 1)); - ASSERT.that(multimapView.asMap().entrySet()).has().item( + ASSERT.that(multimapView.asMap().entrySet()).hasContentsAnyOrder( Maps.immutableEntry( "foo", (Collection<Integer>) Collections.singleton(1))); multimapView.clear(); @@ -551,52 +549,6 @@ public class MultimapsTest extends AbstractMultimapTest { private static final long serialVersionUID = 0; } - public void testNewMultimapWithCollectionRejectingNegativeElements() { - CountingSupplier<Set<Integer>> factory = new SetSupplier() { - @Override - public Set<Integer> getImpl() { - final Set<Integer> backing = super.getImpl(); - return new ForwardingSet<Integer>() { - @Override - protected Set<Integer> delegate() { - return backing; - } - - @Override - public boolean add(Integer element) { - checkArgument(element >= 0); - return super.add(element); - } - - @Override - public boolean addAll(Collection<? extends Integer> collection) { - return standardAddAll(collection); - } - }; - } - }; - - Map<Color, Collection<Integer>> map = Maps.newEnumMap(Color.class); - Multimap<Color, Integer> multimap = Multimaps.newMultimap(map, factory); - try { - multimap.put(Color.BLUE, -1); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException expected) { - // expected - } - multimap.put(Color.RED, 1); - multimap.put(Color.BLUE, 2); - try { - multimap.put(Color.GREEN, -1); - fail("Expected IllegalArgumentException"); - } catch (IllegalArgumentException expected) { - // expected - } - ASSERT.that(multimap.entries()).has().allOf( - Maps.immutableEntry(Color.RED, 1), - Maps.immutableEntry(Color.BLUE, 2)); - } - public void testNewMultimap() { // The ubiquitous EnumArrayBlockingQueueMultimap CountingSupplier<Queue<Integer>> factory = new QueueSupplier(); @@ -666,15 +618,15 @@ public class MultimapsTest extends AbstractMultimapTest { SerializableTester.reserializeAndAssert(multimap); } - private static class SetSupplier extends CountingSupplier<Set<Integer>> { - @Override public Set<Integer> getImpl() { + private static class SetSupplier extends CountingSupplier<HashSet<Integer>> { + @Override public HashSet<Integer> getImpl() { return new HashSet<Integer>(4); } private static final long serialVersionUID = 0; } public void testNewSetMultimap() { - CountingSupplier<Set<Integer>> factory = new SetSupplier(); + CountingSupplier<HashSet<Integer>> factory = new SetSupplier(); Map<Color, Collection<Integer>> map = Maps.newHashMap(); SetMultimap<Color, Integer> multimap = Multimaps.newSetMultimap(map, factory); @@ -688,7 +640,7 @@ public class MultimapsTest extends AbstractMultimapTest { @GwtIncompatible("SerializableTester") public void testNewSetMultimapSerialization() { - CountingSupplier<Set<Integer>> factory = new SetSupplier(); + CountingSupplier<HashSet<Integer>> factory = new SetSupplier(); Map<Color, Collection<Integer>> map = Maps.newHashMap(); SetMultimap<Color, Integer> multimap = Multimaps.newSetMultimap(map, factory); multimap.putAll(Color.BLUE, asList(3, 1, 4)); @@ -762,6 +714,39 @@ public class MultimapsTest extends AbstractMultimapTest { assertEquals(stringToObject, outputMap); } + // NOTE: evil, never do this + private abstract static class IterableIterator<T> + extends ForwardingIterator<T> implements Iterable<T> { + @Override + public Iterator<T> iterator() { + return this; + } + } + + @SuppressWarnings("deprecation") // that is the purpose of this test + public void testIndexIterableIterator() { + final Multimap<String, Object> stringToObject = + new ImmutableMultimap.Builder<String, Object>() + .put("1", 1) + .put("1", 1L) + .put("1", "1") + .put("2", 2) + .put("2", 2L) + .build(); + + IterableIterator<Object> iterIter = new IterableIterator<Object>() { + private final Iterator<Object> iterator = stringToObject.values().iterator(); + + public Iterator<Object> delegate() { + return iterator; + } + }; + + ImmutableMultimap<String, Object> outputMap = + Multimaps.index(iterIter, Functions.toStringFunction()); + assertEquals(stringToObject, outputMap); + } + public void testIndex_ordering() { final Multimap<Integer, String> expectedIndex = new ImmutableListMultimap.Builder<Integer, String>() @@ -814,10 +799,11 @@ public class MultimapsTest extends AbstractMultimapTest { return in * in; } }; - Multimap<String, Integer> transformed = Multimaps.transformValues(multimap, square); - ASSERT.that(transformed.entries()).has().allOf(immutableEntry("a", 4), + Multimap<String, Integer> transformed = + Multimaps.transformValues(multimap, square); + ASSERT.that(transformed.entries()).hasContentsInOrder(immutableEntry("a", 4), immutableEntry("a", 16), immutableEntry("b", 9), immutableEntry("b", 9), - immutableEntry("c", 36)).inOrder(); + immutableEntry("c", 36)); } @GwtIncompatible(value = "untested") @@ -833,7 +819,7 @@ public class MultimapsTest extends AbstractMultimapTest { }); Entry<String, String> entry = multimap.entries().iterator().next(); entry.setValue("bbb"); - ASSERT.that(transformed.entries()).has().allOf(immutableEntry("a", 3)).inOrder(); + ASSERT.that(transformed.entries()).hasContentsInOrder(immutableEntry("a", 3)); } @GwtIncompatible(value = "untested") @@ -848,9 +834,9 @@ public class MultimapsTest extends AbstractMultimapTest { }; ListMultimap<String, Integer> transformed = Multimaps.transformValues(multimap, square); - ASSERT.that(transformed.entries()).has().allOf(immutableEntry("a", 4), + ASSERT.that(transformed.entries()).hasContentsInOrder(immutableEntry("a", 4), immutableEntry("a", 16), immutableEntry("b", 9), immutableEntry("b", 9), - immutableEntry("c", 36)).inOrder(); + immutableEntry("c", 36)); } @GwtIncompatible(value = "untested") @@ -866,8 +852,8 @@ public class MultimapsTest extends AbstractMultimapTest { }; Multimap<String, String> transformed = Multimaps.transformEntries(multimap, transformer); - ASSERT.that(transformed.entries()).has().allOf(immutableEntry("a", "a"), - immutableEntry("a", "a"), immutableEntry("b", "nob")).inOrder(); + ASSERT.that(transformed.entries()).hasContentsInOrder(immutableEntry("a", "a"), + immutableEntry("a", "a"), immutableEntry("b", "nob")); } @GwtIncompatible(value = "untested") @@ -889,24 +875,13 @@ public class MultimapsTest extends AbstractMultimapTest { assertEquals("{a=[a1, a4, a4], b=[b6]}", transformed.toString()); } - public <K, V> void testSynchronizedMultimapSampleCodeCompilation() { - K key = null; - - Multimap<K, V> multimap = Multimaps.synchronizedMultimap( - HashMultimap.<K, V>create()); - Collection<V> values = multimap.get(key); // Needn't be in synchronized block - synchronized (multimap) { // Synchronizing on multimap, not values! - Iterator<V> i = values.iterator(); // Must be in synchronized block - while (i.hasNext()) { - foo(i.next()); - } - } - } - - private static void foo(Object o) {} - @GwtIncompatible("NullPointerTester") - public void testNullPointers() { - new NullPointerTester().testAllPublicStaticMethods(Multimaps.class); + public void testNullPointers() throws Exception { + NullPointerTester tester = new NullPointerTester(); + tester.setDefault(Multimap.class, ImmutableMultimap.of()); + tester.setDefault(ListMultimap.class, ImmutableListMultimap.of()); + tester.setDefault(EntryTransformer.class, ALWAYS_NULL); + tester.ignore(Multimaps.class.getDeclaredMethod("index", Object.class, Function.class)); + tester.testAllPublicStaticMethods(Multimaps.class); } } |