package jetbrains.mps.baseLanguage.unitTest.runtime;

/*Generated by MPS */

import org.junit.jupiter.api.extension.TestInstancePostProcessor;
import org.junit.jupiter.api.extension.BeforeAllCallback;
import java.util.concurrent.atomic.AtomicReference;
import jetbrains.mps.tool.environment.Environment;
import org.junit.jupiter.api.extension.ExtensionContext;
import jetbrains.mps.tool.environment.EnvironmentAware;

public class EnvironmentAwareExtension implements TestInstancePostProcessor, BeforeAllCallback {

  private static AtomicReference<Environment> ourEnvironment = new AtomicReference<Environment>();

  public static void setEnvironment(Environment environment) {
    ourEnvironment.set(environment);
  }

  public EnvironmentAwareExtension() {
  }

  @Override
  public void beforeAll(ExtensionContext context) throws Exception {
  }

  @Override
  public void postProcessTestInstance(Object object, ExtensionContext context) throws Exception {
    if (object instanceof EnvironmentAware) {
      ((EnvironmentAware) object).setEnvironment(ourEnvironment.get());
    }
  }

}
