Skip to content

Commit 437ea03

Browse files
authored
Merge pull request #344 from trancexpress/XpectRunner_JUnit5_singleton_rework
Extract parts of XpectRunner singleton
2 parents e27f2f2 + e9656eb commit 437ea03

File tree

7 files changed

+58
-10
lines changed

7 files changed

+58
-10
lines changed

org.eclipse.xpect.ui/src/org/eclipse/xpect/ui/util/XpectFileAccess.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.eclipse.xpect.XpectFile;
3131
import org.eclipse.xpect.registry.ILanguageInfo;
3232
import org.eclipse.xpect.runner.XpectRunner;
33+
import org.eclipse.xpect.runner.XpectTestGlobalState;
3334

3435
import com.google.inject.Injector;
3536

@@ -67,8 +68,8 @@ protected static ResourceSet cloneResourceSet(ResourceSet rs) {
6768
// need delegation or nothing because of "java" protocol
6869
// result.setResourceFactoryRegistry(rs.getResourceFactoryRegistry());
6970
result.setURIConverter(rs.getURIConverter());
70-
if (XpectRunner.testClassloader != null) {
71-
result.setClasspathURIContext(XpectRunner.testClassloader);
71+
if (XpectTestGlobalState.INSTANCE.testClass() != null) {
72+
result.setClasspathURIContext(XpectTestGlobalState.INSTANCE.testClass().getClassLoader());
7273
result.setClasspathUriResolver(new ClassloaderClasspathUriResolver());
7374
} else if (rs instanceof XtextResourceSet) {
7475
XtextResourceSet xrs = (XtextResourceSet) rs;

org.eclipse.xpect.ui/src/org/eclipse/xpect/ui/util/XpectUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.eclipse.xpect.XpectFile;
2929
import org.eclipse.xpect.XpectJavaModel;
3030
import org.eclipse.xpect.runner.XpectRunner;
31+
import org.eclipse.xpect.runner.XpectTestGlobalState;
3132
import org.eclipse.xpect.ui.internal.XpectActivator;
3233

3334
import com.google.inject.Injector;
@@ -40,8 +41,8 @@ public static XpectFile loadFile(IFile file) {
4041
Injector injector = XpectActivator.getInstance().getInjector(XpectActivator.ORG_ECLIPSE_XPECT_XPECT);
4142
XtextResourceSet rs = new XtextResourceSet();
4243
IJavaProject javaProject = JavaCore.create(file.getProject());
43-
if (XpectRunner.testClassloader != null) {
44-
rs.setClasspathURIContext(XpectRunner.testClassloader);
44+
if (XpectTestGlobalState.INSTANCE.testClass() != null) {
45+
rs.setClasspathURIContext(XpectTestGlobalState.INSTANCE.testClass().getClassLoader());
4546
rs.setClasspathUriResolver(new ClassloaderClasspathUriResolver());
4647
} else if (javaProject != null && javaProject.exists()) {
4748
rs.setClasspathURIContext(javaProject);

org.eclipse.xpect/src/org/eclipse/xpect/runner/XpectRunner.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,12 @@ public XpectRunner(Class<?> testClass) throws InitializationError {
7272
this.uriProvider = findUriProvider(testClass);
7373
this.xpectInjector = findXpectInjector();
7474
this.xpectJavaModel = XpectJavaModelManager.createJavaModel(testClass);
75+
/*
76+
* NOTE:
77+
* Do this before the state creation, otherwise the parts that depend on
78+
* the singleton won't initialize properly and tests will fail to run!
79+
*/
80+
XpectTestGlobalState.INSTANCE.set(xpectJavaModel, testClass);
7581
this.state = TestExecutor.createState(createRootConfiguration());
7682
}
7783

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Simeon Andreev and others.
3+
* This program and the accompanying materials are made
4+
* available under the terms of the Eclipse Public License 2.0
5+
* which is available at https://www.eclipse.org/legal/epl-2.0/
6+
*
7+
* SPDX-License-Identifier: EPL-2.0
8+
*
9+
* Contributors:
10+
* Simeon Andreev - Initial contribution and API
11+
*******************************************************************************/
12+
package org.eclipse.xpect.runner;
13+
14+
import org.eclipse.xpect.XpectJavaModel;
15+
16+
/**
17+
* @author Simeon Andreev - Initial contribution and API
18+
*/
19+
public class XpectTestGlobalState {
20+
21+
public static final XpectTestGlobalState INSTANCE = new XpectTestGlobalState();
22+
23+
private XpectJavaModel model;
24+
private Class<?> testClass;
25+
26+
public void set(XpectJavaModel model, Class<?> testClass) {
27+
this.model = model;
28+
this.testClass = testClass;
29+
}
30+
31+
public XpectJavaModel model() {
32+
return model;
33+
}
34+
35+
public Class<?> testClass() {
36+
return testClass;
37+
}
38+
}

org.eclipse.xpect/src/org/eclipse/xpect/services/XtResourceServiceProviderProvider.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.eclipse.xpect.XpectConstants;
1818
import org.eclipse.xpect.registry.ILanguageInfo;
1919
import org.eclipse.xpect.runner.XpectRunner;
20+
import org.eclipse.xpect.runner.XpectTestGlobalState;
2021
import org.eclipse.xpect.util.IXtInjectorProvider;
2122

2223
import com.google.inject.Injector;
@@ -32,8 +33,8 @@ private XtResourceServiceProviderProvider() {
3233
}
3334

3435
public IResourceServiceProvider get(URI uri, String contentType) {
35-
if (XpectRunner.INSTANCE != null) {
36-
Injector injector = IXtInjectorProvider.INSTANCE.getInjector(XpectRunner.INSTANCE.getXpectJavaModel(), uri);
36+
if (XpectTestGlobalState.INSTANCE.model() != null) {
37+
Injector injector = IXtInjectorProvider.INSTANCE.getInjector(XpectTestGlobalState.INSTANCE.model(), uri);
3738
if (injector != null)
3839
return injector.getInstance(IResourceServiceProvider.class);
3940
}

org.eclipse.xpect/src/org/eclipse/xpect/util/ClasspathUtil.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import org.apache.log4j.Logger;
2828
import org.eclipse.xpect.runner.XpectRunner;
29+
import org.eclipse.xpect.runner.XpectTestGlobalState;
2930

3031
import com.google.common.base.Joiner;
3132
import com.google.common.collect.Sets;
@@ -81,8 +82,8 @@ public static Collection<URL> findResources(String... fileNames) {
8182
}
8283
}
8384
// for some reason, ucl.getURLs() doesn't catch the current project in standalone maven surefire
84-
if (XpectRunner.INSTANCE != null) {
85-
Class<?> clazz = XpectRunner.INSTANCE.getTestClass().getJavaClass();
85+
if (XpectTestGlobalState.INSTANCE.testClass() != null) {
86+
Class<?> clazz = XpectTestGlobalState.INSTANCE.testClass();
8687
String[] segments = clazz.getName().split("\\.");
8788
String fileName = Joiner.on('/').join(segments) + ".class";
8889
URL resource = clazz.getClassLoader().getResource(fileName);

org.eclipse.xpect/src/org/eclipse/xpect/util/EnvironmentUtil.java

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

1515
import org.eclipse.emf.ecore.plugin.EcorePlugin;
1616
import org.eclipse.xpect.Environment;
17-
import org.eclipse.xpect.runner.XpectRunner;
17+
import org.eclipse.xpect.runner.XpectTestGlobalState;
1818

1919
import com.google.common.base.Joiner;
2020

2121
public class EnvironmentUtil {
2222
public static final Environment ENVIRONMENT = detectEnvironement();
2323

2424
private static Environment detectEnvironement() {
25-
if (XpectRunner.testClassloader != null) {
25+
if (XpectTestGlobalState.INSTANCE.testClass() != null) {
2626
if (EcorePlugin.IS_ECLIPSE_RUNNING)
2727
return Environment.PLUGIN_TEST;
2828
else

0 commit comments

Comments
 (0)