List<String> importsAndSnippet = extractImports(snippet.getSnippet());
String imports = importsAndSnippet.get(0);
String snippetMinusImports = importsAndSnippet.get(1);
SnippetFixture fixture = snippet.getFixture();
String fullSnippet = imports + fixture.pre() + snippetMinusImports + fixture.post();
Script script;
try {
script = groovyShell.parse(fullSnippet, snippet.getClassName());
} catch (MultipleCompilationErrorsException e) {
Message error = e.getErrorCollector().getError(0);
if (error instanceof SyntaxErrorMessage) {
//noinspection ThrowableResultOfMethodCallIgnored
throw new CompileException(e, ((SyntaxErrorMessage) error).getCause().getLine());
} else {
throw e;
}
}
ClassLoader previousContextClassLoader = Thread.currentThread().getContextClassLoader();
try {
Thread.currentThread().setContextClassLoader(groovyShell.getClassLoader());
fixture.setup();
try {
script.run();
} finally {
fixture.cleanup();
}
} finally {
Thread.currentThread().setContextClassLoader(previousContextClassLoader);
}
}