Skip to content

Commit d227db1

Browse files
committed
Migrate HeadlessApplicationRule to JUnit 5 Extension
Created HeadlessApplicationExtension to replace the JUnit 4 HeadlessApplicationRule. Migrated HeadlessApplicationElementTest and its subclasses (HeadlessApplicationTest, UIEventTypesTest, UIEventsTest, UIStartupTest) to use the new extension and JUnit 5 annotations/assertions.
1 parent 88e2b87 commit d227db1

File tree

6 files changed

+107
-52
lines changed

6 files changed

+107
-52
lines changed

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@
1717
import org.eclipse.e4.core.contexts.IEclipseContext;
1818
import org.eclipse.e4.ui.internal.workbench.UIEventPublisher;
1919
import org.eclipse.e4.ui.model.application.MApplicationElement;
20-
import org.eclipse.e4.ui.tests.rules.HeadlessApplicationRule;
20+
import org.eclipse.e4.ui.tests.rules.HeadlessApplicationExtension;
2121
import org.eclipse.e4.ui.workbench.modeling.EModelService;
2222
import org.eclipse.emf.common.notify.Notifier;
23-
import org.junit.Before;
24-
import org.junit.Rule;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.extension.RegisterExtension;
2525

2626
public abstract class HeadlessApplicationElementTest {
2727

2828
protected MApplicationElement applicationElement;
2929
protected EModelService ems;
3030

31-
@Rule
32-
public HeadlessApplicationRule rule = new HeadlessApplicationRule();
31+
@RegisterExtension
32+
public HeadlessApplicationExtension extension = new HeadlessApplicationExtension();
3333

34-
@Before
34+
@BeforeEach
3535
public void setUp() throws Exception {
36-
IEclipseContext applicationContext = rule.getApplicationContext();
36+
IEclipseContext applicationContext = extension.getApplicationContext();
3737
applicationElement = createApplicationElement(applicationContext);
3838
ems = applicationContext.get(EModelService.class);
3939

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414

1515
package org.eclipse.e4.ui.tests.application;
1616

17-
import static org.junit.Assert.assertEquals;
18-
import static org.junit.Assert.assertFalse;
19-
import static org.junit.Assert.assertNotNull;
20-
import static org.junit.Assert.assertNull;
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertFalse;
19+
import static org.junit.jupiter.api.Assertions.assertNotNull;
20+
import static org.junit.jupiter.api.Assertions.assertNull;
2121

2222
import java.util.HashSet;
2323
import java.util.List;
@@ -49,17 +49,17 @@
4949
import org.eclipse.emf.ecore.resource.Resource;
5050
import org.eclipse.emf.ecore.resource.ResourceSet;
5151
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
52-
import org.junit.After;
53-
import org.junit.Before;
54-
import org.junit.Test;
52+
import org.junit.jupiter.api.AfterEach;
53+
import org.junit.jupiter.api.BeforeEach;
54+
import org.junit.jupiter.api.Test;
5555

5656
public abstract class HeadlessApplicationTest extends HeadlessApplicationElementTest {
5757

5858
protected MApplication application;
5959

6060
protected IPresentationEngine renderer;
6161

62-
@Before
62+
@BeforeEach
6363
@Override
6464

6565
public void setUp() throws Exception {
@@ -76,7 +76,7 @@ public void setUp() throws Exception {
7676
}
7777
}
7878

79-
@After
79+
@AfterEach
8080
public void tearDown() throws Exception {
8181
for (MWindow window : application.getChildren()) {
8282
renderer.removeGui(window);
@@ -233,10 +233,10 @@ protected MApplicationElement createApplicationElement(
233233
protected abstract String getURI();
234234

235235
protected IPresentationEngine createPresentationEngine(String renderingEngineURI) throws Exception {
236-
IContributionFactory contributionFactory = rule.getApplicationContext()
236+
IContributionFactory contributionFactory = extension.getApplicationContext()
237237
.get(IContributionFactory.class);
238238
Object newEngine = contributionFactory.create(renderingEngineURI,
239-
rule.getApplicationContext());
239+
extension.getApplicationContext());
240240
return (IPresentationEngine) newEngine;
241241
}
242242

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

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

1515
package org.eclipse.e4.ui.tests.application;
1616

17-
import static org.junit.Assert.assertEquals;
18-
import static org.junit.Assert.assertNotNull;
19-
import static org.junit.Assert.assertTrue;
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertNotNull;
19+
import static org.junit.jupiter.api.Assertions.assertTrue;
2020

2121
import java.util.Arrays;
2222
import org.eclipse.e4.core.contexts.IEclipseContext;
@@ -25,8 +25,8 @@
2525
import org.eclipse.e4.ui.model.application.MApplicationFactory;
2626
import org.eclipse.e4.ui.workbench.UIEvents;
2727
import org.eclipse.emf.common.util.EList;
28-
import org.junit.Before;
29-
import org.junit.Test;
28+
import org.junit.jupiter.api.BeforeEach;
29+
import org.junit.jupiter.api.Test;
3030
import org.osgi.service.event.Event;
3131

3232
/**
@@ -42,11 +42,11 @@ protected MApplicationElement createApplicationElement(IEclipseContext appContex
4242
return MApplicationFactory.INSTANCE.createApplication();
4343
}
4444

45-
@Before
45+
@BeforeEach
4646
@Override
4747
public void setUp() throws Exception {
4848
super.setUp();
49-
IEventBroker appEB = rule.getApplicationContext().get(IEventBroker.class);
49+
IEventBroker appEB = extension.getApplicationContext().get(IEventBroker.class);
5050

5151
eventCount = 0;
5252
appEB.subscribe(UIEvents.ApplicationElement.TOPIC_TAGS, event -> {
@@ -174,8 +174,8 @@ public void testMove() {
174174
assertEquals(UIEvents.ApplicationElement.TAGS, event.getProperty(UIEvents.EventTags.ATTNAME));
175175
assertEquals(UIEvents.EventTypes.MOVE, event.getProperty(UIEvents.EventTags.TYPE));
176176
assertEquals("2", event.getProperty(UIEvents.EventTags.NEW_VALUE));
177-
assertEquals("former position", 2, event.getProperty(UIEvents.EventTags.OLD_VALUE));
178-
assertEquals("new position", 0, event.getProperty(UIEvents.EventTags.POSITION));
177+
assertEquals(2, event.getProperty(UIEvents.EventTags.OLD_VALUE), "former position");
178+
assertEquals(0, event.getProperty(UIEvents.EventTags.POSITION), "new position");
179179
}
180180

181181
@Test

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

Lines changed: 16 additions & 17 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.Assert.assertEquals;
18-
import static org.junit.Assert.assertFalse;
19-
import static org.junit.Assert.assertNotEquals;
20-
import static org.junit.Assert.assertTrue;
21-
import static org.junit.Assert.fail;
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertFalse;
19+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
21+
import static org.junit.jupiter.api.Assertions.fail;
2222

2323
import java.util.ArrayList;
2424
import java.util.List;
@@ -47,7 +47,7 @@
4747
import org.eclipse.e4.ui.workbench.UIEvents.UILabel;
4848
import org.eclipse.e4.ui.workbench.UIEvents.Window;
4949
import org.eclipse.emf.common.notify.Notifier;
50-
import org.junit.Test;
50+
import org.junit.jupiter.api.Test;
5151
import org.osgi.service.event.EventHandler;
5252

5353
public class UIEventsTest extends HeadlessApplicationElementTest {
@@ -62,14 +62,14 @@ static class EventTester {
6262
EventHandler attListener = event -> {
6363
// In case of * topic check that that event topic starts with the same prefix
6464
if (topic.endsWith("*")) {
65-
assertTrue("Incorrect Topic.", event.getTopic().startsWith(topic.substring(0, topic.length() - 2)));
65+
assertTrue(event.getTopic().startsWith(topic.substring(0, topic.length() - 2)), "Incorrect Topic.");
6666
} else {
67-
assertEquals("Incorrect Topic.", topic, event.getTopic());
67+
assertEquals(topic, event.getTopic(), "Incorrect Topic.");
6868
}
6969

7070
String attId = (String) event.getProperty(EventTags.ATTNAME);
7171
int attIndex = getAttIndex(attId);
72-
assertTrue("Unknown Attribite: " + attId, attIndex >= 0); //$NON-NLS-1$
72+
assertTrue(attIndex >= 0, "Unknown Attribite: " + attId); //$NON-NLS-1$
7373
hasFired[attIndex] = true;
7474
};
7575

@@ -201,7 +201,7 @@ protected MApplicationElement createApplicationElement(
201201

202202
@Test
203203
public void testAllTopics() {
204-
IEventBroker eventBroker = rule.getApplicationContext().get(IEventBroker.class);
204+
IEventBroker eventBroker = extension.getApplicationContext().get(IEventBroker.class);
205205

206206
// Create a tester for each topic
207207
AppElementTester appTester = new AppElementTester(eventBroker);
@@ -225,9 +225,9 @@ public void testAllTopics() {
225225

226226
// Create the test harness and hook up the event publisher
227227
MTestHarness allData = MTestFactory.eINSTANCE.createTestHarness();
228-
final UIEventPublisher ep = new UIEventPublisher(rule.getApplicationContext());
228+
final UIEventPublisher ep = new UIEventPublisher(extension.getApplicationContext());
229229
((Notifier) allData).eAdapters().add(ep);
230-
rule.getApplicationContext().set(UIEventPublisher.class, ep);
230+
extension.getApplicationContext().set(UIEventPublisher.class, ep);
231231

232232
// AppElement
233233
reset(allTesters);
@@ -241,8 +241,7 @@ public void testAllTopics() {
241241
// Test that no-ops don't throw events
242242
appTester.reset();
243243
allData.setElementId(newId);
244-
assertTrue("event thrown on No-Op",
245-
appTester.getAttIds(true).length == 0);
244+
assertTrue(appTester.getAttIds(true).length == 0, "event thrown on No-Op");
246245

247246
// Command
248247
reset(allTesters);
@@ -318,11 +317,11 @@ public void testAllTopics() {
318317
@Test
319318
public void testBrokerCleanup() {
320319
final String testTopic = "test/374534";
321-
IEventBroker appEB = rule.getApplicationContext().get(IEventBroker.class);
320+
IEventBroker appEB = extension.getApplicationContext().get(IEventBroker.class);
322321

323-
IEclipseContext childContext = rule.getApplicationContext().createChild();
322+
IEclipseContext childContext = extension.getApplicationContext().createChild();
324323
IEventBroker childEB = childContext.get(IEventBroker.class);
325-
assertNotEquals("child context has same IEventBroker", appEB, childEB);
324+
assertNotEquals(appEB, childEB, "child context has same IEventBroker");
326325

327326
final boolean[] seen = { false };
328327
childEB.subscribe(testTopic, event -> seen[0] = true);

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

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

1515
package org.eclipse.e4.ui.tests.application;
1616

17-
import static org.junit.Assert.assertNotNull;
18-
import static org.junit.Assert.assertNull;
17+
import static org.junit.jupiter.api.Assertions.assertNotNull;
18+
import static org.junit.jupiter.api.Assertions.assertNull;
1919

2020
import org.eclipse.core.databinding.observable.Realm;
2121
import org.eclipse.e4.core.contexts.IEclipseContext;
@@ -30,16 +30,16 @@
3030
import org.eclipse.e4.ui.workbench.IResourceUtilities;
3131
import org.eclipse.jface.databinding.swt.DisplayRealm;
3232
import org.eclipse.swt.widgets.Display;
33-
import org.junit.After;
34-
import org.junit.Before;
35-
import org.junit.Test;
33+
import org.junit.jupiter.api.AfterEach;
34+
import org.junit.jupiter.api.BeforeEach;
35+
import org.junit.jupiter.api.Test;
3636
import org.w3c.dom.css.CSSStyleDeclaration;
3737

3838
public abstract class UIStartupTest extends HeadlessApplicationTest {
3939

4040
protected Display display;
4141

42-
@Before
42+
@BeforeEach
4343
@Override
4444
public void setUp() throws Exception {
4545
display = Display.getDefault();
@@ -48,7 +48,7 @@ public void setUp() throws Exception {
4848
}
4949
}
5050

51-
@After
51+
@AfterEach
5252
@Override
5353
public void tearDown() throws Exception {
5454
super.tearDown();
@@ -158,7 +158,7 @@ private static IEclipseContext getActiveChildContext(
158158
protected IEclipseContext createApplicationContext() {
159159
final IEclipseContext[] contexts = new IEclipseContext[1];
160160
Realm.runWithDefault(DisplayRealm.getRealm(display), () -> {
161-
contexts[0] = rule.getApplicationContext();
161+
contexts[0] = extension.getApplicationContext();
162162
contexts[0].set(IResourceUtilities.class.getName(), new ResourceUtility());
163163
contexts[0].set(IStylingEngine.class, new IStylingEngine() {
164164
@Override
@@ -196,4 +196,5 @@ protected void createGUI(final MUIElement uiRoot) {
196196
Realm.runWithDefault(DisplayRealm.getRealm(display), () -> UIStartupTest.super.createGUI(uiRoot));
197197
}
198198

199+
199200
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2018 vogella GmbH and others.
3+
*
4+
* This program and the accompanying materials
5+
* are made available under the terms of the Eclipse Public License 2.0
6+
* which accompanies this distribution, and is available at
7+
* https://www.eclipse.org/legal/epl-2.0/
8+
*
9+
* SPDX-License-Identifier: EPL-2.0
10+
*
11+
* Contributors:
12+
* Lars Vogel <[email protected]> - initial API and implementation
13+
******************************************************************************/
14+
15+
package org.eclipse.e4.ui.tests.rules;
16+
17+
import org.eclipse.e4.core.commands.CommandServiceAddon;
18+
import org.eclipse.e4.core.contexts.ContextInjectionFactory;
19+
import org.eclipse.e4.core.contexts.IEclipseContext;
20+
import org.eclipse.e4.ui.internal.workbench.swt.E4Application;
21+
import org.eclipse.e4.ui.services.ContextServiceAddon;
22+
import org.junit.jupiter.api.extension.AfterEachCallback;
23+
import org.junit.jupiter.api.extension.BeforeEachCallback;
24+
import org.junit.jupiter.api.extension.ExtensionContext;
25+
26+
27+
public class HeadlessApplicationExtension implements BeforeEachCallback, AfterEachCallback {
28+
private IEclipseContext applicationContext;
29+
30+
/**
31+
* @return the applicationContext
32+
*/
33+
public IEclipseContext getApplicationContext() {
34+
return applicationContext;
35+
}
36+
37+
@Override
38+
public void beforeEach(ExtensionContext context) throws Exception {
39+
applicationContext = createApplicationContext();
40+
}
41+
42+
@Override
43+
public void afterEach(ExtensionContext context) throws Exception {
44+
if (applicationContext != null) {
45+
applicationContext.dispose();
46+
}
47+
}
48+
49+
protected IEclipseContext createApplicationContext() {
50+
final IEclipseContext appContext = E4Application.createDefaultContext();
51+
ContextInjectionFactory.make(CommandServiceAddon.class, appContext);
52+
ContextInjectionFactory.make(ContextServiceAddon.class, appContext);
53+
return appContext;
54+
}
55+
}

0 commit comments

Comments
 (0)