package com.intellij.openapi.vfs;

import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.util.SystemInfo;
import com.intellij.testFramework.IdeaTestCase;

import java.io.File;

public class JarFileSystemTest extends IdeaTestCase{
  public void testFindFile() throws Exception{
    Sdk jdk = ModuleRootManager.getInstance(myModule).getSdk();
    VirtualFile jdkHome = jdk.getHomeDirectory();
    String rtJarPath = new File(jdkHome.getPath() + "/src.zip").getCanonicalPath().replace(File.separator, "/");

    VirtualFile file1 = findByPath(rtJarPath + JarFileSystem.JAR_SEPARATOR);
    assertTrue(file1.isDirectory());

    VirtualFile file2 = findByPath(rtJarPath + JarFileSystem.JAR_SEPARATOR + "java");
    assertTrue(file2.isDirectory());

    VirtualFile file3 = file1.findChild("java");
    assertEquals(file2, file3);

    VirtualFile file4 = findByPath(rtJarPath + JarFileSystem.JAR_SEPARATOR + "java/lang/Object.java");
    assertTrue(!file4.isDirectory());

    byte[] bytes = file4.contentsToByteArray();
    assertNotNull(bytes);
    assertTrue(bytes.length > 10);
  }

  public void testMetaInf() throws Exception{
    Sdk jdk = ModuleRootManager.getInstance(myModule).getSdk();
    VirtualFile jdkHome = jdk.getHomeDirectory();
    String rtJarPath = jdkHome.getPath() + "/jre/lib/rt.jar";

    VirtualFile jarRoot = JarFileSystem.getInstance().findFileByPath(rtJarPath + JarFileSystem.JAR_SEPARATOR);
    assertNotNull(jarRoot);

    VirtualFile metaInf = jarRoot.findChild("META-INF");
    assertNotNull(metaInf);

    VirtualFile[] children = metaInf.getChildren();
    assertEquals(1, children.length);
  }

  private static VirtualFile findByPath(String path) {
    VirtualFile file = JarFileSystem.getInstance().findFileByPath(path);
    assertNotNull(file);
    final String filePath = file.getPath();
    final String message = "paths are not equal, path1 = " + path + " found: " + filePath;
    if (SystemInfo.isFileSystemCaseSensitive) {
      assertEquals(message, path, file.getPath());
    }
    else {
      assertTrue(message, path.equalsIgnoreCase(filePath));
    }
    return file;
  }
}

@@ -1,8 +1,609 @@
+/*%0A * Copyright 2000-2013 JetBrains s.r.o.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 
@@ -732,53 +732,8 @@
 er;%0A
-import com.intellij.openapi.util.SystemInfo;%0A
 impo
@@ -792,20 +792,104 @@
 java.io.
-File
+IOException;%0A%0Aimport static com.intellij.testFramework.PlatformTestUtil.assertPathsEqual
 ;%0A%0Apubli
@@ -934,16 +934,17 @@
 TestCase
+ 
 %7B%0A  publ
@@ -965,32 +965,34 @@
 ndFile() throws 
+IO
 Exception%7B%0A    S
@@ -988,233 +988,55 @@
 tion
+ 
 %7B%0A    S
-dk jdk = ModuleRootManager.getInstance(myModule).getSdk();%0A    VirtualFile jdkHome = jdk.getHomeDirectory();%0A    String rtJarPath = new File(jdkHome.getPath() + %22/src.zip%22).getCanonicalPath().replace(File.separator, %22/
+tring rtJarPath = getJdkRtPath(%22src.zip
 %22);%0A
@@ -1052,21 +1052,23 @@
 ualFile 
-file1
+jarRoot
  = findB
@@ -1130,21 +1130,23 @@
 ertTrue(
-file1
+jarRoot
 .isDirec
@@ -1307,13 +1307,15 @@
 3 = 
-file1
+jarRoot
 .fin
@@ -1651,185 +1651,47 @@
 f() 
-throws Exception%7B%0A    Sdk jdk = ModuleRootManager.getInstance(myModule).getSdk();%0A    VirtualFile jdkHome = jdk.getHomeDirectory();%0A    String rtJarPath = jdkHome.getPath() + %22/
+%7B%0A    String rtJarPath = getJdkRtPath(%22
 jre/
@@ -1701,16 +1701,17 @@
 /rt.jar%22
+)
 ;%0A%0A    V
@@ -1735,44 +1735,12 @@
 t = 
-JarFileSystem.getInstance().findFile
+find
 ByPa
@@ -1798,23 +1798,20 @@
 sert
-NotNull
+True
 (jarRoot
 );%0A%0A
@@ -1806,16 +1806,30 @@
 (jarRoot
+.isDirectory()
 );%0A%0A    
@@ -2019,33 +2019,23 @@
 ate 
-static VirtualFile findBy
+String getJdkRt
 Path
@@ -2042,17 +2042,25 @@
 (String 
-p
+relativeP
 ath) %7B%0A 
@@ -2066,74 +2066,65 @@
     
-VirtualFile file = JarFileSystem.getInstance().findFileByPath(path
+Sdk jdk = ModuleRootManager.getInstance(myModule).getSdk(
 );%0A 
@@ -2144,332 +2144,345 @@
 ull(
-file
+jdk
 );%0A    
-final String filePath = file.getPath();%0A    final String message = %22paths are not equal, path1 = %22 + p
+VirtualFile jdkHome = jdk.getHomeDirectory();%0A    assertNotNull(jdkHome);%0A    return jdkHome.getP
 ath
+()
  + %22
- found: %22 + filePath;%0A    if (SystemInfo.isFileSystemCaseSensitive) %7B%0A      assertEquals(message, path, file.get
+/%22 + relativePath;%0A  %7D%0A%0A  private static VirtualFile findByPath(String path) %7B%0A    VirtualFile file = JarFileSystem.getInstance().findFileBy
 Path(
-)
+path
 );%0A    
-%7D%0A    else %7B%0A      assertTrue(message, path.e
+assertNotNull(file);%0A    assertPathsE
 qual
-sIgnoreCase(file
+(path, file.get
 Path
+(
 ));%0A
-    %7D%0A
     

Test cleanup