Skip to content

Commit c2f75c4

Browse files
HeikoKlareakurtakov
authored andcommitted
Replace fail() calls in org.eclipse.ui.tests
This replaces JUnit fail() calls in org.eclipse.ui.tests by various means: - Replace exception catching and fail() statements with assertThrows() - Remove exception catching and fail() statements by letting the test methods simply throw the exception
1 parent 0a70451 commit c2f75c4

15 files changed

+211
-449
lines changed

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/api/IEditorRegistryTest.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import static org.junit.Assert.assertNotNull;
2222
import static org.junit.Assert.assertNull;
2323
import static org.junit.Assert.assertTrue;
24-
import static org.junit.Assert.fail;
2524

2625
import java.util.Arrays;
2726
import java.util.List;
@@ -89,13 +88,13 @@ public void setUp() {
8988
}
9089

9190
@After
92-
public void tearDown() {
91+
public void tearDown() throws CoreException {
9392
if (proj != null) {
9493
try {
9594
FileUtil.deleteProject(proj);
9695
} catch (CoreException e) {
9796
TestPlugin.getDefault().getLog().log(e.getStatus());
98-
fail();
97+
throw e;
9998
}
10099
}
101100
}
@@ -360,11 +359,7 @@ public void testRemovePropertyListener() {
360359

361360
// removing the listener that is not registered yet should have no
362361
// effect
363-
try {
364-
fReg.removePropertyListener(listener);
365-
} catch (Throwable e) {
366-
fail();
367-
}
362+
fReg.removePropertyListener(listener);
368363
}
369364

370365
/**

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/api/IWorkbenchWindowTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.tests.api;
1515

16+
import static org.junit.Assert.assertThrows;
17+
1618
import org.eclipse.core.resources.ResourcesPlugin;
1719
import org.eclipse.jface.action.StatusLineManager;
1820
import org.eclipse.swt.custom.CLabel;
@@ -175,13 +177,10 @@ public void testOpenPage2() throws Throwable {
175177
}
176178

177179
// test openPage() fails
178-
try {
179-
page = fWin.openPage("*************", ResourcesPlugin.getWorkspace());
180-
fail();
181-
} catch (WorkbenchException ex) {
182-
}
183-
184-
page.close();
180+
assertThrows(WorkbenchException.class, () -> {
181+
IWorkbenchPage p = fWin.openPage("*************", ResourcesPlugin.getWorkspace());
182+
p.close();
183+
});
185184
}
186185

187186
@Test

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/dynamicplugins/AcceleratorConfigurationsExtensionDynamicTest.java

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.tests.dynamicplugins;
1515

16+
import static org.junit.Assert.assertThrows;
17+
1618
import org.eclipse.core.commands.common.NamedHandleObject;
1719
import org.eclipse.core.commands.common.NotDefinedException;
1820
import org.eclipse.ui.internal.registry.IWorkbenchRegistryConstants;
@@ -72,40 +74,27 @@ protected final String getInstallLocation() {
7274

7375
/**
7476
* Tests whether the items defined in the extension point can be added and
75-
* removed dynamically. It tests that the data doesn't exist, and then loads
76-
* the extension. It tests that the data then exists, and unloads the
77-
* extension. It tests that the data then doesn't exist.
77+
* removed dynamically. It tests that the data doesn't exist, and then loads the
78+
* extension. It tests that the data then exists, and unloads the extension. It
79+
* tests that the data then doesn't exist.
80+
*
81+
* @throws NotDefinedException
7882
*/
7983
@Test
80-
public final void testAcceleratorConfigurations() {
84+
public final void testAcceleratorConfigurations() throws NotDefinedException {
8185
final IBindingService service = getWorkbench().getAdapter(IBindingService.class);
82-
NamedHandleObject namedHandleObject;
8386

84-
namedHandleObject = service.getScheme("monkey");
85-
try {
86-
namedHandleObject.getName();
87-
fail();
88-
} catch (final NotDefinedException e) {
89-
assertTrue(true);
90-
}
87+
NamedHandleObject namedHandleObject1 = service.getScheme("monkey");
88+
assertThrows(NotDefinedException.class, () -> namedHandleObject1.getName());
9189

9290
getBundle();
9391

94-
namedHandleObject = service.getScheme("monkey");
95-
try {
96-
assertTrue("Monkey".equals(namedHandleObject.getName()));
97-
} catch (final NotDefinedException e) {
98-
fail();
99-
}
92+
NamedHandleObject namedHandleObject2 = service.getScheme("monkey");
93+
assertTrue("Monkey".equals(namedHandleObject2.getName()));
10094

10195
removeBundle();
10296

103-
namedHandleObject = service.getScheme("monkey");
104-
try {
105-
namedHandleObject.getName();
106-
fail();
107-
} catch (final NotDefinedException e) {
108-
assertTrue(true);
109-
}
97+
NamedHandleObject namedHandleObject3 = service.getScheme("monkey");
98+
assertThrows(NotDefinedException.class, () -> namedHandleObject3.getName());
11099
}
111100
}

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/dynamicplugins/AcceleratorScopesExtensionDynamicTest.java

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.tests.dynamicplugins;
1515

16+
import static org.junit.Assert.assertThrows;
17+
1618
import org.eclipse.core.commands.common.NamedHandleObject;
1719
import org.eclipse.core.commands.common.NotDefinedException;
1820
import org.eclipse.ui.contexts.IContextService;
@@ -72,40 +74,26 @@ protected final String getInstallLocation() {
7274

7375
/**
7476
* Tests whether the items defined in the extension point can be added and
75-
* removed dynamically. It tests that the data doesn't exist, and then loads
76-
* the extension. It tests that the data then exists, and unloads the
77-
* extension. It tests that the data then doesn't exist.
77+
* removed dynamically. It tests that the data doesn't exist, and then loads the
78+
* extension. It tests that the data then exists, and unloads the extension. It
79+
* tests that the data then doesn't exist.
80+
*
81+
* @throws NotDefinedException
7882
*/
7983
@Test
80-
public final void testAcceleratorScopes() {
84+
public final void testAcceleratorScopes() throws NotDefinedException {
8185
final IContextService service = getWorkbench().getAdapter(IContextService.class);
82-
NamedHandleObject namedHandleObject;
83-
84-
namedHandleObject = service.getContext("monkey");
85-
try {
86-
namedHandleObject.getName();
87-
fail();
88-
} catch (final NotDefinedException e) {
89-
assertTrue(true);
90-
}
86+
NamedHandleObject namedHandleObject1 = service.getContext("monkey");
87+
assertThrows(NotDefinedException.class, () -> namedHandleObject1.getName());
9188

9289
getBundle();
9390

94-
namedHandleObject = service.getContext("monkey");
95-
try {
96-
assertTrue("Monkey".equals(namedHandleObject.getName()));
97-
} catch (final NotDefinedException e) {
98-
fail();
99-
}
91+
NamedHandleObject namedHandleObject2 = service.getContext("monkey");
92+
assertTrue("Monkey".equals(namedHandleObject2.getName()));
10093

10194
removeBundle();
10295

103-
namedHandleObject = service.getContext("monkey");
104-
try {
105-
namedHandleObject.getName();
106-
fail();
107-
} catch (final NotDefinedException e) {
108-
assertTrue(true);
109-
}
96+
NamedHandleObject namedHandleObject3 = service.getContext("monkey");
97+
assertThrows(NotDefinedException.class, () -> namedHandleObject3.getName());
11098
}
11199
}

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/dynamicplugins/ActionDefinitionsExtensionDynamicTest.java

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.tests.dynamicplugins;
1515

16+
import static org.junit.Assert.assertThrows;
17+
1618
import org.eclipse.core.commands.common.NamedHandleObject;
1719
import org.eclipse.core.commands.common.NotDefinedException;
1820
import org.eclipse.ui.commands.ICommandService;
@@ -72,40 +74,27 @@ protected final String getInstallLocation() {
7274

7375
/**
7476
* Tests whether the items defined in the extension point can be added and
75-
* removed dynamically. It tests that the data doesn't exist, and then loads
76-
* the extension. It tests that the data then exists, and unloads the
77-
* extension. It tests that the data then doesn't exist.
77+
* removed dynamically. It tests that the data doesn't exist, and then loads the
78+
* extension. It tests that the data then exists, and unloads the extension. It
79+
* tests that the data then doesn't exist.
80+
*
81+
* @throws NotDefinedException
7882
*/
7983
@Test
80-
public final void testActionDefinitions() {
84+
public final void testActionDefinitions() throws NotDefinedException {
8185
final ICommandService service = getWorkbench().getAdapter(ICommandService.class);
82-
NamedHandleObject namedHandleObject;
8386

84-
namedHandleObject = service.getCommand("monkey");
85-
try {
86-
namedHandleObject.getName();
87-
fail();
88-
} catch (final NotDefinedException e) {
89-
assertTrue(true);
90-
}
87+
NamedHandleObject namedHandleObject1 = service.getCommand("monkey");
88+
assertThrows(NotDefinedException.class, () -> namedHandleObject1.getName());
9189

9290
getBundle();
9391

94-
namedHandleObject = service.getCommand("monkey");
95-
try {
96-
assertTrue("Monkey".equals(namedHandleObject.getName()));
97-
} catch (final NotDefinedException e) {
98-
fail();
99-
}
92+
NamedHandleObject namedHandleObject2 = service.getCommand("monkey");
93+
assertTrue("Monkey".equals(namedHandleObject2.getName()));
10094

10195
removeBundle();
10296

103-
namedHandleObject = service.getCommand("monkey");
104-
try {
105-
namedHandleObject.getName();
106-
fail();
107-
} catch (final NotDefinedException e) {
108-
assertTrue(true);
109-
}
97+
NamedHandleObject namedHandleObject3 = service.getCommand("monkey");
98+
assertThrows(NotDefinedException.class, () -> namedHandleObject3.getName());
11099
}
111100
}

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/dynamicplugins/BindingsExtensionDynamicTest.java

Lines changed: 14 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*******************************************************************************/
1414
package org.eclipse.ui.tests.dynamicplugins;
1515

16+
import static org.junit.Assert.assertThrows;
17+
1618
import org.eclipse.core.commands.common.NotDefinedException;
1719
import org.eclipse.jface.bindings.Binding;
1820
import org.eclipse.jface.bindings.Scheme;
@@ -76,19 +78,18 @@ protected final String getInstallLocation() {
7678

7779
/**
7880
* Tests whether the items defined in the extension point can be added and
79-
* removed dynamically. It tests that the data doesn't exist, and then loads
80-
* the extension. It tests that the data then exists, and unloads the
81-
* extension. It tests that the data then doesn't exist.
81+
* removed dynamically. It tests that the data doesn't exist, and then loads the
82+
* extension. It tests that the data then exists, and unloads the extension. It
83+
* tests that the data then doesn't exist.
8284
*
83-
* @throws ParseException
84-
* If "M1+W" can't be parsed by the extension point.
85+
* @throws ParseException If "M1+W" can't be parsed by the extension point.
86+
* @throws NotDefinedException
8587
*/
8688
@Test
87-
public void testBindings() throws ParseException {
89+
public void testBindings() throws ParseException, NotDefinedException {
8890
final IBindingService bindingService = getWorkbench().getAdapter(IBindingService.class);
8991
final TriggerSequence triggerSequence = KeySequence.getInstance("M1+W");
9092
Binding[] bindings;
91-
Scheme scheme;
9293
boolean found;
9394

9495
found = false;
@@ -113,13 +114,8 @@ public void testBindings() throws ParseException {
113114
}
114115
}
115116
assertTrue(!found);
116-
scheme = bindingService.getScheme("monkey");
117-
try {
118-
scheme.getName();
119-
fail();
120-
} catch (final NotDefinedException e) {
121-
assertTrue(true);
122-
}
117+
Scheme scheme1 = bindingService.getScheme("monkey");
118+
assertThrows(NotDefinedException.class, () -> scheme1.getName());
123119

124120
getBundle();
125121

@@ -145,12 +141,8 @@ public void testBindings() throws ParseException {
145141
}
146142
}
147143
assertTrue(found);
148-
scheme = bindingService.getScheme("monkey");
149-
try {
150-
assertTrue("Monkey".equals(scheme.getName()));
151-
} catch (final NotDefinedException e) {
152-
fail();
153-
}
144+
Scheme scheme2 = bindingService.getScheme("monkey");
145+
assertTrue("Monkey".equals(scheme2.getName()));
154146

155147
removeBundle();
156148

@@ -176,12 +168,7 @@ public void testBindings() throws ParseException {
176168
}
177169
}
178170
assertTrue(!found);
179-
scheme = bindingService.getScheme("monkey");
180-
try {
181-
scheme.getName();
182-
fail();
183-
} catch (final NotDefinedException e) {
184-
assertTrue(true);
185-
}
171+
Scheme scheme3 = bindingService.getScheme("monkey");
172+
assertThrows(NotDefinedException.class, () -> scheme3.getName());
186173
}
187174
}

0 commit comments

Comments
 (0)