public void testCopyOf_plainIterable() { CountingIterable iterable = new CountingIterable(); Multiset<String> multiset = ImmutableSortedMultiset.copyOf(iterable); assertEquals(HashMultiset.create(asList("a", "b", "a")), multiset); assertEquals(1, iterable.count); }
public void testCopyOf_shortcut_empty() { Collection<String> c = ImmutableSortedMultiset.of(); assertSame(c, ImmutableSortedMultiset.copyOf(c)); }
public void testCopyOfDefensiveCopy() { // Depending on JDK version, either toArray() or toArray(T[]) may be called... use this class // rather than mocking to ensure that one of those methods is called. class TestArrayList<E> extends ArrayList<E> { boolean toArrayCalled = false; @Override public Object[] toArray() { toArrayCalled = true; return super.toArray(); } @Override public <T> T[] toArray(T[] a) { toArrayCalled = true; return super.toArray(a); } } // Test that toArray() is used to make a defensive copy in copyOf(), so concurrently modified // synchronized collections can be safely copied. TestArrayList<String> toCopy = new TestArrayList<>(); ImmutableSortedMultiset<String> unused = ImmutableSortedMultiset.copyOf(Ordering.natural(), toCopy); assertTrue(toCopy.toArrayCalled); }
public void testCreation_arrayContainingOnlyNull() { String[] array = new String[] {null}; try { ImmutableSortedMultiset.copyOf(array); fail(); } catch (NullPointerException expected) { } }
public void testCopyOf_multisetContainingNull() { Multiset<String> c = HashMultiset.create(asList("a", null, "b")); try { ImmutableSortedMultiset.copyOf(c); fail(); } catch (NullPointerException expected) { } }
public void testCopyOf_multiset_oneElement() { Multiset<String> c = HashMultiset.create(asList("a")); Multiset<String> multiset = ImmutableSortedMultiset.copyOf(c); assertEquals(HashMultiset.create(asList("a")), multiset); }
public void testCopyOf_iteratorContainingNull() { Iterator<String> iterator = asList("a", null, "b").iterator(); try { ImmutableSortedMultiset.copyOf(iterator); fail(); } catch (NullPointerException expected) { } }
public void testCopyOf_multiset_empty() { Multiset<String> c = HashMultiset.create(); Multiset<String> multiset = ImmutableSortedMultiset.copyOf(c); assertTrue(multiset.isEmpty()); }
public void testSerialization_empty() { Collection<String> c = ImmutableSortedMultiset.of(); assertSame(c, SerializableTester.reserialize(c)); }
public void testCopyOf_multiset_general() { Multiset<String> c = HashMultiset.create(asList("a", "b", "a")); Multiset<String> multiset = ImmutableSortedMultiset.copyOf(c); assertEquals(HashMultiset.create(asList("a", "b", "a")), multiset); }
public void testBuilderAddHandlesNullsCorrectly() { ImmutableSortedMultiset.Builder<String> builder = ImmutableSortedMultiset.naturalOrder(); try { builder.add((String) null); fail("expected NullPointerException"); } catch (NullPointerException expected) { } }
public void testCreation_emptyArray() { String[] array = new String[0]; Multiset<String> multiset = ImmutableSortedMultiset.copyOf(array); assertTrue(multiset.isEmpty()); }
public void testCopyOf_shortcut_singleton() { Collection<String> c = ImmutableSortedMultiset.of("a"); assertSame(c, ImmutableSortedMultiset.copyOf(c)); }
public void testCreation_arrayOfOneElement() { String[] array = new String[] {"a"}; Multiset<String> multiset = ImmutableSortedMultiset.copyOf(array); assertEquals(HashMultiset.create(asList("a")), multiset); }
public void testBuilderAddCopiesHandlesNullsCorrectly() { ImmutableSortedMultiset.Builder<String> builder = ImmutableSortedMultiset.naturalOrder(); try { builder.addCopies(null, 2); fail("expected NullPointerException"); } catch (NullPointerException expected) { } }
public void testCreation_noArgs() { Multiset<String> multiset = ImmutableSortedMultiset.of(); assertTrue(multiset.isEmpty()); }
public void testCopyOf_shortcut_immutableMultiset() { Collection<String> c = ImmutableSortedMultiset.of("a", "b", "c"); assertSame(c, ImmutableSortedMultiset.copyOf(c)); }
public void testCopyOf_iterator_general() { Iterator<String> iterator = asList("a", "b", "a").iterator(); Multiset<String> multiset = ImmutableSortedMultiset.copyOf(iterator); assertEquals(HashMultiset.create(asList("a", "b", "a")), multiset); }
public void testBuilderAddCopiesIllegal() { ImmutableSortedMultiset.Builder<String> builder = ImmutableSortedMultiset.naturalOrder(); try { builder.addCopies("a", -2); fail("expected IllegalArgumentException"); } catch (IllegalArgumentException expected) { } }
@SuppressWarnings("unchecked") public void testCopyOfSortedDefensiveCopy() { // Depending on JDK version, either toArray() or toArray(T[]) may be called... use this class // rather than mocking to ensure that one of those methods is called. class TestHashSet<E> extends HashSet<E> { boolean toArrayCalled = false; @Override public Object[] toArray() { toArrayCalled = true; return super.toArray(); } @Override public <T> T[] toArray(T[] a) { toArrayCalled = true; return super.toArray(a); } } // Test that toArray() is used to make a defensive copy in copyOf(), so concurrently modified // synchronized collections can be safely copied. SortedMultiset<String> toCopy = mock(SortedMultiset.class); TestHashSet<Entry<String>> entrySet = new TestHashSet<>(); when((Comparator<Comparable<String>>) toCopy.comparator()) .thenReturn(Ordering.<Comparable<String>>natural()); when(toCopy.entrySet()).thenReturn(entrySet); ImmutableSortedMultiset<String> unused = ImmutableSortedMultiset.copyOfSorted(toCopy); assertTrue(entrySet.toArrayCalled); }