11/*******************************************************************************
2- * Copyright (c) 2000, 2018 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
1717import static org .junit .jupiter .api .Assertions .assertEquals ;
1818import static org .junit .jupiter .api .Assertions .assertFalse ;
1919import static org .junit .jupiter .api .Assertions .assertNull ;
20+ import static org .junit .jupiter .api .Assertions .assertThrows ;
2021import static org .junit .jupiter .api .Assertions .assertTrue ;
21- import static org .junit .jupiter .api .Assertions .fail ;
2222
2323import org .eclipse .swt .SWT ;
2424import org .eclipse .swt .graphics .Color ;
@@ -50,13 +50,7 @@ public void setUp() {
5050
5151@ Test
5252public void test_ConstructorLorg_eclipse_swt_widgets_TreeI () {
53- try {
54- new TreeItem ((TreeItem )null , SWT .NULL );
55- fail ("No exception thrown for parent == null" );
56- }
57- catch (IllegalArgumentException e ) {
58- }
59-
53+ assertThrows (IllegalArgumentException .class , () -> new TreeItem ((TreeItem )null , SWT .NULL ), "No exception thrown for parent == null" );
6054 for (int i =0 ; i <10 ; i ++) {
6155 new TreeItem (tree , SWT .NONE );
6256 }
@@ -67,12 +61,7 @@ public void test_ConstructorLorg_eclipse_swt_widgets_TreeI() {
6761
6862@ Test
6963public void test_ConstructorLorg_eclipse_swt_widgets_TreeII () {
70- try {
71- new TreeItem (tree , SWT .NONE , 5 );
72- fail ("No exception thrown for illegal index argument" );
73- }
74- catch (IllegalArgumentException e ) {
75- }
64+ assertThrows (IllegalArgumentException .class , () -> new TreeItem (tree , SWT .NONE , 5 ), "No exception thrown for illegal index argument" );
7665}
7766
7867@ Test
@@ -87,11 +76,7 @@ public void test_ConstructorLorg_eclipse_swt_widgets_TreeItemI() {
8776
8877@ Test
8978public void test_ConstructorLorg_eclipse_swt_widgets_TreeItemII () {
90- try {
91- new TreeItem (treeItem , SWT .NONE , 5 );
92- fail ("No exception thrown for illegal index argument" );
93- }
94- catch (IllegalArgumentException e ) {}
79+ assertThrows (IllegalArgumentException .class , () -> new TreeItem (treeItem , SWT .NONE , 5 ), "No exception thrown for illegal index argument" );
9580 assertEquals (1 , tree .getItemCount ());
9681}
9782
@@ -643,26 +628,11 @@ public void test_getItemI() {
643628
644629 for (int i = 0 ; i < number ; i ++)
645630 assertEquals (items [i ], treeItem .getItem (i ));
646- try {
647- treeItem .getItem (number );
648- fail ("No exception thrown for illegal index argument" );
649- }
650- catch (IllegalArgumentException e ) {
651- }
631+ assertThrows (IllegalArgumentException .class , () -> treeItem .getItem (number ), "No exception thrown for illegal index argument" );
652632
653- try {
654- treeItem .getItem (number +1 );
655- fail ("No exception thrown for illegal index argument" );
656- }
657- catch (IllegalArgumentException e ) {
658- }
633+ assertThrows (IllegalArgumentException .class , () -> treeItem .getItem (number +1 ), "No exception thrown for illegal index argument" );
659634
660- try {
661- treeItem .getItem (-1 );
662- fail ("No exception thrown for illegal index argument" );
663- }
664- catch (IllegalArgumentException e ) {
665- }
635+ assertThrows (IllegalArgumentException .class , () -> treeItem .getItem (-1 ), "No exception thrown for illegal index argument" );
666636}
667637
668638@ Test
@@ -744,13 +714,9 @@ public void test_setBackgroundILorg_eclipse_swt_graphics_Color() {
744714 treeItem .setBackground (null );
745715 assertEquals (tree .getBackground (),treeItem .getBackground (0 ));
746716
747- try {
748- Color color = new Color (255 , 0 , 0 );
749- color .dispose ();
750- treeItem .setBackground (color );
751- fail ("No exception thrown for color disposed" );
752- } catch (IllegalArgumentException e ) {
753- }
717+ Color color = new Color (255 , 0 , 0 );
718+ color .dispose ();
719+ assertThrows (IllegalArgumentException .class , () -> treeItem .setBackground (color ), "No exception thrown for color disposed" );
754720}
755721
756722@ Test
@@ -761,11 +727,7 @@ public void test_setBackgroundLorg_eclipse_swt_graphics_Color() {
761727 treeItem .setBackground (null );
762728 assertEquals (tree .getBackground (),treeItem .getBackground ());
763729 color .dispose ();
764- try {
765- treeItem .setBackground (color );
766- fail ("No exception thrown for color disposed" );
767- } catch (IllegalArgumentException e ) {
768- }
730+ assertThrows (IllegalArgumentException .class , () -> treeItem .setBackground (color ), "No exception thrown for color disposed" );
769731}
770732
771733@ Test
@@ -812,20 +774,15 @@ public void test_setFontLorg_eclipse_swt_graphics_Font() {
812774 treeItem .setFont (font );
813775 assertEquals (treeItem .getFont (), font );
814776
815- font = new Font (treeItem .getDisplay (), SwtTestUtil .testFontName , 10 , SWT .NORMAL );
816- treeItem .setFont (font );
817- assertEquals (treeItem .getFont (), font );
777+ Font font2 = new Font (treeItem .getDisplay (), SwtTestUtil .testFontName , 10 , SWT .NORMAL );
778+ treeItem .setFont (font2 );
779+ assertEquals (treeItem .getFont (), font2 );
818780
819781 treeItem .setFont (null );
820782 assertEquals (treeItem .getFont (), tree .getFont ());
821783
822- font .dispose ();
823- try {
824- treeItem .setFont (font );
825- treeItem .setFont (null );
826- fail ("No exception thrown for disposed font" );
827- } catch (IllegalArgumentException e ) {
828- }
784+ font2 .dispose ();
785+ assertThrows (IllegalArgumentException .class , () -> treeItem .setFont (font2 ), "No exception thrown for disposed font" );
829786}
830787
831788@ Test
@@ -871,12 +828,7 @@ public void test_setFontILorg_eclipse_swt_graphics_Font() {
871828 font .dispose ();
872829 font2 .dispose ();
873830
874- try {
875- treeItem .setFont (0 , font );
876- treeItem .setFont (0 , null );
877- fail ("No exception thrown for disposed font" );
878- } catch (IllegalArgumentException e ) {
879- }
831+ assertThrows (IllegalArgumentException .class , () -> treeItem .setFont (0 , font ), "No exception thrown for disposed font" );
880832}
881833
882834@ Test
@@ -918,13 +870,9 @@ public void test_setForegroundILorg_eclipse_swt_graphics_Color() {
918870 treeItem .setForeground (null );
919871 assertEquals (tree .getForeground (),treeItem .getForeground (0 ));
920872
921- try {
922- Color color = new Color (255 , 0 , 0 );
923- color .dispose ();
924- treeItem .setForeground (color );
925- fail ("No exception thrown for color disposed" );
926- } catch (IllegalArgumentException e ) {
927- }
873+ Color color = new Color (255 , 0 , 0 );
874+ color .dispose ();
875+ assertThrows (IllegalArgumentException .class , () -> treeItem .setForeground (color ), "No exception thrown for color disposed" );
928876}
929877
930878@ Test
@@ -935,11 +883,7 @@ public void test_setForegroundLorg_eclipse_swt_graphics_Color() {
935883 treeItem .setForeground (null );
936884 assertEquals (tree .getForeground (),treeItem .getForeground ());
937885 color .dispose ();
938- try {
939- treeItem .setForeground (color );
940- fail ("No exception thrown for color disposed" );
941- } catch (IllegalArgumentException e ) {
942- }
886+ assertThrows (IllegalArgumentException .class , () -> treeItem .setForeground (color ), "No exception thrown for color disposed" );
943887}
944888
945889@ Test
@@ -984,12 +928,7 @@ public void test_setGrayedZ() {
984928 for (int i = 0 ; i < images .length ; i ++) {
985929 assertEquals (images [i ], treeItem .getImage (i ));
986930 }
987- try {
988- treeItem .setImage ((Image []) null );
989- fail ("No exception thrown for images == null" );
990- }
991- catch (IllegalArgumentException e ) {
992- }
931+ assertThrows (IllegalArgumentException .class , () -> treeItem .setImage ((Image []) null ), "No exception thrown for images == null" );
993932}
994933
995934@ Test
@@ -1025,25 +964,15 @@ public void test_setImageILorg_eclipse_swt_graphics_Image() {
1025964 assertEquals (images [0 ], treeItem .getImage (0 ));
1026965
1027966 images [0 ].dispose ();
1028- try {
1029- treeItem .setImage (0 , images [0 ]);
1030- treeItem .setImage (0 , null );
1031- fail ("No exception thrown for disposed font" );
1032- } catch (IllegalArgumentException e ) {
1033- }
967+ assertThrows (IllegalArgumentException .class , () -> treeItem .setImage (0 , images [0 ]), "No exception thrown for disposed font" );
1034968}
1035969
1036970@ Test
1037971public void test_setText$Ljava_lang_String () {
1038972 final String TestString = "test" ;
1039973 final String TestStrings [] = new String [] {TestString , TestString + "1" , TestString + "2" };
1040974
1041- try {
1042- treeItem .setText ((String []) null );
1043- fail ("No exception thrown for strings == null" );
1044- }
1045- catch (IllegalArgumentException e ) {
1046- }
975+ assertThrows (IllegalArgumentException .class , () -> treeItem .setText ((String []) null ), "No exception thrown for strings == null" );
1047976
1048977 /*
1049978 * Test the getText/setText API with a Tree that has only
@@ -1131,19 +1060,9 @@ public void test_setTextILjava_lang_String(){
11311060 assertEquals (0 , treeItem .getText (-1 ).length ());
11321061
11331062
1134- try {
1135- treeItem .setText (-1 , null );
1136- fail ("No exception thrown for string == null" );
1137- }
1138- catch (IllegalArgumentException e ) {
1139- }
1063+ assertThrows (IllegalArgumentException .class , () -> treeItem .setText (-1 , null ), "No exception thrown for string == null" );
11401064
1141- try {
1142- treeItem .setText (0 , null );
1143- fail ("No exception thrown for string == null" );
1144- }
1145- catch (IllegalArgumentException e ) {
1146- }
1065+ assertThrows (IllegalArgumentException .class , () -> treeItem .setText (0 , null ), "No exception thrown for string == null" );
11471066
11481067 /*
11491068 * Test the getText/setText API with a small table that
0 commit comments