Skip to content

Commit ddeb49e

Browse files
HeikoKlareakurtakov
authored andcommitted
Replace try-catch with assertThrows in org.eclipse.ui.tests
Replaces several usages of try-catch statements to validate if an exception was thrown by assertThrows statements.
1 parent f1bbc96 commit ddeb49e

File tree

12 files changed

+63
-205
lines changed

12 files changed

+63
-205
lines changed

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
package org.eclipse.ui.tests.api;
1515

1616
import static org.junit.Assert.assertNotNull;
17-
import static org.junit.Assert.fail;
17+
import static org.junit.Assert.assertThrows;
1818

1919
import org.eclipse.core.runtime.CoreException;
2020
import org.eclipse.ui.internal.WorkbenchPlugin;
@@ -31,14 +31,8 @@ public class Bug42616Test {
3131

3232
@Test
3333
public void testErrorCondition() {
34-
try {
35-
WorkbenchPlugin.createExtension(null, null);
36-
fail("createExtension with nulls succeeded");
37-
} catch (CoreException e) {
38-
// ensure that exception has a root cause.
39-
assertNotNull("Cause is null", e.getStatus().getException());
40-
} catch (Throwable t) {
41-
fail("Throwable not wrapped in core exception.");
42-
}
34+
CoreException e = assertThrows(CoreException.class, () -> WorkbenchPlugin.createExtension(null, null));
35+
// ensure that exception has a root cause.
36+
assertNotNull("Cause is null", e.getStatus().getException());
4337
}
4438
}

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package org.eclipse.ui.tests.api;
1616

1717
import static org.junit.Assert.assertArrayEquals;
18+
import static org.junit.Assert.assertThrows;
1819

1920
import java.util.Arrays;
2021

@@ -301,11 +302,8 @@ public void testGetWorkingSets() throws Throwable {
301302
fWorkingSetManager.addWorkingSet(fWorkingSet);
302303
assertArrayEquals(new IWorkingSet[] { fWorkingSet }, fWorkingSetManager.getWorkingSets());
303304

304-
try {
305-
fWorkingSetManager.addWorkingSet(fWorkingSet);
306-
fail("Added the same set twice");
307-
} catch (RuntimeException exception) {
308-
}
305+
assertThrows("added same set twice", RuntimeException.class,
306+
() -> fWorkingSetManager.addWorkingSet(fWorkingSet));
309307
assertArrayEquals(new IWorkingSet[] { fWorkingSet }, fWorkingSetManager.getWorkingSets());
310308

311309
IWorkingSet workingSet2 = fWorkingSetManager.createWorkingSet(

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

Lines changed: 10 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
import static org.junit.Assert.assertEquals;
1717
import static org.junit.Assert.assertNotNull;
18-
import static org.junit.Assert.fail;
18+
import static org.junit.Assert.assertThrows;
1919

2020
import java.io.IOException;
2121
import java.io.Reader;
@@ -51,20 +51,10 @@ public class XMLMementoTest {
5151
*/
5252
@Test
5353
public void testCreateReadRootReaderExceptionCases() {
54-
try {
55-
XMLMemento.createReadRoot(new StringReader("Invalid format"));
56-
fail("should throw WorkbenchException because of invalid format");
57-
} catch (WorkbenchException e) {
58-
// expected
59-
}
60-
try {
61-
XMLMemento.createReadRoot(new StringReader(
62-
"<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"));
63-
fail("should throw WorkbenchException because there is no element");
64-
} catch (WorkbenchException e) {
65-
// expected
66-
}
67-
try {
54+
assertThrows(WorkbenchException.class, () -> XMLMemento.createReadRoot(new StringReader("Invalid format")));
55+
assertThrows("no exception even though there is noe element", WorkbenchException.class,
56+
() -> XMLMemento.createReadRoot(new StringReader("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>")));
57+
assertThrows(WorkbenchException.class, () ->
6858
XMLMemento.createReadRoot(new Reader() {
6959

7060
@Override
@@ -77,11 +67,7 @@ public int read(char[] arg0, int arg1, int arg2)
7767
throws IOException {
7868
throw new IOException();
7969
}
80-
});
81-
fail("should throw WorkbenchException because of IOException");
82-
} catch (WorkbenchException e) {
83-
// expected
84-
}
70+
}));
8571
}
8672

8773
@Test
@@ -113,29 +99,14 @@ public void testCreateWriteRoot() {
11399

114100
@Test
115101
public void testSpacesInRootAreIllegal() {
116-
try {
117-
XMLMemento.createWriteRoot("with space");
118-
fail("should fail");
119-
} catch (Exception e) {
120-
// expected
121-
}
102+
assertThrows(Exception.class, () -> XMLMemento.createWriteRoot("with space"));
122103
}
123104

124105
@Test
125106
public void testSpacesInKeysAreIllegal() {
126107
XMLMemento memento = XMLMemento.createWriteRoot("foo");
127-
try {
128-
memento.createChild("with space", "bar");
129-
fail("should fail");
130-
} catch (Exception e) {
131-
// expected
132-
}
133-
try {
134-
memento.putString("with space", "bar");
135-
fail("should fail");
136-
} catch (Exception e) {
137-
// expected
138-
}
108+
assertThrows(Exception.class, () -> memento.createChild("with space", "bar"));
109+
assertThrows(Exception.class, () -> memento.putString("with space", "bar"));
139110
}
140111

141112
@Test
@@ -508,12 +479,7 @@ public void testIllegalKeys() {
508479

509480
for (final String key : illegalKeys) {
510481
XMLMemento memento = XMLMemento.createWriteRoot("foo");
511-
try {
512-
memento.putString(key, "some string");
513-
fail("putString with illegal key should fail");
514-
} catch (Exception ex) {
515-
// expected
516-
}
482+
assertThrows("should fail with illegal key", Exception.class, () -> memento.putString(key, "some string"));
517483
}
518484
}
519485

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/commands/ActionDelegateProxyTest.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
import static org.junit.Assert.assertFalse;
1919
import static org.junit.Assert.assertNotNull;
2020
import static org.junit.Assert.assertNull;
21+
import static org.junit.Assert.assertThrows;
2122
import static org.junit.Assert.assertTrue;
22-
import static org.junit.Assert.fail;
2323

2424
import java.io.ByteArrayInputStream;
2525
import java.io.InputStream;
@@ -104,12 +104,7 @@ public void testEditorActionDelegate() throws Exception {
104104
IHandlerService service = window.getService(IHandlerService.class);
105105
assertFalse(EditorActionDelegate.executed);
106106
EditorActionDelegate.part = null;
107-
try {
108-
service.executeCommand(STAY_COMMAND, null);
109-
fail("the command is not yet handled");
110-
} catch (NotHandledException e) {
111-
// good
112-
}
107+
assertThrows(NotHandledException.class, () -> service.executeCommand(STAY_COMMAND, null));
113108
assertFalse(EditorActionDelegate.executed);
114109
assertNull(EditorActionDelegate.part);
115110

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/commands/CommandCallbackTest.java

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
package org.eclipse.ui.tests.commands;
1717

18+
import static org.junit.Assert.assertThrows;
19+
1820
import java.util.HashMap;
1921
import java.util.Map;
2022

@@ -156,16 +158,8 @@ public void setTooltip(String text) {
156158
public void testNoParametersNoCallbacks() throws Exception {
157159
ParameterizedCommand pc1 = new ParameterizedCommand(cmd1, null);
158160
ParameterizedCommand pc2 = new ParameterizedCommand(cmd1, null);
159-
try {
160-
commandService.registerElementForCommand(pc1, null);
161-
fail("Callback should not register");
162-
} catch (NotDefinedException e) {
163-
}
164-
try {
165-
commandService.registerElementForCommand(pc2, null);
166-
fail("Callback 2 should not register");
167-
} catch (NotDefinedException e) {
168-
}
161+
assertThrows(NotDefinedException.class, () -> commandService.registerElementForCommand(pc1, null));
162+
assertThrows(NotDefinedException.class, () -> commandService.registerElementForCommand(pc2, null));
169163

170164
commandService.refreshElements(CMD1_ID + ".1", null);
171165
assertEquals(0, cmd1Handler.callbacks);

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/commands/CommandParameterTypeTest.java

Lines changed: 15 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
import static org.junit.Assert.assertEquals;
1818
import static org.junit.Assert.assertFalse;
1919
import static org.junit.Assert.assertNotNull;
20+
import static org.junit.Assert.assertThrows;
2021
import static org.junit.Assert.assertTrue;
21-
import static org.junit.Assert.fail;
22+
23+
import java.util.concurrent.atomic.AtomicReference;
2224

2325
import org.eclipse.core.commands.Command;
2426
import org.eclipse.core.commands.IParameter;
@@ -61,17 +63,8 @@ public void testSubtract() throws CommandException {
6163
*/
6264
@Test
6365
public void testSubtractTypeError() {
64-
try {
65-
// try to pass a Boolean instead of an Integer
66-
testSubtract(Integer.valueOf(3), Boolean.FALSE, 3);
67-
fail("expected ParameterValueConversionException");
68-
}
69-
catch (ParameterValueConversionException ex) {
70-
// passed
71-
}
72-
catch (Exception ex) {
73-
fail("expected ParameterValueConversionException");
74-
}
66+
// try to pass a Boolean instead of an Integer
67+
assertThrows(ParameterValueConversionException.class, () -> testSubtract(Integer.valueOf(3), Boolean.FALSE, 3));
7568
}
7669

7770
/**
@@ -127,22 +120,14 @@ private void testConvertStringToInteger(String value, int expected,
127120
ICommandService commandService = getCommandService();
128121
ParameterType type = commandService.getParameterType(TYPE);
129122

130-
Object converted = null;
123+
AtomicReference<Object> converted = new AtomicReference<>();
131124
if (expectFail) {
132-
try {
133-
converted = type.getValueConverter().convertToObject(value);
134-
fail("expected ParameterValueConversionException");
135-
} catch (ParameterValueConversionException ex) {
136-
// passed
137-
return;
138-
} catch (Exception ex) {
139-
fail("expected ParameterValueConversionException");
140-
}
125+
assertThrows(ParameterValueConversionException.class,
126+
() -> converted.set(type.getValueConverter().convertToObject(value)));
141127
} else {
142-
converted = type.getValueConverter().convertToObject(value);
128+
converted.set(type.getValueConverter().convertToObject(value));
129+
assertEquals(Integer.valueOf(expected), converted.get());
143130
}
144-
145-
assertEquals(Integer.valueOf(expected), converted);
146131
}
147132

148133
/**
@@ -163,21 +148,14 @@ private void testConvertIntegerToString(Object value, String expected,
163148
ICommandService commandService = getCommandService();
164149
ParameterType type = commandService.getParameterType(TYPE);
165150

166-
String converted = null;
151+
AtomicReference<Object> converted = new AtomicReference<>();
167152
if (expectFail) {
168-
try {
169-
converted = type.getValueConverter().convertToString(value);
170-
fail("expected ParameterValueConversionException");
171-
} catch (ParameterValueConversionException ex) {
172-
// passed
173-
return;
174-
} catch (Exception ex) {
175-
fail("expected ParameterValueConversionException");
176-
}
153+
assertThrows(ParameterValueConversionException.class,
154+
() -> converted.set(type.getValueConverter().convertToString(value)));
177155
} else {
178-
converted = type.getValueConverter().convertToString(value);
156+
converted.set(type.getValueConverter().convertToString(value));
157+
assertEquals(expected, converted.get());
179158
}
180-
assertEquals(expected, converted);
181159
}
182160

183161
/**

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/commands/CommandSerializationTest.java

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
import static org.junit.Assert.assertEquals;
1717
import static org.junit.Assert.assertNotNull;
18+
import static org.junit.Assert.assertThrows;
1819
import static org.junit.Assert.assertTrue;
19-
import static org.junit.Assert.fail;
2020

2121
import java.util.Map;
2222

@@ -293,27 +293,13 @@ private void testDeserializeAndSerialize(
293293
private void expectSerializationException(String serializedParameterizedCommand) {
294294
ICommandService commandService = getCommandService();
295295

296-
try {
297-
commandService.deserialize(serializedParameterizedCommand);
298-
fail("expected SerializationException");
299-
} catch (SerializationException ex) {
300-
// passed
301-
} catch (NotDefinedException ex) {
302-
fail("expected SerializationException");
303-
}
296+
assertThrows(SerializationException.class, () -> commandService.deserialize(serializedParameterizedCommand));
304297
}
305298

306299
private void expectNotDefinedException(String serializedParameterizedCommand) {
307300
ICommandService commandService = getCommandService();
308301

309-
try {
310-
commandService.deserialize(serializedParameterizedCommand);
311-
fail("expected NotDefinedException");
312-
} catch (SerializationException ex) {
313-
fail("expected NotDefinedException");
314-
} catch (NotDefinedException ex) {
315-
// passed
316-
}
302+
assertThrows(NotDefinedException.class, () -> commandService.deserialize(serializedParameterizedCommand));
317303
}
318304

319305
private ICommandService getCommandService() {

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/commands/HandlerActivationTest.java

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
package org.eclipse.ui.tests.commands;
1616

17+
import static org.junit.Assert.assertThrows;
18+
1719
import java.util.HashMap;
1820
import java.util.Map;
1921

@@ -240,15 +242,8 @@ private void makeHandler(String handler, String context,
240242

241243
@Test
242244
public void testExceptionThrowingHandler(){
243-
244-
try {
245-
handlerService.executeCommand("org.eclipse.ui.tests.command.handlerException", null);
246-
fail("An exception should be thrown for this handler");
247-
} catch (Exception e) {
248-
if(!(e instanceof ExecutionException)) {
249-
fail("Unexpected exception while executing command", e);
250-
}
251-
}
245+
assertThrows(ExecutionException.class,
246+
() -> handlerService.executeCommand("org.eclipse.ui.tests.command.handlerException", null));
252247
}
253248

254249

@@ -419,24 +414,14 @@ public void testLocalContext() throws Exception {
419414
CMD_ID, handler));
420415
Command cmd = commandService.getCommand(CMD_ID);
421416
ParameterizedCommand pcmd = new ParameterizedCommand(cmd, null);
422-
try {
423-
handlerService.executeCommand(pcmd, null);
424-
fail("this should not be executable");
425-
} catch (NotEnabledException e) {
426-
// good
427-
}
417+
assertThrows(NotEnabledException.class, () -> handlerService.executeCommand(pcmd, null));
428418
assertFalse(cmd.isEnabled());
429419
window.getActivePage().showView(IPageLayout.ID_OUTLINE);
430420
IEvaluationContext outlineContext = handlerService.createContextSnapshot(false);
431421
handlerService.executeCommand(pcmd, null);
432422
assertTrue(cmd.isEnabled());
433423

434-
try {
435-
handlerService.executeCommandInContext(pcmd, null, oldContext);
436-
fail("this should not be executable");
437-
} catch (NotEnabledException e) {
438-
// good
439-
}
424+
assertThrows(NotEnabledException.class, () -> handlerService.executeCommandInContext(pcmd, null, oldContext));
440425

441426
assertTrue(cmd.isEnabled());
442427
handlerService.executeCommandInContext(pcmd, null, outlineContext);

tests/org.eclipse.ui.tests/Eclipse UI Tests/org/eclipse/ui/tests/concurrency/ModalContextCrashTest.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
*******************************************************************************/
1515
package org.eclipse.ui.tests.concurrency;
1616

17+
import static org.junit.Assert.assertThrows;
1718
import static org.junit.Assert.fail;
1819

1920
import java.lang.reflect.InvocationTargetException;
@@ -33,13 +34,8 @@ public class ModalContextCrashTest {
3334
@Test
3435
public void testCrash() throws Exception {
3536
IRunnableWithProgress operation = new CrashingRunnable();
36-
try{
37-
PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(true, false, operation);
38-
fail("Should have an invocation target exception");
39-
}
40-
catch (InvocationTargetException e){
41-
//We should get this
42-
}
37+
assertThrows(InvocationTargetException.class,
38+
() -> PlatformUI.getWorkbench().getActiveWorkbenchWindow().run(true, false, operation));
4339
if (Thread.interrupted()) {
4440
fail("Thread was interrupted at end of test");
4541
}

0 commit comments

Comments
 (0)