From e9656ebab508852708f99a8704b9b6a5577a9c8e Mon Sep 17 00:00:00 2001 From: Simeon Andreev Date: Sun, 26 May 2024 17:37:33 +0300 Subject: [PATCH] Extract parts of XpectRunner singleton This change extracts parts of the singleton XpectRunner to another class, so that other implementations can set the values of the singleton. This change is required, in order to provide alternatives of XpectRunner, such as a JUnit 5 alternative, without depending on XpectRunner. Preparation for: #262 --- .../xpect/ui/util/XpectFileAccess.java | 5 ++- .../org/eclipse/xpect/ui/util/XpectUtil.java | 5 ++- .../org/eclipse/xpect/runner/XpectRunner.java | 6 +++ .../xpect/runner/XpectTestGlobalState.java | 38 +++++++++++++++++++ .../XtResourceServiceProviderProvider.java | 5 ++- .../org/eclipse/xpect/util/ClasspathUtil.java | 5 ++- .../eclipse/xpect/util/EnvironmentUtil.java | 4 +- 7 files changed, 58 insertions(+), 10 deletions(-) create mode 100644 org.eclipse.xpect/src/org/eclipse/xpect/runner/XpectTestGlobalState.java diff --git a/org.eclipse.xpect.ui/src/org/eclipse/xpect/ui/util/XpectFileAccess.java b/org.eclipse.xpect.ui/src/org/eclipse/xpect/ui/util/XpectFileAccess.java index 2edf2a91..5cfff814 100644 --- a/org.eclipse.xpect.ui/src/org/eclipse/xpect/ui/util/XpectFileAccess.java +++ b/org.eclipse.xpect.ui/src/org/eclipse/xpect/ui/util/XpectFileAccess.java @@ -30,6 +30,7 @@ import org.eclipse.xpect.XpectFile; import org.eclipse.xpect.registry.ILanguageInfo; import org.eclipse.xpect.runner.XpectRunner; +import org.eclipse.xpect.runner.XpectTestGlobalState; import com.google.inject.Injector; @@ -67,8 +68,8 @@ protected static ResourceSet cloneResourceSet(ResourceSet rs) { // need delegation or nothing because of "java" protocol // result.setResourceFactoryRegistry(rs.getResourceFactoryRegistry()); result.setURIConverter(rs.getURIConverter()); - if (XpectRunner.testClassloader != null) { - result.setClasspathURIContext(XpectRunner.testClassloader); + if (XpectTestGlobalState.INSTANCE.testClass() != null) { + result.setClasspathURIContext(XpectTestGlobalState.INSTANCE.testClass().getClassLoader()); result.setClasspathUriResolver(new ClassloaderClasspathUriResolver()); } else if (rs instanceof XtextResourceSet) { XtextResourceSet xrs = (XtextResourceSet) rs; diff --git a/org.eclipse.xpect.ui/src/org/eclipse/xpect/ui/util/XpectUtil.java b/org.eclipse.xpect.ui/src/org/eclipse/xpect/ui/util/XpectUtil.java index deed9c45..2486165a 100644 --- a/org.eclipse.xpect.ui/src/org/eclipse/xpect/ui/util/XpectUtil.java +++ b/org.eclipse.xpect.ui/src/org/eclipse/xpect/ui/util/XpectUtil.java @@ -28,6 +28,7 @@ import org.eclipse.xpect.XpectFile; import org.eclipse.xpect.XpectJavaModel; import org.eclipse.xpect.runner.XpectRunner; +import org.eclipse.xpect.runner.XpectTestGlobalState; import org.eclipse.xpect.ui.internal.XpectActivator; import com.google.inject.Injector; @@ -40,8 +41,8 @@ public static XpectFile loadFile(IFile file) { Injector injector = XpectActivator.getInstance().getInjector(XpectActivator.ORG_ECLIPSE_XPECT_XPECT); XtextResourceSet rs = new XtextResourceSet(); IJavaProject javaProject = JavaCore.create(file.getProject()); - if (XpectRunner.testClassloader != null) { - rs.setClasspathURIContext(XpectRunner.testClassloader); + if (XpectTestGlobalState.INSTANCE.testClass() != null) { + rs.setClasspathURIContext(XpectTestGlobalState.INSTANCE.testClass().getClassLoader()); rs.setClasspathUriResolver(new ClassloaderClasspathUriResolver()); } else if (javaProject != null && javaProject.exists()) { rs.setClasspathURIContext(javaProject); diff --git a/org.eclipse.xpect/src/org/eclipse/xpect/runner/XpectRunner.java b/org.eclipse.xpect/src/org/eclipse/xpect/runner/XpectRunner.java index f92b5d27..b9cd1f95 100644 --- a/org.eclipse.xpect/src/org/eclipse/xpect/runner/XpectRunner.java +++ b/org.eclipse.xpect/src/org/eclipse/xpect/runner/XpectRunner.java @@ -72,6 +72,12 @@ public XpectRunner(Class testClass) throws InitializationError { this.uriProvider = findUriProvider(testClass); this.xpectInjector = findXpectInjector(); this.xpectJavaModel = XpectJavaModelManager.createJavaModel(testClass); + /* + * NOTE: + * Do this before the state creation, otherwise the parts that depend on + * the singleton won't initialize properly and tests will fail to run! + */ + XpectTestGlobalState.INSTANCE.set(xpectJavaModel, testClass); this.state = TestExecutor.createState(createRootConfiguration()); } diff --git a/org.eclipse.xpect/src/org/eclipse/xpect/runner/XpectTestGlobalState.java b/org.eclipse.xpect/src/org/eclipse/xpect/runner/XpectTestGlobalState.java new file mode 100644 index 00000000..a589541f --- /dev/null +++ b/org.eclipse.xpect/src/org/eclipse/xpect/runner/XpectTestGlobalState.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2024 Simeon Andreev and others. + * This program and the accompanying materials are made + * available under the terms of the Eclipse Public License 2.0 + * which is available at https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Simeon Andreev - Initial contribution and API + *******************************************************************************/ +package org.eclipse.xpect.runner; + +import org.eclipse.xpect.XpectJavaModel; + +/** + * @author Simeon Andreev - Initial contribution and API + */ +public class XpectTestGlobalState { + + public static final XpectTestGlobalState INSTANCE = new XpectTestGlobalState(); + + private XpectJavaModel model; + private Class testClass; + + public void set(XpectJavaModel model, Class testClass) { + this.model = model; + this.testClass = testClass; + } + + public XpectJavaModel model() { + return model; + } + + public Class testClass() { + return testClass; + } +} diff --git a/org.eclipse.xpect/src/org/eclipse/xpect/services/XtResourceServiceProviderProvider.java b/org.eclipse.xpect/src/org/eclipse/xpect/services/XtResourceServiceProviderProvider.java index 9f769082..d954886c 100644 --- a/org.eclipse.xpect/src/org/eclipse/xpect/services/XtResourceServiceProviderProvider.java +++ b/org.eclipse.xpect/src/org/eclipse/xpect/services/XtResourceServiceProviderProvider.java @@ -17,6 +17,7 @@ import org.eclipse.xpect.XpectConstants; import org.eclipse.xpect.registry.ILanguageInfo; import org.eclipse.xpect.runner.XpectRunner; +import org.eclipse.xpect.runner.XpectTestGlobalState; import org.eclipse.xpect.util.IXtInjectorProvider; import com.google.inject.Injector; @@ -32,8 +33,8 @@ private XtResourceServiceProviderProvider() { } public IResourceServiceProvider get(URI uri, String contentType) { - if (XpectRunner.INSTANCE != null) { - Injector injector = IXtInjectorProvider.INSTANCE.getInjector(XpectRunner.INSTANCE.getXpectJavaModel(), uri); + if (XpectTestGlobalState.INSTANCE.model() != null) { + Injector injector = IXtInjectorProvider.INSTANCE.getInjector(XpectTestGlobalState.INSTANCE.model(), uri); if (injector != null) return injector.getInstance(IResourceServiceProvider.class); } diff --git a/org.eclipse.xpect/src/org/eclipse/xpect/util/ClasspathUtil.java b/org.eclipse.xpect/src/org/eclipse/xpect/util/ClasspathUtil.java index e032b258..ff278d5d 100644 --- a/org.eclipse.xpect/src/org/eclipse/xpect/util/ClasspathUtil.java +++ b/org.eclipse.xpect/src/org/eclipse/xpect/util/ClasspathUtil.java @@ -26,6 +26,7 @@ import org.apache.log4j.Logger; import org.eclipse.xpect.runner.XpectRunner; +import org.eclipse.xpect.runner.XpectTestGlobalState; import com.google.common.base.Joiner; import com.google.common.collect.Sets; @@ -81,8 +82,8 @@ public static Collection findResources(String... fileNames) { } } // for some reason, ucl.getURLs() doesn't catch the current project in standalone maven surefire - if (XpectRunner.INSTANCE != null) { - Class clazz = XpectRunner.INSTANCE.getTestClass().getJavaClass(); + if (XpectTestGlobalState.INSTANCE.testClass() != null) { + Class clazz = XpectTestGlobalState.INSTANCE.testClass(); String[] segments = clazz.getName().split("\\."); String fileName = Joiner.on('/').join(segments) + ".class"; URL resource = clazz.getClassLoader().getResource(fileName); diff --git a/org.eclipse.xpect/src/org/eclipse/xpect/util/EnvironmentUtil.java b/org.eclipse.xpect/src/org/eclipse/xpect/util/EnvironmentUtil.java index 66e6807f..e07583e7 100644 --- a/org.eclipse.xpect/src/org/eclipse/xpect/util/EnvironmentUtil.java +++ b/org.eclipse.xpect/src/org/eclipse/xpect/util/EnvironmentUtil.java @@ -14,7 +14,7 @@ import org.eclipse.emf.ecore.plugin.EcorePlugin; import org.eclipse.xpect.Environment; -import org.eclipse.xpect.runner.XpectRunner; +import org.eclipse.xpect.runner.XpectTestGlobalState; import com.google.common.base.Joiner; @@ -22,7 +22,7 @@ public class EnvironmentUtil { public static final Environment ENVIRONMENT = detectEnvironement(); private static Environment detectEnvironement() { - if (XpectRunner.testClassloader != null) { + if (XpectTestGlobalState.INSTANCE.testClass() != null) { if (EcorePlugin.IS_ECLIPSE_RUNNING) return Environment.PLUGIN_TEST; else