package jetbrains.mps.baseLanguage.unitTest.execution.settings;
/*Generated by MPS */
import jetbrains.mps.execution.api.settings.IPersistentConfiguration;
import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
import org.jetbrains.annotations.NotNull;
import jetbrains.mps.execution.api.settings.PersistentConfigurationContext;
import com.intellij.execution.configurations.RuntimeConfigurationException;
import com.intellij.execution.configurations.RuntimeConfigurationError;
import jetbrains.mps.project.MPSProject;
import jetbrains.mps.ide.project.ProjectHelper;
import org.jdom.Element;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.util.xmlb.XmlSerializer;
import com.intellij.openapi.util.InvalidDataException;
import jetbrains.mps.execution.lib.ClonableList;
import jetbrains.mps.baseLanguage.unitTest.execution.client.RunCachesManager;
import jetbrains.mps.baseLanguage.unitTest.execution.client.ITestNodeWrapper;
import java.util.List;
import jetbrains.mps.internal.collections.runtime.ListSequence;
import jetbrains.mps.smodel.ModelAccessHelper;
import jetbrains.mps.util.Computable;
import jetbrains.mps.execution.configurations.implementation.plugin.plugin.JUnitInProcessExecutor;
import jetbrains.mps.lang.test.util.RunStateEnum;
import org.jetbrains.mps.openapi.model.SNodeReference;
import jetbrains.mps.util.Reference;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.application.ModalityState;
import jetbrains.mps.internal.collections.runtime.ISelector;
import org.apache.log4j.Level;
import com.intellij.openapi.project.Project;
public class JUnitSettings_Configuration implements IPersistentConfiguration {
private static final Logger LOG = LogManager.getLogger(JUnitSettings_Configuration.class);
@NotNull
private JUnitSettings_Configuration.MyState myState = new JUnitSettings_Configuration.MyState();
public void checkConfiguration(final PersistentConfigurationContext context) throws RuntimeConfigurationException {
if (this.getRunType() < 0 || this.getRunType() > JUnitRunTypes.values().length) {
throw new RuntimeConfigurationError("Type of test not selected.");
}
// We do not validate, only check if there is something to test, since validating everything be very slow
// see MPS-8781 JUnit run configuration check method performance.
MPSProject mpsProject = ProjectHelper.fromIdeaProject(myProject);
checkCachesDirIsFreeToLock();
checkInProcessRunIsSingle();
if (!((eq_jtq3ac_a0a0g0c(this.getRunType(), JUnitRunTypes.PROJECT.ordinal())))) {
check(mpsProject);
}
}
@Override
public void writeExternal(Element element) throws WriteExternalException {
element.addContent(XmlSerializer.serialize(myState));
}
@Override
public void readExternal(Element element) throws InvalidDataException {
if (element == null) {
throw new InvalidDataException("Cant read " + this + ": element is null.");
}
XmlSerializer.deserializeInto(myState, (Element) element.getChildren().get(0));
}
public String getModel() {
return myState.myModel;
}
public String getModule() {
return myState.myModule;
}
public boolean getInProcess() {
return myState.myInProcess;
}
public boolean getReuseCaches() {
return myState.myReuseCaches;
}
public boolean getDebug() {
return myState.myDebug;
}
public String getCachesPath() {
return myState.myCachesPath;
}
public ClonableList<String> getTestCases() {
return myState.myTestCases;
}
public ClonableList<String> getTestMethods() {
return myState.myTestMethods;
}
public int getRunType() {
return myState.myRunType;
}
public void setModel(String value) {
myState.myModel = value;
}
public void setModule(String value) {
myState.myModule = value;
}
public void setInProcess(boolean value) {
myState.myInProcess = value;
}
public void setReuseCaches(boolean value) {
myState.myReuseCaches = value;
}
public void setDebug(boolean value) {
myState.myDebug = value;
}
public void setCachesPath(String value) {
myState.myCachesPath = value;
}
public void setTestCases(ClonableList<String> value) {
myState.myTestCases = value;
}
public void setTestMethods(ClonableList<String> value) {
myState.myTestMethods = value;
}
public void setRunType(int value) {
myState.myRunType = value;
}
public String getDefaultPath() {
return new DefaultCachesPathChooser().chooseDir();
}
public JUnitRunTypes getJUnitRunType() {
return JUnitRunTypes.values()[this.getRunType()];
}
public void setJUnitRunType(JUnitRunTypes runType) {
this.setRunType(runType.ordinal());
}
public boolean canSaveCachesPath() {
return this.getReuseCaches() && !(RunCachesManager.isLocked(this.getCachesPath()));
}
public boolean canExecuteInProcess(Iterable<ITestNodeWrapper> testNodes) {
return this.getInProcess() && !(this.getDebug());
}
public List<ITestNodeWrapper> getTests(final MPSProject project) {
return ListSequence.fromList(collectTests(project)).toListSequence();
}
private void check(final MPSProject project) throws RuntimeConfigurationException {
final JUnitSettings_Configuration settings = this;
String errorMsg = new ModelAccessHelper(project.getRepository()).runReadAction(new Computable<String>() {
public String compute() {
JUnitRunTypes chosenType = getJUnitRunType();
return chosenType.check(settings, project);
}
});
if ((errorMsg != null && errorMsg.length() > 0)) {
throw new RuntimeConfigurationError(errorMsg);
}
}
private void checkInProcessRunIsSingle() throws RuntimeConfigurationException {
if (this.getInProcess() && JUnitInProcessExecutor.getRunState().get() != RunStateEnum.IDLE) {
throw new RuntimeConfigurationError("There is already another instance running tests in-process. Only one instance is allowed to run in-process.");
}
}
private void checkCachesDirIsFreeToLock() throws RuntimeConfigurationException {
if (!(this.getInProcess()) && this.getReuseCaches() && !(canSaveCachesPath())) {
throw new RuntimeConfigurationError("The chosen caches directory is already locked by another run. Please choose another one.");
}
}
public List<ITestNodeWrapper> getTestsUnderProgress(final MPSProject project) {
return ListSequence.fromList(collectTests(project)).toListSequence();
}
public List<SNodeReference> getTestsToMake(final MPSProject project) {
final Reference<List<ITestNodeWrapper>> toTest = new Reference<List<ITestNodeWrapper>>();
ApplicationManager.getApplication().invokeAndWait(new Runnable() {
public void run() {
toTest.set(getTestsUnderProgress(project));
}
}, ModalityState.NON_MODAL);
return ListSequence.fromList(toTest.get()).select(new ISelector<ITestNodeWrapper, SNodeReference>() {
public SNodeReference select(ITestNodeWrapper it) {
return it.getNodePointer();
}
}).toListSequence();
}
private List<ITestNodeWrapper> collectTests(final MPSProject project) {
return getJUnitRunType().collect(this, project);
}
@Override
public JUnitSettings_Configuration clone() {
JUnitSettings_Configuration clone = null;
try {
clone = createCloneTemplate();
clone.myState = (JUnitSettings_Configuration.MyState) myState.clone();
return clone;
} catch (CloneNotSupportedException ex) {
if (LOG.isEnabledFor(Level.ERROR)) {
LOG.error("", ex);
}
}
return clone;
}
public class MyState {
public String myModel;
public String myModule;
public boolean myInProcess = true;
public boolean myReuseCaches = true;
public boolean myDebug = false;
public String myCachesPath = getDefaultPath();
public ClonableList<String> myTestCases = new ClonableList<String>();
public ClonableList<String> myTestMethods = new ClonableList<String>();
public int myRunType = JUnitRunTypes.PROJECT.ordinal();
public MyState() {
}
@Override
public Object clone() throws CloneNotSupportedException {
JUnitSettings_Configuration.MyState state = new JUnitSettings_Configuration.MyState();
state.myModel = myModel;
state.myModule = myModule;
state.myInProcess = myInProcess;
state.myReuseCaches = myReuseCaches;
state.myDebug = myDebug;
state.myCachesPath = myCachesPath;
if (myTestCases != null) {
state.myTestCases = myTestCases.clone();
}
if (myTestMethods != null) {
state.myTestMethods = myTestMethods.clone();
}
state.myRunType = myRunType;
return state;
}
}
public JUnitSettings_Configuration(Project project) {
myProject = project;
}
private final Project myProject;
public JUnitSettings_Configuration createCloneTemplate() {
return new JUnitSettings_Configuration(myProject);
}
public JUnitSettings_Configuration_Editor getEditor() {
return new JUnitSettings_Configuration_Editor(myProject);
}
private static boolean eq_jtq3ac_a0a0g0c(Object a, Object b) {
return (a != null ? a.equals(b) : a == b);
}
}