<commit_before>package org.jetbrains.plugins.scala.performance

import java.io.File
import java.util

import com.intellij.ide.highlighter.JavaFileType
import com.intellij.lang.javascript.boilerplate.GithubDownloadUtil
import com.intellij.openapi.externalSystem.model.ProjectSystemId
import com.intellij.openapi.externalSystem.settings.ExternalProjectSettings
import com.intellij.openapi.externalSystem.test.ExternalSystemImportingTestCase
import com.intellij.openapi.projectRoots.ProjectJdkTable
import com.intellij.openapi.projectRoots.impl.JavaAwareProjectJdkTableImpl
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.vfs.newvfs.impl.VfsRootAccess
import com.intellij.openapi.vfs.{LocalFileSystem, VirtualFile}
import com.intellij.platform.templates.github.ZipUtil
import com.intellij.psi.search.{FileTypeIndex, GlobalSearchScope}
import com.intellij.testFramework.IdeaTestUtil
import org.jetbrains.SbtStructureSetup
import org.jetbrains.plugins.scala.finder.SourceFilterScope
import org.jetbrains.plugins.scala.util.TestUtils
import org.jetbrains.plugins.scala.{ScalaFileType, extensions}
import org.jetbrains.sbt.project.SbtProjectSystem
import org.jetbrains.sbt.project.settings.SbtProjectSettings
import org.junit.Assert

/**
  * Author: <NAME>
  * Date: 11/17/2015
  */
abstract class DownloadingAndImportingTestCase extends ExternalSystemImportingTestCase with SbtStructureSetup {

  implicit class IntExt(val i: Int) {
    def seconds: Int = i * 1000
  }

  override protected def getCurrentExternalProjectSettings: ExternalProjectSettings = {
    val settings = new SbtProjectSettings
    val internalSdk = JavaAwareProjectJdkTableImpl.getInstanceEx.getInternalJdk
    val sdk = if (internalSdk == null) IdeaTestUtil.getMockJdk18
    else internalSdk
    settings.setJdk(sdk.getName)
    settings.setCreateEmptyContentRootDirectories(true)
    settings
  }

  override protected def getExternalSystemId: ProjectSystemId = SbtProjectSystem.Id

  override protected def getTestsTempDir: String = ""

  def rootDirPath: String = s"${TestUtils.getTestDataPath}/projects"

  def projectDirPath: String = s"$rootDirPath/$githubRepoName"

  def downloadURL: String = s"https://github.com/$githubUsername/$githubRepoName/archive/$revision.zip"

  def outputZipFileName = s"$rootDirPath/zipFiles/$githubRepoName-$githubUsername-$revision"

  override def setUpInWriteAction(): Unit = {
    super.setUpInWriteAction()
    val outputZipFile = new File(outputZipFileName)
    val projectDir = new File(projectDirPath)
    if (!outputZipFile.exists() && !projectDir.exists()) {
      //don't download if zip file is already there
      GithubDownloadUtil.downloadAtomically(null, downloadURL, outputZipFile, githubUsername, githubRepoName)
    }
    if (!projectDir.exists()) {
      //don't unpack if the project is already unpacked
      ZipUtil.unzip(null, projectDir, outputZipFile, null, null, true)
    }
    Assert.assertTrue("Project dir does not exist. Download or unpack failed!", projectDir.exists())
    myProjectRoot = LocalFileSystem.getInstance.refreshAndFindFileByIoFile(projectDir)
    setUpSbtLauncherAndStructure(myProject)
    extensions.inWriteAction {
      val internalSdk = JavaAwareProjectJdkTableImpl.getInstanceEx.getInternalJdk
      val sdk = if (internalSdk == null) IdeaTestUtil.getMockJdk17
      else internalSdk

      if (ProjectJdkTable.getInstance().findJdk(sdk.getName) == null) {
        ProjectJdkTable.getInstance().addJdk(sdk)
      }
      ProjectRootManager.getInstance(myProject).setProjectSdk(sdk)
    }
  }

  override def setUp(): Unit = {
    super.setUp()

    VfsRootAccess.SHOULD_PERFORM_ACCESS_CHECK = false
    importProject()
  }

  def findFile(filename: String): VirtualFile = {
    import scala.collection.JavaConversions._
    val searchScope =
      new SourceFilterScope(GlobalSearchScope.getScopeRestrictedByFileTypes(GlobalSearchScope.projectScope(myProject),<commit_msg>Fixed compilation.<commit_after>package org.jetbrains.plugins.scala.performance

import java.io.File
import java.util

import com.intellij.ide.highlighter.JavaFileType
import com.intellij.lang.javascript.boilerplate.GithubDownloadUtil
import com.intellij.openapi.externalSystem.model.ProjectSystemId
import com.intellij.openapi.externalSystem.settings.ExternalProjectSettings
import com.intellij.openapi.externalSystem.test.ExternalSystemImportingTestCase
import com.intellij.openapi.projectRoots.ProjectJdkTable
import com.intellij.openapi.projectRoots.impl.JavaAwareProjectJdkTableImpl
import com.intellij.openapi.roots.ProjectRootManager
import com.intellij.openapi.vfs.{LocalFileSystem, VirtualFile}
import com.intellij.platform.templates.github.ZipUtil
import com.intellij.psi.search.{FileTypeIndex, GlobalSearchScope}
import com.intellij.testFramework.IdeaTestUtil
import org.jetbrains.SbtStructureSetup
import org.jetbrains.plugins.scala.finder.SourceFilterScope
import org.jetbrains.plugins.scala.util.TestUtils
import org.jetbrains.plugins.scala.{ScalaFileType, extensions}
import org.jetbrains.sbt.project.SbtProjectSystem
import org.jetbrains.sbt.project.settings.SbtProjectSettings
import org.junit.Assert

/**
  * Author: <NAME>
  * Date: 11/17/2015
  */
abstract class DownloadingAndImportingTestCase extends ExternalSystemImportingTestCase with SbtStructureSetup {

  implicit class IntExt(val i: Int) {
    def seconds: Int = i * 1000
  }

  override protected def getCurrentExternalProjectSettings: ExternalProjectSettings = {
    val settings = new SbtProjectSettings
    val internalSdk = JavaAwareProjectJdkTableImpl.getInstanceEx.getInternalJdk
    val sdk = if (internalSdk == null) IdeaTestUtil.getMockJdk18
    else internalSdk
    settings.setJdk(sdk.getName)
    settings.setCreateEmptyContentRootDirectories(true)
    settings
  }

  override protected def getExternalSystemId: ProjectSystemId = SbtProjectSystem.Id

  override protected def getTestsTempDir: String = ""

  def rootDirPath: String = s"${TestUtils.getTestDataPath}/projects"

  def projectDirPath: String = s"$rootDirPath/$githubRepoName"

  def downloadURL: String = s"https://github.com/$githubUsername/$githubRepoName/archive/$revision.zip"

  def outputZipFileName = s"$rootDirPath/zipFiles/$githubRepoName-$githubUsername-$revision"

  override def setUpInWriteAction(): Unit = {
    super.setUpInWriteAction()
    val outputZipFile = new File(outputZipFileName)
    val projectDir = new File(projectDirPath)
    if (!outputZipFile.exists() && !projectDir.exists()) {
      //don't download if zip file is already there
      GithubDownloadUtil.downloadAtomically(null, downloadURL, outputZipFile, githubUsername, githubRepoName)
    }
    if (!projectDir.exists()) {
      //don't unpack if the project is already unpacked
      ZipUtil.unzip(null, projectDir, outputZipFile, null, null, true)
    }
    Assert.assertTrue("Project dir does not exist. Download or unpack failed!", projectDir.exists())
    myProjectRoot = LocalFileSystem.getInstance.refreshAndFindFileByIoFile(projectDir)
    setUpSbtLauncherAndStructure(myProject)
    extensions.inWriteAction {
      val internalSdk = JavaAwareProjectJdkTableImpl.getInstanceEx.getInternalJdk
      val sdk = if (internalSdk == null) IdeaTestUtil.getMockJdk17
      else internalSdk

      if (ProjectJdkTable.getInstance().findJdk(sdk.getName) == null) {
        ProjectJdkTable.getInstance().addJdk(sdk)
      }
      ProjectRootManager.getInstance(myProject).setProjectSdk(sdk)
    }
  }

  override def setUp(): Unit = {
    super.setUp()

    importProject()
  }

  def findFile(filename: String): VirtualFile = {
    import scala.collection.JavaConversions._
    val searchScope =
      new SourceFilterScope(GlobalSearchScope.getScopeRestrictedByFileTypes(GlobalSearchScope.projectScope(myProject),
{'fraction_non_alphanumeric': 0.08632817495843459, 'fraction_numerical': 0.004092594960992455, 'mean_word_length': 5.191607284243863, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5047450', 'n_tokens_mistral': 2382, 'n_tokens_neox': 2271, 'n_words': 420}