package me.artspb.idea.pitest.plugin.configuration.junit;

import com.intellij.execution.junit.JUnitConfiguration;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.JavaDirectoryService;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiManager;
import com.intellij.psi.PsiPackage;
import me.artspb.idea.pitest.plugin.configuration.PitestConfiguration;

import static me.artspb.idea.pitest.plugin.Constants.ANY_IN_PACKAGE_PATTERN;
import static org.apache.commons.lang.StringUtils.isEmpty;

/**
 * @author Artem Khvastunov &lt;contact@artspb.me&gt;
 */
class DirectoryBasedConfigurator extends Configurator {

    @Override
    public void configure(JUnitConfiguration jUnitConfiguration, PitestConfiguration configuration) {
        JUnitConfiguration.Data data = jUnitConfiguration.getPersistentData();
        String targetTests = "";
        String targetClasses = "";
        String dirName = data.getDirName();
        VirtualFile dir = LocalFileSystem.getInstance().findFileByPath(FileUtil.toSystemIndependentName(dirName));
        if (dir != null) {
            PsiDirectory directory = PsiManager.getInstance(jUnitConfiguration.getProject()).findDirectory(dir);
            if (directory != null) {
                PsiPackage psiPackage = JavaDirectoryService.getInstance().getPackage(directory);
                if (psiPackage != null) {
                    String packageName = psiPackage.getQualifiedName();
                    targetTests = isEmpty(packageName) ? "" : packageName + ANY_IN_PACKAGE_PATTERN;
                    targetClasses = targetTests;
                }
            }
        }
        configure(configuration, targetTests, targetClasses);
    }
}

@@ -108,16 +108,121 @@
 ration;%0A
+import com.intellij.openapi.application.ApplicationManager;%0Aimport com.intellij.openapi.util.Computable;%0A
 import c
@@ -877,16 +877,22 @@
 nfigure(
+final 
 JUnitCon
@@ -1158,16 +1158,22 @@
         
+final 
 VirtualF
@@ -1334,16 +1334,200 @@
 ectory =
+ ApplicationManager.getApplication().runReadAction(new Computable%3CPsiDirectory%3E() %7B%0A                @Override%0A                public PsiDirectory compute() %7B%0A                    return
  PsiMana
@@ -1595,16 +1595,50 @@
 y(dir);%0A
+                %7D%0A            %7D);%0A
         

access PSI in read action