<commit_before>package org.jetbrains.plugins.cbt.project

import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.externalSystem.service.project.ProjectDataManager
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.project.Project
import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory
import org.jetbrains.plugins.cbt.project.settings.CbtExecutionSettings
import org.jetbrains.plugins.cbt.project.template.CbtProjectImporter
import org.junit.Assert._
import org.junit.Test

class ProjectImportTest {
  @Test
  def testSimple(): Unit = {
    val buildInfo =
      """<project name="NAME" root="ROOT" rootModule="module">
        |    <modules>
        |        <module name="rootModule" root="/projects/testProject"
        |                target="ROOT/target" scalaVersion="2.11.8" type="default">
        |            <sourceDirs>
        |                <dir>ROOT/src</dir>
        |            </sourceDirs>
        |        </module>
        |    </modules>
        |</project>""".stripMargin
    val project = importProject("testSimple", buildInfo)
    val moduleNames = ModuleManager.getInstance(project).getModules.map(_.getName).toSeq
    assertEquals(Seq("rootModule"), moduleNames)
  }

  private def importProject(name: String, buildInfo: String): Project = {
    val testFixture = IdeaTestFixtureFactory.getFixtureFactory.createFixtureBuilder(name).getFixture
    testFixture.setUp()
    val project = testFixture.getProject
    val xml = scala.xml.XML.loadString(buildInfo.replaceAllLiterally("NAME", name).replaceAllLiterally("ROOT", project.getBasePath))
    val settings = new CbtExecutionSettings(project.getBasePath, false, true, true, Seq.empty)
    CbtProjectImporter.importProject(xml, settings)
      .map { projectNode =>
        ServiceManager.getService(classOf[ProjectDataManager]).importData(projectNode, project, true)
      }
      .get
    project
  }<commit_msg>Now project test works with cbt project model<commit_after>package org.jetbrains.plugins.cbt.project

import java.io.File

import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.externalSystem.service.project.ProjectDataManager
import com.intellij.openapi.module.ModuleManager
import com.intellij.openapi.project
import com.intellij.testFramework.fixtures.IdeaTestFixtureFactory
import org.jetbrains.plugins.cbt._
import org.jetbrains.plugins.cbt.project.model.{CbtProjectConverter, CbtProjectInfo}
import org.jetbrains.plugins.cbt.project.settings.CbtExecutionSettings
import org.junit.Assert._
import org.junit.Test
class ProjectImportTest {
  @Test
  def testSimple(): Unit = {
    import CbtProjectInfo._
    val projectInfo = Project(name = "NAME",
      root = "ROOT".toFile,
      modules = Seq(Module(
        name = "rootModule",
        root = "ROOT".toFile,
        sourceDirs = Seq.empty,
        scalaVersion = "2.11.8",
        target = "ROOT/target".toFile,
        moduleType = ModuleType.Default,
        binaryDependencies = Seq.empty,
        moduleDependencies = Seq.empty,
        classpath = Seq.empty,
        parentBuild = None,
        scalacOptions = Seq.empty
      )),
      libraries = Seq.empty,
      cbtLibraries = Seq.empty,
      scalaCompilers = Seq.empty)

    val project = importProject("testSimple", projectInfo)
    val actualModuleNames = ModuleManager.getInstance(project).getModules.map(_.getName).toSeq
    val expectedModuleNames = projectInfo.modules.map(_.name)
    assertEquals(expectedModuleNames, actualModuleNames)
  }

  def replacePaths(project: CbtProjectInfo.Project, name: String, path: String): CbtProjectInfo.Project = {
    import CbtProjectInfo._
    def replace(obj: AnyRef): AnyRef = obj match {
      case project: Project =>
        project.copy(root = replace(project.root).asInstanceOf[File],
          modules = project.modules.map(replace(_).asInstanceOf[Module]),
          libraries = project.libraries.map(replace(_).asInstanceOf[Library]),
          cbtLibraries = project.cbtLibraries.map(replace(_).asInstanceOf[CbtProjectInfo.Library]),
          scalaCompilers = project.scalaCompilers.map(replace(_).asInstanceOf[CbtProjectInfo.ScalaCompiler])
        )
      case module: Module =>
        module.copy(root = replace(module.root).asInstanceOf[File],
          sourceDirs = module.sourceDirs.map(replace(_).asInstanceOf[File]),
          target = replace(module.target).asInstanceOf[File]
        )
      case file: File =>
        file.getPath.replaceAllLiterally("NAME", name).replaceAllLiterally("ROOT", path).toFile
    }

    replace(project).asInstanceOf[Project]
  }
  private def importProject(name: String, projectInfo: CbtProjectInfo.Project): project.Project = {
    val testFixture = IdeaTestFixtureFactory.getFixtureFactory.createFixtureBuilder(name).getFixture
    testFixture.setUp()
    val project = testFixture.getProject
    val settings = new CbtExecutionSettings(project.getBasePath, false, true, true, Seq.empty)
    CbtProjectConverter(replacePaths(projectInfo, name, project.getBasePath), settings)
      .map { projectNode =>
        ServiceManager.getService(classOf[ProjectDataManager]).importData(projectNode, project, true)
      }
      .get
    project
  }
{'fraction_non_alphanumeric': 0.10666666666666667, 'fraction_numerical': 0.0015238095238095239, 'mean_word_length': 3.7094170403587445, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 3}, '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': '6742743', 'n_tokens_mistral': 1533, 'n_tokens_neox': 1508, 'n_words': 256}