<commit_before>package org.jetbrains.idea.maven.repository;

import com.intellij.openapi.progress.EmptyProgressIndicator;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import org.apache.maven.embedder.MavenEmbedder;
import org.apache.maven.embedder.MavenEmbedderException;
import org.jetbrains.idea.maven.MavenImportingTestCase;
import org.jetbrains.idea.maven.embedder.MavenEmbedderFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;

public class MavenIndicesTest extends MavenImportingTestCase {
  private MavenWithDataTestFixture myDataTestFixture;
  private MavenIndices myIndices;
  private MavenEmbedder myEmbedder;
  private File myIndexDir;

  @Override
  public void setUp() throws Exception {
    super.setUp();

    myDataTestFixture = new MavenWithDataTestFixture(myDir);
    myDataTestFixture.setUp();

    initIndex();
  }

  @Override
  protected void tearDown() throws Exception {
    shutdownIndex();
    super.tearDown();
  }

  private void initIndex() throws Exception {
    initIndex("index");
  }

  private void initIndex(String relativeDir) {
    myEmbedder = MavenEmbedderFactory.createEmbedderForExecute(getMavenCoreSettings());
    myIndexDir = new File(myDir, relativeDir);
    myIndices = new MavenIndices(myEmbedder, myIndexDir);
  }

  private void shutdownIndex() throws MavenEmbedderException {
    myIndices.close();
    myEmbedder.stop();
  }

  public void testAddingLocal() throws Exception {
    MavenIndex i = new LocalMavenIndex(myDataTestFixture.getTestDataPath("local1"));
    myIndices.add(i);
    myIndices.update(i, myProject, new EmptyProgressIndicator());

    assertUnorderedElementsAreEqual(myIndices.getGroupIds(), "junit");
  }

  public void testAddingSeveral() throws Exception {
    MavenIndex i1 = new LocalMavenIndex(myDataTestFixture.getTestDataPath("local1"));
    MavenIndex i2 = new LocalMavenIndex(myDataTestFixture.getTestDataPath("local2"));
    myIndices.add(i1);
    myIndices.add(i2);
    myIndices.update(i1, myProject, new EmptyProgressIndicator());
    myIndices.update(i2, myProject, new EmptyProgressIndicator());

    assertUnorderedElementsAreEqual(myIndices.getGroupIds(), "junit", "jmock");
  }

  public void testAddingWithoutUpdate() throws Exception {
    myIndices.add(new LocalMavenIndex(myDataTestFixture.getTestDataPath("local1")));
    assertTrue(myIndices.getGroupIds().isEmpty());
  }

  public void testUpdatingLocalClearsPreviousIndex() throws Exception {
    MavenIndex i = new LocalMavenIndex(myDataTestFixture.getTestDataPath("local1"));
    myIndices.add(i);

    myIndices.update(i, myProject, new EmptyProgressIndicator());
    assertUnorderedElementsAreEqual(myIndices.getGroupIds(), "junit");

    myIndices.change(i, myDataTestFixture.getTestDataPath("local2"));
    myIndices.update(i, myProject, new EmptyProgressIndicator());
    assertUnorderedElementsAreEqual(myIndices.getGroupIds(), "jmock");
  }

  public void testChanging() throws Exception {
    MavenIndex i = new LocalMavenIndex(myDataTestFixture.getTestDataPath("local1"));
    myIndices.add(i);

    myIndices.update(i, myProject, new EmptyProgressIndicator());
    assertUnorderedElementsAreEqual(myIndices.getGroupIds(), "junit");

    myIndices.change(i, myDataTestFixture.getTestDataPath("local2"));
    myIndices.update(i, myProject, new EmptyProgressIndicator());

    assertUnorderedElementsAreEqual(myIndices.getGroupIds(), "jmock");
  }

  public void testAddingRemote() throws Exception {
    MavenIndex i = new RemoteMavenIndex("file:///" + myDataTestFixture.getTestDataPath("remote"));
    myIndices.add(i);
    myIndices.update(i, myProject, new EmptyProgressIndicator());

    assertUnorderedElementsAreEqual(myIndices.getGroupIds(), "junit");
  }

  public void testUpdatingRemote() throws Exception {
    if (ignore()) return;
    
    MavenIndex i = new RemoteMavenIndex("file:///" + myDataTestFixture.getTestDataPath("remote"));
    myIndices.add(i);
    myIndices.update(i, myProject, new EmptyProgressIndicator());

    //shouldn't throw 'The existing index is for repository [remote] and not for repository [xxx]'
    myIndices.update(i, myProject, new EmptyProgressIndicator());
  }

  public void testAddingProjectIndex() throws Exception {
    createProjectPom("<groupId>group</groupId>" +
                     "<artifactId>project</artifactId>" +
                     "<version>version</version>");

    VirtualFile m1 = createModulePom("m1",<commit_msg>Maven: test ignored<commit_after>package org.jetbrains.idea.maven.repository;

import com.intellij.openapi.progress.EmptyProgressIndicator;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VirtualFile;
import org.apache.maven.embedder.MavenEmbedder;
import org.apache.maven.embedder.MavenEmbedderException;
import org.jetbrains.idea.maven.MavenImportingTestCase;
import org.jetbrains.idea.maven.embedder.MavenEmbedderFactory;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;

public class MavenIndicesTest extends MavenImportingTestCase {
  private MavenWithDataTestFixture myDataTestFixture;
  private MavenIndices myIndices;
  private MavenEmbedder myEmbedder;
  private File myIndexDir;

  @Override
  public void setUp() throws Exception {
    super.setUp();

    myDataTestFixture = new MavenWithDataTestFixture(myDir);
    myDataTestFixture.setUp();

    initIndex();
  }

  @Override
  protected void tearDown() throws Exception {
    shutdownIndex();
    super.tearDown();
  }

  private void initIndex() throws Exception {
    initIndex("index");
  }

  private void initIndex(String relativeDir) {
    myEmbedder = MavenEmbedderFactory.createEmbedderForExecute(getMavenCoreSettings());
    myIndexDir = new File(myDir, relativeDir);
    myIndices = new MavenIndices(myEmbedder, myIndexDir);
  }

  private void shutdownIndex() throws MavenEmbedderException {
    myIndices.close();
    myEmbedder.stop();
  }

  public void testAddingLocal() throws Exception {
    MavenIndex i = new LocalMavenIndex(myDataTestFixture.getTestDataPath("local1"));
    myIndices.add(i);
    myIndices.update(i, myProject, new EmptyProgressIndicator());

    assertUnorderedElementsAreEqual(myIndices.getGroupIds(), "junit");
  }

  public void testAddingSeveral() throws Exception {
    MavenIndex i1 = new LocalMavenIndex(myDataTestFixture.getTestDataPath("local1"));
    MavenIndex i2 = new LocalMavenIndex(myDataTestFixture.getTestDataPath("local2"));
    myIndices.add(i1);
    myIndices.add(i2);
    myIndices.update(i1, myProject, new EmptyProgressIndicator());
    myIndices.update(i2, myProject, new EmptyProgressIndicator());

    assertUnorderedElementsAreEqual(myIndices.getGroupIds(), "junit", "jmock");
  }

  public void testAddingWithoutUpdate() throws Exception {
    myIndices.add(new LocalMavenIndex(myDataTestFixture.getTestDataPath("local1")));
    assertTrue(myIndices.getGroupIds().isEmpty());
  }

  public void testUpdatingLocalClearsPreviousIndex() throws Exception {
    MavenIndex i = new LocalMavenIndex(myDataTestFixture.getTestDataPath("local1"));
    myIndices.add(i);

    myIndices.update(i, myProject, new EmptyProgressIndicator());
    assertUnorderedElementsAreEqual(myIndices.getGroupIds(), "junit");

    myIndices.change(i, myDataTestFixture.getTestDataPath("local2"));
    myIndices.update(i, myProject, new EmptyProgressIndicator());
    assertUnorderedElementsAreEqual(myIndices.getGroupIds(), "jmock");
  }

  public void testChanging() throws Exception {
    MavenIndex i = new LocalMavenIndex(myDataTestFixture.getTestDataPath("local1"));
    myIndices.add(i);

    myIndices.update(i, myProject, new EmptyProgressIndicator());
    assertUnorderedElementsAreEqual(myIndices.getGroupIds(), "junit");

    myIndices.change(i, myDataTestFixture.getTestDataPath("local2"));
    myIndices.update(i, myProject, new EmptyProgressIndicator());

    assertUnorderedElementsAreEqual(myIndices.getGroupIds(), "jmock");
  }

  public void testAddingRemote() throws Exception {
    MavenIndex i = new RemoteMavenIndex("file:///" + myDataTestFixture.getTestDataPath("remote"));
    myIndices.add(i);
    myIndices.update(i, myProject, new EmptyProgressIndicator());

    assertUnorderedElementsAreEqual(myIndices.getGroupIds(), "junit");
  }

  public void testUpdatingRemote() throws Exception {
    // for some reason nexus holds 'timestamp' file and tearDown cannot delete it and fails.
    if (ignore()) return;

    MavenIndex i = new RemoteMavenIndex("file:///" + myDataTestFixture.getTestDataPath("remote"));
    myIndices.add(i);
    myIndices.update(i, myProject, new EmptyProgressIndicator());

    //shouldn't throw 'The existing index is for repository [remote] and not for repository [xxx]'
    myIndices.update(i, myProject, new EmptyProgressIndicator());
  }

  public void testAddingProjectIndex() throws Exception {
    createProjectPom("<groupId>group</groupId>" +
                     "<artifactId>project</artifactId>" +
                     "<version>version</version>");

    VirtualFile m1 = createModulePom("m1",
{'fraction_non_alphanumeric': 0.11000546746856206, 'fraction_numerical': 0.0034991798797156916, 'mean_word_length': 5.209097080787508, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '143949', 'n_tokens_mistral': 2779, 'n_tokens_neox': 2728, 'n_words': 512}