package jetbrains.mps.lang.test.runtime;
/*Generated by MPS */
import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.project.Project;
import org.apache.log4j.Level;
import jetbrains.mps.smodel.ModelAccess;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.mps.openapi.model.SModelReference;
import org.jetbrains.mps.openapi.persistence.PersistenceFacade;
import jetbrains.mps.project.ProjectManager;
import org.jetbrains.mps.openapi.module.SRepository;
import jetbrains.mps.smodel.ModelAccessHelper;
import jetbrains.mps.util.Computable;
import jetbrains.mps.util.MacrosFactory;
import java.io.File;
import java.io.IOException;
public class TransformationTestLightRunner extends TransformationTestRunner {
private static final Logger LOG = LogManager.getLogger(TransformationTestLightRunner.class);
public TransformationTestLightRunner() {
super(new LightEnvironment());
}
@Override
public void initTest(@NotNull final TransformationTest test, @NotNull String projectPath, String modelName, boolean reopenProject) throws Exception {
Project testProject = findProject(projectPath);
if (testProject == null) {
if (LOG.isEnabledFor(Level.WARN)) {
LOG.warn(String.format("Test project %s is not open. Gonna try any project with model %s", projectPath, modelName));
}
// fallback - take any project with the given model
testProject = findAnyProjectWithModel(modelName);
}
if (testProject == null) {
throw new IllegalStateException("Cannot execute test in-process : the context project containing the model " + modelName + " was not found");
}
doInitTest(test, testProject, modelName);
ModelAccess.instance().flushEventQueue();
}
@Override
public void runTest(@NotNull final TransformationTest projectTest, String className, final String methodName, boolean runInCommand) throws Throwable {
super.runTest(projectTest, className, methodName, runInCommand);
}
@Nullable
private Project findAnyProjectWithModel(String modelName) {
final SModelReference modelRef = PersistenceFacade.getInstance().createModelReference(modelName);
for (Project project : ProjectManager.getInstance().getOpenedProjects()) {
final SRepository repo = project.getRepository();
boolean found = new ModelAccessHelper(repo).runReadAction(new Computable<Boolean>() {
public Boolean compute() {
return modelRef.resolve(repo) != null;
}
});
if (found) {
return project;
}
}
return null;
}
@Nullable
private Project findProject(@NotNull String projectPath) {
String expandedProjectPath = MacrosFactory.getGlobal().expandPath(projectPath);
File projectFile = new File(expandedProjectPath);
for (Project project : ProjectManager.getInstance().getOpenedProjects()) {
if (projectHasPath(project, projectFile)) {
return project;
}
}
return null;
}
private static boolean projectHasPath(Project project, File path) {
File projectFile = project.getProjectFile();
if (projectFile == null) {
return false;
}
try {
String projectPath = projectFile.getCanonicalPath();
String testedPath = path.getCanonicalPath();
return projectPath.equals(testedPath);
} catch (IOException e) {
e.printStackTrace();
}
return false;
}
}