Skip to content

Commit 0a29129

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
Use assertThrows even in GWT/J2CL/J2KT-compatible code, common.collect edition.
(continuing the path started in cl/675634517) And clean up a few other warnings. RELNOTES=n/a PiperOrigin-RevId: 686155720
1 parent 77243d6 commit 0a29129

File tree

109 files changed

+2378
-5033
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+2378
-5033
lines changed

android/guava-tests/test/com/google/common/collect/AbstractFilteredMapTest.java

Lines changed: 15 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.common.collect;
1818

19+
import static com.google.common.collect.ReflectionFreeAssertThrows.assertThrows;
20+
1921
import com.google.common.annotations.GwtCompatible;
2022
import com.google.common.base.Predicate;
2123
import com.google.common.base.Predicates;
@@ -42,11 +44,7 @@ public void testFilteredKeysIllegalPut() {
4244
filtered.put("b", 2);
4345
assertEquals(ImmutableMap.of("a", 1, "b", 2), filtered);
4446

45-
try {
46-
filtered.put("yyy", 3);
47-
fail();
48-
} catch (IllegalArgumentException expected) {
49-
}
47+
assertThrows(IllegalArgumentException.class, () -> filtered.put("yyy", 3));
5048
}
5149

5250
public void testFilteredKeysIllegalPutAll() {
@@ -56,11 +54,9 @@ public void testFilteredKeysIllegalPutAll() {
5654
filtered.put("b", 2);
5755
assertEquals(ImmutableMap.of("a", 1, "b", 2), filtered);
5856

59-
try {
60-
filtered.putAll(ImmutableMap.of("c", 3, "zzz", 4, "b", 5));
61-
fail();
62-
} catch (IllegalArgumentException expected) {
63-
}
57+
assertThrows(
58+
IllegalArgumentException.class,
59+
() -> filtered.putAll(ImmutableMap.of("c", 3, "zzz", 4, "b", 5)));
6460

6561
assertEquals(ImmutableMap.of("a", 1, "b", 2), filtered);
6662
}
@@ -91,11 +87,7 @@ public void testFilteredValuesIllegalPut() {
9187
unfiltered.put("c", 5);
9288
assertEquals(ImmutableMap.of("a", 2, "b", 4), filtered);
9389

94-
try {
95-
filtered.put("yyy", 3);
96-
fail();
97-
} catch (IllegalArgumentException expected) {
98-
}
90+
assertThrows(IllegalArgumentException.class, () -> filtered.put("yyy", 3));
9991
assertEquals(ImmutableMap.of("a", 2, "b", 4), filtered);
10092
}
10193

@@ -107,11 +99,9 @@ public void testFilteredValuesIllegalPutAll() {
10799
unfiltered.put("c", 5);
108100
assertEquals(ImmutableMap.of("a", 2, "b", 4), filtered);
109101

110-
try {
111-
filtered.putAll(ImmutableMap.of("c", 4, "zzz", 5, "b", 6));
112-
fail();
113-
} catch (IllegalArgumentException expected) {
114-
}
102+
assertThrows(
103+
IllegalArgumentException.class,
104+
() -> filtered.putAll(ImmutableMap.of("c", 4, "zzz", 5, "b", 6)));
115105
assertEquals(ImmutableMap.of("a", 2, "b", 4), filtered);
116106
}
117107

@@ -123,11 +113,7 @@ public void testFilteredValuesIllegalSetValue() {
123113
assertEquals(ImmutableMap.of("a", 2, "b", 4), filtered);
124114

125115
Entry<String, Integer> entry = filtered.entrySet().iterator().next();
126-
try {
127-
entry.setValue(5);
128-
fail();
129-
} catch (IllegalArgumentException expected) {
130-
}
116+
assertThrows(IllegalArgumentException.class, () -> entry.setValue(5));
131117

132118
assertEquals(ImmutableMap.of("a", 2, "b", 4), filtered);
133119
}
@@ -158,11 +144,7 @@ public void testFilteredEntriesIllegalPut() {
158144
filtered.put("chicken", 7);
159145
assertEquals(ImmutableMap.of("cat", 3, "horse", 5, "chicken", 7), filtered);
160146

161-
try {
162-
filtered.put("cow", 7);
163-
fail();
164-
} catch (IllegalArgumentException expected) {
165-
}
147+
assertThrows(IllegalArgumentException.class, () -> filtered.put("cow", 7));
166148
assertEquals(ImmutableMap.of("cat", 3, "horse", 5, "chicken", 7), filtered);
167149
}
168150

@@ -177,11 +159,9 @@ public void testFilteredEntriesIllegalPutAll() {
177159
filtered.put("chicken", 7);
178160
assertEquals(ImmutableMap.of("cat", 3, "horse", 5, "chicken", 7), filtered);
179161

180-
try {
181-
filtered.putAll(ImmutableMap.of("sheep", 5, "cow", 7));
182-
fail();
183-
} catch (IllegalArgumentException expected) {
184-
}
162+
assertThrows(
163+
IllegalArgumentException.class,
164+
() -> filtered.putAll(ImmutableMap.of("sheep", 5, "cow", 7)));
185165
assertEquals(ImmutableMap.of("cat", 3, "horse", 5, "chicken", 7), filtered);
186166
}
187167

android/guava-tests/test/com/google/common/collect/AbstractImmutableSetTest.java

Lines changed: 34 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.common.collect;
1818

19+
import static com.google.common.collect.ReflectionFreeAssertThrows.assertThrows;
1920
import static com.google.common.collect.testing.IteratorFeature.UNMODIFIABLE;
2021
import static com.google.common.truth.Truth.assertThat;
2122
import static com.google.common.truth.Truth.assertWithMessage;
@@ -132,20 +133,12 @@ public void testCopyOf_arrayOfOneElement() {
132133
}
133134

134135
public void testCopyOf_nullArray() {
135-
try {
136-
copyOf((String[]) null);
137-
fail();
138-
} catch (NullPointerException expected) {
139-
}
136+
assertThrows(NullPointerException.class, () -> copyOf((String[]) null));
140137
}
141138

142139
public void testCopyOf_arrayContainingOnlyNull() {
143140
@Nullable String[] array = new @Nullable String[] {null};
144-
try {
145-
copyOf((String[]) array);
146-
fail();
147-
} catch (NullPointerException expected) {
148-
}
141+
assertThrows(NullPointerException.class, () -> copyOf((String[]) array));
149142
}
150143

151144
public void testCopyOf_collection_empty() {
@@ -178,11 +171,7 @@ public void testCopyOf_collection_general() {
178171

179172
public void testCopyOf_collectionContainingNull() {
180173
Collection<@Nullable String> c = MinimalCollection.of("a", null, "b");
181-
try {
182-
copyOf((Collection<String>) c);
183-
fail();
184-
} catch (NullPointerException expected) {
185-
}
174+
assertThrows(NullPointerException.class, () -> copyOf((Collection<String>) c));
186175
}
187176

188177
enum TestEnum {
@@ -228,11 +217,7 @@ public void testCopyOf_iterator_general() {
228217

229218
public void testCopyOf_iteratorContainingNull() {
230219
Iterator<@Nullable String> c = Iterators.forArray("a", null, "b");
231-
try {
232-
copyOf((Iterator<String>) c);
233-
fail();
234-
} catch (NullPointerException expected) {
235-
}
220+
assertThrows(NullPointerException.class, () -> copyOf((Iterator<String>) c));
236221
}
237222

238223
private static class CountingIterable implements Iterable<String> {
@@ -398,76 +383,59 @@ public void testComplexBuilder() {
398383
abstract int getComplexBuilderSetLastElement();
399384

400385
public void testBuilderAddHandlesNullsCorrectly() {
386+
{
401387
ImmutableSet.Builder<String> builder = this.<String>builder();
402-
try {
403-
builder.add((String) null);
404-
fail("expected NullPointerException"); // COV_NF_LINE
405-
} catch (NullPointerException expected) {
388+
assertThrows(NullPointerException.class, () -> builder.add((String) null));
406389
}
407390

408-
builder = this.<String>builder();
409-
try {
410-
builder.add((String[]) null);
411-
fail("expected NullPointerException"); // COV_NF_LINE
412-
} catch (NullPointerException expected) {
391+
{
392+
ImmutableSet.Builder<String> builder = this.<String>builder();
393+
assertThrows(NullPointerException.class, () -> builder.add((String[]) null));
413394
}
414395

415-
builder = this.<String>builder();
416-
try {
417-
builder.add("a", (String) null);
418-
fail("expected NullPointerException"); // COV_NF_LINE
419-
} catch (NullPointerException expected) {
396+
{
397+
ImmutableSet.Builder<String> builder = this.<String>builder();
398+
assertThrows(NullPointerException.class, () -> builder.add("a", (String) null));
420399
}
421400

422-
builder = this.<String>builder();
423-
try {
424-
builder.add("a", "b", (String) null);
425-
fail("expected NullPointerException"); // COV_NF_LINE
426-
} catch (NullPointerException expected) {
401+
{
402+
ImmutableSet.Builder<String> builder = this.<String>builder();
403+
assertThrows(NullPointerException.class, () -> builder.add("a", "b", (String) null));
427404
}
428405

429-
builder = this.<String>builder();
430-
try {
431-
builder.add("a", "b", "c", null);
432-
fail("expected NullPointerException"); // COV_NF_LINE
433-
} catch (NullPointerException expected) {
406+
{
407+
ImmutableSet.Builder<String> builder = this.<String>builder();
408+
assertThrows(NullPointerException.class, () -> builder.add("a", "b", "c", null));
434409
}
435410

436-
builder = this.<String>builder();
437-
try {
438-
builder.add("a", "b", null, "c");
439-
fail("expected NullPointerException"); // COV_NF_LINE
440-
} catch (NullPointerException expected) {
411+
{
412+
ImmutableSet.Builder<String> builder = this.<String>builder();
413+
assertThrows(NullPointerException.class, () -> builder.add("a", "b", null, "c"));
441414
}
442415
}
443416

444417
public void testBuilderAddAllHandlesNullsCorrectly() {
418+
{
445419
ImmutableSet.Builder<String> builder = this.<String>builder();
446-
try {
447-
builder.addAll((Iterable<String>) null);
448-
fail("expected NullPointerException"); // COV_NF_LINE
449-
} catch (NullPointerException expected) {
420+
assertThrows(NullPointerException.class, () -> builder.addAll((Iterable<String>) null));
450421
}
451422

452-
try {
453-
builder.addAll((Iterator<String>) null);
454-
fail("expected NullPointerException"); // COV_NF_LINE
455-
} catch (NullPointerException expected) {
423+
{
424+
ImmutableSet.Builder<String> builder = this.<String>builder();
425+
assertThrows(NullPointerException.class, () -> builder.addAll((Iterator<String>) null));
456426
}
457427

458-
builder = this.<String>builder();
428+
{
429+
ImmutableSet.Builder<String> builder = this.<String>builder();
459430
List<@Nullable String> listWithNulls = asList("a", null, "b");
460-
try {
461-
builder.addAll((List<String>) listWithNulls);
462-
fail("expected NullPointerException"); // COV_NF_LINE
463-
} catch (NullPointerException expected) {
431+
assertThrows(NullPointerException.class, () -> builder.addAll((List<String>) listWithNulls));
464432
}
465433

434+
{
435+
ImmutableSet.Builder<String> builder = this.<String>builder();
466436
Iterable<@Nullable String> iterableWithNulls = MinimalIterable.of("a", null, "b");
467-
try {
468-
builder.addAll((Iterable<String>) iterableWithNulls);
469-
fail("expected NullPointerException"); // COV_NF_LINE
470-
} catch (NullPointerException expected) {
437+
assertThrows(
438+
NullPointerException.class, () -> builder.addAll((Iterable<String>) iterableWithNulls));
471439
}
472440
}
473441

android/guava-tests/test/com/google/common/collect/AbstractImmutableTableTest.java

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package com.google.common.collect;
1818

19+
import static com.google.common.collect.ReflectionFreeAssertThrows.assertThrows;
20+
1921
import com.google.common.annotations.GwtCompatible;
2022
import junit.framework.TestCase;
2123

@@ -32,45 +34,27 @@ public abstract class AbstractImmutableTableTest extends TestCase {
3234

3335
public final void testClear() {
3436
for (Table<Character, Integer, String> testInstance : getTestInstances()) {
35-
try {
36-
testInstance.clear();
37-
fail();
38-
} catch (UnsupportedOperationException e) {
39-
// success
40-
}
37+
assertThrows(UnsupportedOperationException.class, () -> testInstance.clear());
4138
}
4239
}
4340

4441
public final void testPut() {
4542
for (Table<Character, Integer, String> testInstance : getTestInstances()) {
46-
try {
47-
testInstance.put('a', 1, "blah");
48-
fail();
49-
} catch (UnsupportedOperationException e) {
50-
// success
51-
}
43+
assertThrows(UnsupportedOperationException.class, () -> testInstance.put('a', 1, "blah"));
5244
}
5345
}
5446

5547
public final void testPutAll() {
5648
for (Table<Character, Integer, String> testInstance : getTestInstances()) {
57-
try {
58-
testInstance.putAll(ImmutableTable.of('a', 1, "blah"));
59-
fail();
60-
} catch (UnsupportedOperationException e) {
61-
// success
62-
}
49+
assertThrows(
50+
UnsupportedOperationException.class,
51+
() -> testInstance.putAll(ImmutableTable.of('a', 1, "blah")));
6352
}
6453
}
6554

6655
public final void testRemove() {
6756
for (Table<Character, Integer, String> testInstance : getTestInstances()) {
68-
try {
69-
testInstance.remove('a', 1);
70-
fail();
71-
} catch (UnsupportedOperationException e) {
72-
// success
73-
}
57+
assertThrows(UnsupportedOperationException.class, () -> testInstance.remove('a', 1));
7458
}
7559
}
7660

0 commit comments

Comments
 (0)