org.eclipse.xtext.testing.GlobalRegistries.GlobalStateMemento Java Examples

The following examples show how to use org.eclipse.xtext.testing.GlobalRegistries.GlobalStateMemento. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CliTools.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private <T> T withoutCorruptingGlobalState(Supplier<T> operation) {
	GlobalStateMemento originalGlobalState = GlobalRegistries.makeCopyOfGlobalState();
	try {
		return operation.get();
	} finally {
		originalGlobalState.restoreGlobalState();
	}
}
 
Example #2
Source File: N4CliHelper.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Copies the n4js libraries to the given testing workspace {@code location}.
 *
 * Only includes n4js libraries, for whose project name {@code n4jsLibrariesPredicate} returns {@code true}.
 *
 * @throws IOException
 *             In case the copying is not successful.
 */
public static void copyN4jsLibsToLocation(Path location, Predicate<N4JSProjectName> n4jsLibrariesPredicate)
		throws IOException {

	GlobalStateMemento originalGlobalState = null;
	if (!JSONStandaloneSetup.isSetUp()) {
		originalGlobalState = GlobalRegistries.makeCopyOfGlobalState();
		JSONStandaloneSetup.doSetup();
	}

	try {
		N4jsLibsAccess.installN4jsLibs(
				location,
				true,
				false, // do not use symbolic links (because some tests modify the files in the destination folder)
				false, // do not delete on exit (because tests using this method are responsible for cleaning up)
				libName -> !N4JS_LIBS_BLACKLIST.contains(libName) && n4jsLibrariesPredicate.test(libName));
	} finally {
		if (originalGlobalState != null) {
			// Restore the original global state (if we had to change it)
			// This is important for these cases in particular:
			// 1) tests that invoke N4jscBase#doMain() should run *without* any global setup, because #doMain()
			// should work if invoked from a plain Java main() method; if we provided the JSON setup as a
			// side-effect of this utility method we would "help" the implementation of #doMain() and might overlook
			// problems in #doMain()'s own setup.
			// 2) some tests deliberately run without any or with an incomplete setup in order to test some failure
			// behavior in this broken case.
			originalGlobalState.restoreGlobalState();
		}
	}
}