1616
1717package com .google .common .collect .testing ;
1818
19+ import static com .google .common .collect .testing .ReflectionFreeAssertThrows .assertThrows ;
1920import static java .util .Collections .singleton ;
2021
2122import com .google .common .annotations .GwtCompatible ;
@@ -280,11 +281,7 @@ public void testClear() {
280281 map .clear ();
281282 assertTrue (map .isEmpty ());
282283 } else {
283- try {
284- map .clear ();
285- fail ("Expected UnsupportedOperationException." );
286- } catch (UnsupportedOperationException expected ) {
287- }
284+ assertThrows (UnsupportedOperationException .class , () -> map .clear ());
288285 }
289286 assertInvariants (map );
290287 }
@@ -479,18 +476,10 @@ public void testEntrySetIteratorRemove() {
479476 // iterator.remove().
480477 assertFalse (entrySet .contains (entryCopy ));
481478 assertInvariants (map );
482- try {
483- iterator .remove ();
484- fail ("Expected IllegalStateException." );
485- } catch (IllegalStateException expected ) {
486- }
479+ assertThrows (IllegalStateException .class , () -> iterator .remove ());
487480 } else {
488481 iterator .next ();
489- try {
490- iterator .remove ();
491- fail ("Expected UnsupportedOperationException." );
492- } catch (UnsupportedOperationException expected ) {
493- }
482+ assertThrows (UnsupportedOperationException .class , () -> iterator .remove ());
494483 }
495484 assertInvariants (map );
496485 }
@@ -510,11 +499,8 @@ public void testEntrySetRemove() {
510499 assertTrue (didRemove );
511500 assertEquals (initialSize - 1 , map .size ());
512501 } else {
513- try {
514- entrySet .remove (entrySet .iterator ().next ());
515- fail ("Expected UnsupportedOperationException." );
516- } catch (UnsupportedOperationException expected ) {
517- }
502+ assertThrows (
503+ UnsupportedOperationException .class , () -> entrySet .remove (entrySet .iterator ().next ()));
518504 }
519505 assertInvariants (map );
520506 }
@@ -661,11 +647,7 @@ public void testEntrySetRemoveAll() {
661647 // have undefined behavior after entrySet.removeAll(entriesToRemove),
662648 assertFalse (entrySet .contains (entryToRemoveCopy ));
663649 } else {
664- try {
665- entrySet .removeAll (entriesToRemove );
666- fail ("Expected UnsupportedOperationException." );
667- } catch (UnsupportedOperationException expected ) {
668- }
650+ assertThrows (UnsupportedOperationException .class , () -> entrySet .removeAll (entriesToRemove ));
669651 }
670652 assertInvariants (map );
671653 }
@@ -680,11 +662,7 @@ public void testEntrySetRemoveAllNullFromEmpty() {
680662
681663 Set <Entry <K , V >> entrySet = map .entrySet ();
682664 if (supportsRemove ) {
683- try {
684- entrySet .removeAll (null );
685- fail ("Expected NullPointerException." );
686- } catch (NullPointerException expected ) {
687- }
665+ assertThrows (NullPointerException .class , () -> entrySet .removeAll (null ));
688666 } else {
689667 try {
690668 entrySet .removeAll (null );
@@ -715,11 +693,7 @@ public void testEntrySetRetainAll() {
715693 assertTrue (entrySet .contains (entry ));
716694 }
717695 } else {
718- try {
719- entrySet .retainAll (entriesToRetain );
720- fail ("Expected UnsupportedOperationException." );
721- } catch (UnsupportedOperationException expected ) {
722- }
696+ assertThrows (UnsupportedOperationException .class , () -> entrySet .retainAll (entriesToRetain ));
723697 }
724698 assertInvariants (map );
725699 }
@@ -763,11 +737,7 @@ public void testEntrySetClear() {
763737 entrySet .clear ();
764738 assertTrue (entrySet .isEmpty ());
765739 } else {
766- try {
767- entrySet .clear ();
768- fail ("Expected UnsupportedOperationException." );
769- } catch (UnsupportedOperationException expected ) {
770- }
740+ assertThrows (UnsupportedOperationException .class , () -> entrySet .clear ());
771741 }
772742 assertInvariants (map );
773743 }
@@ -1002,11 +972,7 @@ public void testPutNewKey() {
1002972 assertEquals (initialSize + 1 , map .size ());
1003973 assertNull (oldValue );
1004974 } else {
1005- try {
1006- map .put (keyToPut , valueToPut );
1007- fail ("Expected UnsupportedOperationException." );
1008- } catch (UnsupportedOperationException expected ) {
1009- }
975+ assertThrows (UnsupportedOperationException .class , () -> map .put (keyToPut , valueToPut ));
1010976 }
1011977 assertInvariants (map );
1012978 }
@@ -1030,11 +996,7 @@ public void testPutExistingKey() {
1030996 assertTrue (map .containsValue (valueToPut ));
1031997 assertEquals (initialSize , map .size ());
1032998 } else {
1033- try {
1034- map .put (keyToPut , valueToPut );
1035- fail ("Expected UnsupportedOperationException." );
1036- } catch (UnsupportedOperationException expected ) {
1037- }
999+ assertThrows (UnsupportedOperationException .class , () -> map .put (keyToPut , valueToPut ));
10381000 }
10391001 assertInvariants (map );
10401002 }
@@ -1058,11 +1020,7 @@ public void testPutNullKey() {
10581020 assertTrue (map .containsKey (null ));
10591021 assertTrue (map .containsValue (valueToPut ));
10601022 } else {
1061- try {
1062- map .put (null , valueToPut );
1063- fail ("Expected RuntimeException" );
1064- } catch (RuntimeException expected ) {
1065- }
1023+ assertThrows (RuntimeException .class , () -> map .put (null , valueToPut ));
10661024 }
10671025 assertInvariants (map );
10681026 }
@@ -1088,11 +1046,7 @@ public void testPutNullValue() {
10881046 assertTrue (map .containsValue (null ));
10891047 assertEquals (initialSize + 1 , map .size ());
10901048 } else {
1091- try {
1092- map .put (keyToPut , null );
1093- fail ("Expected RuntimeException" );
1094- } catch (RuntimeException expected ) {
1095- }
1049+ assertThrows (RuntimeException .class , () -> map .put (keyToPut , null ));
10961050 }
10971051 assertInvariants (map );
10981052 }
@@ -1119,11 +1073,7 @@ public void testPutNullValueForExistingKey() {
11191073 assertTrue (map .containsValue (null ));
11201074 assertEquals (initialSize , map .size ());
11211075 } else {
1122- try {
1123- map .put (keyToPut , null );
1124- fail ("Expected RuntimeException" );
1125- } catch (RuntimeException expected ) {
1126- }
1076+ assertThrows (RuntimeException .class , () -> map .put (keyToPut , null ));
11271077 }
11281078 assertInvariants (map );
11291079 }
@@ -1147,11 +1097,7 @@ public void testPutAllNewKey() {
11471097 assertTrue (map .containsValue (valueToPut ));
11481098 assertEquals (initialSize + 1 , map .size ());
11491099 } else {
1150- try {
1151- map .putAll (mapToPut );
1152- fail ("Expected UnsupportedOperationException." );
1153- } catch (UnsupportedOperationException expected ) {
1154- }
1100+ assertThrows (UnsupportedOperationException .class , () -> map .putAll (mapToPut ));
11551101 }
11561102 assertInvariants (map );
11571103 }
@@ -1175,11 +1121,7 @@ public void testPutAllExistingKey() {
11751121 assertTrue (map .containsKey (keyToPut ));
11761122 assertTrue (map .containsValue (valueToPut ));
11771123 } else {
1178- try {
1179- map .putAll (mapToPut );
1180- fail ("Expected UnsupportedOperationException." );
1181- } catch (UnsupportedOperationException expected ) {
1182- }
1124+ assertThrows (UnsupportedOperationException .class , () -> map .putAll (mapToPut ));
11831125 }
11841126 assertEquals (initialSize , map .size ());
11851127 assertInvariants (map );
@@ -1202,11 +1144,7 @@ public void testRemove() {
12021144 assertFalse (map .containsKey (keyToRemove ));
12031145 assertEquals (initialSize - 1 , map .size ());
12041146 } else {
1205- try {
1206- map .remove (keyToRemove );
1207- fail ("Expected UnsupportedOperationException." );
1208- } catch (UnsupportedOperationException expected ) {
1209- }
1147+ assertThrows (UnsupportedOperationException .class , () -> map .remove (keyToRemove ));
12101148 }
12111149 assertInvariants (map );
12121150 }
@@ -1225,11 +1163,7 @@ public void testRemoveMissingKey() {
12251163 assertNull (map .remove (keyToRemove ));
12261164 assertEquals (initialSize , map .size ());
12271165 } else {
1228- try {
1229- map .remove (keyToRemove );
1230- fail ("Expected UnsupportedOperationException." );
1231- } catch (UnsupportedOperationException expected ) {
1232- }
1166+ assertThrows (UnsupportedOperationException .class , () -> map .remove (keyToRemove ));
12331167 }
12341168 assertInvariants (map );
12351169 }
@@ -1254,11 +1188,7 @@ public void testKeySetRemove() {
12541188 assertEquals (initialSize - 1 , map .size ());
12551189 assertFalse (map .containsKey (key ));
12561190 } else {
1257- try {
1258- keys .remove (key );
1259- fail ("Expected UnsupportedOperationException." );
1260- } catch (UnsupportedOperationException expected ) {
1261- }
1191+ assertThrows (UnsupportedOperationException .class , () -> keys .remove (key ));
12621192 }
12631193 assertInvariants (map );
12641194 }
@@ -1279,11 +1209,8 @@ public void testKeySetRemoveAll() {
12791209 assertEquals (initialSize - 1 , map .size ());
12801210 assertFalse (map .containsKey (key ));
12811211 } else {
1282- try {
1283- keys .removeAll (Collections .singleton (key ));
1284- fail ("Expected UnsupportedOperationException." );
1285- } catch (UnsupportedOperationException expected ) {
1286- }
1212+ assertThrows (
1213+ UnsupportedOperationException .class , () -> keys .removeAll (Collections .singleton (key )));
12871214 }
12881215 assertInvariants (map );
12891216 }
@@ -1303,11 +1230,8 @@ public void testKeySetRetainAll() {
13031230 assertEquals (1 , map .size ());
13041231 assertTrue (map .containsKey (key ));
13051232 } else {
1306- try {
1307- keys .retainAll (Collections .singleton (key ));
1308- fail ("Expected UnsupportedOperationException." );
1309- } catch (UnsupportedOperationException expected ) {
1310- }
1233+ assertThrows (
1234+ UnsupportedOperationException .class , () -> keys .retainAll (Collections .singleton (key )));
13111235 }
13121236 assertInvariants (map );
13131237 }
@@ -1325,11 +1249,7 @@ public void testKeySetClear() {
13251249 keySet .clear ();
13261250 assertTrue (keySet .isEmpty ());
13271251 } else {
1328- try {
1329- keySet .clear ();
1330- fail ("Expected UnsupportedOperationException." );
1331- } catch (UnsupportedOperationException expected ) {
1332- }
1252+ assertThrows (UnsupportedOperationException .class , () -> keySet .clear ());
13331253 }
13341254 assertInvariants (map );
13351255 }
@@ -1344,11 +1264,7 @@ public void testKeySetRemoveAllNullFromEmpty() {
13441264
13451265 Set <K > keySet = map .keySet ();
13461266 if (supportsRemove ) {
1347- try {
1348- keySet .removeAll (null );
1349- fail ("Expected NullPointerException." );
1350- } catch (NullPointerException expected ) {
1351- }
1267+ assertThrows (NullPointerException .class , () -> keySet .removeAll (null ));
13521268 } else {
13531269 try {
13541270 keySet .removeAll (null );
@@ -1427,18 +1343,10 @@ public void testValuesIteratorRemove() {
14271343 // removed value, because the underlying map can have multiple mappings
14281344 // to the same value.)
14291345 assertInvariants (map );
1430- try {
1431- iterator .remove ();
1432- fail ("Expected IllegalStateException." );
1433- } catch (IllegalStateException expected ) {
1434- }
1346+ assertThrows (IllegalStateException .class , () -> iterator .remove ());
14351347 } else {
14361348 iterator .next ();
1437- try {
1438- iterator .remove ();
1439- fail ("Expected UnsupportedOperationException." );
1440- } catch (UnsupportedOperationException expected ) {
1441- }
1349+ assertThrows (UnsupportedOperationException .class , () -> iterator .remove ());
14421350 }
14431351 assertInvariants (map );
14441352 }
@@ -1460,11 +1368,9 @@ public void testValuesRemove() {
14601368 // removed value, because the underlying map can have multiple mappings
14611369 // to the same value.)
14621370 } else {
1463- try {
1464- valueCollection .remove (valueCollection .iterator ().next ());
1465- fail ("Expected UnsupportedOperationException." );
1466- } catch (UnsupportedOperationException expected ) {
1467- }
1371+ assertThrows (
1372+ UnsupportedOperationException .class ,
1373+ () -> valueCollection .remove (valueCollection .iterator ().next ()));
14681374 }
14691375 assertInvariants (map );
14701376 }
@@ -1513,11 +1419,8 @@ public void testValuesRemoveAll() {
15131419 assertFalse (valuesToRemove .contains (value ));
15141420 }
15151421 } else {
1516- try {
1517- valueCollection .removeAll (valuesToRemove );
1518- fail ("Expected UnsupportedOperationException." );
1519- } catch (UnsupportedOperationException expected ) {
1520- }
1422+ assertThrows (
1423+ UnsupportedOperationException .class , () -> valueCollection .removeAll (valuesToRemove ));
15211424 }
15221425 assertInvariants (map );
15231426 }
@@ -1567,11 +1470,8 @@ public void testValuesRetainAll() {
15671470 assertTrue (valuesToRetain .contains (value ));
15681471 }
15691472 } else {
1570- try {
1571- valueCollection .retainAll (valuesToRetain );
1572- fail ("Expected UnsupportedOperationException." );
1573- } catch (UnsupportedOperationException expected ) {
1574- }
1473+ assertThrows (
1474+ UnsupportedOperationException .class , () -> valueCollection .retainAll (valuesToRetain ));
15751475 }
15761476 assertInvariants (map );
15771477 }
@@ -1615,11 +1515,7 @@ public void testValuesClear() {
16151515 valueCollection .clear ();
16161516 assertTrue (valueCollection .isEmpty ());
16171517 } else {
1618- try {
1619- valueCollection .clear ();
1620- fail ("Expected UnsupportedOperationException." );
1621- } catch (UnsupportedOperationException expected ) {
1622- }
1518+ assertThrows (UnsupportedOperationException .class , () -> valueCollection .clear ());
16231519 }
16241520 assertInvariants (map );
16251521 }
0 commit comments