Skip to content

Commit f32e1de

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Use assertThrows even in GWT/J2CL/J2KT-compatible code, common.collect.testing.google edition.
(continuing the path started in cl/675634517) RELNOTES=n/a PiperOrigin-RevId: 686916124
1 parent 7b62a51 commit f32e1de

40 files changed

+436
-450
lines changed

android/guava-testlib/src/com/google/common/collect/testing/google/AbstractMultisetSetCountTester.java

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static com.google.common.collect.testing.features.CollectionFeature.SUPPORTS_REMOVE;
2424
import static com.google.common.collect.testing.features.CollectionSize.SEVERAL;
2525
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
26+
import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;
2627

2728
import com.google.common.annotations.GwtCompatible;
2829
import com.google.common.annotations.GwtIncompatible;
@@ -192,24 +193,14 @@ public void testSetCount_zeroToOne_supported() {
192193
public void testSetCountZeroToOneConcurrentWithIteration() {
193194
Iterator<E> iterator = collection.iterator();
194195
assertSetCount(e3(), 1);
195-
try {
196-
iterator.next();
197-
fail("Expected ConcurrentModificationException");
198-
} catch (ConcurrentModificationException expected) {
199-
// success
200-
}
196+
assertThrows(ConcurrentModificationException.class, () -> iterator.next());
201197
}
202198

203199
@CollectionFeature.Require({SUPPORTS_ADD, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
204200
public void testSetCountZeroToOneConcurrentWithEntrySetIteration() {
205201
Iterator<Entry<E>> iterator = getMultiset().entrySet().iterator();
206202
assertSetCount(e3(), 1);
207-
try {
208-
iterator.next();
209-
fail("Expected ConcurrentModificationException");
210-
} catch (ConcurrentModificationException expected) {
211-
// success
212-
}
203+
assertThrows(ConcurrentModificationException.class, () -> iterator.next());
213204
}
214205

215206
@CollectionFeature.Require(SUPPORTS_ADD)
@@ -252,25 +243,15 @@ public void testSetCount_oneToZero_supported() {
252243
public void testSetCountOneToZeroConcurrentWithIteration() {
253244
Iterator<E> iterator = collection.iterator();
254245
assertSetCount(e0(), 0);
255-
try {
256-
iterator.next();
257-
fail("Expected ConcurrentModificationException");
258-
} catch (ConcurrentModificationException expected) {
259-
// success
260-
}
246+
assertThrows(ConcurrentModificationException.class, () -> iterator.next());
261247
}
262248

263249
@CollectionFeature.Require({SUPPORTS_REMOVE, FAILS_FAST_ON_CONCURRENT_MODIFICATION})
264250
@CollectionSize.Require(absent = ZERO)
265251
public void testSetCountOneToZeroConcurrentWithEntrySetIteration() {
266252
Iterator<Entry<E>> iterator = getMultiset().entrySet().iterator();
267253
assertSetCount(e0(), 0);
268-
try {
269-
iterator.next();
270-
fail("Expected ConcurrentModificationException");
271-
} catch (ConcurrentModificationException expected) {
272-
// success
273-
}
254+
assertThrows(ConcurrentModificationException.class, () -> iterator.next());
274255
}
275256

276257
@CollectionSize.Require(SEVERAL)
@@ -325,11 +306,7 @@ public void testSetCount_addNull_nullSupported() {
325306

326307
@CollectionFeature.Require(value = SUPPORTS_ADD, absent = ALLOWS_NULL_VALUES)
327308
public void testSetCount_addNull_nullUnsupported() {
328-
try {
329-
setCountNoCheckReturnValue(null, 1);
330-
fail("adding null with setCount() should throw NullPointerException");
331-
} catch (NullPointerException expected) {
332-
}
309+
assertThrows(NullPointerException.class, () -> setCountNoCheckReturnValue(null, 1));
333310
}
334311

335312
@CollectionFeature.Require(ALLOWS_NULL_VALUES)
@@ -362,11 +339,7 @@ public void testSetCount_existingNoNopNull_nullSupported() {
362339

363340
@CollectionFeature.Require(SUPPORTS_REMOVE)
364341
public void testSetCount_negative_removeSupported() {
365-
try {
366-
setCountNoCheckReturnValue(e3(), -1);
367-
fail("calling setCount() with a negative count should throw IllegalArgumentException");
368-
} catch (IllegalArgumentException expected) {
369-
}
342+
assertThrows(IllegalArgumentException.class, () -> setCountNoCheckReturnValue(e3(), -1));
370343
}
371344

372345
@CollectionFeature.Require(absent = SUPPORTS_REMOVE)

android/guava-testlib/src/com/google/common/collect/testing/google/BiMapEntrySetTester.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
2121
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
2222
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
23+
import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;
2324

2425
import com.google.common.annotations.GwtCompatible;
2526
import com.google.common.collect.testing.features.CollectionSize;
@@ -48,11 +49,7 @@ public void testSetValue_valueAbsent() {
4849
public void testSetValue_valuePresent() {
4950
for (Entry<K, V> entry : getMap().entrySet()) {
5051
if (entry.getKey().equals(k0())) {
51-
try {
52-
entry.setValue(v1());
53-
fail("Expected IllegalArgumentException");
54-
} catch (IllegalArgumentException expected) {
55-
}
52+
assertThrows(IllegalArgumentException.class, () -> entry.setValue(v1()));
5653
}
5754
}
5855
expectUnchanged();
@@ -62,11 +59,7 @@ public void testSetValue_valuePresent() {
6259
@CollectionSize.Require(absent = ZERO)
6360
public void testSetValueNullUnsupported() {
6461
for (Entry<K, V> entry : getMap().entrySet()) {
65-
try {
66-
entry.setValue(null);
67-
fail("Expected NullPointerException");
68-
} catch (NullPointerException expected) {
69-
}
62+
assertThrows(NullPointerException.class, () -> entry.setValue(null));
7063
expectUnchanged();
7164
}
7265
}

android/guava-testlib/src/com/google/common/collect/testing/google/BiMapPutTester.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
2323
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
2424
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
25+
import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;
2526

2627
import com.google.common.annotations.GwtCompatible;
2728
import com.google.common.collect.testing.Helpers;
@@ -39,12 +40,7 @@ public class BiMapPutTester<K, V> extends AbstractBiMapTester<K, V> {
3940
@CollectionSize.Require(ZERO)
4041
public void testPutWithSameValueFails() {
4142
getMap().put(k0(), v0());
42-
try {
43-
getMap().put(k1(), v0());
44-
fail("Expected IllegalArgumentException");
45-
} catch (IllegalArgumentException expected) {
46-
// success
47-
}
43+
assertThrows(IllegalArgumentException.class, () -> getMap().put(k1(), v0()));
4844
// verify that the bimap is unchanged
4945
expectAdded(e0());
5046
}

android/guava-testlib/src/com/google/common/collect/testing/google/MultimapAsMapGetTester.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
2525
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
2626
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
27+
import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;
2728

2829
import com.google.common.annotations.GwtCompatible;
2930
import com.google.common.collect.Multimap;
@@ -90,11 +91,7 @@ public void testRemoveNullValue() {
9091
@MapFeature.Require(value = SUPPORTS_PUT, absent = ALLOWS_NULL_VALUES)
9192
public void testAddNullValueUnsupported() {
9293
Collection<V> result = multimap().asMap().get(k0());
93-
try {
94-
result.add(null);
95-
fail("Expected NullPointerException");
96-
} catch (NullPointerException expected) {
97-
}
94+
assertThrows(NullPointerException.class, () -> result.add(null));
9895
}
9996

10097
@CollectionSize.Require(absent = ZERO)

android/guava-testlib/src/com/google/common/collect/testing/google/MultimapAsMapTester.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
2525
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
2626
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
27+
import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;
2728

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

8283
@MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
8384
public void testAsMapGetNullKeyUnsupported() {
84-
try {
85-
multimap().asMap().get(null);
86-
fail("Expected NullPointerException");
87-
} catch (NullPointerException expected) {
88-
}
85+
assertThrows(NullPointerException.class, () -> multimap().asMap().get(null));
8986
}
9087

9188
@CollectionSize.Require(absent = ZERO)

android/guava-testlib/src/com/google/common/collect/testing/google/MultimapClearTester.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
2121
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
2222
import static com.google.common.collect.testing.google.GoogleHelpers.assertEmpty;
23+
import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;
2324

2425
import com.google.common.annotations.GwtCompatible;
2526
import com.google.common.collect.Multimap;
@@ -42,11 +43,7 @@ public class MultimapClearTester<K, V> extends AbstractMultimapTester<K, V, Mult
4243
@CollectionSize.Require(absent = ZERO)
4344
@MapFeature.Require(absent = SUPPORTS_REMOVE)
4445
public void testClearUnsupported() {
45-
try {
46-
multimap().clear();
47-
fail("Expected UnsupportedOperationException");
48-
} catch (UnsupportedOperationException expected) {
49-
}
46+
assertThrows(UnsupportedOperationException.class, () -> multimap().clear());
5047
}
5148

5249
private void assertCleared() {

android/guava-testlib/src/com/google/common/collect/testing/google/MultimapContainsEntryTester.java

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
2222
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
2323
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
24+
import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;
2425

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

7071
@MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
7172
public void testContainsEntryNullDisallowedBecauseKeyQueriesDisallowed() {
72-
try {
73-
multimap().containsEntry(null, v3());
74-
fail("Expected NullPointerException");
75-
} catch (NullPointerException expected) {
76-
// success
77-
}
73+
assertThrows(NullPointerException.class, () -> multimap().containsEntry(null, v3()));
7874
}
7975

8076
@MapFeature.Require(absent = ALLOWS_NULL_VALUE_QUERIES)
8177
public void testContainsEntryNullDisallowedBecauseValueQueriesDisallowed() {
82-
try {
83-
multimap().containsEntry(k3(), null);
84-
fail("Expected NullPointerException");
85-
} catch (NullPointerException expected) {
86-
// success
87-
}
78+
assertThrows(NullPointerException.class, () -> multimap().containsEntry(k3(), null));
8879
}
8980
}

android/guava-testlib/src/com/google/common/collect/testing/google/MultimapContainsKeyTester.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
2020
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEYS;
2121
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_KEY_QUERIES;
22+
import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;
2223

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

8384
@MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
8485
public void testContainsKeyNullDisallowed() {
85-
try {
86-
multimap().containsKey(null);
87-
fail("Expected NullPointerException");
88-
} catch (NullPointerException expected) {
89-
// success
90-
}
86+
assertThrows(NullPointerException.class, () -> multimap().containsKey(null));
9187
}
9288
}

android/guava-testlib/src/com/google/common/collect/testing/google/MultimapContainsValueTester.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.collect.testing.features.CollectionSize.ZERO;
2020
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
2121
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUE_QUERIES;
22+
import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;
2223

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

6061
@MapFeature.Require(absent = ALLOWS_NULL_VALUE_QUERIES)
6162
public void testContainsNullValueFails() {
62-
try {
63-
multimap().containsValue(null);
64-
fail("Expected NullPointerException");
65-
} catch (NullPointerException expected) {
66-
// success
67-
}
63+
assertThrows(NullPointerException.class, () -> multimap().containsValue(null));
6864
}
6965
}

android/guava-testlib/src/com/google/common/collect/testing/google/MultimapGetTester.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static com.google.common.collect.testing.features.MapFeature.ALLOWS_NULL_VALUES;
2626
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_PUT;
2727
import static com.google.common.collect.testing.features.MapFeature.SUPPORTS_REMOVE;
28+
import static com.google.common.collect.testing.google.ReflectionFreeAssertThrows.assertThrows;
2829

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

143144
@MapFeature.Require(absent = ALLOWS_NULL_KEY_QUERIES)
144145
public void testGetNullForbidden() {
145-
try {
146-
multimap().get(null);
147-
fail("Expected NullPointerException");
148-
} catch (NullPointerException expected) {
149-
// success
150-
}
146+
assertThrows(NullPointerException.class, () -> multimap().get(null));
151147
}
152148

153149
@MapFeature.Require(ALLOWS_NULL_VALUES)

0 commit comments

Comments
 (0)