From 1251fb6b09453939348ebef1a9b8efc9b7f80289 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Sun, 1 Dec 2013 18:19:41 +0100 Subject: [PATCH 01/10] spelling --- .../src/com/intellij/openapi/fileEditor/FileEditorProvider.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/platform/platform-api/src/com/intellij/openapi/fileEditor/FileEditorProvider.java b/platform/platform-api/src/com/intellij/openapi/fileEditor/FileEditorProvider.java index 946ca43c167d..8ff35a9f994a 100644 --- a/platform/platform-api/src/com/intellij/openapi/fileEditor/FileEditorProvider.java +++ b/platform/platform-api/src/com/intellij/openapi/fileEditor/FileEditorProvider.java @@ -62,7 +62,7 @@ public interface FileEditorProvider { void disposeEditor(@NotNull FileEditor editor); /** - * Deserializes state from the specified sourceElemet + * Deserialize state from the specified sourceElement */ @NotNull FileEditorState readState(@NotNull Element sourceElement, @NotNull Project project, @NotNull VirtualFile file); From aaa236a2dd99cbb2e18558c74c4d8b5a493a6688 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Sun, 1 Dec 2013 18:22:02 +0100 Subject: [PATCH 02/10] overrides --- .../editor/impl/ImageFileEditorProvider.java | 74 ++++++++++--------- 1 file changed, 41 insertions(+), 33 deletions(-) diff --git a/images/src/org/intellij/images/editor/impl/ImageFileEditorProvider.java b/images/src/org/intellij/images/editor/impl/ImageFileEditorProvider.java index da0f8f1c9a56..2e708b985458 100644 --- a/images/src/org/intellij/images/editor/impl/ImageFileEditorProvider.java +++ b/images/src/org/intellij/images/editor/impl/ImageFileEditorProvider.java @@ -31,46 +31,54 @@ import org.jetbrains.annotations.NotNull; * @author Alexey Efimov */ final class ImageFileEditorProvider implements FileEditorProvider, DumbAware { - @NonNls private static final String EDITOR_TYPE_ID = "images"; + @NonNls private static final String EDITOR_TYPE_ID = "images"; - private final ImageFileTypeManager typeManager; + private final ImageFileTypeManager typeManager; - ImageFileEditorProvider(ImageFileTypeManager typeManager) { - this.typeManager = typeManager; - } + ImageFileEditorProvider(ImageFileTypeManager typeManager) { + this.typeManager = typeManager; + } - public boolean accept(@NotNull Project project, @NotNull VirtualFile file) { - return typeManager.isImage(file); - } + @Override + public boolean accept(@NotNull Project project, @NotNull VirtualFile file) { + return typeManager.isImage(file); + } - @NotNull - public FileEditor createEditor(@NotNull Project project, @NotNull VirtualFile file) { - return new ImageFileEditorImpl(project, file); - } + @Override + @NotNull + public FileEditor createEditor(@NotNull Project project, @NotNull VirtualFile file) { + return new ImageFileEditorImpl(project, file); + } - public void disposeEditor(@NotNull FileEditor editor) { - Disposer.dispose(editor); - } + @Override + public void disposeEditor(@NotNull FileEditor editor) { + Disposer.dispose(editor); + } - @NotNull - public FileEditorState readState(@NotNull Element sourceElement, @NotNull Project project, @NotNull VirtualFile file) { - return new FileEditorState() { - public boolean canBeMergedWith(FileEditorState otherState, FileEditorStateLevel level) { - return false; - } - }; - } + @Override + @NotNull + public FileEditorState readState(@NotNull Element sourceElement, @NotNull Project project, @NotNull VirtualFile file) { + return new FileEditorState() { + @Override + public boolean canBeMergedWith(FileEditorState otherState, FileEditorStateLevel level) { + return false; + } + }; + } - public void writeState(@NotNull FileEditorState state, @NotNull Project project, @NotNull Element targetElement) { - } + @Override + public void writeState(@NotNull FileEditorState state, @NotNull Project project, @NotNull Element targetElement) { + } - @NotNull - public String getEditorTypeId() { - return EDITOR_TYPE_ID; - } + @Override + @NotNull + public String getEditorTypeId() { + return EDITOR_TYPE_ID; + } - @NotNull - public FileEditorPolicy getPolicy() { - return FileEditorPolicy.HIDE_DEFAULT_EDITOR; - } + @Override + @NotNull + public FileEditorPolicy getPolicy() { + return FileEditorPolicy.HIDE_DEFAULT_EDITOR; + } } From 561c8d170a220c4954e81bb9038814c4259f85c3 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Sun, 1 Dec 2013 18:50:20 +0100 Subject: [PATCH 03/10] NullFileEditorState +review --- .../editor/impl/ImageFileEditorProvider.java | 7 +----- .../openapi/fileEditor/FileEditorState.java | 2 +- .../fileEditor/NullFileEditorState.java | 23 +++++++++++++++++++ .../impl/IdeDocumentHistoryImpl.java | 5 ++-- .../xml/ui/PerspectiveFileEditorProvider.java | 7 ++++++ 5 files changed, 34 insertions(+), 10 deletions(-) create mode 100644 platform/platform-api/src/com/intellij/openapi/fileEditor/NullFileEditorState.java diff --git a/images/src/org/intellij/images/editor/impl/ImageFileEditorProvider.java b/images/src/org/intellij/images/editor/impl/ImageFileEditorProvider.java index 2e708b985458..439661afcfea 100644 --- a/images/src/org/intellij/images/editor/impl/ImageFileEditorProvider.java +++ b/images/src/org/intellij/images/editor/impl/ImageFileEditorProvider.java @@ -58,12 +58,7 @@ final class ImageFileEditorProvider implements FileEditorProvider, DumbAware { @Override @NotNull public FileEditorState readState(@NotNull Element sourceElement, @NotNull Project project, @NotNull VirtualFile file) { - return new FileEditorState() { - @Override - public boolean canBeMergedWith(FileEditorState otherState, FileEditorStateLevel level) { - return false; - } - }; + return new NullFileEditorState(); } @Override diff --git a/platform/platform-api/src/com/intellij/openapi/fileEditor/FileEditorState.java b/platform/platform-api/src/com/intellij/openapi/fileEditor/FileEditorState.java index aa356d405a8d..277a679e2b92 100644 --- a/platform/platform-api/src/com/intellij/openapi/fileEditor/FileEditorState.java +++ b/platform/platform-api/src/com/intellij/openapi/fileEditor/FileEditorState.java @@ -23,8 +23,8 @@ package com.intellij.openapi.fileEditor; * @author Vladimir Kondratyev */ public interface FileEditorState { - FileEditorState INSTANCE = new FileEditorState() { + @Override public boolean canBeMergedWith(FileEditorState otherState, FileEditorStateLevel level) { return true; } diff --git a/platform/platform-api/src/com/intellij/openapi/fileEditor/NullFileEditorState.java b/platform/platform-api/src/com/intellij/openapi/fileEditor/NullFileEditorState.java new file mode 100644 index 000000000000..d9c822f314cd --- /dev/null +++ b/platform/platform-api/src/com/intellij/openapi/fileEditor/NullFileEditorState.java @@ -0,0 +1,23 @@ +/* + * Copyright 2000-2013 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.openapi.fileEditor; + +public final class NullFileEditorState implements FileEditorState { + @Override + public boolean canBeMergedWith(FileEditorState otherState, FileEditorStateLevel level) { + return false; + } +} \ No newline at end of file diff --git a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/IdeDocumentHistoryImpl.java b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/IdeDocumentHistoryImpl.java index 8d8636cc69f3..ed16bb677525 100644 --- a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/IdeDocumentHistoryImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/IdeDocumentHistoryImpl.java @@ -22,7 +22,6 @@ import com.intellij.openapi.command.CommandListener; import com.intellij.openapi.command.CommandProcessor; import com.intellij.openapi.command.impl.CommandMerger; import com.intellij.openapi.components.*; -import com.intellij.openapi.components.StoragePathMacros; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.EditorFactory; @@ -310,7 +309,7 @@ public class IdeDocumentHistoryImpl extends IdeDocumentHistory implements Projec } } - return VfsUtil.toVirtualFileArray(files); + return VfsUtilCore.toVirtualFileArray(files); } @Override @@ -521,7 +520,7 @@ public class IdeDocumentHistoryImpl extends IdeDocumentHistory implements Projec private final VirtualFile myFile; private final FileEditorState myNavigationState; private final String myEditorTypeId; - private WeakReference myWindow; + private final WeakReference myWindow; public PlaceInfo(@NotNull VirtualFile file, FileEditorState navigationState, String editorTypeId, @Nullable EditorWindow window) { myNavigationState = navigationState; diff --git a/xml/dom-openapi/src/com/intellij/util/xml/ui/PerspectiveFileEditorProvider.java b/xml/dom-openapi/src/com/intellij/util/xml/ui/PerspectiveFileEditorProvider.java index 882f21303585..c9650fc48d8e 100644 --- a/xml/dom-openapi/src/com/intellij/util/xml/ui/PerspectiveFileEditorProvider.java +++ b/xml/dom-openapi/src/com/intellij/util/xml/ui/PerspectiveFileEditorProvider.java @@ -27,31 +27,38 @@ import org.jetbrains.annotations.NotNull; * @author peter */ public abstract class PerspectiveFileEditorProvider extends WeighedFileEditorProvider { + @Override @NotNull public abstract PerspectiveFileEditor createEditor(@NotNull Project project, @NotNull VirtualFile file); + @Override public void disposeEditor(@NotNull FileEditor editor) { Disposer.dispose(editor); } + @Override @NotNull public FileEditorState readState(@NotNull Element sourceElement, @NotNull Project project, @NotNull VirtualFile file) { return new FileEditorState() { + @Override public boolean canBeMergedWith(FileEditorState otherState, FileEditorStateLevel level) { return true; } }; } + @Override public void writeState(@NotNull FileEditorState state, @NotNull Project project, @NotNull Element targetElement) { } + @Override @NotNull @NonNls public final String getEditorTypeId() { return getComponentName(); } + @Override @NotNull public final FileEditorPolicy getPolicy() { return FileEditorPolicy.PLACE_AFTER_DEFAULT_EDITOR; From 3226823708070a3a78b6bc6eb800539f5da5c499 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Mon, 2 Dec 2013 09:39:41 +0100 Subject: [PATCH 04/10] spelling --- .../com/intellij/openapi/fileEditor/FileEditorManager.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/platform/platform-api/src/com/intellij/openapi/fileEditor/FileEditorManager.java b/platform/platform-api/src/com/intellij/openapi/fileEditor/FileEditorManager.java index aba033d9f239..366f69db9cc6 100644 --- a/platform/platform-api/src/com/intellij/openapi/fileEditor/FileEditorManager.java +++ b/platform/platform-api/src/com/intellij/openapi/fileEditor/FileEditorManager.java @@ -137,11 +137,11 @@ public abstract class FileEditorManager { /** * @deprecated use addTopComponent */ - public abstract void showEditorAnnotation(@NotNull FileEditor editor, @NotNull JComponent annotationComoponent); + public abstract void showEditorAnnotation(@NotNull FileEditor editor, @NotNull JComponent annotationComponent); /** * @deprecated use removeTopComponent */ - public abstract void removeEditorAnnotation(@NotNull FileEditor editor, @NotNull JComponent annotationComoponent); + public abstract void removeEditorAnnotation(@NotNull FileEditor editor, @NotNull JComponent annotationComponent); public abstract void addTopComponent(@NotNull final FileEditor editor, @NotNull final JComponent component); public abstract void removeTopComponent(@NotNull final FileEditor editor, @NotNull final JComponent component); From 569f3d2ca6da22d064cfd4796e06119ffdcc705e Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Mon, 2 Dec 2013 10:07:35 +0100 Subject: [PATCH 05/10] cleanup --- .../src/com/intellij/ui/EditorNotificationPanel.java | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/platform/platform-api/src/com/intellij/ui/EditorNotificationPanel.java b/platform/platform-api/src/com/intellij/ui/EditorNotificationPanel.java index 30560c3b8447..0e0679826fac 100644 --- a/platform/platform-api/src/com/intellij/ui/EditorNotificationPanel.java +++ b/platform/platform-api/src/com/intellij/ui/EditorNotificationPanel.java @@ -28,7 +28,6 @@ import org.jetbrains.annotations.NonNls; import javax.swing.*; import javax.swing.event.HyperlinkEvent; -import javax.swing.event.HyperlinkListener; import java.awt.*; /** @@ -63,6 +62,7 @@ public class EditorNotificationPanel extends JPanel { public HyperlinkLabel createActionLabel(final String text, @NonNls final String actionId) { return createActionLabel(text, new Runnable() { + @Override public void run() { executeAction(actionId); } @@ -71,11 +71,10 @@ public class EditorNotificationPanel extends JPanel { public HyperlinkLabel createActionLabel(final String text, final Runnable action) { HyperlinkLabel label = new HyperlinkLabel(text, PlatformColors.BLUE, getBackground(), PlatformColors.BLUE); - label.addHyperlinkListener(new HyperlinkListener() { - public void hyperlinkUpdate(final HyperlinkEvent e) { - if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { - action.run(); - } + label.addHyperlinkListener(new HyperlinkAdapter() { + @Override + protected void hyperlinkActivated(HyperlinkEvent e) { + action.run(); } }); myLinksPanel.add(label); From 3da930f7384b9348e194ae957f5cd6ba1e02d65a Mon Sep 17 00:00:00 2001 From: Dmitry Avdeev Date: Mon, 2 Dec 2013 13:22:02 +0400 Subject: [PATCH 06/10] range re-fixed --- .../reference/impl/providers/SchemaReferencesProvider.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xml/xml-psi-impl/src/com/intellij/psi/impl/source/resolve/reference/impl/providers/SchemaReferencesProvider.java b/xml/xml-psi-impl/src/com/intellij/psi/impl/source/resolve/reference/impl/providers/SchemaReferencesProvider.java index 23cb9dbee8f6..450ae1043c6e 100644 --- a/xml/xml-psi-impl/src/com/intellij/psi/impl/source/resolve/reference/impl/providers/SchemaReferencesProvider.java +++ b/xml/xml-psi-impl/src/com/intellij/psi/impl/source/resolve/reference/impl/providers/SchemaReferencesProvider.java @@ -117,7 +117,7 @@ public class SchemaReferencesProvider extends PsiReferenceProvider { } public TextRange getRangeInElement() { - return new TextRange(1,myElement.getTextLength()-1); + return ElementManipulators.getValueTextRange(myElement); } @Nullable From dc6469b787ad905d0266928e77a99d3f7cf9e5de Mon Sep 17 00:00:00 2001 From: Eugene Zhuravlev Date: Mon, 2 Dec 2013 10:18:50 +0100 Subject: [PATCH 07/10] more robust javac server shutdown --- .../org/jetbrains/jps/javac/JavacServer.java | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/jps/jps-builders/src/org/jetbrains/jps/javac/JavacServer.java b/jps/jps-builders/src/org/jetbrains/jps/javac/JavacServer.java index 72ecdaeeeef1..41d85eae1781 100644 --- a/jps/jps-builders/src/org/jetbrains/jps/javac/JavacServer.java +++ b/jps/jps-builders/src/org/jetbrains/jps/javac/JavacServer.java @@ -31,7 +31,8 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.jps.api.CanceledStatus; import org.jetbrains.jps.service.SharedThreadPool; -import javax.tools.*; +import javax.tools.Diagnostic; +import javax.tools.JavaFileObject; import java.io.File; import java.util.*; @@ -72,6 +73,7 @@ public class JavacServer { } public static void main(String[] args) { + JavacServer server = null; try { int port = DEFAULT_SERVER_PORT; if (args.length > 0) { @@ -84,12 +86,13 @@ public class JavacServer { } } - final JavacServer server = new JavacServer(); + server = new JavacServer(); server.start(port); + final JavacServer finalServer = server; Runtime.getRuntime().addShutdownHook(new Thread("Shutdown hook thread") { @Override public void run() { - server.stop(); + finalServer.stop(); } }); @@ -99,7 +102,14 @@ public class JavacServer { catch (Throwable e) { System.err.println(SERVER_ERROR_START_MESSAGE + e.getMessage()); e.printStackTrace(System.err); - System.exit(-1); + try { + if (server != null) { + server.stop(); + } + } + finally { + System.exit(-1); + } } } From f3faf8b8302435a95f1c87671e5c44299629c494 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Fri, 29 Nov 2013 18:05:43 +0400 Subject: [PATCH 08/10] test the actual FileDocumentManager instead of a mock implementation --- .../impl/FileDocumentManagerImpl.java | 107 ++-- .../FileDocumentManagerImplTest.java | 562 ++++++++---------- 2 files changed, 328 insertions(+), 341 deletions(-) diff --git a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileDocumentManagerImpl.java b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileDocumentManagerImpl.java index ad6e1ff8a02e..5c26205ea268 100644 --- a/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileDocumentManagerImpl.java +++ b/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/FileDocumentManagerImpl.java @@ -18,6 +18,7 @@ package com.intellij.openapi.fileEditor.impl; import com.intellij.AppTopics; import com.intellij.CommonBundle; import com.intellij.codeStyle.CodeStyleFacade; +import com.intellij.openapi.Disposable; import com.intellij.openapi.application.AccessToken; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.command.CommandProcessor; @@ -49,6 +50,7 @@ import com.intellij.openapi.ui.DialogBuilder; import com.intellij.openapi.ui.DialogWrapper; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.util.Comparing; +import com.intellij.openapi.util.Disposer; import com.intellij.openapi.util.Key; import com.intellij.openapi.util.io.FileUtilRt; import com.intellij.openapi.util.text.StringUtil; @@ -63,6 +65,7 @@ import com.intellij.testFramework.LightVirtualFile; import com.intellij.ui.UIBundle; import com.intellij.ui.components.JBScrollPane; import com.intellij.util.Function; +import com.intellij.util.PairProcessor; import com.intellij.util.ThrowableRunnable; import com.intellij.util.containers.ConcurrentHashSet; import com.intellij.util.messages.MessageBus; @@ -649,47 +652,67 @@ public class FileDocumentManagerImpl extends FileDocumentManager implements Appl myMultiCaster.fileContentReloaded(file, document); } - protected boolean askReloadFromDisk(final VirtualFile file, final Document document) { + private PairProcessor askReloadFromDisk = new PairProcessor() { + @Override + public boolean process(final VirtualFile file, final Document document) { + String message = UIBundle.message("file.cache.conflict.message.text", file.getPresentableUrl()); + + final DialogBuilder builder = new DialogBuilder((Project)null); + builder.setCenterPanel(new JLabel(message, Messages.getQuestionIcon(), SwingConstants.CENTER)); + builder.addOkAction().setText(UIBundle.message("file.cache.conflict.load.fs.changes.button")); + builder.addCancelAction().setText(UIBundle.message("file.cache.conflict.keep.memory.changes.button")); + builder.addAction(new AbstractAction(UIBundle.message("file.cache.conflict.show.difference.button")) { + @Override + public void actionPerformed(ActionEvent e) { + String title = UIBundle.message("file.cache.conflict.for.file.dialog.title", file.getPresentableUrl()); + final ProjectEx project = (ProjectEx)ProjectLocator.getInstance().guessProjectForFile(file); + + SimpleDiffRequest request = new SimpleDiffRequest(project, title); + FileType fileType = file.getFileType(); + String fsContent = LoadTextUtil.loadText(file).toString(); + request.setContents(new SimpleContent(fsContent, fileType), + new DocumentContent(project, document, fileType)); + request.setContentTitles(UIBundle.message("file.cache.conflict.diff.content.file.system.content"), + UIBundle.message("file.cache.conflict.diff.content.memory.content")); + DialogBuilder diffBuilder = new DialogBuilder(project); + DiffPanelImpl diffPanel = (DiffPanelImpl)DiffManager.getInstance().createDiffPanel(diffBuilder.getWindow(), project, diffBuilder, null); + diffPanel.getOptions().setShowSourcePolicy(DiffPanelOptions.ShowSourcePolicy.DONT_SHOW); + diffBuilder.setCenterPanel(diffPanel.getComponent()); + diffBuilder.setDimensionServiceKey("FileDocumentManager.FileCacheConflict"); + diffPanel.setDiffRequest(request); + diffBuilder.addOkAction().setText(UIBundle.message("file.cache.conflict.save.changes.button")); + diffBuilder.addCancelAction(); + diffBuilder.setTitle(title); + if (diffBuilder.show() == DialogWrapper.OK_EXIT_CODE) { + builder.getDialogWrapper().close(DialogWrapper.CANCEL_EXIT_CODE); + } + } + }); + builder.setTitle(UIBundle.message("file.cache.conflict.dialog.title")); + builder.setButtonsAlignment(SwingConstants.CENTER); + builder.setHelpId("reference.dialogs.fileCacheConflict"); + return builder.show() == 0; + } + }; + + @TestOnly + public void setAskReloadFromDisk(@NotNull Disposable disposable, + @NotNull PairProcessor newProcessor) { + final PairProcessor old = askReloadFromDisk; + askReloadFromDisk = newProcessor; + Disposer.register(disposable, new Disposable() { + @Override + public void dispose() { + askReloadFromDisk = old; + } + }); + } + + private boolean askReloadFromDisk(final VirtualFile file, final Document document) { ApplicationManager.getApplication().assertIsDispatchThread(); if (!isDocumentUnsaved(document)) return true; - String message = UIBundle.message("file.cache.conflict.message.text", file.getPresentableUrl()); - if (ApplicationManager.getApplication().isUnitTestMode()) throw new RuntimeException(message); - final DialogBuilder builder = new DialogBuilder((Project)null); - builder.setCenterPanel(new JLabel(message, Messages.getQuestionIcon(), SwingConstants.CENTER)); - builder.addOkAction().setText(UIBundle.message("file.cache.conflict.load.fs.changes.button")); - builder.addCancelAction().setText(UIBundle.message("file.cache.conflict.keep.memory.changes.button")); - builder.addAction(new AbstractAction(UIBundle.message("file.cache.conflict.show.difference.button")) { - @Override - public void actionPerformed(ActionEvent e) { - String title = UIBundle.message("file.cache.conflict.for.file.dialog.title", file.getPresentableUrl()); - final ProjectEx project = (ProjectEx)ProjectLocator.getInstance().guessProjectForFile(file); - - SimpleDiffRequest request = new SimpleDiffRequest(project, title); - FileType fileType = file.getFileType(); - String fsContent = LoadTextUtil.loadText(file).toString(); - request.setContents(new SimpleContent(fsContent, fileType), - new DocumentContent(project, document, fileType)); - request.setContentTitles(UIBundle.message("file.cache.conflict.diff.content.file.system.content"), - UIBundle.message("file.cache.conflict.diff.content.memory.content")); - DialogBuilder diffBuilder = new DialogBuilder(project); - DiffPanelImpl diffPanel = (DiffPanelImpl)DiffManager.getInstance().createDiffPanel(diffBuilder.getWindow(), project, diffBuilder, null); - diffPanel.getOptions().setShowSourcePolicy(DiffPanelOptions.ShowSourcePolicy.DONT_SHOW); - diffBuilder.setCenterPanel(diffPanel.getComponent()); - diffBuilder.setDimensionServiceKey("FileDocumentManager.FileCacheConflict"); - diffPanel.setDiffRequest(request); - diffBuilder.addOkAction().setText(UIBundle.message("file.cache.conflict.save.changes.button")); - diffBuilder.addCancelAction(); - diffBuilder.setTitle(title); - if (diffBuilder.show() == DialogWrapper.OK_EXIT_CODE) { - builder.getDialogWrapper().close(DialogWrapper.CANCEL_EXIT_CODE); - } - } - }); - builder.setTitle(UIBundle.message("file.cache.conflict.dialog.title")); - builder.setButtonsAlignment(SwingConstants.CENTER); - builder.setHelpId("reference.dialogs.fileCacheConflict"); - return builder.show() == 0; + return askReloadFromDisk.process(file, document); } @Override @@ -799,7 +822,13 @@ public class FileDocumentManagerImpl extends FileDocumentManager implements Appl return FileDocumentManagerListener.EP_NAME.getExtensions(); } - protected void handleErrorsOnSave(@NotNull Map failures) { + private void handleErrorsOnSave(@NotNull Map failures) { + if (ApplicationManager.getApplication().isUnitTestMode()) { + for (IOException exception : failures.values()) { + throw new RuntimeException(exception); + } + return; + } for (IOException exception : failures.values()) { LOG.warn(exception); } diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/FileDocumentManagerImplTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/FileDocumentManagerImplTest.java index dffec2b1f7c5..f8f1610d7b38 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/FileDocumentManagerImplTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/FileDocumentManagerImplTest.java @@ -15,87 +15,48 @@ */ package com.intellij.openapi.fileEditor; -import com.intellij.codeStyle.CodeStyleFacade; -import com.intellij.codeStyle.DefaultCodeStyleFacade; -import com.intellij.ide.DataManager; -import com.intellij.ide.impl.DataManagerImpl; -import com.intellij.mock.MockCommandProcessor; -import com.intellij.mock.MockEditorFactory; +import com.intellij.AppTopics; import com.intellij.mock.MockVirtualFile; -import com.intellij.openapi.command.CommandProcessor; -import com.intellij.openapi.command.UndoConfirmationPolicy; +import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.editor.Document; -import com.intellij.openapi.editor.EditorFactory; import com.intellij.openapi.editor.ex.DocumentEx; import com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl; -import com.intellij.openapi.fileTypes.*; -import com.intellij.openapi.fileTypes.impl.FileTypeManagerImpl; -import com.intellij.openapi.fileTypes.impl.InternalFileTypeFactory; -import com.intellij.openapi.project.Project; -import com.intellij.openapi.project.ProjectLocator; -import com.intellij.openapi.project.ProjectManager; -import com.intellij.openapi.project.impl.DefaultProjectLocator; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.vfs.CharsetToolkit; +import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFileEvent; -import com.intellij.openapi.vfs.VirtualFileManager; -import com.intellij.testFramework.MockSchemesManagerFactory; -import com.intellij.testFramework.PlatformLiteFixture; +import com.intellij.testFramework.PlatformTestCase; import com.intellij.util.LocalTimeCounter; -import org.easymock.classextension.EasyMock; +import com.intellij.util.PairProcessor; import org.jetbrains.annotations.NotNull; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.IOException; import java.io.OutputStream; import java.util.Arrays; -import java.util.Collection; -import java.util.Map; -public class FileDocumentManagerImplTest extends PlatformLiteFixture { - private MyMockFileDocumentManager myDocumentManager; +public class FileDocumentManagerImplTest extends PlatformTestCase { + private FileDocumentManagerImpl myDocumentManager; + public Boolean myReloadFromDisk; @Override protected void setUp() throws Exception { super.setUp(); - initApplication(); - registerExtension(FileTypeFactory.FILE_TYPE_FACTORY_EP, new InternalFileTypeFactory()); - registerExtensionPoint(FileDocumentSynchronizationVetoer.EP_NAME, FileDocumentSynchronizationVetoer.class); - getApplication().registerService(CommandProcessor.class, new MyMockCommandProcessor()); - getApplication().registerService(CodeStyleFacade.class, new DefaultCodeStyleFacade()); - getApplication().registerService(ProjectLocator.class, new DefaultProjectLocator()); - - MockEditorFactory editorFactory = new MockEditorFactory(); - getApplication().registerService(EditorFactory.class, editorFactory); - final LanguageFileType[] fileType = {null}; - getApplication().addComponent(FileTypeManager.class, new FileTypeManagerImpl(null, new MockSchemesManagerFactory()) { - @NotNull + myReloadFromDisk = Boolean.TRUE; + FileDocumentManagerImpl impl = (FileDocumentManagerImpl)FileDocumentManager.getInstance(); + impl.setAskReloadFromDisk(getTestRootDisposable(), new PairProcessor() { @Override - public FileType getFileTypeByFileName(@NotNull String fileName) { - return fileType[0]; - } - - @NotNull - @Override - public FileType getFileTypeByFile(@NotNull VirtualFile file) { - return fileType[0]; - } - - @NotNull - @Override - public FileType getFileTypeByExtension(@NotNull String extension) { - return fileType[0]; + public boolean process(VirtualFile file, Document document) { + if (myReloadFromDisk == null) { + fail(); + return false; + } + return myReloadFromDisk.booleanValue(); } }); - - fileType[0] = StdFileTypes.JAVA; - - getApplication().getComponent(FileTypeManager.class); - - final VirtualFileManager virtualFileManager = EasyMock.createMock(VirtualFileManager.class); - final ProjectManager projectManager = EasyMock.createMock(ProjectManager.class); - myDocumentManager = new MyMockFileDocumentManager(virtualFileManager, projectManager); - getApplication().registerService(FileDocumentManager.class, myDocumentManager); - getApplication().registerService(DataManager.class, new DataManagerImpl()); + myDocumentManager = impl; } public void testGetCachedDocument_Cached() throws Exception { @@ -104,14 +65,14 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture { } public void testGetCachedDocument_NotCached() throws Exception { - final VirtualFile file = newTextFile(); + final VirtualFile file = createFile(); final Document document = myDocumentManager.getDocument(file); assertSame(myDocumentManager.getCachedDocument(file), document); } public void testGetDocument_CreateNew() throws Exception { - final VirtualFile file = newTextFile(); + final VirtualFile file = createFile(); final Document document = myDocumentManager.getDocument(file); assertNotNull(document); assertEquals("test", document.getText()); @@ -120,7 +81,7 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture { } public void testGetDocument_CreateNew_ReadOnly() throws Exception { - final MockVirtualFile file = newTextFile(); + final VirtualFile file = createFile(); file.setWritable(false); final Document document = myDocumentManager.getDocument(file); assertNotNull(document); @@ -130,7 +91,7 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture { } public void testGetDocument_ReturnCachedValueTwice() throws Exception { - final VirtualFile file = newTextFile(); + final VirtualFile file = createFile(); final Document document = myDocumentManager.getDocument(file); assertNotNull(document); assertEquals("test", document.getText()); @@ -140,7 +101,7 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture { } public void testGetDocument_CreatesNewAfterGCed() throws Exception { - final VirtualFile file = newTextFile(); + final VirtualFile file = createFile(); Document document = myDocumentManager.getDocument(file); int idCode = System.identityHashCode(document); //noinspection UnusedAssignment @@ -159,7 +120,7 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture { } public void testGetUnsavedDocuments_CreatedDocument() throws Exception { - final VirtualFile file = newTextFile(); + final VirtualFile file = createFile(); myDocumentManager.getDocument(file); final Document[] unsavedDocuments = myDocumentManager.getUnsavedDocuments(); @@ -167,10 +128,16 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture { } public void testGetUnsavedDocuments_ModifiedDocument() throws Exception { - final VirtualFile file = newTextFile(); - Document document = myDocumentManager.getDocument(file); + final VirtualFile file = createFile(); + final Document document = myDocumentManager.getDocument(file); assertNotNull(file.toString(), document); - document.insertString(0, "xxx"); + WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() { + @Override + public void run() { + document.insertString(0, "xxx"); + } + }); + final Document[] unsavedDocuments = myDocumentManager.getUnsavedDocuments(); assertEquals(1, unsavedDocuments.length); @@ -179,10 +146,16 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture { } public void testGetUnsavedDocuments_afterSaveAllDocuments() throws Exception { - final VirtualFile file = newTextFile(); - Document document = myDocumentManager.getDocument(file); + final VirtualFile file = createFile(); + final Document document = myDocumentManager.getDocument(file); assertNotNull(file.toString(), document); - document.insertString(0, "xxx"); + WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() { + @Override + public void run() { + document.insertString(0, "xxx"); + } + }); + myDocumentManager.saveAllDocuments(); final Document[] unsavedDocuments = myDocumentManager.getUnsavedDocuments(); @@ -190,10 +163,16 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture { } public void testGetUnsavedDocuments_afterSaveDocuments() throws Exception { - final VirtualFile file = newTextFile(); - Document document = myDocumentManager.getDocument(file); + final VirtualFile file = createFile(); + final Document document = myDocumentManager.getDocument(file); assertNotNull(file.toString(), document); - document.insertString(0, "xxx"); + WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() { + @Override + public void run() { + document.insertString(0, "xxx"); + } + }); + myDocumentManager.saveDocument(document); final Document[] unsavedDocuments = myDocumentManager.getUnsavedDocuments(); @@ -201,34 +180,55 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture { } public void testGetUnsavedDocuments_afterSaveDocumentWithProblems() throws Exception { - final VirtualFile file = new MockVirtualFile("test.txt", "test") { - @Override - @NotNull - public OutputStream getOutputStream(Object requestor, long newModificationStamp, long newTimeStamp) throws IOException { - throw new IOException(""); + try { + final VirtualFile file = new MockVirtualFile("test.txt", "test") { + @Override + @NotNull + public OutputStream getOutputStream(Object requestor, long newModificationStamp, long newTimeStamp) throws IOException { + throw new IOException(""); + } + }; + + final Document document = myDocumentManager.getDocument(file); + assertNotNull(file.toString(), document); + WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() { + @Override + public void run() { + document.insertString(0, "xxx"); + } + }); + + + try { + myDocumentManager.saveDocument(document); + fail("must throw IOException"); + } + catch (RuntimeException e) { + assertTrue(e.getCause() instanceof IOException); } - }; - Document document = myDocumentManager.getDocument(file); - assertNotNull(file.toString(), document); - document.insertString(0, "xxx"); - - myDocumentManager.saveDocument(document); - - assertNotNull(myDocumentManager.myExceptionOnSave); - - final Document[] unsavedDocuments = myDocumentManager.getUnsavedDocuments(); - assertEquals(1, unsavedDocuments.length); - assertSame(document, unsavedDocuments[0]); - assertTrue(Arrays.equals("test".getBytes("UTF-8"), file.contentsToByteArray())); + final Document[] unsavedDocuments = myDocumentManager.getUnsavedDocuments(); + assertEquals(1, unsavedDocuments.length); + assertSame(document, unsavedDocuments[0]); + assertTrue(Arrays.equals("test".getBytes("UTF-8"), file.contentsToByteArray())); + } + finally { + myDocumentManager.dropAllUnsavedDocuments(); + } } public void testUnsavedDocument_DoNotGC() throws Exception { - final VirtualFile file = newTextFile(); + final VirtualFile file = createFile(); Document document = myDocumentManager.getDocument(file); - assertNotNull(file.toString(), document); - document.insertString(0, "xxx"); int idCode = System.identityHashCode(document); + assertNotNull(file.toString(), document); + WriteCommandAction.runWriteCommandAction(myProject, new Runnable() { + @Override + public void run() { + myDocumentManager.getDocument(file).insertString(0, "xxx"); + } + }); + //noinspection UnusedAssignment document = null; @@ -240,10 +240,16 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture { } public void testUnsavedDocument_GcAfterSave() throws Exception { - final VirtualFile file = newTextFile(); + final VirtualFile file = createFile(); Document document = myDocumentManager.getDocument(file); assertNotNull(file.toString(), document); - document.insertString(0, "xxx"); + WriteCommandAction.runWriteCommandAction(myProject, new Runnable() { + @Override + public void run() { + myDocumentManager.getDocument(file).insertString(0, "xxx"); + } + }); + int idCode = System.identityHashCode(document); //noinspection UnusedAssignment document = null; @@ -258,7 +264,7 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture { } public void testSaveDocument_DocumentWasNotChanged() throws Exception { - final VirtualFile file = newTextFile(); + final VirtualFile file = createFile(); final long stamp = file.getModificationStamp(); Document document = myDocumentManager.getDocument(file); assertNotNull(file.toString(), document); @@ -267,11 +273,17 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture { } public void testSaveDocument_DocumentWasChanged() throws Exception { - final VirtualFile file = newTextFile(); + final VirtualFile file = createFile(); final long stamp = file.getModificationStamp(); - Document document = myDocumentManager.getDocument(file); + final Document document = myDocumentManager.getDocument(file); assertNotNull(file.toString(), document); - document.insertString(0, "xxx "); + WriteCommandAction.runWriteCommandAction(myProject, new Runnable() { + @Override + public void run() { + document.insertString(0, "xxx "); + } + }); + myDocumentManager.saveDocument(document); assertTrue(stamp != file.getModificationStamp()); @@ -280,11 +292,17 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture { } public void testSaveAllDocuments_DocumentWasChanged() throws Exception { - final VirtualFile file = newTextFile(); + final VirtualFile file = createFile(); final long stamp = file.getModificationStamp(); - Document document = myDocumentManager.getDocument(file); + final Document document = myDocumentManager.getDocument(file); assertNotNull(file.toString(), document); - document.insertString(0, "xxx "); + WriteCommandAction.runWriteCommandAction(myProject, new Runnable() { + @Override + public void run() { + document.insertString(0, "xxx "); + } + }); + myDocumentManager.saveAllDocuments(); assertTrue(stamp != file.getModificationStamp()); @@ -292,7 +310,7 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture { } public void testGetFile() throws Exception { - final VirtualFile file = newTextFile(); + final VirtualFile file = createFile(); Document document = myDocumentManager.getDocument(file); assertNotNull(file.toString(), document); assertSame(file, myDocumentManager.getFile(document)); @@ -307,35 +325,51 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture { public void testRememberSeparators() throws Exception { final VirtualFile file = new MockVirtualFile("test.txt", "test\rtest"); - Document document = myDocumentManager.getDocument(file); + final Document document = myDocumentManager.getDocument(file); assertNotNull(file.toString(), document); - document.insertString(0, "xxx "); + WriteCommandAction.runWriteCommandAction(myProject, new Runnable() { + @Override + public void run() { + document.insertString(0, "xxx "); + } + }); + myDocumentManager.saveAllDocuments(); assertTrue(Arrays.equals("xxx test\rtest".getBytes("UTF-8"), file.contentsToByteArray())); } public void testContentChanged_noDocument() throws Exception { - final MockVirtualFile file = new MockVirtualFile("test.txt", "test\rtest"); - file.setListener(myDocumentManager); - file.setContent(null, "xxx", true); + final VirtualFile file = createFile(); + setContent(file, "xxx"); assertNull(myDocumentManager.getCachedDocument(file)); } + VirtualFile createFile(String name, String content) throws IOException { + File file = createTempFile(name, content); + VirtualFile virtualFile = LocalFileSystem.getInstance().findFileByIoFile(file); + assertNotNull(virtualFile); + return virtualFile; + } + VirtualFile createFile() throws IOException { + return createFile("test.txt", "test"); + } + void setContent(VirtualFile file, String content) throws IOException { + file.setBinaryContent(content.getBytes(CharsetToolkit.UTF8_CHARSET)); + } + public void testContentChanged_documentPresent() throws Exception { - final MockVirtualFile file = new MockVirtualFile("test.txt", "test\rtest"); - file.setListener(myDocumentManager); + VirtualFile file = createFile(); Document document = myDocumentManager.getDocument(file); - file.setContent(null, "xxx", true); + setContent(file, "xxx"); assertNotNull(file.toString(), document); assertEquals("xxx", document.getText()); assertEquals(file.getModificationStamp(), document.getModificationStamp()); } public void testContentChanged_ignoreEventsFromSelf() throws Exception { - final MockVirtualFile file = new MockVirtualFile("test.txt", "test\rtest"); - file.setListener(myDocumentManager); + final VirtualFile file = createFile("test.txt", "test\rtest"); Document document = myDocumentManager.getDocument(file); - file.setContent(myDocumentManager, "xxx", true); + file.setBinaryContent("xxx".getBytes(CharsetToolkit.UTF8_CHARSET), -1,-1,myDocumentManager); assertNotNull(file.toString(), document); assertEquals("test\ntest", document.getText()); } @@ -358,9 +392,15 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture { }; } }; - Document document = myDocumentManager.getDocument(file); + final Document document = myDocumentManager.getDocument(file); assertNotNull(file.toString(), document); - document.insertString(0, "xxx"); + WriteCommandAction.runWriteCommandAction(myProject, new Runnable() { + @Override + public void run() { + document.insertString(0, "xxx"); + } + }); + final long stamp = document.getModificationStamp(); myDocumentManager.saveAllDocuments(); @@ -368,59 +408,63 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture { } public void testContentChanged_reloadChangedDocument() throws Exception { - final MockVirtualFile file = new MockVirtualFile("test.txt", "test\rtest"); - file.setListener(myDocumentManager); - Document document = myDocumentManager.getDocument(file); + final VirtualFile file = createFile(); + final Document document = myDocumentManager.getDocument(file); assertNotNull(file.toString(), document); - document.insertString(0, "zzz"); + WriteCommandAction.runWriteCommandAction(myProject, new Runnable() { + @Override + public void run() { + document.insertString(0, "zzz"); + } + }); - myDocumentManager.myReloadFromDisk = Boolean.TRUE; - try { - file.setContent(null, "xxx", true); - assertEquals("xxx", document.getText()); - assertEquals(file.getModificationStamp(), document.getModificationStamp()); - assertEquals(0, myDocumentManager.getUnsavedDocuments().length); - } - finally { - myDocumentManager.myReloadFromDisk = null; - } + myReloadFromDisk = Boolean.TRUE; + setContent(file, "xxx"); + + assertEquals("xxx", document.getText()); + assertEquals(file.getModificationStamp(), document.getModificationStamp()); + assertEquals(0, myDocumentManager.getUnsavedDocuments().length); } public void testContentChanged_DoNotReloadChangedDocument() throws Exception { - final MockVirtualFile file = newTextFile(); - file.setListener(myDocumentManager); - Document document = myDocumentManager.getDocument(file); + final VirtualFile file = createFile(); + final Document document = myDocumentManager.getDocument(file); assertNotNull(file.toString(), document); - document.insertString(0, "old "); + WriteCommandAction.runWriteCommandAction(myProject, new Runnable() { + @Override + public void run() { + document.insertString(0, "old "); + } + }); - myDocumentManager.myReloadFromDisk = Boolean.FALSE; - try { - long oldDocumentStamp = document.getModificationStamp(); + myReloadFromDisk = Boolean.FALSE; + long oldDocumentStamp = document.getModificationStamp(); - file.setContent(null, "xxx", true); + file.setBinaryContent("xxx".getBytes(CharsetToolkit.UTF8_CHARSET)); - assertEquals("old test", document.getText()); - assertEquals(oldDocumentStamp, document.getModificationStamp()); - } - finally { - myDocumentManager.myReloadFromDisk = null; - } + assertEquals("old test", document.getText()); + assertEquals(oldDocumentStamp, document.getModificationStamp()); } public void testSaveDocument_DoNotSaveIfModStampEqualsToFile() throws Exception { - final VirtualFile file = new MockVirtualFile("test.txt", "test") { - @NotNull - @Override - public OutputStream getOutputStream(Object requestor, long newModificationStamp, long newTimeStamp) throws IOException { - fail(); - throw new IOException(); - } - }; - DocumentEx document = (DocumentEx)myDocumentManager.getDocument(file); + final VirtualFile file = createFile(); + final DocumentEx document = (DocumentEx)myDocumentManager.getDocument(file); assertNotNull(file.toString(), document); - document.insertString(0, "zzz"); - document.setModificationStamp(file.getModificationStamp()); + WriteCommandAction.runWriteCommandAction(myProject, new Runnable() { + @Override + public void run() { + document.insertString(0, "zzz"); + document.setModificationStamp(file.getModificationStamp()); + } + }); + + getProject().getMessageBus().connect(getTestRootDisposable()).subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter() { + @Override + public void beforeDocumentSaving(@NotNull Document documentToSave) { + assertNotSame(document, documentToSave); + } + }); myDocumentManager.saveDocument(document); } @@ -442,23 +486,19 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture { document.insertString(0, "zzz"); file.setContent(null, "xxx", false); - myDocumentManager.myReloadFromDisk = Boolean.TRUE; - try { - myDocumentManager.saveAllDocuments(); - long fileStamp = file.getModificationStamp(); + myReloadFromDisk = Boolean.TRUE; + myDocumentManager.saveAllDocuments(); + long fileStamp = file.getModificationStamp(); - assertEquals("xxx", document.getText()); - assertEquals(file.getModificationStamp(), document.getModificationStamp()); - assertEquals(file.getModificationStamp(), fileStamp); - assertEquals(0, myDocumentManager.getUnsavedDocuments().length); - } - finally { - myDocumentManager.myReloadFromDisk = null; - } + assertEquals("xxx", document.getText()); + assertEquals(file.getModificationStamp(), document.getModificationStamp()); + assertEquals(file.getModificationStamp(), fileStamp); + assertEquals(0, myDocumentManager.getUnsavedDocuments().length); } public void testContentChanged_doNotReloadChangedDocumentOnSave() throws Exception { - final MockVirtualFile file = new MockVirtualFile("test.txt", "test") { + final MockVirtualFile file = + new MockVirtualFile("test.txt", "test") { @Override public void refresh(boolean asynchronous, boolean recursive, Runnable postRunnable) { long oldStamp = getModificationStamp(); @@ -467,154 +507,72 @@ public class FileDocumentManagerImplTest extends PlatformLiteFixture { } }; - myDocumentManager.myReloadFromDisk = Boolean.FALSE; - try { - Document document = myDocumentManager.getDocument(file); - assertNotNull(file.toString(), document); - document.insertString(0, "old "); - long documentStamp = document.getModificationStamp(); + myReloadFromDisk = Boolean.FALSE; + final Document document = myDocumentManager.getDocument(file); + assertNotNull(file.toString(), document); + WriteCommandAction.runWriteCommandAction(myProject, new Runnable() { + @Override + public void run() { + document.insertString(0, "old "); + } + }); - file.setContent(null, "xxx", false); + long documentStamp = document.getModificationStamp(); - myDocumentManager.saveAllDocuments(); + file.setContent(null, "xxx", false); - assertEquals("old test", document.getText()); - assertEquals(file.getModificationStamp(), document.getModificationStamp()); - assertTrue(Arrays.equals("old test".getBytes("UTF-8"), file.contentsToByteArray())); - assertEquals(documentStamp, document.getModificationStamp()); - } - finally { - myDocumentManager.myReloadFromDisk = null; - } + myDocumentManager.saveAllDocuments(); + + assertEquals("old test", document.getText()); + assertEquals(file.getModificationStamp(), document.getModificationStamp()); + assertTrue(Arrays.equals("old test".getBytes("UTF-8"), file.contentsToByteArray())); + assertEquals(documentStamp, document.getModificationStamp()); } - public void testReplaceDocumentTestWithTheSameTest() throws Exception { - final boolean[] canCallSave = {true}; - final VirtualFile file = new MockVirtualFile("test.txt", "test") { - @NotNull - @Override - public OutputStream getOutputStream(Object requestor, long newModificationStamp, long newTimeStamp) throws IOException { - if (!canCallSave[0]) fail(); - return super.getOutputStream(requestor, newModificationStamp, newTimeStamp); - } - }; - DocumentEx document = (DocumentEx)myDocumentManager.getDocument(file); + public void testReplaceDocumentTextWithTheSameText() throws Exception { + final VirtualFile file = createFile(); + final DocumentEx document = (DocumentEx)myDocumentManager.getDocument(file); - String newText = "test text"; + final String newText = "test text"; assertNotNull(file.toString(), document); - document.replaceString(0, document.getTextLength(), newText); - assertTrue(myDocumentManager.isDocumentUnsaved(document)); - myDocumentManager.saveDocument(document); + WriteCommandAction.runWriteCommandAction(myProject, new Runnable() { + @Override + public void run() { + document.replaceString(0, document.getTextLength(), newText); + assertTrue(myDocumentManager.isDocumentUnsaved(document)); + myDocumentManager.saveDocument(document); - canCallSave[0] = false; + getProject().getMessageBus().connect(getTestRootDisposable()) + .subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter() { + @Override + public void beforeDocumentSaving(@NotNull Document documentToSave) { + assertNotSame(document, documentToSave); + } + }); - final long modificationStamp = document.getModificationStamp(); + final long modificationStamp = document.getModificationStamp(); - document.replaceString(0, document.getTextLength(), newText); - - if (myDocumentManager.isDocumentUnsaved(document)) { - assertTrue(document.getModificationStamp() > modificationStamp); - } - else { - assertEquals(modificationStamp, document.getModificationStamp()); - } + document.replaceString(0, document.getTextLength(), newText); + if (myDocumentManager.isDocumentUnsaved(document)) { + assertTrue(document.getModificationStamp() > modificationStamp); + } + else { + assertEquals(modificationStamp, document.getModificationStamp()); + } + } + }); } public void testExternalReplaceWithTheSameText() throws Exception { - final long[] modificationStamp = new long[1]; - modificationStamp[0] = 1; - - final VirtualFile file = new MockVirtualFile("test.txt", "test") { - @NotNull - @Override - public OutputStream getOutputStream(Object requestor, long newModificationStamp, long newTimeStamp) throws IOException { - fail(); - throw new IOException(); - } - - @Override - public long getModificationStamp() { - return modificationStamp[0]; - } - - @Override - public void refresh(boolean asynchronous, boolean recursive, Runnable postRunnable) { - myDocumentManager.contentsChanged(new VirtualFileEvent(null, this, null, 1, 2)); - } - }; + final VirtualFile file = createFile(); + long modificationStamp = file.getModificationStamp(); DocumentEx document = (DocumentEx)myDocumentManager.getDocument(file); - modificationStamp[0] = 2; + FileUtil.writeToFile(new File(file.getPath()), "xxx"); file.refresh(false, false); assertNotNull(file.toString(), document); - assertEquals(2, document.getModificationStamp()); - } - private static MockVirtualFile newTextFile() { - return new MockVirtualFile("test.txt", "test"); - } - - private static class MyMockFileDocumentManager extends FileDocumentManagerImpl { - private static final FileDocumentManagerListener[] LISTENERS = new FileDocumentManagerListener[0]; - - private Collection myExceptionOnSave = null; - private Boolean myReloadFromDisk = null; - - public MyMockFileDocumentManager(VirtualFileManager virtualFileManager, ProjectManager projectManager) { - super(virtualFileManager, projectManager); - } - - @Override - protected void handleErrorsOnSave(@NotNull Map failures) { - myExceptionOnSave = failures.values(); - } - - @Override - protected boolean askReloadFromDisk(VirtualFile file, Document document) { - if (myReloadFromDisk == null) { - fail(); - return false; - } - else { - return myReloadFromDisk.booleanValue(); - } - } - - @NotNull - @Override - protected FileDocumentManagerListener[] getListeners() { - return LISTENERS; - } - } - - private static class MyMockCommandProcessor extends MockCommandProcessor { - @Override - public void executeCommand(Project project, @NotNull Runnable runnable, String name, Object groupId) { - runnable.run(); - } - - @Override - public void executeCommand(Project project, - @NotNull Runnable runnable, - String name, - Object groupId, - @NotNull UndoConfirmationPolicy confirmationPolicy, - Document document) { - runnable.run(); - } - - @Override - public void executeCommand(Project project, - @NotNull Runnable runnable, - String name, - Object groupId, - @NotNull UndoConfirmationPolicy confirmationPolicy) { - runnable.run(); - } - - @Override - public void executeCommand(@NotNull Runnable runnable, String name, Object groupId) { - runnable.run(); - } + assertNotSame(file.getModificationStamp(), modificationStamp); + assertEquals(file.getModificationStamp(), document.getModificationStamp()); } } From 6bd1c83d6d5295b7df856edb2b7db290b9bfa583 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Mon, 2 Dec 2013 13:46:11 +0400 Subject: [PATCH 09/10] cleanup --- .../openapi/editor/actionSystem/EditorWriteActionHandler.java | 1 - 1 file changed, 1 deletion(-) diff --git a/platform/platform-api/src/com/intellij/openapi/editor/actionSystem/EditorWriteActionHandler.java b/platform/platform-api/src/com/intellij/openapi/editor/actionSystem/EditorWriteActionHandler.java index 235214be85a9..e66ce8248ff8 100644 --- a/platform/platform-api/src/com/intellij/openapi/editor/actionSystem/EditorWriteActionHandler.java +++ b/platform/platform-api/src/com/intellij/openapi/editor/actionSystem/EditorWriteActionHandler.java @@ -17,7 +17,6 @@ package com.intellij.openapi.editor.actionSystem; import com.intellij.openapi.actionSystem.CommonDataKeys; import com.intellij.openapi.actionSystem.DataContext; -import com.intellij.openapi.actionSystem.PlatformDataKeys; import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.editor.*; import com.intellij.openapi.editor.event.DocumentEvent; From 7abab1494c473f620ffbe8bc1f5581f040222a3f Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Mon, 2 Dec 2013 13:51:36 +0400 Subject: [PATCH 10/10] test classes combined --- .../FileDocumentManagerImplTest.java | 88 +++++++++++++- .../RealFileDocumentManagerTest.java | 113 ------------------ 2 files changed, 86 insertions(+), 115 deletions(-) delete mode 100644 platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/RealFileDocumentManagerTest.java diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/FileDocumentManagerImplTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/FileDocumentManagerImplTest.java index f8f1610d7b38..a5cdcbef8439 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/FileDocumentManagerImplTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/FileDocumentManagerImplTest.java @@ -17,16 +17,22 @@ package com.intellij.openapi.fileEditor; import com.intellij.AppTopics; import com.intellij.mock.MockVirtualFile; +import com.intellij.openapi.application.AccessToken; +import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.command.WriteCommandAction; import com.intellij.openapi.editor.Document; import com.intellij.openapi.editor.ex.DocumentEx; import com.intellij.openapi.fileEditor.impl.FileDocumentManagerImpl; import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.util.io.IoTestUtil; import com.intellij.openapi.vfs.CharsetToolkit; import com.intellij.openapi.vfs.LocalFileSystem; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.openapi.vfs.VirtualFileEvent; -import com.intellij.testFramework.PlatformTestCase; +import com.intellij.psi.PsiDocumentManager; +import com.intellij.psi.PsiFile; +import com.intellij.testFramework.PlatformLangTestCase; +import com.intellij.util.IncorrectOperationException; import com.intellij.util.LocalTimeCounter; import com.intellij.util.PairProcessor; import org.jetbrains.annotations.NotNull; @@ -37,7 +43,7 @@ import java.io.IOException; import java.io.OutputStream; import java.util.Arrays; -public class FileDocumentManagerImplTest extends PlatformTestCase { +public class FileDocumentManagerImplTest extends PlatformLangTestCase { private FileDocumentManagerImpl myDocumentManager; public Boolean myReloadFromDisk; @@ -575,4 +581,82 @@ public class FileDocumentManagerImplTest extends PlatformTestCase { assertNotSame(file.getModificationStamp(), modificationStamp); assertEquals(file.getModificationStamp(), document.getModificationStamp()); } + + + public void testFileTypeModificationDocumentPreservation() throws Exception { + File ioFile = IoTestUtil.createTestFile("test.html", "some text"); + VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(ioFile); + assertNotNull(ioFile.getPath(), file); + + FileDocumentManager documentManager = FileDocumentManager.getInstance(); + Document original = documentManager.getDocument(file); + assertNotNull(file.getPath(), original); + + renameFile(file, "test.wtf"); + Document afterRename = documentManager.getDocument(file); + assertTrue(afterRename + " != " + original, afterRename == original); + } + + public void testFileTypeChangeDocumentDetach() throws Exception { + File ioFile = IoTestUtil.createTestFile("test.html", "some text"); + VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(ioFile); + assertNotNull(ioFile.getPath(), file); + + FileDocumentManager documentManager = FileDocumentManager.getInstance(); + Document original = documentManager.getDocument(file); + assertNotNull(file.getPath(), original); + + renameFile(file, "test.png"); + Document afterRename = documentManager.getDocument(file); + assertNull(afterRename + " != null", afterRename); + } + + private static void renameFile(VirtualFile file, String newName) throws IOException { + AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(null); + try { + file.rename(null, newName); + } + finally { + token.finish(); + } + } + + public void testNoPSIModificationsDuringSave() throws IOException { + File ioFile = IoTestUtil.createTestFile("test.txt", "some text"); + VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(ioFile); + assertNotNull(ioFile.getPath(), virtualFile); + + FileDocumentManager documentManager = FileDocumentManager.getInstance(); + Document original = documentManager.getDocument(virtualFile); + assertNotNull(virtualFile.getPath(), original); + + final PsiFile file = getPsiFile(original); + FileDocumentManagerListener saveListener = new FileDocumentManagerAdapter() { + @Override + public void beforeDocumentSaving(@NotNull Document document) { + WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() { + @Override + public void run() { + try { + file.getFirstChild().delete(); + fail("Must not modify PSI inside save listener"); + } + catch (IncorrectOperationException e) { + assertEquals("Must not modify PSI inside save listener", e.getMessage()); + } + } + }); + } + }; + getProject().getMessageBus().connect(getTestRootDisposable()).subscribe(AppTopics.FILE_DOCUMENT_SYNC, saveListener); + final Document document = PsiDocumentManager.getInstance(getProject()).getDocument(file); + WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() { + @Override + public void run() { + document.insertString(1,"y"); + } + }); + + FileDocumentManager.getInstance().saveAllDocuments(); + } } diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/RealFileDocumentManagerTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/RealFileDocumentManagerTest.java deleted file mode 100644 index 07be9d45e8ee..000000000000 --- a/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/RealFileDocumentManagerTest.java +++ /dev/null @@ -1,113 +0,0 @@ -/* - * Copyright 2000-2013 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.openapi.fileEditor; - -import com.intellij.AppTopics; -import com.intellij.openapi.application.AccessToken; -import com.intellij.openapi.application.ApplicationManager; -import com.intellij.openapi.command.WriteCommandAction; -import com.intellij.openapi.editor.Document; -import com.intellij.openapi.util.io.IoTestUtil; -import com.intellij.openapi.vfs.LocalFileSystem; -import com.intellij.openapi.vfs.VirtualFile; -import com.intellij.psi.PsiDocumentManager; -import com.intellij.psi.PsiFile; -import com.intellij.testFramework.PlatformLangTestCase; -import com.intellij.util.IncorrectOperationException; -import org.jetbrains.annotations.NotNull; - -import java.io.File; -import java.io.IOException; - -public class RealFileDocumentManagerTest extends PlatformLangTestCase { - public void testFileTypeModificationDocumentPreservation() throws Exception { - File ioFile = IoTestUtil.createTestFile("test.html", "some text"); - VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(ioFile); - assertNotNull(ioFile.getPath(), file); - - FileDocumentManager documentManager = FileDocumentManager.getInstance(); - Document original = documentManager.getDocument(file); - assertNotNull(file.getPath(), original); - - renameFile(file, "test.wtf"); - Document afterRename = documentManager.getDocument(file); - assertTrue(afterRename + " != " + original, afterRename == original); - } - - public void testFileTypeChangeDocumentDetach() throws Exception { - File ioFile = IoTestUtil.createTestFile("test.html", "some text"); - VirtualFile file = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(ioFile); - assertNotNull(ioFile.getPath(), file); - - FileDocumentManager documentManager = FileDocumentManager.getInstance(); - Document original = documentManager.getDocument(file); - assertNotNull(file.getPath(), original); - - renameFile(file, "test.png"); - Document afterRename = documentManager.getDocument(file); - assertNull(afterRename + " != null", afterRename); - } - - private static void renameFile(VirtualFile file, String newName) throws IOException { - AccessToken token = ApplicationManager.getApplication().acquireWriteActionLock(RealFileDocumentManagerTest.class); - try { - file.rename(RealFileDocumentManagerTest.class, newName); - } - finally { - token.finish(); - } - } - - public void testNoPSIModificationsDuringSave() throws IOException { - File ioFile = IoTestUtil.createTestFile("test.txt", "some text"); - VirtualFile virtualFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(ioFile); - assertNotNull(ioFile.getPath(), virtualFile); - - FileDocumentManager documentManager = FileDocumentManager.getInstance(); - Document original = documentManager.getDocument(virtualFile); - assertNotNull(virtualFile.getPath(), original); - - final PsiFile file = getPsiFile(original); - FileDocumentManagerListener saveListener = new FileDocumentManagerAdapter() { - @Override - public void beforeDocumentSaving(@NotNull Document document) { - WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() { - @Override - public void run() { - try { - file.getFirstChild().delete(); - fail("Must not modify PSI inside save listener"); - } - catch (IncorrectOperationException e) { - assertEquals("Must not modify PSI inside save listener", e.getMessage()); - } - } - }); - } - }; - getProject().getMessageBus().connect(getTestRootDisposable()).subscribe(AppTopics.FILE_DOCUMENT_SYNC, saveListener); - final Document document = PsiDocumentManager.getInstance(getProject()).getDocument(file); - WriteCommandAction.runWriteCommandAction(getProject(), new Runnable() { - @Override - public void run() { - document.insertString(1,"y"); - } - }); - - FileDocumentManager.getInstance().saveAllDocuments(); - } - -}