package com.intellij.compiler;

import com.intellij.openapi.module.Module;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;

import static com.intellij.util.io.TestFileSystemItem.fs;

/**
 * @author nik
 */
public class JavaCompilerBasicTest extends BaseCompilerTestCase {

  public void testAddRemoveJavaClass() throws IOException {
    final VirtualFile file = createFile("src/A.java", "public class A {}");
    VirtualFile srcRoot = file.getParent();
    final Module module = addModule("a", srcRoot);
    make(module);
    assertOutput(module, fs().file("A.class"));

    //perform second make to clear BuildManager.ProjectData.myNeedRescan which was set by 'rootsChanged' event caused by creation of output directory
    make(module);
    assertOutput(module, fs().file("A.class"));

    File b = new File(VfsUtilCore.virtualToIoFile(srcRoot), "B.java");
    FileUtil.writeToFile(b, "public class B{}");
    LocalFileSystem.getInstance().refreshIoFiles(Collections.singletonList(b));
    make(module);
    assertOutput(module, fs().file("A.class").file("B.class"));

    deleteFile(file);
    make(module);
    assertOutput(module, fs().file("B.class"));
  }


  public void testSymlinksInSources() throws IOException {
    final VirtualFile file = createFile("src/A.java", "public class A {}");
    VirtualFile srcRoot = file.getParent();
    final File linkFile = new File(srcRoot.getParent().getPath(), "src-link");
    FileUtil.delete(linkFile); // ensure the link does not exist
    String symlink = Files.createSymbolicLink(linkFile.toPath(), Paths.get(srcRoot.getPath())).getFileName().toString();
    srcRoot.getParent().refresh(false, true);
    VirtualFile sourceRoot = srcRoot.getParent().findChild(symlink);
    assertNotNull(sourceRoot);
    final Module module = addModule("a", sourceRoot);

    make(module);
    assertOutput(module, fs().file("A.class"));
  }
}

@@ -1879,83 +1879,104 @@
 ();%0A
+%0A
     
-s
+VirtualFile sou
 rc
+e
 Root
-.getParent().refresh(false, true);%0A    VirtualFile sourceRoot = 
+ = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(new File(
 srcR
@@ -1995,26 +1995,27 @@
 t().
-findChild(
+getPath(), 
 symlink)
 ;%0A  
@@ -2010,16 +2010,17 @@
 symlink)
+)
 ;%0A    as

simplify for IDEA-CR-42678