public void testFileLength() throws Exception {
    File file = FileUtil.createTempFile("test", "txt");
    FileUtil.writeToFile(file, "hello");
    VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file);
    assertNotNull(virtualFile);
    String s = VfsUtilCore.loadText(virtualFile);
    assertEquals("hello", s);
    assertEquals(5, virtualFile.getLength());

    FileUtil.writeToFile(file, "new content");
    ((PersistentFSImpl) PersistentFS.getInstance()).cleanPersistedContents();
    s = VfsUtilCore.loadText(virtualFile);
    assertEquals("new content", s);
    assertEquals(11, virtualFile.getLength());
  }
  @NotNull
  private static String findMainClass(VirtualFile gradleHome, VirtualFile script, Project project) {
    final String userDefined = System.getProperty("gradle.launcher.class");
    if (StringUtil.isNotEmpty(userDefined)) {
      return userDefined;
    }

    VirtualFile launcher = gradleHome.findFileByRelativePath("bin/gradle");
    if (launcher == null) {
      launcher = gradleHome.findFileByRelativePath("bin/gradle.bat");
    }
    if (launcher != null) {
      try {
        final String text = StringUtil.convertLineSeparators(VfsUtilCore.loadText(launcher));
        final Matcher matcher = MAIN_CLASS_NAME_PATTERN.matcher(text);
        if (matcher.find()) {
          String candidate = matcher.group(1);
          if (StringUtil.isNotEmpty(candidate)) {
            return candidate;
          }
        }
      } catch (IOException ignored) {
      }
    }

    final PsiFile grFile = PsiManager.getInstance(project).findFile(script);
    if (grFile != null
        && JavaPsiFacade.getInstance(project)
                .findClass("org.gradle.BootstrapMain", grFile.getResolveScope())
            != null) {
      return "org.gradle.BootstrapMain";
    }

    return "org.gradle.launcher.GradleMain";
  }
  public void testHardLinks() throws Exception {
    if (!SystemInfo.isWindows && !SystemInfo.isUnix) {
      System.err.println(getName() + " skipped: " + SystemInfo.OS_NAME);
      return;
    }

    final boolean safeWrite = GeneralSettings.getInstance().isUseSafeWrite();
    final File dir = FileUtil.createTempDirectory("hardlinks.", ".dir", false);
    final SafeWriteRequestor requestor = new SafeWriteRequestor() {};
    try {
      GeneralSettings.getInstance().setUseSafeWrite(false);

      final File targetFile = new File(dir, "targetFile");
      assertTrue(targetFile.createNewFile());
      final File hardLinkFile =
          IoTestUtil.createHardLink(targetFile.getAbsolutePath(), "hardLinkFile");

      final VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(targetFile);
      assertNotNull(file);
      file.setBinaryContent("hello".getBytes("UTF-8"), 0, 0, requestor);
      assertTrue(file.getLength() > 0);

      final VirtualFile check =
          LocalFileSystem.getInstance().refreshAndFindFileByIoFile(hardLinkFile);
      assertNotNull(check);
      assertEquals(file.getLength(), check.getLength());
      assertEquals("hello", VfsUtilCore.loadText(check));
    } finally {
      GeneralSettings.getInstance().setUseSafeWrite(safeWrite);
      FileUtil.delete(dir);
    }
  }
  private void doQuickDocGenerationTestWithCheckExpectedResult(
      Object completionVariant, final String... baseFileNames) throws Exception {
    final DocumentationTestContext context = new DocumentationTestContext(baseFileNames);
    String pathname = getTestDataPath() + baseFileNames[0] + ".expected.html";
    VirtualFile vfile = LocalFileSystem.getInstance().findFileByIoFile(new File(pathname));
    assertNotNull(pathname + " not found", vfile);
    String expectedText = StringUtil.convertLineSeparators(VfsUtilCore.loadText(vfile));
    assertEquals(expectedText, StringUtil.convertLineSeparators(context.generateDoc()));

    if (completionVariant != null) {
      vfile =
          LocalFileSystem.getInstance()
              .findFileByIoFile(
                  new File(getTestDataPath() + baseFileNames[0] + ".expected.completion.html"));
      expectedText = StringUtil.convertLineSeparators(VfsUtilCore.loadText(vfile), "\n");
      assertEquals(
          expectedText,
          StringUtil.convertLineSeparators(
              context.generateDocForCompletion(completionVariant), "\n"));
    }
  }
  public void testFindFileByUrl() throws Exception {

    File file1 = new File(PathManagerEx.getTestDataPath());
    file1 = new File(file1, "vfs");
    file1 = new File(file1, "findFileByUrl");
    VirtualFile file0 = VfsUtil.findFileByURL(file1.toURI().toURL());
    assertNotNull(file0);
    assertTrue(file0.isDirectory());
    final VirtualFile[] children = file0.getChildren();
    List<VirtualFile> list = new ArrayList<VirtualFile>();
    final VirtualFileFilter fileFilter =
        new VirtualFileFilter() {
          @Override
          public boolean accept(VirtualFile file) {
            return !file.getName().endsWith(".new");
          }
        };
    for (VirtualFile child : children) {
      if (fileFilter.accept(child)) {
        list.add(child);
      }
    }
    assertEquals(2, list.size()); // "CVS" dir ignored

    File file2 = new File(file1, "test.zip");
    URL url2 = file2.toURI().toURL();
    url2 = new URL("jar", "", url2.toExternalForm() + "!/");
    url2 = new URL(url2, "com/intellij/installer");
    url2 = new URL(url2.toExternalForm());
    file0 = VfsUtil.findFileByURL(url2);
    assertNotNull(file0);
    assertTrue(file0.isDirectory());

    File file3 = new File(file1, "1.txt");
    file0 = VfsUtil.findFileByURL(file3.toURI().toURL());
    String content = VfsUtilCore.loadText(file0);
    assertNotNull(file0);
    assertFalse(file0.isDirectory());
    assertEquals(content, "test text");
  }
 private static void appendToFile(
     @NotNull VirtualFile file, @NotNull String templateName, @Nullable Map templateAttributes)
     throws ConfigurationException {
   FileTemplateManager manager = FileTemplateManager.getInstance();
   FileTemplate template = manager.getInternalTemplate(templateName);
   try {
     VfsUtil.saveText(
         file,
         VfsUtilCore.loadText(file)
             + (templateAttributes != null
                 ? template.getText(templateAttributes)
                 : template.getText()));
   } catch (IOException e) {
     LOG.warn(
         String.format(
             "Unexpected exception on appending template %s config",
             GradleConstants.SYSTEM_ID.getReadableName()),
         e);
     throw new ConfigurationException(
         e.getMessage(),
         String.format(
             "Can't append %s template config text", GradleConstants.SYSTEM_ID.getReadableName()));
   }
 }
 private static String loadContent(VirtualFile p, String name) throws IOException {
   final VirtualFile appConfig = p.findChild(name);
   assertNotNull(appConfig);
   return VfsUtilCore.loadText(appConfig);
 }