Skip to content

Commit

Permalink
Use assertThrows even in GWT/J2CL/J2KT-compatible code, `common.col…
Browse files Browse the repository at this point in the history
…lect.testing.google` edition.

(continuing the path started in cl/675634517)

RELNOTES=n/a
PiperOrigin-RevId: 686998315
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Oct 17, 2024
1 parent 7b62a51 commit 9597b0d
Show file tree
Hide file tree
Showing 40 changed files with 436 additions and 450 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;

import com.google.common.annotations.GwtCompatible;
import com.google.common.annotations.GwtIncompatible;
Expand Down Expand Up @@ -192,24 +193,14 @@ public void testSetCount_zeroToOne_supported() {
public void testSetCountZeroToOneConcurrentWithIteration() {
Iterator<E> iterator = collection.iterator();
assertSetCount(e3(), 1);
try {
iterator.next();
fail("Expected ConcurrentModificationException");
} catch (ConcurrentModificationException expected) {
// success
}
assertThrows(ConcurrentModificationException.class, () -> iterator.next());
}

@CollectionFeature.Require({SUPPORTS_ADD, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
public void testSetCountZeroToOneConcurrentWithEntrySetIteration() {
Iterator<Entry<E>> iterator = getMultiset().entrySet().iterator();
assertSetCount(e3(), 1);
try {
iterator.next();
fail("Expected ConcurrentModificationException");
} catch (ConcurrentModificationException expected) {
// success
}
assertThrows(ConcurrentModificationException.class, () -> iterator.next());
}

@CollectionFeature.Require(SUPPORTS_ADD)
Expand Down Expand Up @@ -252,25 +243,15 @@ public void testSetCount_oneToZero_supported() {
public void testSetCountOneToZeroConcurrentWithIteration() {
Iterator<E> iterator = collection.iterator();
assertSetCount(e0(), 0);
try {
iterator.next();
fail("Expected ConcurrentModificationException");
} catch (ConcurrentModificationException expected) {
// success
}
assertThrows(ConcurrentModificationException.class, () -> iterator.next());
}

@CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
@CollectionSize.Require(absent = ZERO)
public void testSetCountOneToZeroConcurrentWithEntrySetIteration() {
Iterator<Entry<E>> iterator = getMultiset().entrySet().iterator();
assertSetCount(e0(), 0);
try {
iterator.next();
fail("Expected ConcurrentModificationException");
} catch (ConcurrentModificationException expected) {
// success
}
assertThrows(ConcurrentModificationException.class, () -> iterator.next());
}

@CollectionSize.Require(SEVERAL)
Expand Down Expand Up @@ -325,11 +306,7 @@ public void testSetCount_addNull_nullSupported() {

@CollectionFeature.Require(value = SUPPORTS_ADD, absent = ALLOWS_NULL_VALUES)
public void testSetCount_addNull_nullUnsupported() {
try {
setCountNoCheckReturnValue(null, 1);
fail("adding null with setCount() should throw NullPointerException");
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> setCountNoCheckReturnValue(null, 1));
}

@CollectionFeature.Require(ALLOWS_NULL_VALUES)
Expand Down Expand Up @@ -362,11 +339,7 @@ public void testSetCount_existingNoNopNull_nullSupported() {

@CollectionFeature.Require(SUPPORTS_REMOVE)
public void testSetCount_negative_removeSupported() {
try {
setCountNoCheckReturnValue(e3(), -1);
fail("calling setCount() with a negative count should throw IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
assertThrows(IllegalArgumentException.class, () -> setCountNoCheckReturnValue(e3(), -1));
}

@CollectionFeature.Require(absent = SUPPORTS_REMOVE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.testing.features.CollectionSize;
Expand Down Expand Up @@ -48,11 +49,7 @@ public void testSetValue_valueAbsent() {
public void testSetValue_valuePresent() {
for (Entry<K, V> entry : getMap().entrySet()) {
if (entry.getKey().equals(k0())) {
try {
entry.setValue(v1());
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException expected) {
}
assertThrows(IllegalArgumentException.class, () -> entry.setValue(v1()));
}
}
expectUnchanged();
Expand All @@ -62,11 +59,7 @@ public void testSetValue_valuePresent() {
@CollectionSize.Require(absent = ZERO)
public void testSetValueNullUnsupported() {
for (Entry<K, V> entry : getMap().entrySet()) {
try {
entry.setValue(null);
fail("Expected NullPointerException");
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> entry.setValue(null));
expectUnchanged();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.testing.Helpers;
Expand All @@ -39,12 +40,7 @@ public class BiMapPutTester<K, V> extends AbstractBiMapTester<K, V> {
@CollectionSize.Require(ZERO)
public void testPutWithSameValueFails() {
getMap().put(k0(), v0());
try {
getMap().put(k1(), v0());
fail("Expected IllegalArgumentException");
} catch (IllegalArgumentException expected) {
// success
}
assertThrows(IllegalArgumentException.class, () -> getMap().put(k1(), v0()));
// verify that the bimap is unchanged
expectAdded(e0());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.Multimap;
Expand Down Expand Up @@ -90,11 +91,7 @@ public void testRemoveNullValue() {
@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
public void testAddNullValueUnsupported() {
Collection<V> result = multimap().asMap().get(k0());
try {
result.add(null);
fail("Expected NullPointerException");
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> result.add(null));
}

@CollectionSize.Require(absent = ZERO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.Iterables;
Expand Down Expand Up @@ -81,11 +82,7 @@ public void testAsMapGetNullKeyAbsent() {

@MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
public void testAsMapGetNullKeyUnsupported() {
try {
multimap().asMap().get(null);
fail("Expected NullPointerException");
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> multimap().asMap().get(null));
}

@CollectionSize.Require(absent = ZERO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
import static com.google.common.collect.testing.google.GoogleHelpers.assertEmpty;
import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.Multimap;
Expand All @@ -42,11 +43,7 @@ public class MultimapClearTester<K, V> extends AbstractMultimapTester<K, V, Mult
@CollectionSize.Require(absent = ZERO)
@MapFeature.Require(absent = SUPPORTS_REMOVE)
public void testClearUnsupported() {
try {
multimap().clear();
fail("Expected UnsupportedOperationException");
} catch (UnsupportedOperationException expected) {
}
assertThrows(UnsupportedOperationException.class, () -> multimap().clear());
}

private void assertCleared() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.Multimap;
Expand Down Expand Up @@ -69,21 +70,11 @@ public void testContainsEntryNullNo() {

@MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
public void testContainsEntryNullDisallowedBecauseKeyQueriesDisallowed() {
try {
multimap().containsEntry(null, v3());
fail("Expected NullPointerException");
} catch (NullPointerException expected) {
// success
}
assertThrows(NullPointerException.class, () -> multimap().containsEntry(null, v3()));
}

@MapFeature.Require(absent = ALLOWS_NULL_VALUE_QUERIES)
public void testContainsEntryNullDisallowedBecauseValueQueriesDisallowed() {
try {
multimap().containsEntry(k3(), null);
fail("Expected NullPointerException");
} catch (NullPointerException expected) {
// success
}
assertThrows(NullPointerException.class, () -> multimap().containsEntry(k3(), null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.Multimap;
Expand Down Expand Up @@ -82,11 +83,6 @@ public void testContainsKeyNullAbsent() {

@MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
public void testContainsKeyNullDisallowed() {
try {
multimap().containsKey(null);
fail("Expected NullPointerException");
} catch (NullPointerException expected) {
// success
}
assertThrows(NullPointerException.class, () -> multimap().containsKey(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.Multimap;
Expand Down Expand Up @@ -59,11 +60,6 @@ public void testContainsNullValueNo() {

@MapFeature.Require(absent = ALLOWS_NULL_VALUE_QUERIES)
public void testContainsNullValueFails() {
try {
multimap().containsValue(null);
fail("Expected NullPointerException");
} catch (NullPointerException expected) {
// success
}
assertThrows(NullPointerException.class, () -> multimap().containsValue(null));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.Multimap;
Expand Down Expand Up @@ -142,12 +143,7 @@ public void testGetNullAbsent() {

@MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
public void testGetNullForbidden() {
try {
multimap().get(null);
fail("Expected NullPointerException");
} catch (NullPointerException expected) {
// success
}
assertThrows(NullPointerException.class, () -> multimap().get(null));
}

@MapFeature.Require(ALLOWS_NULL_VALUES)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;

import com.google.common.annotations.GwtCompatible;
import com.google.common.collect.Multimap;
Expand All @@ -40,11 +41,9 @@ public class MultimapPutAllMultimapTester<K, V>
extends AbstractMultimapTester<K, V, Multimap<K, V>> {
@MapFeature.Require(absent = SUPPORTS_PUT)
public void testPutUnsupported() {
try {
multimap().putAll(getSubjectGenerator().create(Helpers.mapEntry(k3(), v3())));
fail("Expected UnsupportedOperationException");
} catch (UnsupportedOperationException expected) {
}
assertThrows(
UnsupportedOperationException.class,
() -> multimap().putAll(getSubjectGenerator().create(Helpers.mapEntry(k3(), v3()))));
}

@MapFeature.Require(SUPPORTS_PUT)
Expand Down Expand Up @@ -80,22 +79,14 @@ public void testPutAllWithNullKey() {
@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
public void testPutAllRejectsNullValue() {
Multimap<K, V> source = getSubjectGenerator().create(Helpers.mapEntry(k0(), null));
try {
multimap().putAll(source);
fail("Expected NullPointerException");
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> multimap().putAll(source));
expectUnchanged();
}

@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_KEYS)
public void testPutAllRejectsNullKey() {
Multimap<K, V> source = getSubjectGenerator().create(Helpers.mapEntry(null, v0()));
try {
multimap().putAll(source);
fail("Expected NullPointerException");
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> multimap().putAll(source));
expectUnchanged();
}

Expand Down
Loading

0 comments on commit 9597b0d

Please sign in to comment.