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 testCopyOf_multiset_empty() { Multiset<String> c = HashMultiset.create(); Multiset<String> multiset = ImmutableSortedMultiset.copyOf(c); assertTrue(multiset.isEmpty()); }
public void testCreation_emptyArray() { String[] array = new String[0]; Multiset<String> multiset = ImmutableSortedMultiset.copyOf(array); assertTrue(multiset.isEmpty()); }
public void testCreation_noArgs() { Multiset<String> multiset = ImmutableSortedMultiset.of(); assertTrue(multiset.isEmpty()); }
@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); }
public void testCopyOf_iterator_empty() { Iterator<String> iterator = Iterators.emptyIterator(); Multiset<String> multiset = ImmutableSortedMultiset.copyOf(iterator); assertTrue(multiset.isEmpty()); }
public void testCopyOf_collection_empty() { // "<String>" is required to work around a javac 1.5 bug. Collection<String> c = MinimalCollection.<String>of(); Multiset<String> multiset = ImmutableSortedMultiset.copyOf(c); assertTrue(multiset.isEmpty()); }
public void testCopyOfDuplicateInconsistentWithEquals() { IntegerDiv10 three = new IntegerDiv10(3); IntegerDiv10 eleven = new IntegerDiv10(11); IntegerDiv10 twelve = new IntegerDiv10(12); IntegerDiv10 twenty = new IntegerDiv10(20); List<IntegerDiv10> original = ImmutableList.of(three, eleven, twelve, twenty); Multiset<IntegerDiv10> copy = ImmutableSortedMultiset.copyOf(original); assertTrue(copy.contains(eleven)); assertTrue(copy.contains(twelve)); } }
public void testCopyOf_multiset_empty() { Multiset<String> c = HashMultiset.create(); Multiset<String> multiset = ImmutableSortedMultiset.copyOf(c); assertTrue(multiset.isEmpty()); }
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_emptyArray() { String[] array = new String[0]; Multiset<String> multiset = ImmutableSortedMultiset.copyOf(array); assertTrue(multiset.isEmpty()); }
public void testCreation_noArgs() { Multiset<String> multiset = ImmutableSortedMultiset.of(); assertTrue(multiset.isEmpty()); }
@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); }
public void testCopyOf_collection_empty() { // "<String>" is required to work around a javac 1.5 bug. Collection<String> c = MinimalCollection.<String>of(); Multiset<String> multiset = ImmutableSortedMultiset.copyOf(c); assertTrue(multiset.isEmpty()); }
public void testCopyOf_iterator_empty() { Iterator<String> iterator = Iterators.emptyIterator(); Multiset<String> multiset = ImmutableSortedMultiset.copyOf(iterator); assertTrue(multiset.isEmpty()); }
public void testCopyOfDuplicateInconsistentWithEquals() { IntegerDiv10 three = new IntegerDiv10(3); IntegerDiv10 eleven = new IntegerDiv10(11); IntegerDiv10 twelve = new IntegerDiv10(12); IntegerDiv10 twenty = new IntegerDiv10(20); List<IntegerDiv10> original = ImmutableList.of(three, eleven, twelve, twenty); Multiset<IntegerDiv10> copy = ImmutableSortedMultiset.copyOf(original); assertTrue(copy.contains(eleven)); assertTrue(copy.contains(twelve)); } }