From c2b71c9875ae4a39e023c99af9a45f3c333dc84a Mon Sep 17 00:00:00 2001 From: peter Date: Mon, 24 Nov 2014 19:00:08 +0100 Subject: [PATCH] wrap vfile writes in tests into write action --- .../compiler/BaseCompilerTestCase.java | 5 ++- .../codeInsight/CodeInsightTestCase.java | 18 ++++++++--- .../testFramework/CompilerTester.java | 31 ++++++++++--------- .../testFramework/PlatformTestCase.java | 21 +++++++++++++ .../vcs/AbstractVcsTestCase.java | 9 ++++++ 5 files changed, 61 insertions(+), 23 deletions(-) diff --git a/java/compiler/impl/testSrc/com/intellij/compiler/BaseCompilerTestCase.java b/java/compiler/impl/testSrc/com/intellij/compiler/BaseCompilerTestCase.java index 5ca8ff6e3478..a59768c07bf2 100644 --- a/java/compiler/impl/testSrc/com/intellij/compiler/BaseCompilerTestCase.java +++ b/java/compiler/impl/testSrc/com/intellij/compiler/BaseCompilerTestCase.java @@ -18,7 +18,6 @@ import com.intellij.openapi.util.Ref; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.LocalFileSystem; -import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.newvfs.NewVirtualFile; import com.intellij.packaging.artifacts.Artifact; @@ -300,10 +299,10 @@ public abstract class BaseCompilerTestCase extends ModuleTestCase { changeFile(file, null); } - protected void changeFile(VirtualFile file, final String newText) { + protected void changeFile(final VirtualFile file, @Nullable final String newText) { try { if (newText != null) { - VfsUtil.saveText(file, newText); + setFileText(file, newText); } ((NewVirtualFile)file).setTimeStamp(file.getTimeStamp() + 10); } diff --git a/java/testFramework/src/com/intellij/codeInsight/CodeInsightTestCase.java b/java/testFramework/src/com/intellij/codeInsight/CodeInsightTestCase.java index 8fcd8c52a493..3aeb8bb8d14f 100644 --- a/java/testFramework/src/com/intellij/codeInsight/CodeInsightTestCase.java +++ b/java/testFramework/src/com/intellij/codeInsight/CodeInsightTestCase.java @@ -23,10 +23,14 @@ import com.intellij.openapi.Disposable; import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.application.Result; +import com.intellij.openapi.application.WriteAction; import com.intellij.openapi.command.CommandProcessor; import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.command.undo.UndoManager; -import com.intellij.openapi.editor.*; +import com.intellij.openapi.editor.Document; +import com.intellij.openapi.editor.Editor; +import com.intellij.openapi.editor.EditorFactory; +import com.intellij.openapi.editor.LogicalPosition; import com.intellij.openapi.editor.actionSystem.EditorActionHandler; import com.intellij.openapi.editor.actionSystem.EditorActionManager; import com.intellij.openapi.editor.actionSystem.TypedAction; @@ -159,7 +163,7 @@ public abstract class CodeInsightTestCase extends PsiTestCase { return configureByText(fileType, text, null); } - protected PsiFile configureByText(@NotNull final FileType fileType, @NonNls @NotNull String text, @Nullable String _extension) { + protected PsiFile configureByText(@NotNull final FileType fileType, @NonNls @NotNull final String text, @Nullable String _extension) { try { final String extension = _extension == null ? fileType.getDefaultExtension():_extension; @@ -169,15 +173,19 @@ public abstract class CodeInsightTestCase extends PsiTestCase { if (fileTypeManager.getFileTypeByExtension(extension) != fileType) { new WriteCommandAction(getProject()) { @Override - protected void run(Result result) throws Exception { + protected void run(@NotNull Result result) throws Exception { fileTypeManager.associateExtension(fileType, extension); } }.execute(); } final VirtualFile vFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(tempFile); assert vFile != null; - vFile.setCharset(CharsetToolkit.UTF8_CHARSET); - VfsUtil.saveText(vFile, text); + new WriteAction() { + protected void run(@NotNull Result result) throws Throwable { + vFile.setCharset(CharsetToolkit.UTF8_CHARSET); + VfsUtil.saveText(vFile, text); + } + }.execute(); final VirtualFile vdir = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(dir); diff --git a/java/testFramework/src/com/intellij/testFramework/CompilerTester.java b/java/testFramework/src/com/intellij/testFramework/CompilerTester.java index ba1519ada136..7b0496f0fc0e 100644 --- a/java/testFramework/src/com/intellij/testFramework/CompilerTester.java +++ b/java/testFramework/src/com/intellij/testFramework/CompilerTester.java @@ -39,6 +39,7 @@ import com.intellij.util.Consumer; import com.intellij.util.ObjectUtils; import com.intellij.util.concurrency.Semaphore; import com.intellij.util.ui.UIUtil; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.junit.Assert; @@ -120,26 +121,26 @@ public class CompilerTester { return path.findFileByRelativePath(className.replace('.', '/') + ".class"); } - public void touch(VirtualFile file) throws IOException { - file.setBinaryContent(file.contentsToByteArray(), -1, file.getTimeStamp() + 1); - File ioFile = VfsUtil.virtualToIoFile(file); - assert ioFile.setLastModified(ioFile.lastModified() - 100000); - file.refresh(false, false); + public void touch(final VirtualFile file) throws IOException { + new WriteAction() { + @Override + protected void run(@NotNull Result result) throws Throwable { + file.setBinaryContent(file.contentsToByteArray(), -1, file.getTimeStamp() + 1); + File ioFile = VfsUtil.virtualToIoFile(file); + assert ioFile.setLastModified(ioFile.lastModified() - 100000); + file.refresh(false, false); + } + }.execute().throwException(); } public void setFileText(final PsiFile file, final String text) throws IOException { - UIUtil.invokeAndWaitIfNeeded(new Runnable() { + new WriteAction() { @Override - public void run() { - try { - final VirtualFile virtualFile = file.getVirtualFile(); - VfsUtil.saveText(ObjectUtils.assertNotNull(virtualFile), text); - } - catch (IOException e) { - throw new RuntimeException(e); - } + protected void run(@NotNull Result result) throws Throwable { + final VirtualFile virtualFile = file.getVirtualFile(); + VfsUtil.saveText(ObjectUtils.assertNotNull(virtualFile), text); } - }); + }.execute().throwException(); touch(file.getVirtualFile()); } diff --git a/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java b/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java index fb2fcd2a31ec..13b6c70ee97b 100644 --- a/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java +++ b/platform/testFramework/src/com/intellij/testFramework/PlatformTestCase.java @@ -54,6 +54,7 @@ import com.intellij.openapi.util.EmptyRunnable; import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.io.FileUtilRt; import com.intellij.openapi.vfs.LocalFileSystem; +import com.intellij.openapi.vfs.VfsUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.impl.local.LocalFileSystemImpl; import com.intellij.openapi.vfs.newvfs.impl.VirtualDirectoryImpl; @@ -892,4 +893,24 @@ public abstract class PlatformTestCase extends UsefulTestCase implements DataPro } }.execute().throwException(); } + + public static void setFileText(@NotNull final VirtualFile file, @NotNull final String text) throws IOException { + new WriteAction() { + @Override + protected void run(@NotNull Result result) throws Throwable { + VfsUtil.saveText(file, text); + } + }.execute().throwException(); + } + + public static void setBinaryContent(final VirtualFile file, final byte[] content) { + new WriteAction() { + @Override + protected void run(@NotNull Result result) throws Throwable { + file.setBinaryContent(content); + } + }.execute().throwException(); + } + + } diff --git a/platform/testFramework/testSrc/com/intellij/testFramework/vcs/AbstractVcsTestCase.java b/platform/testFramework/testSrc/com/intellij/testFramework/vcs/AbstractVcsTestCase.java index 4df8f9cdeb53..b64e222e7792 100644 --- a/platform/testFramework/testSrc/com/intellij/testFramework/vcs/AbstractVcsTestCase.java +++ b/platform/testFramework/testSrc/com/intellij/testFramework/vcs/AbstractVcsTestCase.java @@ -31,6 +31,7 @@ import com.intellij.openapi.vcs.changes.*; import com.intellij.openapi.vcs.impl.ProjectLevelVcsManagerImpl; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; +import com.intellij.testFramework.PlatformTestCase; import com.intellij.testFramework.UsefulTestCase; import com.intellij.testFramework.builders.EmptyModuleFixtureBuilder; import com.intellij.testFramework.fixtures.IdeaProjectTestFixture; @@ -327,4 +328,12 @@ public abstract class AbstractVcsTestCase { return annotation; } + public void setFileText(@NotNull final VirtualFile file, @NotNull final String text) throws IOException { + PlatformTestCase.setFileText(file, text); + } + + public static void setBinaryContent(final VirtualFile file, final byte[] content) { + PlatformTestCase.setBinaryContent(file, content); + } + }