From 68e340a0c44875a57b42d172dac66709ea263162 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Fri, 30 Aug 2013 21:08:28 +0400 Subject: [PATCH] vfs: .jar refresh test --- .../openapi/vfs/JarFileSystemTest.java | 46 +++++++++++++++++++ .../intellij/openapi/util/io/IoTestUtil.java | 10 ++-- 2 files changed, 53 insertions(+), 3 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/openapi/vfs/JarFileSystemTest.java b/java/java-tests/testSrc/com/intellij/openapi/vfs/JarFileSystemTest.java index 52be1927ad5d..053eb932d3eb 100644 --- a/java/java-tests/testSrc/com/intellij/openapi/vfs/JarFileSystemTest.java +++ b/java/java-tests/testSrc/com/intellij/openapi/vfs/JarFileSystemTest.java @@ -15,11 +15,20 @@ */ package com.intellij.openapi.vfs; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.projectRoots.Sdk; import com.intellij.openapi.roots.ModuleRootManager; +import com.intellij.openapi.util.Ref; +import com.intellij.openapi.util.io.IoTestUtil; +import com.intellij.openapi.vfs.newvfs.BulkFileListener; +import com.intellij.openapi.vfs.newvfs.events.VFileContentChangeEvent; +import com.intellij.openapi.vfs.newvfs.events.VFileEvent; import com.intellij.testFramework.IdeaTestCase; +import org.jetbrains.annotations.NotNull; +import java.io.File; import java.io.IOException; +import java.util.List; import static com.intellij.testFramework.PlatformTestUtil.assertPathsEqual; @@ -57,6 +66,38 @@ public class JarFileSystemTest extends IdeaTestCase { assertEquals(1, children.length); } + public void testJarRefresh() throws IOException { + File jar = IoTestUtil.createTestJar(); + VirtualFile vFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(jar); + assertNotNull(vFile); + + final VirtualFile entry = findByPath(jar.getPath() + JarFileSystem.JAR_SEPARATOR + "entry.txt"); + assertContent(entry, "test"); + + final Ref updated = Ref.create(false); + ApplicationManager.getApplication().getMessageBus().connect(myTestRootDisposable).subscribe( + VirtualFileManager.VFS_CHANGES, + new BulkFileListener.Adapter() { + @Override + public void before(@NotNull List events) { + for (VFileEvent event : events) { + if (event instanceof VFileContentChangeEvent && entry.equals(event.getFile())) { + updated.set(true); + break; + } + } + } + } + ); + + IoTestUtil.writeEntry(jar, "entry.txt", "update"); + vFile.refresh(false, false); + + assertTrue(updated.get()); + assertTrue(entry.isValid()); + assertContent(entry, "update"); + } + private String getJdkRtPath(String relativePath) { Sdk jdk = ModuleRootManager.getInstance(myModule).getSdk(); assertNotNull(jdk); @@ -71,4 +112,9 @@ public class JarFileSystemTest extends IdeaTestCase { assertPathsEqual(path, file.getPath()); return file; } + + private static void assertContent(VirtualFile file, String expected) throws IOException { + String content = new String(file.contentsToByteArray()); + assertEquals(expected, content); + } } diff --git a/platform/util/testSrc/com/intellij/openapi/util/io/IoTestUtil.java b/platform/util/testSrc/com/intellij/openapi/util/io/IoTestUtil.java index 7c88491d8a1e..8c26f84ae0f6 100644 --- a/platform/util/testSrc/com/intellij/openapi/util/io/IoTestUtil.java +++ b/platform/util/testSrc/com/intellij/openapi/util/io/IoTestUtil.java @@ -211,16 +211,20 @@ public class IoTestUtil { @NotNull public static File createTestJar() throws IOException { File jarFile = FileUtil.createTempFile("test.", ".jar"); + writeEntry(jarFile, "entry.txt", "test"); + return jarFile; + } + + public static void writeEntry(@NotNull File jarFile, @NotNull String name, @NotNull String content) throws IOException { JarOutputStream stream = new JarOutputStream(new FileOutputStream(jarFile)); try { - stream.putNextEntry(new JarEntry("entry.txt")); - stream.write("test".getBytes("UTF-8")); + stream.putNextEntry(new JarEntry(name)); + stream.write(content.getBytes("UTF-8")); stream.closeEntry(); } finally { stream.close(); } - return jarFile; } @NotNull