Skip to content

Commit

Permalink
Extract parts of XpectRunner singleton
Browse files Browse the repository at this point in the history
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: eclipse#262
  • Loading branch information
trancexpress committed May 26, 2024
1 parent 56f9d5d commit e9656eb
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -81,8 +82,8 @@ public static Collection<URL> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@

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;

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
Expand Down

0 comments on commit e9656eb

Please sign in to comment.