// Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.java.openapi.roots.impl;

import com.intellij.openapi.roots.ContentIterator;
import com.intellij.openapi.roots.ModuleRootModificationUtil;
import com.intellij.openapi.roots.ProjectRootManager;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.testFramework.IdeaTestCase;
import com.intellij.testFramework.PlatformTestCase;
import com.intellij.testFramework.PsiTestUtil;
import com.intellij.testFramework.VfsTestUtil;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

@PlatformTestCase.WrapInCommand
public class DirectoryIndexRestoreTest extends IdeaTestCase {
  private VirtualFile myTempVFile;
  private String myTestDirPath;

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

    LocalFileSystem fs = LocalFileSystem.getInstance();
    File temp = createTempDirectory();
    myTempVFile = fs.refreshAndFindFileByIoFile(temp);
    assertNotNull(myTempVFile);
    File root = new File(temp, "top/d1/d2/root");
    assertTrue(root.mkdirs());
    VirtualFile rootVFile = fs.refreshAndFindFileByIoFile(root);
    assertNotNull(rootVFile);

    VirtualFile moduleDir = createChildDirectory(rootVFile, "module");
    VirtualFile srcDir = createChildDirectory(moduleDir, "src");
    VirtualFile testDir = createChildDirectory(srcDir, "pkg");
    ModuleRootModificationUtil.setModuleSdk(myModule, null);
    PsiTestUtil.addContentRoot(myModule, moduleDir);
    PsiTestUtil.addSourceRoot(myModule, srcDir);

    myTestDirPath = testDir.getPath();

    // to not interfere with previous test firing vfs events
    VirtualFileManager.getInstance().syncRefresh();
  }

  public void testDeepDeleteAndRecreate() throws IOException {
    AtomicInteger counter = new AtomicInteger(0);
    ContentIterator iterator = file -> {
      boolean found = file.getPath().equals(myTestDirPath);
      if (found) counter.incrementAndGet();
      return !found;
    };
    File topFile = new File(myTempVFile.getPath(), "top");
    File bakFile = new File(myTempVFile.getPath(), "top.bak");
    String topPath = FileUtil.toSystemIndependentName(topFile.getPath());

    ProjectRootManager.getInstance(myProject).getFileIndex().iterateContent(iterator);
    assertEquals(1, counter.get());

    FileUtil.rename(topFile, bakFile);
    List<String> events1 = VfsTestUtil.print(VfsTestUtil.getEvents(() -> myTempVFile.refresh(false, true)));
    assertEquals(Collections.singletonList("D : " + topPath), events1);
    ProjectRootManager.getInstance(myProject).getFileIndex().iterateContent(iterator);
    assertEquals(1, counter.get());

    FileUtil.rename(bakFile, topFile);
    List<String> events2 = VfsTestUtil.print(VfsTestUtil.getEvents(() -> myTempVFile.refresh(false, true)));
    assertEquals(Collections.singletonList("C : " + topPath), events2);
    ProjectRootManager.getInstance(myProject).getFileIndex().iterateContent(iterator);
    assertEquals(2, counter.get());
  }
}
@@ -801,26 +801,21 @@
 va.util.
-Collection
+Array
 s;%0Aimpor
@@ -2342,24 +2342,58 @@
 (), %22top%22);%0A
+    assertTrue(topFile.exists());%0A
     File bak
@@ -2439,24 +2439,59 @@
 %22top.bak%22);%0A
+    assertFalse(bakFile.exists());%0A
     String t
@@ -2833,47 +2833,133 @@
     
-assertEquals(Collections.singletonList(
+String bakPath = FileUtil.toSystemIndependentName(bakFile.getPath());%0A    assertSameElements(Arrays.asList(%22C : %22 + bakPath, 
 %22D :
@@ -3269,41 +3269,53 @@
 sert
-Equals(Collections.singletonList(
+SameElements(Arrays.asList(%22D : %22 + bakPath, 
 %22C :

test fix