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` edition.

(continuing the path started in cl/675634517)

RELNOTES=n/a
PiperOrigin-RevId: 687014605
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Oct 17, 2024
1 parent 8a1c90e commit 99be0a4
Show file tree
Hide file tree
Showing 91 changed files with 1,444 additions and 1,616 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.google.common.collect.testing;

import static com.google.common.collect.testing.ReflectionFreeAssertThrows.assertThrows;
import static java.util.Collections.singleton;

import com.google.common.annotations.GwtCompatible;
Expand Down Expand Up @@ -280,11 +281,7 @@ public void testClear() {
map.clear();
assertTrue(map.isEmpty());
} else {
try {
map.clear();
fail("Expected UnsupportedOperationException.");
} catch (UnsupportedOperationException expected) {
}
assertThrows(UnsupportedOperationException.class, () -> map.clear());
}
assertInvariants(map);
}
Expand Down Expand Up @@ -479,18 +476,10 @@ public void testEntrySetIteratorRemove() {
// iterator.remove().
assertFalse(entrySet.contains(entryCopy));
assertInvariants(map);
try {
iterator.remove();
fail("Expected IllegalStateException.");
} catch (IllegalStateException expected) {
}
assertThrows(IllegalStateException.class, () -> iterator.remove());
} else {
iterator.next();
try {
iterator.remove();
fail("Expected UnsupportedOperationException.");
} catch (UnsupportedOperationException expected) {
}
assertThrows(UnsupportedOperationException.class, () -> iterator.remove());
}
assertInvariants(map);
}
Expand All @@ -510,11 +499,8 @@ public void testEntrySetRemove() {
assertTrue(didRemove);
assertEquals(initialSize - 1, map.size());
} else {
try {
entrySet.remove(entrySet.iterator().next());
fail("Expected UnsupportedOperationException.");
} catch (UnsupportedOperationException expected) {
}
assertThrows(
UnsupportedOperationException.class, () -> entrySet.remove(entrySet.iterator().next()));
}
assertInvariants(map);
}
Expand Down Expand Up @@ -661,11 +647,7 @@ public void testEntrySetRemoveAll() {
// have undefined behavior after entrySet.removeAll(entriesToRemove),
assertFalse(entrySet.contains(entryToRemoveCopy));
} else {
try {
entrySet.removeAll(entriesToRemove);
fail("Expected UnsupportedOperationException.");
} catch (UnsupportedOperationException expected) {
}
assertThrows(UnsupportedOperationException.class, () -> entrySet.removeAll(entriesToRemove));
}
assertInvariants(map);
}
Expand All @@ -680,11 +662,7 @@ public void testEntrySetRemoveAllNullFromEmpty() {

Set<Entry<K, V>> entrySet = map.entrySet();
if (supportsRemove) {
try {
entrySet.removeAll(null);
fail("Expected NullPointerException.");
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> entrySet.removeAll(null));
} else {
try {
entrySet.removeAll(null);
Expand Down Expand Up @@ -715,11 +693,7 @@ public void testEntrySetRetainAll() {
assertTrue(entrySet.contains(entry));
}
} else {
try {
entrySet.retainAll(entriesToRetain);
fail("Expected UnsupportedOperationException.");
} catch (UnsupportedOperationException expected) {
}
assertThrows(UnsupportedOperationException.class, () -> entrySet.retainAll(entriesToRetain));
}
assertInvariants(map);
}
Expand Down Expand Up @@ -763,11 +737,7 @@ public void testEntrySetClear() {
entrySet.clear();
assertTrue(entrySet.isEmpty());
} else {
try {
entrySet.clear();
fail("Expected UnsupportedOperationException.");
} catch (UnsupportedOperationException expected) {
}
assertThrows(UnsupportedOperationException.class, () -> entrySet.clear());
}
assertInvariants(map);
}
Expand Down Expand Up @@ -1002,11 +972,7 @@ public void testPutNewKey() {
assertEquals(initialSize + 1, map.size());
assertNull(oldValue);
} else {
try {
map.put(keyToPut, valueToPut);
fail("Expected UnsupportedOperationException.");
} catch (UnsupportedOperationException expected) {
}
assertThrows(UnsupportedOperationException.class, () -> map.put(keyToPut, valueToPut));
}
assertInvariants(map);
}
Expand All @@ -1030,11 +996,7 @@ public void testPutExistingKey() {
assertTrue(map.containsValue(valueToPut));
assertEquals(initialSize, map.size());
} else {
try {
map.put(keyToPut, valueToPut);
fail("Expected UnsupportedOperationException.");
} catch (UnsupportedOperationException expected) {
}
assertThrows(UnsupportedOperationException.class, () -> map.put(keyToPut, valueToPut));
}
assertInvariants(map);
}
Expand All @@ -1058,11 +1020,7 @@ public void testPutNullKey() {
assertTrue(map.containsKey(null));
assertTrue(map.containsValue(valueToPut));
} else {
try {
map.put(null, valueToPut);
fail("Expected RuntimeException");
} catch (RuntimeException expected) {
}
assertThrows(RuntimeException.class, () -> map.put(null, valueToPut));
}
assertInvariants(map);
}
Expand All @@ -1088,11 +1046,7 @@ public void testPutNullValue() {
assertTrue(map.containsValue(null));
assertEquals(initialSize + 1, map.size());
} else {
try {
map.put(keyToPut, null);
fail("Expected RuntimeException");
} catch (RuntimeException expected) {
}
assertThrows(RuntimeException.class, () -> map.put(keyToPut, null));
}
assertInvariants(map);
}
Expand All @@ -1119,11 +1073,7 @@ public void testPutNullValueForExistingKey() {
assertTrue(map.containsValue(null));
assertEquals(initialSize, map.size());
} else {
try {
map.put(keyToPut, null);
fail("Expected RuntimeException");
} catch (RuntimeException expected) {
}
assertThrows(RuntimeException.class, () -> map.put(keyToPut, null));
}
assertInvariants(map);
}
Expand All @@ -1147,11 +1097,7 @@ public void testPutAllNewKey() {
assertTrue(map.containsValue(valueToPut));
assertEquals(initialSize + 1, map.size());
} else {
try {
map.putAll(mapToPut);
fail("Expected UnsupportedOperationException.");
} catch (UnsupportedOperationException expected) {
}
assertThrows(UnsupportedOperationException.class, () -> map.putAll(mapToPut));
}
assertInvariants(map);
}
Expand All @@ -1175,11 +1121,7 @@ public void testPutAllExistingKey() {
assertTrue(map.containsKey(keyToPut));
assertTrue(map.containsValue(valueToPut));
} else {
try {
map.putAll(mapToPut);
fail("Expected UnsupportedOperationException.");
} catch (UnsupportedOperationException expected) {
}
assertThrows(UnsupportedOperationException.class, () -> map.putAll(mapToPut));
}
assertEquals(initialSize, map.size());
assertInvariants(map);
Expand All @@ -1202,11 +1144,7 @@ public void testRemove() {
assertFalse(map.containsKey(keyToRemove));
assertEquals(initialSize - 1, map.size());
} else {
try {
map.remove(keyToRemove);
fail("Expected UnsupportedOperationException.");
} catch (UnsupportedOperationException expected) {
}
assertThrows(UnsupportedOperationException.class, () -> map.remove(keyToRemove));
}
assertInvariants(map);
}
Expand All @@ -1225,11 +1163,7 @@ public void testRemoveMissingKey() {
assertNull(map.remove(keyToRemove));
assertEquals(initialSize, map.size());
} else {
try {
map.remove(keyToRemove);
fail("Expected UnsupportedOperationException.");
} catch (UnsupportedOperationException expected) {
}
assertThrows(UnsupportedOperationException.class, () -> map.remove(keyToRemove));
}
assertInvariants(map);
}
Expand All @@ -1254,11 +1188,7 @@ public void testKeySetRemove() {
assertEquals(initialSize - 1, map.size());
assertFalse(map.containsKey(key));
} else {
try {
keys.remove(key);
fail("Expected UnsupportedOperationException.");
} catch (UnsupportedOperationException expected) {
}
assertThrows(UnsupportedOperationException.class, () -> keys.remove(key));
}
assertInvariants(map);
}
Expand All @@ -1279,11 +1209,8 @@ public void testKeySetRemoveAll() {
assertEquals(initialSize - 1, map.size());
assertFalse(map.containsKey(key));
} else {
try {
keys.removeAll(Collections.singleton(key));
fail("Expected UnsupportedOperationException.");
} catch (UnsupportedOperationException expected) {
}
assertThrows(
UnsupportedOperationException.class, () -> keys.removeAll(Collections.singleton(key)));
}
assertInvariants(map);
}
Expand All @@ -1303,11 +1230,8 @@ public void testKeySetRetainAll() {
assertEquals(1, map.size());
assertTrue(map.containsKey(key));
} else {
try {
keys.retainAll(Collections.singleton(key));
fail("Expected UnsupportedOperationException.");
} catch (UnsupportedOperationException expected) {
}
assertThrows(
UnsupportedOperationException.class, () -> keys.retainAll(Collections.singleton(key)));
}
assertInvariants(map);
}
Expand All @@ -1325,11 +1249,7 @@ public void testKeySetClear() {
keySet.clear();
assertTrue(keySet.isEmpty());
} else {
try {
keySet.clear();
fail("Expected UnsupportedOperationException.");
} catch (UnsupportedOperationException expected) {
}
assertThrows(UnsupportedOperationException.class, () -> keySet.clear());
}
assertInvariants(map);
}
Expand All @@ -1344,11 +1264,7 @@ public void testKeySetRemoveAllNullFromEmpty() {

Set<K> keySet = map.keySet();
if (supportsRemove) {
try {
keySet.removeAll(null);
fail("Expected NullPointerException.");
} catch (NullPointerException expected) {
}
assertThrows(NullPointerException.class, () -> keySet.removeAll(null));
} else {
try {
keySet.removeAll(null);
Expand Down Expand Up @@ -1427,18 +1343,10 @@ public void testValuesIteratorRemove() {
// removed value, because the underlying map can have multiple mappings
// to the same value.)
assertInvariants(map);
try {
iterator.remove();
fail("Expected IllegalStateException.");
} catch (IllegalStateException expected) {
}
assertThrows(IllegalStateException.class, () -> iterator.remove());
} else {
iterator.next();
try {
iterator.remove();
fail("Expected UnsupportedOperationException.");
} catch (UnsupportedOperationException expected) {
}
assertThrows(UnsupportedOperationException.class, () -> iterator.remove());
}
assertInvariants(map);
}
Expand All @@ -1460,11 +1368,9 @@ public void testValuesRemove() {
// removed value, because the underlying map can have multiple mappings
// to the same value.)
} else {
try {
valueCollection.remove(valueCollection.iterator().next());
fail("Expected UnsupportedOperationException.");
} catch (UnsupportedOperationException expected) {
}
assertThrows(
UnsupportedOperationException.class,
() -> valueCollection.remove(valueCollection.iterator().next()));
}
assertInvariants(map);
}
Expand Down Expand Up @@ -1513,11 +1419,8 @@ public void testValuesRemoveAll() {
assertFalse(valuesToRemove.contains(value));
}
} else {
try {
valueCollection.removeAll(valuesToRemove);
fail("Expected UnsupportedOperationException.");
} catch (UnsupportedOperationException expected) {
}
assertThrows(
UnsupportedOperationException.class, () -> valueCollection.removeAll(valuesToRemove));
}
assertInvariants(map);
}
Expand Down Expand Up @@ -1567,11 +1470,8 @@ public void testValuesRetainAll() {
assertTrue(valuesToRetain.contains(value));
}
} else {
try {
valueCollection.retainAll(valuesToRetain);
fail("Expected UnsupportedOperationException.");
} catch (UnsupportedOperationException expected) {
}
assertThrows(
UnsupportedOperationException.class, () -> valueCollection.retainAll(valuesToRetain));
}
assertInvariants(map);
}
Expand Down Expand Up @@ -1615,11 +1515,7 @@ public void testValuesClear() {
valueCollection.clear();
assertTrue(valueCollection.isEmpty());
} else {
try {
valueCollection.clear();
fail("Expected UnsupportedOperationException.");
} catch (UnsupportedOperationException expected) {
}
assertThrows(UnsupportedOperationException.class, () -> valueCollection.clear());
}
assertInvariants(map);
}
Expand Down
Loading

0 comments on commit 99be0a4

Please sign in to comment.