From 8ef40990a2a7a6832b3d9cd939ecd66a42d600a1 Mon Sep 17 00:00:00 2001 From: Aleksandar Kurtakov Date: Mon, 16 Jun 2025 16:27:18 +0300 Subject: [PATCH] Simplify MenuItem tests assertions Use assertThrows and assertFalse to make things more obvious. --- .../Test_org_eclipse_swt_widgets_Item.java | 11 +--- ...Test_org_eclipse_swt_widgets_MenuItem.java | 65 ++++++------------- 2 files changed, 22 insertions(+), 54 deletions(-) diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Item.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Item.java index 52272a0921f..f5b76eda131 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Item.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_Item.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2016 IBM Corporation and others. + * Copyright (c) 2000, 2025 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -15,8 +15,8 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.io.IOException; import java.io.InputStream; @@ -66,12 +66,7 @@ public void test_setTextLjava_lang_String() { assertEquals("a", testStr, item.getText()); item.setText(""); assertTrue("b", item.getText().isEmpty()); - try { - item.setText(null); - fail("No exception thrown for string == null"); - } - catch (IllegalArgumentException e) { - } + assertThrows("No exception thrown for string == null", IllegalArgumentException.class, () -> item.setText(null)); } /* custom */ diff --git a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_MenuItem.java b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_MenuItem.java index ba2fa405b1e..e91141eb752 100644 --- a/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_MenuItem.java +++ b/tests/org.eclipse.swt.tests/JUnit Tests/org/eclipse/swt/tests/junit/Test_org_eclipse_swt_widgets_MenuItem.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2016 IBM Corporation and others. + * Copyright (c) 2000, 2025 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -17,8 +17,8 @@ import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import org.eclipse.swt.SWT; import org.eclipse.swt.events.ArmListener; @@ -52,12 +52,7 @@ public void test_ConstructorLorg_eclipse_swt_widgets_MenuI() { MenuItem mItem = new MenuItem(menu, SWT.NULL); assertNotNull(mItem); - try { - new MenuItem(null, SWT.NULL); - fail("No exception thrown"); - } - catch (IllegalArgumentException e) { - } + assertThrows(IllegalArgumentException.class, () -> new MenuItem(null, SWT.NULL)); mItem = new MenuItem(menu, SWT.CHECK); assertEquals(SWT.CHECK, mItem.getStyle()); mItem.dispose(); @@ -90,21 +85,15 @@ public void test_addArmListenerLorg_eclipse_swt_events_ArmListener() { listenerCalled = false; ArmListener listener = e -> listenerCalled = true; - try { - menuItem.addArmListener(null); - fail("No exception thrown for addArmListener with null argument"); - } catch (IllegalArgumentException e) { - } + assertThrows("No exception thrown for addArmListener with null argument", IllegalArgumentException.class, + () -> menuItem.addArmListener(null)); menuItem.addArmListener(listener); menuItem.notifyListeners(SWT.Arm, new Event()); assertTrue(listenerCalled); - try { - menuItem.removeArmListener(null); - fail("No exception thrown for removeArmListener with null argument"); - } catch (IllegalArgumentException e) { - } + assertThrows("No exception thrown for removeArmListener with null argument", IllegalArgumentException.class, + () -> menuItem.removeArmListener(null)); listenerCalled = false; menuItem.removeArmListener(listener); menuItem.notifyListeners(SWT.Arm, new Event()); @@ -116,21 +105,15 @@ public void test_addHelpListenerLorg_eclipse_swt_events_HelpListener() { listenerCalled = false; HelpListener listener = e -> listenerCalled = true; - try { - menuItem.addHelpListener(null); - fail("No exception thrown for addHelpListener with null argument"); - } catch (IllegalArgumentException e) { - } + assertThrows("No exception thrown for addHelpListener with null argument", IllegalArgumentException.class, + () -> menuItem.addHelpListener(null)); menuItem.addHelpListener(listener); menuItem.notifyListeners(SWT.Help, new Event()); assertTrue(listenerCalled); - try { - menuItem.removeHelpListener(null); - fail("No exception thrown for removeHelpListener with null argument"); - } catch (IllegalArgumentException e) { - } + assertThrows("No exception thrown for removeHelpListener with null argument", IllegalArgumentException.class, + () -> menuItem.removeHelpListener(null)); listenerCalled = false; menuItem.removeHelpListener(listener); menuItem.notifyListeners(SWT.Help, new Event()); @@ -150,21 +133,15 @@ public void widgetDefaultSelected(SelectionEvent e) { } }; - try { - menuItem.addSelectionListener(null); - fail("No exception thrown for addSelectionListener with null argument"); - } catch (IllegalArgumentException e) { - } + assertThrows("No exception thrown for addSelectionListener with null argument", IllegalArgumentException.class, + () -> menuItem.addSelectionListener(null)); menuItem.addSelectionListener(listener); menuItem.notifyListeners(SWT.Selection, new Event()); assertTrue(listenerCalled); - try { - menuItem.removeSelectionListener(null); - fail("No exception thrown for removeSelectionListener with null argument"); - } catch (IllegalArgumentException e) { - } + assertThrows("No exception thrown for removeSelectionListener with null argument", IllegalArgumentException.class, + () -> menuItem.removeSelectionListener(null)); listenerCalled = false; menuItem.removeSelectionListener(listener); menuItem.notifyListeners(SWT.Selection, new Event()); @@ -206,7 +183,7 @@ public void test_isEnabled() { menuItem.setEnabled(true); assertTrue(menuItem.isEnabled()); menuItem.setEnabled(false); - assertEquals(menuItem.isEnabled(), false); + assertFalse(menuItem.isEnabled()); } @Test @@ -220,7 +197,7 @@ public void test_setEnabledZ() { menuItem.setEnabled(true); assertTrue(menuItem.getEnabled()); menuItem.setEnabled(false); - assertEquals(menuItem.getEnabled(), false); + assertFalse(menuItem.getEnabled()); } @Override @@ -250,7 +227,7 @@ public void test_setSelectionZ() { for (int itemStyle : itemStyles) { MenuItem mItem = new MenuItem(menu, itemStyle); mItem.setSelection(false); - assertEquals(mItem.getSelection(), false); + assertFalse(mItem.getSelection()); mItem.setSelection(true); assertTrue(mItem.getSelection()); mItem.dispose(); @@ -262,11 +239,7 @@ public void test_setSelectionZ() { public void test_setTextLjava_lang_String() { menuItem.setText("ABCDEFG"); assertEquals("ABCDEFG", menuItem.getText()); - try { - menuItem.setText(null); - fail("No exception thrown for addArmListener with null argument"); - } catch (IllegalArgumentException e) { - } + assertThrows(IllegalArgumentException.class, () -> menuItem.setText(null)); menuItem.setText("ABCDEFG"); menuItem.setAccelerator(SWT.MOD1 + 'A'); assertTrue(menuItem.getText().startsWith("ABCDEFG"));