1
1
/*******************************************************************************
2
- * Copyright (c) 2000, 2016 IBM Corporation and others.
2
+ * Copyright (c) 2000, 2025 IBM Corporation and others.
3
3
*
4
4
* This program and the accompanying materials
5
5
* are made available under the terms of the Eclipse Public License 2.0
17
17
import static org .junit .Assert .assertFalse ;
18
18
import static org .junit .Assert .assertNotNull ;
19
19
import static org .junit .Assert .assertNull ;
20
+ import static org .junit .Assert .assertThrows ;
20
21
import static org .junit .Assert .assertTrue ;
21
- import static org .junit .Assert .fail ;
22
22
23
23
import org .eclipse .swt .SWT ;
24
24
import org .eclipse .swt .events .ArmListener ;
@@ -52,12 +52,7 @@ public void test_ConstructorLorg_eclipse_swt_widgets_MenuI() {
52
52
MenuItem mItem = new MenuItem (menu , SWT .NULL );
53
53
assertNotNull (mItem );
54
54
55
- try {
56
- new MenuItem (null , SWT .NULL );
57
- fail ("No exception thrown" );
58
- }
59
- catch (IllegalArgumentException e ) {
60
- }
55
+ assertThrows (IllegalArgumentException .class , () -> new MenuItem (null , SWT .NULL ));
61
56
mItem = new MenuItem (menu , SWT .CHECK );
62
57
assertEquals (SWT .CHECK , mItem .getStyle ());
63
58
mItem .dispose ();
@@ -90,21 +85,15 @@ public void test_addArmListenerLorg_eclipse_swt_events_ArmListener() {
90
85
listenerCalled = false ;
91
86
ArmListener listener = e -> listenerCalled = true ;
92
87
93
- try {
94
- menuItem .addArmListener (null );
95
- fail ("No exception thrown for addArmListener with null argument" );
96
- } catch (IllegalArgumentException e ) {
97
- }
88
+ assertThrows ("No exception thrown for addArmListener with null argument" , IllegalArgumentException .class ,
89
+ () -> menuItem .addArmListener (null ));
98
90
99
91
menuItem .addArmListener (listener );
100
92
menuItem .notifyListeners (SWT .Arm , new Event ());
101
93
assertTrue (listenerCalled );
102
94
103
- try {
104
- menuItem .removeArmListener (null );
105
- fail ("No exception thrown for removeArmListener with null argument" );
106
- } catch (IllegalArgumentException e ) {
107
- }
95
+ assertThrows ("No exception thrown for removeArmListener with null argument" , IllegalArgumentException .class ,
96
+ () -> menuItem .removeArmListener (null ));
108
97
listenerCalled = false ;
109
98
menuItem .removeArmListener (listener );
110
99
menuItem .notifyListeners (SWT .Arm , new Event ());
@@ -116,21 +105,15 @@ public void test_addHelpListenerLorg_eclipse_swt_events_HelpListener() {
116
105
listenerCalled = false ;
117
106
HelpListener listener = e -> listenerCalled = true ;
118
107
119
- try {
120
- menuItem .addHelpListener (null );
121
- fail ("No exception thrown for addHelpListener with null argument" );
122
- } catch (IllegalArgumentException e ) {
123
- }
108
+ assertThrows ("No exception thrown for addHelpListener with null argument" , IllegalArgumentException .class ,
109
+ () -> menuItem .addHelpListener (null ));
124
110
125
111
menuItem .addHelpListener (listener );
126
112
menuItem .notifyListeners (SWT .Help , new Event ());
127
113
assertTrue (listenerCalled );
128
114
129
- try {
130
- menuItem .removeHelpListener (null );
131
- fail ("No exception thrown for removeHelpListener with null argument" );
132
- } catch (IllegalArgumentException e ) {
133
- }
115
+ assertThrows ("No exception thrown for removeHelpListener with null argument" , IllegalArgumentException .class ,
116
+ () -> menuItem .removeHelpListener (null ));
134
117
listenerCalled = false ;
135
118
menuItem .removeHelpListener (listener );
136
119
menuItem .notifyListeners (SWT .Help , new Event ());
@@ -150,21 +133,15 @@ public void widgetDefaultSelected(SelectionEvent e) {
150
133
}
151
134
};
152
135
153
- try {
154
- menuItem .addSelectionListener (null );
155
- fail ("No exception thrown for addSelectionListener with null argument" );
156
- } catch (IllegalArgumentException e ) {
157
- }
136
+ assertThrows ("No exception thrown for addSelectionListener with null argument" , IllegalArgumentException .class ,
137
+ () -> menuItem .addSelectionListener (null ));
158
138
159
139
menuItem .addSelectionListener (listener );
160
140
menuItem .notifyListeners (SWT .Selection , new Event ());
161
141
assertTrue (listenerCalled );
162
142
163
- try {
164
- menuItem .removeSelectionListener (null );
165
- fail ("No exception thrown for removeSelectionListener with null argument" );
166
- } catch (IllegalArgumentException e ) {
167
- }
143
+ assertThrows ("No exception thrown for removeSelectionListener with null argument" , IllegalArgumentException .class ,
144
+ () -> menuItem .removeSelectionListener (null ));
168
145
listenerCalled = false ;
169
146
menuItem .removeSelectionListener (listener );
170
147
menuItem .notifyListeners (SWT .Selection , new Event ());
@@ -206,7 +183,7 @@ public void test_isEnabled() {
206
183
menuItem .setEnabled (true );
207
184
assertTrue (menuItem .isEnabled ());
208
185
menuItem .setEnabled (false );
209
- assertEquals (menuItem .isEnabled (), false );
186
+ assertFalse (menuItem .isEnabled ());
210
187
}
211
188
212
189
@ Test
@@ -220,7 +197,7 @@ public void test_setEnabledZ() {
220
197
menuItem .setEnabled (true );
221
198
assertTrue (menuItem .getEnabled ());
222
199
menuItem .setEnabled (false );
223
- assertEquals (menuItem .getEnabled (), false );
200
+ assertFalse (menuItem .getEnabled ());
224
201
}
225
202
226
203
@ Override
@@ -250,7 +227,7 @@ public void test_setSelectionZ() {
250
227
for (int itemStyle : itemStyles ) {
251
228
MenuItem mItem = new MenuItem (menu , itemStyle );
252
229
mItem .setSelection (false );
253
- assertEquals (mItem .getSelection (), false );
230
+ assertFalse (mItem .getSelection ());
254
231
mItem .setSelection (true );
255
232
assertTrue (mItem .getSelection ());
256
233
mItem .dispose ();
@@ -262,11 +239,7 @@ public void test_setSelectionZ() {
262
239
public void test_setTextLjava_lang_String () {
263
240
menuItem .setText ("ABCDEFG" );
264
241
assertEquals ("ABCDEFG" , menuItem .getText ());
265
- try {
266
- menuItem .setText (null );
267
- fail ("No exception thrown for addArmListener with null argument" );
268
- } catch (IllegalArgumentException e ) {
269
- }
242
+ assertThrows (IllegalArgumentException .class , () -> menuItem .setText (null ));
270
243
menuItem .setText ("ABCDEFG" );
271
244
menuItem .setAccelerator (SWT .MOD1 + 'A' );
272
245
assertTrue (menuItem .getText ().startsWith ("ABCDEFG" ));
0 commit comments