Skip to content

Commit 2c05fab

Browse files
HeikoKlareakurtakov
authored andcommitted
Migrate try-catch-fail patterns to assertThrows in tests
Replace legacy try { ...; fail(...); } catch (XException e) {} patterns with JUnit 5 assertThrows(). Also fix a latent test bug: offsets[offsets.length] always threw ArrayIndexOutOfBoundsException before getLineInformationOfOffset was called; replaced with a properly computed out-of-bounds offset.
1 parent 6481ecf commit 2c05fab

15 files changed

Lines changed: 96 additions & 339 deletions

File tree

tests/org.eclipse.e4.ui.tests.css.core/META-INF/MANIFEST.MF

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Export-Package: org.eclipse.e4.ui.tests.css.core;x-internal:=true,
1515
org.eclipse.e4.ui.tests.css.core.util;x-internal:=true
1616
Automatic-Module-Name: org.eclipse.e4.ui.tests.css.core
1717
Import-Package: org.junit.jupiter.api;version="[5.14.0,6.0.0)",
18+
org.junit.jupiter.api.function;version="[5.14.0,6.0.0)",
1819
org.junit.platform.suite.api;version="[1.14.0,2.0.0)",
1920
org.w3c.css.sac;version="1.3.0"
2021
Bundle-Vendor: %Bundle-Vendor

tests/org.eclipse.e4.ui.tests.css.core/src/org/eclipse/e4/ui/tests/css/core/parser/SelectorTest.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@
1616

1717
import static org.junit.jupiter.api.Assertions.assertEquals;
1818
import static org.junit.jupiter.api.Assertions.assertNotNull;
19-
import static org.junit.jupiter.api.Assertions.fail;
20-
21-
import java.io.IOException;
19+
import static org.junit.jupiter.api.Assertions.assertThrows;
2220

2321
import org.eclipse.e4.ui.css.core.engine.CSSEngine;
2422
import org.eclipse.e4.ui.tests.css.core.util.ParserTestUtil;
@@ -69,12 +67,7 @@ void testAttributeSelector() throws Exception {
6967
}
7068

7169
@Test
72-
void testErrorAttributeSelector() throws IOException {
73-
try {
74-
engine.parseSelectors("*[class='Class1'"); // missing ']'
75-
fail("Parser should have errored on missing bracket");
76-
} catch (CSSParseException e) {
77-
// ignore
78-
}
70+
void testErrorAttributeSelector() {
71+
assertThrows(CSSParseException.class, () -> engine.parseSelectors("*[class='Class1'")); // missing ']'
7972
}
8073
}

tests/org.eclipse.e4.ui.tests/META-INF/MANIFEST.MF

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ Import-Package: jakarta.annotation,
3939
jakarta.inject,
4040
org.osgi.service.event,
4141
org.junit.jupiter.api;version="[5.14.0,6.0.0)",
42+
org.junit.jupiter.api.function;version="[5.14.0,6.0.0)",
4243
org.junit.jupiter.api.extension;version="[5.14.0,6.0.0)",
4344
org.junit.platform.suite.api;version="[1.14.0,2.0.0)",
4445
org.opentest4j

tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/tests/application/EModelServiceFindTest.java

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@
1414
******************************************************************************/
1515
package org.eclipse.e4.ui.tests.application;
1616

17-
import static org.junit.jupiter.api.Assertions.assertEquals;
1817
import static org.junit.jupiter.api.Assertions.assertNotNull;
1918
import static org.junit.jupiter.api.Assertions.assertNull;
2019
import static org.junit.jupiter.api.Assertions.assertSame;
21-
import static org.junit.jupiter.api.Assertions.fail;
20+
import static org.junit.jupiter.api.Assertions.assertEquals;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
2222

2323
import java.util.ArrayList;
2424
import java.util.List;
@@ -259,26 +259,9 @@ public void testFindElements_NullCheck() {
259259
EModelService modelService = application.getContext().get(EModelService.class);
260260
assertNotNull(modelService);
261261

262-
try {
263-
modelService.find("a", null);
264-
fail("An exception should have prevented a null parameter to find(*)");
265-
} catch (IllegalArgumentException e) {
266-
// expected
267-
}
268-
269-
try {
270-
modelService.findElements(null, null, null);
271-
fail("An exception should have prevented a null parameter to findElements(*)");
272-
} catch (IllegalArgumentException e) {
273-
// expected
274-
}
275-
276-
try {
277-
modelService.findElements(null, null, null, null, EModelService.ANYWHERE);
278-
fail("An exception should have prevented a null parameter to findElements(*)");
279-
} catch (IllegalArgumentException e) {
280-
// expected
281-
}
262+
assertThrows(IllegalArgumentException.class, () -> modelService.find("a", null));
263+
assertThrows(IllegalArgumentException.class, () -> modelService.findElements(null, null, null));
264+
assertThrows(IllegalArgumentException.class, () -> modelService.findElements(null, null, null, null, EModelService.ANYWHERE));
282265
}
283266

284267
@Test

tests/org.eclipse.e4.ui.tests/src/org/eclipse/e4/ui/tests/application/ModelRobustnessTest.java

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
import static org.junit.jupiter.api.Assertions.assertEquals;
1818
import static org.junit.jupiter.api.Assertions.assertNotNull;
19-
import static org.junit.jupiter.api.Assertions.fail;
19+
import static org.junit.jupiter.api.Assertions.assertThrows;
2020

2121
import java.util.List;
2222
import org.eclipse.e4.ui.internal.workbench.E4XMIResource;
@@ -41,12 +41,8 @@ public void testLoadingInvalidContainments() {
4141
ResourceSet set = new ResourceSetImpl();
4242
Resource resource = null;
4343

44-
try {
45-
resource = set.getResource(uri, true);
46-
fail("This should have thrown an exception");
47-
} catch (Exception e) {
48-
resource = set.getResource(uri, false);
49-
}
44+
assertThrows(Exception.class, () -> set.getResource(uri, true));
45+
resource = set.getResource(uri, false);
5046

5147
assertNotNull(resource);
5248
assertEquals(E4XMIResource.class, resource.getClass());
@@ -75,17 +71,10 @@ public void testAddingInvalidElements() {
7571
MApplication app = MApplicationFactory.INSTANCE.createApplication();
7672
List l = app.getChildren();
7773
l.add(MBasicFactory.INSTANCE.createWindow());
78-
try {
79-
l.add(MBasicFactory.INSTANCE.createPart());
80-
fail("The adding of this should have failed");
81-
} catch (IllegalArgumentException | ArrayStoreException | ClassCastException e) {
82-
// EList.add says this is the expected exception, although testing
83-
// indicates its one of the two previous exceptions that is really
84-
// thrown.
85-
// See bug 407539
86-
} catch (Exception e) {
87-
throw new RuntimeException(e);
88-
}
74+
// EList.add says IllegalArgumentException is the expected exception, although
75+
// testing indicates ArrayStoreException or ClassCastException may be thrown.
76+
// See bug 407539
77+
assertThrows(RuntimeException.class, () -> l.add(MBasicFactory.INSTANCE.createPart()));
8978

9079
l.add(MBasicFactory.INSTANCE.createWindow());
9180
assertEquals(2, l.size());

tests/org.eclipse.jface.tests/src/org/eclipse/jface/tests/dialogs/DialogSettingsTest.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import static org.junit.jupiter.api.Assertions.assertFalse;
2020
import static org.junit.jupiter.api.Assertions.assertNotNull;
2121
import static org.junit.jupiter.api.Assertions.assertNull;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
2223
import static org.junit.jupiter.api.Assertions.assertTrue;
23-
import static org.junit.jupiter.api.Assertions.fail;
2424

2525
import java.io.IOException;
2626
import java.io.StringReader;
@@ -455,14 +455,9 @@ private static void testPutAndGetWithTitle(DialogSettingsChecker dialogSettingsC
455455
}
456456

457457
@Test
458-
@SuppressWarnings("resource")
459458
public void testSaveWithIOException() {
460459
final DialogSettings settings = new DialogSettings("test");
461-
try {
462-
settings.save(new BrokenWriter());
463-
fail("IOException expected");
464-
} catch (IOException e) {
465-
}
460+
assertThrows(IOException.class, () -> settings.save(new BrokenWriter()));
466461
}
467462

468463
private static class BrokenWriter extends Writer {

tests/org.eclipse.jface.tests/src/org/eclipse/jface/tests/labelProviders/DecoratingStyledCellLabelProviderTest.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import static org.junit.jupiter.api.Assertions.assertNotNull;
2020
import static org.junit.jupiter.api.Assertions.assertNotSame;
2121
import static org.junit.jupiter.api.Assertions.assertNull;
22+
import static org.junit.jupiter.api.Assertions.assertThrows;
2223
import static org.junit.jupiter.api.Assertions.assertTrue;
23-
import static org.junit.jupiter.api.Assertions.fail;
2424

2525
import org.eclipse.core.runtime.AssertionFailedException;
2626
import org.eclipse.jface.resource.JFaceResources;
@@ -238,12 +238,7 @@ public void testGetDecorationContext() {
238238

239239
@Test
240240
public void testSetDecorationContext() {
241-
try {
242-
getDecoratingStyledLabelProvider().setDecorationContext(null);
243-
fail("DecoratingStyledCellLabelProvider.setDecorationContext did not throw an exception when passed null");
244-
} catch (AssertionFailedException e) {
245-
// A Good Thing.
246-
}
241+
assertThrows(AssertionFailedException.class, () -> getDecoratingStyledLabelProvider().setDecorationContext(null));
247242
}
248243

249244
@Test

tests/org.eclipse.jface.tests/src/org/eclipse/jface/tests/labelProviders/IDecorationContextTest.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package org.eclipse.jface.tests.labelProviders;
1616

1717
import static org.junit.jupiter.api.Assertions.assertEquals;
18-
import static org.junit.jupiter.api.Assertions.fail;
18+
import static org.junit.jupiter.api.Assertions.assertThrows;
1919

2020
import org.eclipse.core.runtime.AssertionFailedException;
2121
import org.eclipse.jface.viewers.DecoratingStyledCellLabelProvider;
@@ -141,12 +141,7 @@ public void testDefaultContextIsUsed() {
141141
@Test
142142
public void testSetDecorationContextNull() {
143143
DecoratingStyledCellLabelProvider label = getDecoratingStyledCellLabelProvider(false);
144-
try {
145-
label.setDecorationContext(null);
146-
fail("DecoratingStyledCellLabelProvider.setDecorationContext did not throw an exception when passed null");
147-
} catch (AssertionFailedException e) {
148-
// A Good Thing.
149-
}
144+
assertThrows(AssertionFailedException.class, () -> label.setDecorationContext(null));
150145
}
151146

152147
}

tests/org.eclipse.jface.text.tests/src/org/eclipse/jface/text/tests/TextPresentationTest.java

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

1616
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
1717
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertThrows;
1819
import static org.junit.jupiter.api.Assertions.assertTrue;
19-
import static org.junit.jupiter.api.Assertions.fail;
2020

2121
import java.util.ArrayList;
2222
import java.util.Collections;
@@ -1256,23 +1256,17 @@ public void testApplyTextPresentation() {
12561256
public void testIterator() {
12571257
// Test read over iterator end
12581258
Iterator<StyleRange> e= fTextPresentation.getAllStyleRangeIterator();
1259-
try {
1259+
assertThrows(NoSuchElementException.class, () -> {
12601260
for (int i= 0; i < 1000; i++) {
12611261
e.next();
12621262
}
1263-
fail("Iterator has no end.");
1264-
} catch (NoSuchElementException ex) {
1265-
// expected
1266-
}
1267-
e= fTextPresentation.getNonDefaultStyleRangeIterator();
1268-
try {
1263+
});
1264+
Iterator<StyleRange> e2= fTextPresentation.getNonDefaultStyleRangeIterator();
1265+
assertThrows(NoSuchElementException.class, () -> {
12691266
for (int i= 0; i < 1000; i++) {
1270-
e.next();
1267+
e2.next();
12711268
}
1272-
fail("Iterator has no end.");
1273-
} catch (NoSuchElementException ex) {
1274-
// expected
1275-
}
1269+
});
12761270
}
12771271

12781272
// helper method required as long as TextPresentation methods manipulate given arguments

0 commit comments

Comments
 (0)