Search in sources :

Example 6 with MockVirtualFile

use of com.intellij.mock.MockVirtualFile in project intellij-community by JetBrains.

the class FileDocumentManagerImplTest method testContentChanged_ignoreEventsFromSelfOnSave.

public void testContentChanged_ignoreEventsFromSelfOnSave() throws Exception {
    final VirtualFile file = new MockVirtualFile("test.txt", "test\rtest") {

        @NotNull
        @Override
        public OutputStream getOutputStream(final Object requestor, final long newModificationStamp, long newTimeStamp) throws IOException {
            final VirtualFile self = this;
            return new ByteArrayOutputStream() {

                @Override
                public void close() throws IOException {
                    super.close();
                    long oldStamp = getModificationStamp();
                    setModificationStamp(newModificationStamp);
                    setText(toString());
                    myDocumentManager.contentsChanged(new VirtualFileEvent(requestor, self, null, oldStamp, getModificationStamp()));
                }
            };
        }
    };
    final Document document = myDocumentManager.getDocument(file);
    assertNotNull(file.toString(), document);
    WriteCommandAction.runWriteCommandAction(myProject, () -> document.insertString(0, "xxx"));
    final long stamp = document.getModificationStamp();
    myDocumentManager.saveAllDocuments();
    assertEquals(stamp, document.getModificationStamp());
}
Also used : LightVirtualFile(com.intellij.testFramework.LightVirtualFile) MockVirtualFile(com.intellij.mock.MockVirtualFile) MockVirtualFile(com.intellij.mock.MockVirtualFile) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Document(com.intellij.openapi.editor.Document)

Example 7 with MockVirtualFile

use of com.intellij.mock.MockVirtualFile in project intellij-community by JetBrains.

the class FileDocumentManagerImplTest method testGetUnsavedDocuments_afterSaveDocumentWithProblems.

public void testGetUnsavedDocuments_afterSaveDocumentWithProblems() throws Exception {
    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(), () -> document.insertString(0, "xxx"));
        try {
            myDocumentManager.saveDocument(document);
            fail("must throw IOException");
        } catch (RuntimeException e) {
            assertTrue(e.getCause() instanceof IOException);
        }
        final Document[] unsavedDocuments = myDocumentManager.getUnsavedDocuments();
        assertEquals(1, unsavedDocuments.length);
        assertSame(document, unsavedDocuments[0]);
        assertEquals("test", new String(file.contentsToByteArray(), CharsetToolkit.UTF8_CHARSET));
    } finally {
        ApplicationManager.getApplication().runWriteAction(() -> myDocumentManager.dropAllUnsavedDocuments());
    }
}
Also used : LightVirtualFile(com.intellij.testFramework.LightVirtualFile) MockVirtualFile(com.intellij.mock.MockVirtualFile) MockVirtualFile(com.intellij.mock.MockVirtualFile) IOException(java.io.IOException) Document(com.intellij.openapi.editor.Document)

Example 8 with MockVirtualFile

use of com.intellij.mock.MockVirtualFile in project intellij-community by JetBrains.

the class FileDocumentManagerImplTest method testRememberSeparators.

public void testRememberSeparators() throws Exception {
    final VirtualFile file = new MockVirtualFile("test.txt", "test\rtest");
    final Document document = myDocumentManager.getDocument(file);
    assertNotNull(file.toString(), document);
    WriteCommandAction.runWriteCommandAction(myProject, () -> document.insertString(0, "xxx "));
    myDocumentManager.saveAllDocuments();
    assertEquals("xxx test\rtest", new String(file.contentsToByteArray(), CharsetToolkit.UTF8_CHARSET));
}
Also used : LightVirtualFile(com.intellij.testFramework.LightVirtualFile) MockVirtualFile(com.intellij.mock.MockVirtualFile) MockVirtualFile(com.intellij.mock.MockVirtualFile) Document(com.intellij.openapi.editor.Document)

Example 9 with MockVirtualFile

use of com.intellij.mock.MockVirtualFile in project intellij-community by JetBrains.

the class VirtualFilePointerTest method testAlienVirtualFileSystemPointerRemovedFromUrlToIdentityCacheOnDispose.

public void testAlienVirtualFileSystemPointerRemovedFromUrlToIdentityCacheOnDispose() throws IOException {
    VirtualFile mockVirtualFile = new MockVirtualFile("test_name", "test_text");
    Disposable disposable1 = Disposer.newDisposable();
    final VirtualFilePointer pointer = VirtualFilePointerManager.getInstance().create(mockVirtualFile, disposable1, null);
    assertInstanceOf(pointer, IdentityVirtualFilePointer.class);
    assertTrue(pointer.isValid());
    VirtualFile virtualFileWithSameUrl = new MockVirtualFile("test_name", "test_text");
    VirtualFilePointer updatedPointer = VirtualFilePointerManager.getInstance().create(virtualFileWithSameUrl, disposable1, null);
    assertInstanceOf(updatedPointer, IdentityVirtualFilePointer.class);
    assertEquals(1, ((VirtualFilePointerManagerImpl) VirtualFilePointerManager.getInstance()).numberOfCachedUrlToIdentity());
    Disposer.dispose(disposable1);
    assertEquals(0, ((VirtualFilePointerManagerImpl) VirtualFilePointerManager.getInstance()).numberOfCachedUrlToIdentity());
}
Also used : MockVirtualFile(com.intellij.mock.MockVirtualFile) Disposable(com.intellij.openapi.Disposable) MockVirtualFile(com.intellij.mock.MockVirtualFile) VirtualFilePointer(com.intellij.openapi.vfs.pointers.VirtualFilePointer)

Aggregations

MockVirtualFile (com.intellij.mock.MockVirtualFile)9 Document (com.intellij.openapi.editor.Document)7 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)4 MockVirtualLink (com.intellij.mock.MockVirtualLink)1 Disposable (com.intellij.openapi.Disposable)1 VirtualFilePointer (com.intellij.openapi.vfs.pointers.VirtualFilePointer)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1