package jetbrains.mps.baseLanguage.unitTest.execution.server;
/*Generated by MPS */
import java.io.PrintStream;
import org.apache.log4j.BasicConfigurator;
import org.jetbrains.annotations.NotNull;
import org.junit.runner.notification.RunListener;
import org.junit.runner.Request;
import java.io.IOException;
import jetbrains.mps.tool.environment.Environment;
import jetbrains.mps.tool.environment.EnvironmentContainer;
public class DefaultTestExecutor extends AbstractTestExecutor {
private final String[] myArgs;
private final CommandOutputStream myOutStream;
private final CommandOutputStream myErrStream;
public DefaultTestExecutor(String[] args) {
myArgs = args;
myOutStream = new CommandOutputStream(System.out);
myErrStream = new CommandOutputStream(System.err);
}
public void init() {
System.setOut(new PrintStream(myOutStream));
System.setErr(new PrintStream(myErrStream));
BasicConfigurator.configure();
}
public void dispose() {
System.setOut(myOutStream.getOldStream());
System.setErr(myErrStream.getOldStream());
}
@NotNull
@Override
protected RunListener createListener(Iterable<Request> iterable) {
return new DefaultRunListener(myOutStream);
}
@NotNull
@Override
protected TestsContributor createTestsContributor() {
try {
return new CommandLineTestsContributor(myArgs);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Called when BTestCase is executed
*/
public static void main(String[] args) throws ClassNotFoundException, IOException {
DefaultTestExecutor executor = new DefaultTestExecutor(args);
try {
executor.run();
} catch (Throwable t) {
executor.processThrowable(t);
}
executor.exit();
}
protected void run() {
try {
init();
execute();
} finally {
dispose();
}
}
protected void processThrowable(Throwable t) {
t.printStackTrace(System.err);
System.exit(EXIT_CODE_FOR_EXCEPTION);
}
protected void exit() {
Environment env = EnvironmentContainer.get();
if (env != null) {
env.dispose();
}
DefaultRunListener listener = ((DefaultRunListener) this.getListener());
if (listener == null) {
System.exit(EXIT_CODE_FOR_EXCEPTION);
} else {
System.exit(listener.getFailureCount());
}
}
}