11/*******************************************************************************
2- * Copyright (c) 2000, 2021 IBM Corporation and others.
2+ * Copyright (c) 2000, 2025 IBM Corporation and others.
33 *
44 * This program and the accompanying materials
55 * are made available under the terms of the Eclipse Public License 2.0
2020import static org .junit .jupiter .api .Assertions .assertNotNull ;
2121import static org .junit .jupiter .api .Assertions .assertNull ;
2222import static org .junit .jupiter .api .Assertions .assertSame ;
23+ import static org .junit .jupiter .api .Assertions .assertThrows ;
2324import static org .junit .jupiter .api .Assertions .assertTrue ;
2425import static org .junit .jupiter .api .Assertions .fail ;
2526
@@ -97,12 +98,9 @@ else if (e.type == SWT.Dispose)
9798
9899 Display display = new Display ();
99100 try {
100- try {
101- display .addFilter (SWT .Dispose , null );
102- fail ("No exception thrown for addFilter with null argument" );
103- } catch (IllegalArgumentException e ) {
104- assertSWTProblem ("Incorrect exception thrown for addFilter with null argument" , SWT .ERROR_NULL_ARGUMENT , e );
105- }
101+ IllegalArgumentException e = assertThrows (IllegalArgumentException .class ,
102+ () -> display .addFilter (SWT .Dispose , null ), "No exception thrown for addFilter with null argument" );
103+ assertSWTProblem ("Incorrect exception thrown for addFilter with null argument" , SWT .ERROR_NULL_ARGUMENT , e );
106104
107105 display .addFilter (SWT .Close , listener );
108106 } finally {
@@ -127,12 +125,9 @@ else if (e.type == SWT.Dispose)
127125
128126 Display display = new Display ();
129127 try {
130- try {
131- display .addListener (SWT .Close , null );
132- fail ("No exception thrown for addListener with null argument" );
133- } catch (IllegalArgumentException e ) {
134- assertSWTProblem ("Incorrect exception thrown for addListener with null argument" , SWT .ERROR_NULL_ARGUMENT , e );
135- }
128+ IllegalArgumentException e = assertThrows (IllegalArgumentException .class ,
129+ () -> display .addListener (SWT .Close , null ), "No exception thrown for addListener with null argument" );
130+ assertSWTProblem ("Incorrect exception thrown for addListener with null argument" , SWT .ERROR_NULL_ARGUMENT , e );
136131
137132 display .addListener (SWT .Dispose , listener );
138133 } finally {
@@ -141,11 +136,11 @@ else if (e.type == SWT.Dispose)
141136 assertFalse (callbackReceived [CLOSE_CALLBACK ]);
142137 assertTrue (callbackReceived [DISPOSE_CALLBACK ]);
143138
144- display = new Display ();
139+ Display display2 = new Display ();
145140 try {
146- display .addListener (SWT .Close , listener );
141+ display2 .addListener (SWT .Close , listener );
147142 } finally {
148- display .close ();
143+ display2 .close ();
149144 }
150145 assertTrue (callbackReceived [CLOSE_CALLBACK ]);
151146}
@@ -641,18 +636,14 @@ public void test_mapLorg_eclipse_swt_widgets_ControlLorg_eclipse_swt_widgets_Con
641636 overlayShellRtoL1 .dispose ();
642637 overlayShellRtoL2 .dispose ();
643638
644- try {
645- result = display .map (button1 , button2 , 0 , 0 );
646- fail ("No exception thrown for map from control being disposed" );
647- } catch (IllegalArgumentException e ) {
648- assertSWTProblem ("Incorrect exception thrown for map from control being disposed" , SWT .ERROR_INVALID_ARGUMENT , e );
649- }
650- try {
651- result = display .map (button2 , button1 , 0 , 0 );
652- fail ("No exception thrown for map to control being disposed" );
653- } catch (IllegalArgumentException e ) {
654- assertSWTProblem ("Incorrect exception thrown for map to control being disposed" , SWT .ERROR_INVALID_ARGUMENT , e );
655- }
639+ IllegalArgumentException e1 = assertThrows (IllegalArgumentException .class ,
640+ () -> display .map (button1 , button2 , 0 , 0 ), "No exception thrown for map from control being disposed" );
641+ assertSWTProblem ("Incorrect exception thrown for map from control being disposed" , SWT .ERROR_INVALID_ARGUMENT ,
642+ e1 );
643+ IllegalArgumentException e2 = assertThrows (IllegalArgumentException .class ,
644+ () -> display .map (button2 , button1 , 0 , 0 ), "No exception thrown for map to control being disposed" );
645+ assertSWTProblem ("Incorrect exception thrown for map to control being disposed" , SWT .ERROR_INVALID_ARGUMENT ,
646+ e2 );
656647
657648 shell .dispose ();
658649 } finally {
@@ -770,18 +761,16 @@ public void test_mapLorg_eclipse_swt_widgets_ControlLorg_eclipse_swt_widgets_Con
770761 overlayShellRtoL1 .dispose ();
771762 overlayShellRtoL2 .dispose ();
772763
773- try {
774- result = display .map (button1 , button2 , 0 , 0 , 100 , 100 );
775- fail ("No exception thrown for map from control being disposed" );
776- } catch (IllegalArgumentException e ) {
777- assertSWTProblem ("Incorrect exception thrown for map from control being disposed" , SWT .ERROR_INVALID_ARGUMENT , e );
778- }
779- try {
780- result = display .map (button2 , button1 , 0 , 0 , 100 , 100 );
781- fail ("No exception thrown for map to control being disposed" );
782- } catch (IllegalArgumentException e ) {
783- assertSWTProblem ("Incorrect exception thrown for map to control being disposed" , SWT .ERROR_INVALID_ARGUMENT , e );
784- }
764+ IllegalArgumentException e1 = assertThrows (IllegalArgumentException .class ,
765+ () -> display .map (button1 , button2 , 0 , 0 , 100 , 100 ),
766+ "No exception thrown for map from control being disposed" );
767+ assertSWTProblem ("Incorrect exception thrown for map from control being disposed" , SWT .ERROR_INVALID_ARGUMENT ,
768+ e1 );
769+ IllegalArgumentException e2 = assertThrows (IllegalArgumentException .class ,
770+ () -> display .map (button2 , button1 , 0 , 0 , 100 , 100 ),
771+ "No exception thrown for map to control being disposed" );
772+ assertSWTProblem ("Incorrect exception thrown for map to control being disposed" , SWT .ERROR_INVALID_ARGUMENT ,
773+ e2 );
785774
786775 shell .dispose ();
787776 } finally {
@@ -901,25 +890,18 @@ public void test_mapLorg_eclipse_swt_widgets_ControlLorg_eclipse_swt_widgets_Con
901890 overlayShellRtoL1 .dispose ();
902891 overlayShellRtoL2 .dispose ();
903892
904- try {
905- result = display .map (button1 , button2 , point );
906- fail ("No exception thrown for map from control being disposed" );
907- } catch (IllegalArgumentException e ) {
908- assertSWTProblem ("Incorrect exception thrown for map from control being disposed" , SWT .ERROR_INVALID_ARGUMENT , e );
909- }
910- try {
911- result = display .map (button2 , button1 , point );
912- fail ("No exception thrown for map to control being disposed" );
913- } catch (IllegalArgumentException e ) {
914- assertSWTProblem ("Incorrect exception thrown for map to control being disposed" , SWT .ERROR_INVALID_ARGUMENT , e );
915- }
893+ IllegalArgumentException e1 = assertThrows (IllegalArgumentException .class ,
894+ () -> display .map (button1 , button2 , point ), "No exception thrown for map from control being disposed" );
895+ assertSWTProblem ("Incorrect exception thrown for map from control being disposed" , SWT .ERROR_INVALID_ARGUMENT ,
896+ e1 );
897+ IllegalArgumentException e2 = assertThrows (IllegalArgumentException .class ,
898+ () -> display .map (button2 , button1 , point ), "No exception thrown for map to control being disposed" );
899+ assertSWTProblem ("Incorrect exception thrown for map to control being disposed" , SWT .ERROR_INVALID_ARGUMENT ,
900+ e2 );
916901
917- try {
918- result = display .map (button2 , button1 , (Point ) null );
919- fail ("No exception thrown for null point" );
920- } catch (IllegalArgumentException e ) {
921- assertSWTProblem ("Incorrect exception thrown for point being null" , SWT .ERROR_NULL_ARGUMENT , e );
922- }
902+ IllegalArgumentException e3 = assertThrows (IllegalArgumentException .class ,
903+ () -> display .map (button2 , button1 , (Point ) null ), "No exception thrown for null point" );
904+ assertSWTProblem ("Incorrect exception thrown for point being null" , SWT .ERROR_NULL_ARGUMENT , e3 );
923905
924906 shell .dispose ();
925907 } finally {
@@ -1038,25 +1020,18 @@ public void test_mapLorg_eclipse_swt_widgets_ControlLorg_eclipse_swt_widgets_Con
10381020 overlayShellRtoL1 .dispose ();
10391021 overlayShellRtoL2 .dispose ();
10401022
1041- try {
1042- result = display .map (button1 , button2 , rect );
1043- fail ("No exception thrown for map from control being disposed" );
1044- } catch (IllegalArgumentException e ) {
1045- assertSWTProblem ("Incorrect exception thrown for map from control being disposed" , SWT .ERROR_INVALID_ARGUMENT , e );
1046- }
1047- try {
1048- result = display .map (button2 , button1 , rect );
1049- fail ("No exception thrown for map to control being disposed" );
1050- } catch (IllegalArgumentException e ) {
1051- assertSWTProblem ("Incorrect exception thrown for map to control being disposed" , SWT .ERROR_INVALID_ARGUMENT , e );
1052- }
1023+ IllegalArgumentException e1 = assertThrows (IllegalArgumentException .class ,
1024+ () -> display .map (button1 , button2 , rect ), "No exception thrown for map from control being disposed" );
1025+ assertSWTProblem ("Incorrect exception thrown for map from control being disposed" , SWT .ERROR_INVALID_ARGUMENT ,
1026+ e1 );
1027+ IllegalArgumentException e2 = assertThrows (IllegalArgumentException .class ,
1028+ () -> display .map (button2 , button1 , rect ), "No exception thrown for map to control being disposed" );
1029+ assertSWTProblem ("Incorrect exception thrown for map to control being disposed" , SWT .ERROR_INVALID_ARGUMENT ,
1030+ e2 );
10531031
1054- try {
1055- result = display .map (button2 , button1 , (Rectangle ) null );
1056- fail ("No exception thrown for null point" );
1057- } catch (IllegalArgumentException e ) {
1058- assertSWTProblem ("Incorrect exception thrown for rectangle being null" , SWT .ERROR_NULL_ARGUMENT , e );
1059- }
1032+ IllegalArgumentException e3 = assertThrows (IllegalArgumentException .class ,
1033+ () -> display .map (button2 , button1 , (Rectangle ) null ), "No exception thrown for null point" );
1034+ assertSWTProblem ("Incorrect exception thrown for rectangle being null" , SWT .ERROR_NULL_ARGUMENT , e3 );
10601035
10611036 shell .dispose ();
10621037 } finally {
@@ -1071,12 +1046,8 @@ public void test_postLorg_eclipse_swt_widgets_Event() {
10711046
10721047 Display display = new Display ();
10731048 try {
1074- try {
1075- display .post (null );
1076- fail ("No exception thrown for post with null argument" );
1077- } catch (IllegalArgumentException e ) {
1078- assertSWTProblem ("Incorrect exception thrown for post with null argument" , SWT .ERROR_NULL_ARGUMENT , e );
1079- }
1049+ IllegalArgumentException ex = assertThrows (IllegalArgumentException .class , ()-> display .post (null ), "No exception thrown for post with null argument" );
1050+ assertSWTProblem ("Incorrect exception thrown for post with null argument" , SWT .ERROR_NULL_ARGUMENT , ex );
10801051
10811052 Shell shell = new Shell (display , SWT .NO_TRIM );
10821053 shell .setBounds (display .getBounds ());
@@ -1183,12 +1154,8 @@ else if (e.type == SWT.Dispose)
11831154
11841155 Display display = new Display ();
11851156 try {
1186- try {
1187- display .removeFilter (SWT .Dispose , null );
1188- fail ("No exception thrown for removeFilter with null argument" );
1189- } catch (IllegalArgumentException e ) {
1190- assertSWTProblem ("Incorrect exception thrown for removeFilter with null argument" , SWT .ERROR_NULL_ARGUMENT , e );
1191- }
1157+ IllegalArgumentException ex = assertThrows (IllegalArgumentException .class , () -> display .removeFilter (SWT .Dispose , null ), "No exception thrown for removeFilter with null argument" );
1158+ assertSWTProblem ("Incorrect exception thrown for removeFilter with null argument" , SWT .ERROR_NULL_ARGUMENT , ex );
11921159
11931160 display .addFilter (SWT .Close , listener );
11941161 display .removeFilter (SWT .Close , listener );
@@ -1214,12 +1181,11 @@ else if (e.type == SWT.Dispose)
12141181
12151182 Display display = new Display ();
12161183 try {
1217- try {
1218- display .removeListener (SWT .Close , null );
1219- fail ("No exception thrown for removeListener with null argument" );
1220- } catch (IllegalArgumentException e ) {
1221- assertSWTProblem ("Incorrect exception thrown for removeListener with null argument" , SWT .ERROR_NULL_ARGUMENT , e );
1222- }
1184+ IllegalArgumentException ex = assertThrows (IllegalArgumentException .class ,
1185+ () -> display .removeListener (SWT .Close , null ),
1186+ "No exception thrown for removeListener with null argument" );
1187+ assertSWTProblem ("Incorrect exception thrown for removeListener with null argument" , SWT .ERROR_NULL_ARGUMENT ,
1188+ ex );
12231189
12241190 display .addListener (SWT .Dispose , listener );
12251191 display .removeListener (SWT .Dispose , listener );
@@ -1271,12 +1237,10 @@ public void test_setCursorLocationLorg_eclipse_swt_graphics_Point(TestInfo info)
12711237
12721238 Point location = new Point (100 , 50 );
12731239 display .setCursorLocation (location ); // don't put cursor into a corner, since that could trigger special platform events
1274- try {
1275- display .setCursorLocation (null );
1276- fail ("No exception thrown for null argument" );
1277- } catch (IllegalArgumentException e ) {
1278- assertSWTProblem ("Incorrect exception thrown for setCursorLocation with null argument" , SWT .ERROR_NULL_ARGUMENT , e );
1279- }
1240+ IllegalArgumentException ex = assertThrows (IllegalArgumentException .class ,
1241+ () -> display .setCursorLocation (null ), "No exception thrown for null argument" );
1242+ assertSWTProblem ("Incorrect exception thrown for setCursorLocation with null argument" , SWT .ERROR_NULL_ARGUMENT ,
1243+ ex );
12801244 drainEventQueue (display , 150 ); // workaround for https://bugs.eclipse.org/492569
12811245 Point actual = display .getCursorLocation ();
12821246 if (!BUG_492569 && SwtTestUtil .isX11 ()) {
@@ -1331,12 +1295,10 @@ public void test_setSynchronizerLorg_eclipse_swt_widgets_Synchronizer() {
13311295 final boolean [] asyncExec3Ran = new boolean [] {false };
13321296
13331297 try {
1334- try {
1335- display .setSynchronizer (null );
1336- fail ("No exception thrown for post with null argument" );
1337- } catch (IllegalArgumentException e ) {
1338- assertSWTProblem ("Incorrect exception thrown for set synchronizer with null argument" , SWT .ERROR_NULL_ARGUMENT , e );
1339- }
1298+ IllegalArgumentException ex = assertThrows (IllegalArgumentException .class , () -> display .setSynchronizer (null ),
1299+ "No exception thrown for post with null argument" );
1300+ assertSWTProblem ("Incorrect exception thrown for set synchronizer with null argument" , SWT .ERROR_NULL_ARGUMENT ,
1301+ ex );
13401302
13411303 class MySynchronizer extends Synchronizer {
13421304 boolean invoked = false ;
@@ -1467,9 +1429,9 @@ public void test_syncCall_dispose() {
14671429public void test_syncCall_RuntimeException () {
14681430 final Display display = new Display ();
14691431 try {
1470- int depth = display .syncCall (() -> {throw new IllegalArgumentException ( "42" );});
1471- fail ( "should not be reached " + depth );
1472- } catch ( RuntimeException e ) {
1432+ RuntimeException e = assertThrows ( RuntimeException . class , () -> display .syncCall (() -> {
1433+ throw new IllegalArgumentException ( "42" );
1434+ }));
14731435 assertEquals ("42" , e .getMessage ());
14741436 } finally {
14751437 display .dispose ();
@@ -1479,9 +1441,9 @@ public void test_syncCall_RuntimeException() {
14791441public void test_syncCall_Exception () {
14801442 final Display display = new Display ();
14811443 try {
1482- int depth = display .syncCall (() -> {throw new IOException ( "42" );});
1483- fail ( "should not be reached " + depth );
1484- } catch ( IOException e ) {
1444+ IOException e = assertThrows ( IOException . class , () -> display .syncCall (() -> {
1445+ throw new IOException ( "42" );
1446+ }));
14851447 assertEquals ("42" , e .getMessage ());
14861448 } finally {
14871449 display .dispose ();
@@ -1491,12 +1453,11 @@ public void test_syncCall_Exception() {
14911453public void test_syncCall_SWTException () {
14921454 final Display display = new Display ();
14931455 display .dispose ();
1494- try {
1495- int magic =display .syncCall (() -> {display .dispose (); return 42 ;});
1496- fail ("should not be reached " +magic );
1497- } catch (SWTException e ) {
1498- assertEquals ("Device is disposed" , e .getMessage ());
1499- }
1456+ SWTException e = assertThrows (SWTException .class , () -> display .syncCall (() -> {
1457+ display .dispose ();
1458+ return 42 ;
1459+ }));
1460+ assertEquals ("Device is disposed" , e .getMessage ());
15001461}
15011462@ Test
15021463public void test_syncCall_concurrentCallable () throws Exception {
@@ -1514,9 +1475,7 @@ public void test_syncCall_concurrentCallable_Exception() {
15141475 final Display display = new Display ();
15151476 try {
15161477 java .util .concurrent .Callable <Integer > c =() -> {throw new IOException ("42" );};
1517- int depth =display .syncCall (c ::call );
1518- fail ("should not be reached " +depth );
1519- } catch (Exception e ) {
1478+ Exception e = assertThrows (Exception .class , () ->display .syncCall (c ::call ));
15201479 assertEquals ("42" , e .getMessage ());
15211480 } finally {
15221481 display .dispose ();
@@ -1530,12 +1489,9 @@ public void test_timerExecILjava_lang_Runnable() {
15301489 final boolean [] timerExecRan = new boolean [] {false };
15311490 final boolean [] threadRan = new boolean [] {false };
15321491
1533- try {
1534- display .timerExec (0 , null );
1535- fail ("No exception thrown for timerExec with null runnable" );
1536- } catch (IllegalArgumentException e ) {
1537- assertSWTProblem ("Incorrect exception thrown for timerExec with null runnable" , SWT .ERROR_NULL_ARGUMENT , e );
1538- }
1492+ IllegalArgumentException e = assertThrows (IllegalArgumentException .class , () -> display .timerExec (0 , null ),
1493+ "No exception thrown for timerExec with null runnable" );
1494+ assertSWTProblem ("Incorrect exception thrown for timerExec with null runnable" , SWT .ERROR_NULL_ARGUMENT , e );
15391495
15401496 display .timerExec (-100 , () -> timerExecRan [0 ] = true );
15411497
0 commit comments