/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.testFramework;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;

import java.io.File;
import java.io.IOException;
import java.util.Collection;

public class TempFiles {
  private final Collection<File> myFilesToDelete;

  public TempFiles(Collection<File> filesToDelete) {
    myFilesToDelete = filesToDelete;
  }

  public VirtualFile createVFile(String prefix) {
    return getVFileByFile(createTempFile(prefix));
  }

  public VirtualFile createVFile(String prefix, String postfix) {
    return getVFileByFile(createTempFile(prefix, postfix));
  }

  public File createTempFile(String prefix) {
    return createTempFile(prefix, "_Temp_File_");
  }

  public File createTempFile(String prefix, String postfix) {
    try {
      File tempFile = FileUtil.createTempFile(prefix, postfix);
      tempFileCreated(tempFile);
      return tempFile;
    }
    catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  private void tempFileCreated(File tempFile) {
    myFilesToDelete.add(tempFile);
    tempFile.deleteOnExit();
  }

  public static VirtualFile getVFileByFile(File tempFile) {
    refreshVfs();
    return LocalFileSystem.getInstance().findFileByIoFile(tempFile);
  }

  public static void refreshVfs() {
    ApplicationManager.getApplication().runWriteAction(new Runnable() {
      @Override
      public void run() {
        VirtualFileManager.getInstance().refresh(false);
      }
    });
  }

  public File createTempDir() {
    return createTempDir("dir");
  }

  private File createTempDir(String prefix) {
    try {
      File dir = FileUtil.createTempDirectory(prefix, "test");
      tempFileCreated(dir);
      return dir;
    }
    catch (IOException e) {
      throw new RuntimeException(e);
    }
  }

  public VirtualFile createTempVDir() {
    return createTempVDir("dir");
  }

  public VirtualFile createTempVDir(String prefix) {
    return getVFileByFile(createTempDir(prefix));
  }

  public File createTempSubDir(File content) {
    File subDir = new File(content, "source");
    if (!subDir.mkdir()) throw new RuntimeException(subDir.toString());
    tempFileCreated(subDir);
    return subDir;
  }

  public String createTempPath() {
    File tempFile = createTempFile("xxx");
    String absolutePath = tempFile.getAbsolutePath();
    tempFile.delete();
    return absolutePath;
  }

  public void deleteAll() {
    for (File file : myFilesToDelete) {
      FileUtil.delete(file);
    }
  }

  public VirtualFile createVFile(VirtualFile parentDir, String name, String text) {
    try {
      final VirtualFile virtualFile = parentDir.createChildData(this, name);
      VfsUtil.saveText(virtualFile, text + "\n");
      return virtualFile;
    }
    catch (IOException e) {
      throw new RuntimeException(e);
    }
  }
}

@@ -20,10 +20,10 @@
 0-20
-09
+12
  Jet
@@ -635,68 +635,8 @@
 k;%0A%0A
-import com.intellij.openapi.application.ApplicationManager;%0A
 impo
@@ -823,51 +823,42 @@
 ort 
-com.intellij.openapi.vfs.VirtualFileManager
+org.jetbrains.annotations.Nullable
 ;%0A%0Ai
@@ -935,16 +935,60 @@
 ction;%0A%0A
+import static org.junit.Assert.assertTrue;%0A%0A
 public c
@@ -1146,24 +1146,36 @@
 elete;%0A  %7D%0A%0A
+  @Nullable%0A
   public Vir
@@ -1260,32 +1260,44 @@
 (prefix));%0A  %7D%0A%0A
+  @Nullable%0A
   public Virtual
@@ -1677,24 +1677,56 @@
 (tempFile);%0A
+      getVFileByFile(tempFile);%0A
       return
@@ -1927,32 +1927,44 @@
 eOnExit();%0A  %7D%0A%0A
+  @Nullable%0A
   public static 
@@ -2011,26 +2011,8 @@
 ) %7B%0A
-    refreshVfs();%0A
     
@@ -2048,17 +2048,27 @@
 tance().
-f
+refreshAndF
 indFileB
@@ -2095,236 +2095,8 @@
  %7D%0A%0A
-  public static void refreshVfs() %7B%0A    ApplicationManager.getApplication().runWriteAction(new Runnable() %7B%0A      @Override%0A      public void run() %7B%0A        VirtualFileManager.getInstance().refresh(false);%0A      %7D%0A    %7D);%0A  %7D%0A%0A
   pu
@@ -2304,24 +2304,51 @@
 eated(dir);%0A
+      getVFileByFile(dir);%0A
       return
@@ -2427,32 +2427,44 @@
 (e);%0A    %7D%0A  %7D%0A%0A
+  @Nullable%0A
   public Virtual
@@ -2518,32 +2518,44 @@
 ir(%22dir%22);%0A  %7D%0A%0A
+  @Nullable%0A
   public Virtual
@@ -2650,227 +2650,8 @@
  %7D%0A%0A
-  public File createTempSubDir(File content) %7B%0A    File subDir = new File(content, %22source%22);%0A    if (!subDir.mkdir()) throw new RuntimeException(subDir.toString());%0A    tempFileCreated(subDir);%0A    return subDir;%0A  %7D%0A%0A
   pu
@@ -2777,24 +2777,49 @@
 ePath();%0A   
+ assertTrue(absolutePath,
  tempFile.de
@@ -2824,16 +2824,17 @@
 delete()
+)
 ;%0A    re

Fix refresh-afraid tests