Skip to content

Commit fea7a36

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
Use assertThrows even in GWT/J2CL/J2KT-compatible code, common.primitives edition.
(continuing the path started in cl/675634517) RELNOTES=n/a PiperOrigin-RevId: 687502286
1 parent cdc2254 commit fea7a36

36 files changed

+656
-1134
lines changed

android/guava-tests/test/com/google/common/primitives/BooleansTest.java

+6-26
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.common.primitives;
1818

19+
import static com.google.common.primitives.ReflectionFreeAssertThrows.assertThrows;
1920
import static com.google.common.truth.Truth.assertThat;
2021
import static com.google.common.truth.Truth.assertWithMessage;
2122

@@ -170,17 +171,8 @@ public void testEnsureCapacity() {
170171
}
171172

172173
public void testEnsureCapacity_fail() {
173-
try {
174-
Booleans.ensureCapacity(ARRAY_FALSE, -1, 1);
175-
fail();
176-
} catch (IllegalArgumentException expected) {
177-
}
178-
try {
179-
// notice that this should even fail when no growth was needed
180-
Booleans.ensureCapacity(ARRAY_FALSE, 1, -1);
181-
fail();
182-
} catch (IllegalArgumentException expected) {
183-
}
174+
assertThrows(IllegalArgumentException.class, () -> Booleans.ensureCapacity(ARRAY_FALSE, -1, 1));
175+
assertThrows(IllegalArgumentException.class, () -> Booleans.ensureCapacity(ARRAY_FALSE, 1, -1));
184176
}
185177

186178
public void testJoin() {
@@ -512,11 +504,7 @@ public void testToArray_threadSafe() {
512504

513505
public void testToArray_withNull() {
514506
List<@Nullable Boolean> list = Arrays.asList(false, true, null);
515-
try {
516-
Booleans.toArray(list);
517-
fail();
518-
} catch (NullPointerException expected) {
519-
}
507+
assertThrows(NullPointerException.class, () -> Booleans.toArray(list));
520508
}
521509

522510
@SuppressWarnings({"CollectionIsEmptyTruth", "CollectionIsNotEmptyTruth"})
@@ -593,16 +581,8 @@ public void testAsListSet() {
593581
List<Boolean> list = Booleans.asList(ARRAY_FALSE);
594582
assertThat(list.set(0, true)).isFalse();
595583
assertThat(list.set(0, false)).isTrue();
596-
try {
597-
list.set(0, null);
598-
fail();
599-
} catch (NullPointerException expected) {
600-
}
601-
try {
602-
list.set(1, true);
603-
fail();
604-
} catch (IndexOutOfBoundsException expected) {
605-
}
584+
assertThrows(NullPointerException.class, () -> list.set(0, null));
585+
assertThrows(IndexOutOfBoundsException.class, () -> list.set(1, true));
606586
}
607587

608588
public void testAsListCanonicalValues() {

android/guava-tests/test/com/google/common/primitives/BytesTest.java

+4-16
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package com.google.common.primitives;
1818

19+
import static com.google.common.primitives.ReflectionFreeAssertThrows.assertThrows;
1920
import static com.google.common.truth.Truth.assertThat;
2021

2122
import com.google.common.annotations.GwtCompatible;
@@ -171,17 +172,8 @@ public void testEnsureCapacity() {
171172
}
172173

173174
public void testEnsureCapacity_fail() {
174-
try {
175-
Bytes.ensureCapacity(ARRAY1, -1, 1);
176-
fail();
177-
} catch (IllegalArgumentException expected) {
178-
}
179-
try {
180-
// notice that this should even fail when no growth was needed
181-
Bytes.ensureCapacity(ARRAY1, 1, -1);
182-
fail();
183-
} catch (IllegalArgumentException expected) {
184-
}
175+
assertThrows(IllegalArgumentException.class, () -> Bytes.ensureCapacity(ARRAY1, -1, 1));
176+
assertThrows(IllegalArgumentException.class, () -> Bytes.ensureCapacity(ARRAY1, 1, -1));
185177
}
186178

187179
public void testToArray() {
@@ -217,11 +209,7 @@ public void testToArray_threadSafe() {
217209

218210
public void testToArray_withNull() {
219211
List<@Nullable Byte> list = Arrays.asList((byte) 0, (byte) 1, null);
220-
try {
221-
Bytes.toArray(list);
222-
fail();
223-
} catch (NullPointerException expected) {
224-
}
212+
assertThrows(NullPointerException.class, () -> Bytes.toArray(list));
225213
}
226214

227215
public void testToArray_withConversion() {

android/guava-tests/test/com/google/common/primitives/CharsTest.java

+11-41
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.common.primitives.Chars.max;
2020
import static com.google.common.primitives.Chars.min;
21+
import static com.google.common.primitives.ReflectionFreeAssertThrows.assertThrows;
2122
import static com.google.common.truth.Truth.assertThat;
2223
import static com.google.common.truth.Truth.assertWithMessage;
2324

@@ -169,11 +170,7 @@ public void testLastIndexOf() {
169170
}
170171

171172
public void testMax_noArgs() {
172-
try {
173-
max();
174-
fail();
175-
} catch (IllegalArgumentException expected) {
176-
}
173+
assertThrows(IllegalArgumentException.class, () -> max());
177174
}
178175

179176
public void testMax() {
@@ -184,11 +181,7 @@ public void testMax() {
184181
}
185182

186183
public void testMin_noArgs() {
187-
try {
188-
min();
189-
fail();
190-
} catch (IllegalArgumentException expected) {
191-
}
184+
assertThrows(IllegalArgumentException.class, () -> min());
192185
}
193186

194187
public void testMin() {
@@ -204,11 +197,8 @@ public void testConstrainToRange() {
204197
assertThat(Chars.constrainToRange((char) 1, (char) 3, (char) 5)).isEqualTo((char) 3);
205198
assertThat(Chars.constrainToRange((char) 255, (char) 250, (char) 254)).isEqualTo((char) 254);
206199
assertThat(Chars.constrainToRange((char) 5, (char) 2, (char) 2)).isEqualTo((char) 2);
207-
try {
208-
Chars.constrainToRange((char) 1, (char) 3, (char) 2);
209-
fail();
210-
} catch (IllegalArgumentException expected) {
211-
}
200+
assertThrows(
201+
IllegalArgumentException.class, () -> Chars.constrainToRange((char) 1, (char) 3, (char) 2));
212202
}
213203

214204
public void testConcat() {
@@ -263,11 +253,8 @@ public void testFromByteArray() {
263253

264254
@GwtIncompatible // Chars.fromByteArray
265255
public void testFromByteArrayFails() {
266-
try {
267-
Chars.fromByteArray(new byte[Chars.BYTES - 1]);
268-
fail();
269-
} catch (IllegalArgumentException expected) {
270-
}
256+
assertThrows(
257+
IllegalArgumentException.class, () -> Chars.fromByteArray(new byte[Chars.BYTES - 1]));
271258
}
272259

273260
@GwtIncompatible // Chars.fromBytes
@@ -305,11 +292,7 @@ public void testByteArrayRoundTrips() {
305292

306293
@GwtIncompatible // Chars.fromByteArray, Chars.toByteArray
307294
public void testByteArrayRoundTripsFails() {
308-
try {
309-
Chars.fromByteArray(new byte[] {0x11});
310-
fail();
311-
} catch (IllegalArgumentException expected) {
312-
}
295+
assertThrows(IllegalArgumentException.class, () -> Chars.fromByteArray(new byte[] {0x11}));
313296
}
314297

315298
public void testEnsureCapacity() {
@@ -321,17 +304,8 @@ public void testEnsureCapacity() {
321304
}
322305

323306
public void testEnsureCapacity_fail() {
324-
try {
325-
Chars.ensureCapacity(ARRAY1, -1, 1);
326-
fail();
327-
} catch (IllegalArgumentException expected) {
328-
}
329-
try {
330-
// notice that this should even fail when no growth was needed
331-
Chars.ensureCapacity(ARRAY1, 1, -1);
332-
fail();
333-
} catch (IllegalArgumentException expected) {
334-
}
307+
assertThrows(IllegalArgumentException.class, () -> Chars.ensureCapacity(ARRAY1, -1, 1));
308+
assertThrows(IllegalArgumentException.class, () -> Chars.ensureCapacity(ARRAY1, 1, -1));
335309
}
336310

337311
public void testJoin() {
@@ -654,11 +628,7 @@ public void testToArray_threadSafe() {
654628

655629
public void testToArray_withNull() {
656630
List<@Nullable Character> list = Arrays.asList((char) 0, (char) 1, null);
657-
try {
658-
Chars.toArray(list);
659-
fail();
660-
} catch (NullPointerException expected) {
661-
}
631+
assertThrows(NullPointerException.class, () -> Chars.toArray(list));
662632
}
663633

664634
@J2ktIncompatible // b/285319375

android/guava-tests/test/com/google/common/primitives/DoublesTest.java

+12-41
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.google.common.primitives.Doubles.max;
2020
import static com.google.common.primitives.Doubles.min;
21+
import static com.google.common.primitives.ReflectionFreeAssertThrows.assertThrows;
2122
import static com.google.common.truth.Truth.assertThat;
2223
import static com.google.common.truth.Truth.assertWithMessage;
2324
import static java.lang.Double.NaN;
@@ -211,11 +212,7 @@ public void testLastIndexOf() {
211212

212213
@GwtIncompatible
213214
public void testMax_noArgs() {
214-
try {
215-
max();
216-
fail();
217-
} catch (IllegalArgumentException expected) {
218-
}
215+
assertThrows(IllegalArgumentException.class, () -> max());
219216
}
220217

221218
public void testMax() {
@@ -233,11 +230,7 @@ public void testMax() {
233230

234231
@GwtIncompatible
235232
public void testMin_noArgs() {
236-
try {
237-
min();
238-
fail();
239-
} catch (IllegalArgumentException expected) {
240-
}
233+
assertThrows(IllegalArgumentException.class, () -> min());
241234
}
242235

243236
public void testMin() {
@@ -260,11 +253,9 @@ public void testConstrainToRange() {
260253
assertThat(Doubles.constrainToRange((double) 0, (double) -5, (double) -1))
261254
.isEqualTo((double) -1);
262255
assertThat(Doubles.constrainToRange((double) 5, (double) 2, (double) 2)).isEqualTo((double) 2);
263-
try {
264-
Doubles.constrainToRange((double) 1, (double) 3, (double) 2);
265-
fail();
266-
} catch (IllegalArgumentException expected) {
267-
}
256+
assertThrows(
257+
IllegalArgumentException.class,
258+
() -> Doubles.constrainToRange((double) 1, (double) 3, (double) 2));
268259
}
269260

270261
public void testConcat() {
@@ -323,17 +314,8 @@ public void testEnsureCapacity() {
323314
}
324315

325316
public void testEnsureCapacity_fail() {
326-
try {
327-
Doubles.ensureCapacity(ARRAY1, -1, 1);
328-
fail();
329-
} catch (IllegalArgumentException expected) {
330-
}
331-
try {
332-
// notice that this should even fail when no growth was needed
333-
Doubles.ensureCapacity(ARRAY1, 1, -1);
334-
fail();
335-
} catch (IllegalArgumentException expected) {
336-
}
317+
assertThrows(IllegalArgumentException.class, () -> Doubles.ensureCapacity(ARRAY1, -1, 1));
318+
assertThrows(IllegalArgumentException.class, () -> Doubles.ensureCapacity(ARRAY1, 1, -1));
337319
}
338320

339321
@GwtIncompatible // Double.toString returns different value in GWT.
@@ -580,11 +562,7 @@ public void testToArray_threadSafe() {
580562

581563
public void testToArray_withNull() {
582564
List<@Nullable Double> list = Arrays.asList((double) 0, (double) 1, null);
583-
try {
584-
Doubles.toArray(list);
585-
fail();
586-
} catch (NullPointerException expected) {
587-
}
565+
assertThrows(NullPointerException.class, () -> Doubles.toArray(list));
588566
}
589567

590568
public void testToArray_withConversion() {
@@ -791,11 +769,8 @@ public void testStringConverter_convert() {
791769
}
792770

793771
public void testStringConverter_convertError() {
794-
try {
795-
Doubles.stringConverter().convert("notanumber");
796-
fail();
797-
} catch (NumberFormatException expected) {
798-
}
772+
assertThrows(
773+
NumberFormatException.class, () -> Doubles.stringConverter().convert("notanumber"));
799774
}
800775

801776
public void testStringConverter_nullConversions() {
@@ -823,10 +798,6 @@ public void testStringConverter_nullPointerTester() throws Exception {
823798
@GwtIncompatible
824799
public void testTryParse_withNullNoGwt() {
825800
assertThat(Doubles.tryParse("null")).isNull();
826-
try {
827-
Doubles.tryParse(null);
828-
fail("Expected NPE");
829-
} catch (NullPointerException expected) {
830-
}
801+
assertThrows(NullPointerException.class, () -> Doubles.tryParse(null));
831802
}
832803
}

0 commit comments

Comments
 (0)