<commit_before>package krasa.mavenrun.action;

import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

import krasa.mavenrun.analyzer.ComparableVersion;

import org.apache.commons.lang.StringUtils;
import org.apache.maven.shared.utils.io.MatchPatterns;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.maven.execution.MavenRunConfigurationType;
import org.jetbrains.idea.maven.execution.MavenRunnerParameters;
import org.jetbrains.idea.maven.model.MavenPlugin;
import org.jetbrains.idea.maven.project.MavenProject;
import org.jetbrains.idea.maven.utils.actions.MavenActionUtil;

import com.intellij.execution.RunnerAndConfigurationSettings;
import com.intellij.execution.actions.BaseRunConfigurationAction;
import com.intellij.execution.actions.ConfigurationContext;
import com.intellij.execution.configurations.LocatableConfiguration;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiJavaFile;
import icons.MavenIcons;

public class RunTestFileAction extends DumbAwareAction {
	private final Logger LOG = Logger.getInstance("#" + getClass().getCanonicalName());

	public RunTestFileAction() {
		super("Test file", "Run current File with Maven", MavenIcons.MavenLogo);
	}

	public RunTestFileAction(String text, @Nullable String description, @Nullable Icon icon) {
		super(text, description, icon);
	}

	public void actionPerformed(AnActionEvent e) {
		MavenProject mavenProject = MavenActionUtil.getMavenProject(e.getDataContext());
		if (mavenProject != null) {

			PsiFile psiFile = LangDataKeys.PSI_FILE.getData(e.getDataContext());
			if (psiFile instanceof PsiJavaFile) {
				List<String> goals = getGoals(e, (PsiJavaFile) psiFile,
						MavenActionUtil.getMavenProject(e.getDataContext()));

				final DataContext context = e.getDataContext();
				MavenRunnerParameters params = new MavenRunnerParameters(true, mavenProject.getDirectory(), goals,
						MavenActionUtil.getProjectsManager(context).getExplicitProfiles());
				run(context, params);
			} else {
				Messages.showWarningDialog(e.getProject(), "Cannot run for current file", "Maven Test File");
			}
		}
	}

	protected void run(DataContext context, MavenRunnerParameters params) {
		MavenRunConfigurationType.runConfiguration(MavenActionUtil.getProject(context), params, null);
	}

	protected List<String> getGoals(AnActionEvent e, PsiJavaFile psiFile, MavenProject mavenProject) {
		List<String> goals = new ArrayList<String>();
		boolean skipTests = isSkipTests(mavenProject);
		goals.add("-DfailIfNoTests=true");
		//so many possibilities...
		if (hasFailSafe(mavenProject) && (skipTests || isExcludedFromSurefire(psiFile, mavenProject))) {
			goals.add("test-compile");
			addFailSafeParameters(e, psiFile, mavenProject, goals);
			goals.add("failsafe:integration-test");
//		} else if (skipTests) { //todo fail or verify?
//			goals.add("-Dtest=" + getTestArgument(e, psiFile));
//			addFailSafeParameters(e, psiFile, mavenProject, goals);
//			goals.add("verify");
		} else {
			goals.add("-Dtest=" + getTestArgument(e, psiFile));
			goals.add("test-compile");
			goals.add("surefire:test");
		}

		return goals;
	}

	private boolean hasFailSafe(MavenProject mavenProject) {
		return getFailsafe(mavenProject) !=null;
	}

	private void addFailSafeParameters(AnActionEvent e, PsiJavaFile psiFile, MavenProject mavenProject,
			List<String> goals) {
		MavenPlugin mavenProjectPlugin = getFailsafe(mavenProject);
		if (mavenProjectPlugin != null) {
			ComparableVersion version = new ComparableVersion(mavenProjectPlugin.getVersion());
			ComparableVersion minimumForMethodTest = new ComparableVersion("2.7.3");
			if (minimumForMethodTest.compareTo(version) == 1) {
				goals.add("-Dit.test=" + getTestArgumentWithoutMethod(e, psiFile));
			} else {
				goals.add("-Dit.test=" + getTestArgument(e, psiFile));
			}
		}
	}

	private MavenPlugin getFailsafe(MavenProject mavenProject) {
		return mavenProject.findPlugin("org.apache.maven.plugins", "maven-failsafe-plugin");
	}

	private boolean isExcludedFromSurefire(PsiJavaFile psiFile, MavenProject mavenProject) {
		boolean excluded = false;<commit_msg>#3 use verify - failsafe:integration-test is not user friendly<commit_after>package krasa.mavenrun.action;

import java.util.ArrayList;
import java.util.List;

import javax.swing.*;

import krasa.mavenrun.analyzer.ComparableVersion;

import org.apache.commons.lang.StringUtils;
import org.apache.maven.shared.utils.io.MatchPatterns;
import org.jdom.Element;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.idea.maven.execution.MavenRunConfigurationType;
import org.jetbrains.idea.maven.execution.MavenRunnerParameters;
import org.jetbrains.idea.maven.model.MavenPlugin;
import org.jetbrains.idea.maven.project.MavenProject;
import org.jetbrains.idea.maven.utils.actions.MavenActionUtil;

import com.intellij.execution.RunnerAndConfigurationSettings;
import com.intellij.execution.actions.BaseRunConfigurationAction;
import com.intellij.execution.actions.ConfigurationContext;
import com.intellij.execution.configurations.LocatableConfiguration;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.project.DumbAwareAction;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiFile;
import com.intellij.psi.PsiJavaFile;
import icons.MavenIcons;

public class RunTestFileAction extends DumbAwareAction {
	private final Logger LOG = Logger.getInstance("#" + getClass().getCanonicalName());

	public RunTestFileAction() {
		super("Test file", "Run current File with Maven", MavenIcons.MavenLogo);
	}

	public RunTestFileAction(String text, @Nullable String description, @Nullable Icon icon) {
		super(text, description, icon);
	}

	public void actionPerformed(AnActionEvent e) {
		MavenProject mavenProject = MavenActionUtil.getMavenProject(e.getDataContext());
		if (mavenProject != null) {

			PsiFile psiFile = LangDataKeys.PSI_FILE.getData(e.getDataContext());
			if (psiFile instanceof PsiJavaFile) {
				List<String> goals = getGoals(e, (PsiJavaFile) psiFile,
						MavenActionUtil.getMavenProject(e.getDataContext()));

				final DataContext context = e.getDataContext();
				MavenRunnerParameters params = new MavenRunnerParameters(true, mavenProject.getDirectory(), goals,
						MavenActionUtil.getProjectsManager(context).getExplicitProfiles());
				run(context, params);
			} else {
				Messages.showWarningDialog(e.getProject(), "Cannot run for current file", "Maven Test File");
			}
		}
	}

	protected void run(DataContext context, MavenRunnerParameters params) {
		MavenRunConfigurationType.runConfiguration(MavenActionUtil.getProject(context), params, null);
	}

	protected List<String> getGoals(AnActionEvent e, PsiJavaFile psiFile, MavenProject mavenProject) {
		List<String> goals = new ArrayList<String>();
		boolean skipTests = isSkipTests(mavenProject);
		goals.add("-DfailIfNoTests=true");
		// so many possibilities...
		if (skipTests || isExcludedFromSurefire(psiFile, mavenProject)) {
			addFailSafeParameters(e, psiFile, mavenProject, goals);
			goals.add("verify");
		} else {
			addSurefureParameters(e, psiFile, goals);
			goals.add("test-compile");
			goals.add("surefire:test");
		}

		return goals;
	}

	private void addSurefureParameters(AnActionEvent e, PsiJavaFile psiFile, List<String> goals) {
		goals.add("-Dtest=" + getTestArgument(e, psiFile));
	}

	private void addFailSafeParameters(AnActionEvent e, PsiJavaFile psiFile, MavenProject mavenProject,
			List<String> goals) {
		MavenPlugin mavenProjectPlugin = mavenProject.findPlugin("org.apache.maven.plugins", "maven-failsafe-plugin");
		if (mavenProjectPlugin != null) {
			ComparableVersion version = new ComparableVersion(mavenProjectPlugin.getVersion());
			ComparableVersion minimumForMethodTest = new ComparableVersion("2.7.3");
			if (minimumForMethodTest.compareTo(version) == 1) {
				goals.add("-Dit.test=" + getTestArgumentWithoutMethod(e, psiFile));
			} else {
				goals.add("-Dit.test=" + getTestArgument(e, psiFile));
			}
		}
	}

	private boolean isExcludedFromSurefire(PsiJavaFile psiFile, MavenProject mavenProject) {
		boolean excluded = false;
{'fraction_non_alphanumeric': 0.11353661353661354, 'fraction_numerical': 0.0010395010395010396, 'mean_word_length': 5.916134185303514, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3307046', 'n_tokens_mistral': 2929, 'n_tokens_neox': 2689, 'n_words': 497}