diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/LanguageServiceAccessorTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/LanguageServiceAccessorTest.java index fa8c3fdba..850a66945 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/LanguageServiceAccessorTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/LanguageServiceAccessorTest.java @@ -21,7 +21,6 @@ import static org.eclipse.lsp4e.LanguageServiceAccessor.hasActiveLanguageServers; import static org.eclipse.lsp4e.test.utils.TestUtils.createFile; import static org.eclipse.lsp4e.test.utils.TestUtils.createProject; -import static org.eclipse.lsp4e.test.utils.TestUtils.createTempFile; import static org.eclipse.lsp4e.test.utils.TestUtils.createUniqueTestFile; import static org.eclipse.lsp4e.test.utils.TestUtils.createUniqueTestFileMultiLS; import static org.eclipse.lsp4e.test.utils.TestUtils.openEditor; @@ -34,6 +33,8 @@ import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.concurrent.CompletableFuture; import java.util.function.Predicate; @@ -58,6 +59,7 @@ import org.eclipse.ui.ide.IDE; import org.eclipse.ui.texteditor.ITextEditor; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; public class LanguageServiceAccessorTest extends AbstractTestWithProject { @@ -368,10 +370,10 @@ public void testLanguageServerEnablementTester() throws Exception { } @Test - public void testLSforExternalThenLocalFile() throws Exception { + public void testLSforExternalThenLocalFile(@TempDir Path tempDir) throws Exception { var wb = UI.getActiveWindow(); - var local = createTempFile("testLSforExternalThenLocalFile", ".lspt"); - var editor = (ITextEditor) IDE.openEditorOnFileStore(wb.getActivePage(), EFS.getStore(local.toURI())); + var local = Files.createFile(tempDir.resolve("testLSforExternalThenLocalFile.lspt")); + var editor = (ITextEditor) IDE.openEditorOnFileStore(wb.getActivePage(), EFS.getStore(local.toUri())); Predicate hasHoverCapabilities = capabilities -> { var hoverProvider = capabilities.getHoverProvider(); diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/color/ColorTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/color/ColorTest.java index f47a30f3a..d4136eef4 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/color/ColorTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/color/ColorTest.java @@ -13,8 +13,8 @@ import static org.eclipse.lsp4e.test.utils.TestUtils.waitForAndAssertCondition; -import java.io.File; -import java.io.FileOutputStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; import org.eclipse.core.filesystem.EFS; @@ -37,6 +37,7 @@ import org.eclipse.ui.ide.IDE; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; public class ColorTest extends AbstractTestWithProject { @@ -56,12 +57,9 @@ public void testColorProvider() throws Exception { } @Test - public void testColorProviderExternalFile() throws Exception { - File file = TestUtils.createTempFile("testColorProviderExternalFile", ".lspt"); - try (var out = new FileOutputStream(file)) { - out.write("\u2588\u2588\u2588\u2588\u2588".getBytes()); - } - ITextViewer viewer = LSPEclipseUtils.getTextViewer(IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toURI()))); + public void testColorProviderExternalFile(@TempDir Path tempDir) throws Exception { + Path file = Files.write(tempDir.resolve("testColorProviderExternalFile.lspt"), "\u2588\u2588\u2588\u2588\u2588".getBytes()); + ITextViewer viewer = LSPEclipseUtils.getTextViewer(IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toUri()))); StyledText widget = viewer.getTextWidget(); waitForAndAssertCondition(3_000, widget.getDisplay(), () -> containsColor(widget, color, 10)); } diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/completion/IncompleteCompletionTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/completion/IncompleteCompletionTest.java index 34afb5d3f..b725afe39 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/completion/IncompleteCompletionTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/completion/IncompleteCompletionTest.java @@ -18,8 +18,9 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.io.File; import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.List; @@ -56,6 +57,7 @@ import org.eclipse.ui.ide.IDE; import org.eclipse.ui.texteditor.ITextEditor; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; public class IncompleteCompletionTest extends AbstractCompletionTest { /* @@ -566,13 +568,13 @@ public void testCompletionWithAdditionalTextEditInsertion() throws Exception { } @Test - public void testCompletionExternalFile() throws Exception { + public void testCompletionExternalFile(@TempDir Path tempDir) throws Exception { final var items = new ArrayList(); items.add(createCompletionItem("FirstClassExternal", CompletionItemKind.Class)); MockLanguageServer.INSTANCE.setCompletionList(new CompletionList(true, items)); - File file = TestUtils.createTempFile("testCompletionExternalFile", ".lspt"); - final var editor = (ITextEditor) IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toURI())); + Path file = Files.createFile(tempDir.resolve("testCompletionExternalFile.lspt")); + final var editor = (ITextEditor) IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toUri())); ITextViewer viewer = LSPEclipseUtils.getTextViewer(editor); ICompletionProposal[] proposals = contentAssistProcessor.computeCompletionProposals(viewer, 0); assertEquals(1, proposals.length); diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/definition/DefinitionTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/definition/DefinitionTest.java index d617126a9..69faea54b 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/definition/DefinitionTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/definition/DefinitionTest.java @@ -14,7 +14,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -41,6 +42,7 @@ import org.eclipse.ui.ide.IDE; import org.eclipse.ui.texteditor.ITextEditor; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; public class DefinitionTest extends AbstractTestWithProject { @@ -82,12 +84,12 @@ public void testDefinitionAndTypeDefinition() throws Exception { } @Test - public void testDefinitionOneLocationExternalFile() throws Exception { + public void testDefinitionOneLocationExternalFile(@TempDir Path tempDir) throws Exception { final var location = new Location("file://test", new Range(new Position(0, 0), new Position(0, 10))); MockLanguageServer.INSTANCE.setDefinition(List.of(location)); - File file = TestUtils.createTempFile("testDocumentLinkExternalFile", ".lspt"); - final var editor = (ITextEditor) IDE.openInternalEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toURI())); + Path file = Files.createFile(tempDir.resolve("testDocumentLinkExternalFile.lspt")); + final var editor = (ITextEditor) IDE.openInternalEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toUri())); ITextViewer viewer = LSPEclipseUtils.getTextViewer(editor); IHyperlink[] hyperlinks = hyperlinkDetector.detectHyperlinks(viewer, new Region(0, 0), true); diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/diagnostics/DiagnosticsTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/diagnostics/DiagnosticsTest.java index f6674f003..01854edde 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/diagnostics/DiagnosticsTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/diagnostics/DiagnosticsTest.java @@ -20,8 +20,8 @@ import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.io.File; -import java.io.FileOutputStream; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -62,6 +62,7 @@ import org.eclipse.ui.ide.IDE; import org.eclipse.ui.texteditor.MarkerUtilities; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; public class DiagnosticsTest extends AbstractTestWithProject { @@ -336,15 +337,12 @@ public void testDiagnosticRedrawingCalls() throws CoreException { } @Test - public void testDiagnosticsOnExternalFile() throws Exception { + public void testDiagnosticsOnExternalFile(@TempDir Path tempDir) throws Exception { MockLanguageServer.INSTANCE.setDiagnostics(List.of(new Diagnostic(new Range(new Position(0, 0), new Position(0, 1)), "This is a warning", DiagnosticSeverity.Warning, null))); - File file = TestUtils.createTempFile("testDiagnosticsOnExternalFile", ".lspt"); + Path file = Files.writeString(tempDir.resolve("testDiagnosticsOnExternalFile.lspt"), "a"); Font font = null; try { - try (var out = new FileOutputStream(file);) { - out.write('a'); - } - ITextViewer viewer = LSPEclipseUtils.getTextViewer(IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toURI()))); + ITextViewer viewer = LSPEclipseUtils.getTextViewer(IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toUri()))); StyledText widget = viewer.getTextWidget(); final var biggerFont = new FontData(); // bigger font to keep color intact in some pixel (not altered by anti-aliasing) biggerFont.setHeight(40); diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/documentLink/DocumentLinkTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/documentLink/DocumentLinkTest.java index be717bda9..6d3f698b9 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/documentLink/DocumentLinkTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/documentLink/DocumentLinkTest.java @@ -14,7 +14,8 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; -import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import org.eclipse.core.filesystem.EFS; @@ -34,6 +35,7 @@ import org.eclipse.ui.ide.IDE; import org.eclipse.ui.texteditor.ITextEditor; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; public class DocumentLinkTest extends AbstractTestWithProject { @@ -63,13 +65,13 @@ public void testDocumentLink() throws Exception { } @Test - public void testDocumentLinkExternalFile() throws Exception { + public void testDocumentLinkExternalFile(@TempDir Path tempDir) throws Exception { final var links = new ArrayList(); links.add(new DocumentLink(new Range(new Position(0, 9), new Position(0, 15)), "file://test0")); MockLanguageServer.INSTANCE.setDocumentLinks(links); - File file = TestUtils.createTempFile("testDocumentLinkExternalFile", ".lspt"); - final var editor = (ITextEditor) IDE.openInternalEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toURI())); + Path file = Files.createFile(tempDir.resolve("testDocumentLinkExternalFile.lspt")); + final var editor = (ITextEditor) IDE.openInternalEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toUri())); ITextViewer viewer = LSPEclipseUtils.getTextViewer(editor); viewer.getDocument().set("Long enough dummy content to match ranges"); diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/DocumentDidChangeTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/DocumentDidChangeTest.java index f1b1bf7c2..87d859d91 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/DocumentDidChangeTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/DocumentDidChangeTest.java @@ -17,7 +17,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; import java.util.function.Predicate; @@ -39,6 +40,7 @@ import org.eclipse.ui.IEditorPart; import org.eclipse.ui.ide.IDE; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; public class DocumentDidChangeTest extends AbstractTestWithProject { @@ -193,12 +195,12 @@ public boolean test(ServerCapabilities t) { } @Test - public void testFullSyncExternalFile() throws Exception { + public void testFullSyncExternalFile(@TempDir Path tempDir) throws Exception { MockLanguageServer.INSTANCE.getInitializeResult().getCapabilities() .setTextDocumentSync(TextDocumentSyncKind.Full); - File file = TestUtils.createTempFile("testFullSyncExternalFile", ".lspt"); - IEditorPart editor = IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toURI())); + Path file = Files.createFile(tempDir.resolve("testFullSyncExternalFile.lspt")); + IEditorPart editor = IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toUri())); ITextViewer viewer = LSPEclipseUtils.getTextViewer(editor); LanguageServers.forDocument(viewer.getDocument()).withFilter(new Predicate() { @Override diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/DocumentDidCloseTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/DocumentDidCloseTest.java index ef9d539cc..c19818103 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/DocumentDidCloseTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/DocumentDidCloseTest.java @@ -14,7 +14,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; @@ -31,6 +32,7 @@ import org.eclipse.ui.IEditorPart; import org.eclipse.ui.ide.IDE; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; public class DocumentDidCloseTest extends AbstractTestWithProject { @@ -54,9 +56,9 @@ public void testClose() throws Exception { } @Test - public void testCloseExternalFile() throws Exception { - File testFile = TestUtils.createTempFile("testCloseExternalFile", ".lspt"); - IEditorPart editor = IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(testFile.toURI())); + public void testCloseExternalFile(@TempDir Path tempDir) throws Exception { + Path testFile = Files.createFile(tempDir.resolve("testCloseExternalFile.lspt")); + IEditorPart editor = IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(testFile.toUri())); // Force LS to initialize and open file LanguageServers.forDocument(LSPEclipseUtils.getDocument(editor.getEditorInput())).anyMatching(); @@ -68,6 +70,6 @@ public void testCloseExternalFile() throws Exception { DidCloseTextDocumentParams lastChange = didCloseExpectation.get(1000, TimeUnit.MILLISECONDS); assertNotNull(lastChange.getTextDocument()); - assertEquals(LSPEclipseUtils.toUri(testFile).toString(), lastChange.getTextDocument().getUri()); + assertEquals(LSPEclipseUtils.toUri(testFile.toFile()).toString(), lastChange.getTextDocument().getUri()); } } diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/DocumentDidOpenTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/DocumentDidOpenTest.java index 404ec281a..86b7e4e78 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/DocumentDidOpenTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/DocumentDidOpenTest.java @@ -14,7 +14,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; @@ -32,6 +33,7 @@ import org.eclipse.ui.ide.IDE; import org.eclipse.ui.texteditor.AbstractTextEditor; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; public class DocumentDidOpenTest extends AbstractTestWithProject { @@ -55,11 +57,11 @@ public void testOpen() throws Exception { } @Test - public void testOpenExternalFile() throws Exception { - File file = TestUtils.createTempFile("testOpenExternalFile", ".lspt"); + public void testOpenExternalFile(@TempDir Path tempDir) throws Exception { + Path file = Files.createFile(tempDir.resolve("testOpenExternalFile.lspt")); final var didOpenExpectation = new CompletableFuture(); MockLanguageServer.INSTANCE.setDidOpenCallback(didOpenExpectation); - IEditorPart editor = IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toURI())); + IEditorPart editor = IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toUri())); // Force LS to initialize and open file LanguageServers.forDocument(LSPEclipseUtils.getDocument(editor.getEditorInput())).anyMatching(); diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/DocumentDidSaveTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/DocumentDidSaveTest.java index 7d695e0ee..eaabb9ad2 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/DocumentDidSaveTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/DocumentDidSaveTest.java @@ -16,7 +16,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import java.io.File; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.concurrent.CompletableFuture; import java.util.concurrent.TimeUnit; @@ -35,6 +36,7 @@ import org.eclipse.ui.IEditorPart; import org.eclipse.ui.ide.IDE; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; public class DocumentDidSaveTest extends AbstractTestWithProject { @@ -67,9 +69,9 @@ public void testSave() throws Exception { } @Test - public void testSaveExternalFile() throws Exception { - File file = TestUtils.createTempFile("testSaveExternalFile", ".lspt"); - IEditorPart editor = IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toURI())); + public void testSaveExternalFile(@TempDir Path tempDir) throws Exception { + Path file = Files.createFile(tempDir.resolve("testSaveExternalFile.lspt")); + IEditorPart editor = IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toUri())); ITextViewer viewer = LSPEclipseUtils.getTextViewer(editor); // make sure that timestamp after save will differ from creation time (no better idea at the moment) @@ -87,7 +89,7 @@ public void testSaveExternalFile() throws Exception { waitForAndAssertCondition(2_000, () -> { DidSaveTextDocumentParams lastChange = didSaveExpectation.get(10, TimeUnit.MILLISECONDS); assertNotNull(lastChange.getTextDocument()); - assertEquals(LSPEclipseUtils.toUri(file).toString(), lastChange.getTextDocument().getUri()); + assertEquals(LSPEclipseUtils.toUri(file.toFile()).toString(), lastChange.getTextDocument().getUri()); return true; }); } diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/LSPEclipseUtilsTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/LSPEclipseUtilsTest.java index 51c70f724..ef9393e54 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/LSPEclipseUtilsTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/LSPEclipseUtilsTest.java @@ -23,7 +23,6 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URI; @@ -75,15 +74,16 @@ import org.junit.jupiter.api.condition.EnabledOnOs; import org.junit.jupiter.api.condition.OS; import org.junit.jupiter.api.extension.RegisterExtension; +import org.junit.jupiter.api.io.TempDir; public class LSPEclipseUtilsTest extends AbstractTestWithProject { public final @RegisterExtension NoErrorLoggedRule noErrorLoggedRule = new NoErrorLoggedRule(); @Test - public void testOpenInEditorExternalFile() throws Exception { - File externalFile = TestUtils.createTempFile("externalFile", ".txt"); - final var location = new Location(LSPEclipseUtils.toUri(externalFile).toString(), new Range(new Position(0, 0), new Position(0, 0))); + public void testOpenInEditorExternalFile(@TempDir Path tempDir) throws Exception { + Path externalFile = Files.createFile(tempDir.resolve("externalFile.txt")); + final var location = new Location(LSPEclipseUtils.toUri(externalFile.toFile()).toString(), new Range(new Position(0, 0), new Position(0, 0))); LSPEclipseUtils.openInEditor(location, UI.getActivePage()); } @@ -144,7 +144,7 @@ public void testWorkspaceEdit_CreateAndPopulateFile() throws Exception { // they should be applied from bottom to top LSPEclipseUtils.applyWorkspaceEdit(workspaceEdit); assertTrue(file.exists()); - assertEquals("abcHere\nabcHere2", new String(Files.readAllBytes(file.getLocation().toFile().toPath()))); + assertEquals("abcHere\nabcHere2", Files.readString(file.getLocation().toPath())); } @Test @@ -370,41 +370,38 @@ public void testResourceOperations() throws Exception { } @Test - public void createExternalFile() throws Exception { - File file = TestUtils.createTempFile(getClass() + "editExternalFile", ".whatever"); - file.delete(); - assertFalse(file.exists()); + public void createExternalFile(@TempDir Path tempDir) throws Exception { + Path file = tempDir.resolve("createExternalFile.whatever"); final var we = new WorkspaceEdit( - List.of(Either.forRight(new CreateFile(file.toURI().toString())))); + List.of(Either.forRight(new CreateFile(file.toUri().toString())))); LSPEclipseUtils.applyWorkspaceEdit(we); - assertTrue(file.isFile()); + assertTrue(Files.exists(file)); } @Test - public void editExternalFile() throws Exception { - File file = TestUtils.createTempFile(getClass() + "editExternalFile", ".whatever"); + public void editExternalFile(@TempDir Path tempDir) throws Exception { + Path file = Files.createFile(tempDir.resolve("editExternalFile.whatever")); final var te = new TextEdit(); te.setRange(new Range(new Position(0, 0), new Position(0, 0))); te.setNewText("abc\ndef"); final var docEdit = new TextDocumentEdit( - new VersionedTextDocumentIdentifier(file.toURI().toString(), null), + new VersionedTextDocumentIdentifier(file.toUri().toString(), null), List.of(te)); final var we = new WorkspaceEdit(List.of(Either.forLeft(docEdit))); LSPEclipseUtils.applyWorkspaceEdit(we); - assertTrue(file.isFile()); - assertEquals("abc\ndef", new String(Files.readAllBytes(file.toPath()))); + assertTrue(Files.isRegularFile(file)); + assertEquals("abc\ndef", Files.readString(file)); } @Test - public void renameExternalFile() throws Exception { - File oldFile = TestUtils.createTempFile(getClass() + "editExternalFile", ".whatever"); - File newFile = new File(oldFile.getAbsolutePath() + "_renamed"); - TestUtils.addManagedTempFile(newFile); + public void renameExternalFile(@TempDir Path tempDir) throws Exception { + Path oldFile = Files.createFile(tempDir.resolve("editExternalFile.whatever")); + Path newFile = tempDir.resolve("editExternalFile_renamed.whatever"); final var we = new WorkspaceEdit(List.of( - Either.forRight(new RenameFile(oldFile.toURI().toString(), newFile.toURI().toString())))); + Either.forRight(new RenameFile(oldFile.toUri().toString(), newFile.toUri().toString())))); LSPEclipseUtils.applyWorkspaceEdit(we); - assertFalse(oldFile.isFile()); - assertTrue(newFile.isFile()); + assertFalse(Files.isRegularFile(oldFile)); + assertTrue(Files.isRegularFile(newFile)); } private String readContent(IFile targetFile) throws IOException, CoreException { @@ -436,14 +433,14 @@ public void testTextEditDoesntAutomaticallySaveOpenResourceFiles() throws Except } @Test - public void testTextEditDoesntAutomaticallySaveOpenExternalFiles() throws Exception { - File file = TestUtils.createTempFile("testTextEditDoesntAutomaticallySaveOpenExternalFiles", ".whatever"); - IEditorPart editor = IDE.openInternalEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toURI())); + public void testTextEditDoesntAutomaticallySaveOpenExternalFiles(@TempDir Path tempDir ) throws Exception { + Path file = Files.createFile(tempDir.resolve("testTextEditDoesntAutomaticallySaveOpenExternalFiles.whatever")); + IEditorPart editor = IDE.openInternalEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toUri())); final var te = new TextEdit(); te.setRange(new Range(new Position(0, 0), new Position(0, 0))); te.setNewText("abc\ndef"); final var docEdit = new TextDocumentEdit( - new VersionedTextDocumentIdentifier(file.toURI().toString(), null), + new VersionedTextDocumentIdentifier(file.toUri().toString(), null), List.of(te)); final var we = new WorkspaceEdit(List.of(Either.forLeft(docEdit))); LSPEclipseUtils.applyWorkspaceEdit(we); @@ -465,7 +462,7 @@ private IPath generateNonExistingIPath(String directory, final String fileExtens } @Test - public void testGetFile() throws Exception { + public void testGetFile(@TempDir Path tempDir) throws Exception { IPath path; /* @@ -495,18 +492,16 @@ public void testGetFile() throws Exception { /* * test absolute path to existing files outside of current workspace */ - path = org.eclipse.core.runtime.Path.fromOSString(TestUtils.createTempFile("testGetFile", ".txt").getAbsolutePath()); + + path = IPath.fromPath(Files.createFile(tempDir.resolve("testGetFile.txt"))); assertNull(LSPEclipseUtils.getFile(path)); } @Test - public void testGetOpenEditorExternalFile() throws Exception { - File file = TestUtils.createTempFile("testDiagnosticsOnExternalFile", ".lspt"); - try (var out = new FileOutputStream(file)) { - out.write('a'); - } - IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toURI())); - assertNotEquals(Collections.emptySet(), LSPEclipseUtils.findOpenEditorsFor(file.toURI())); + public void testGetOpenEditorExternalFile(@TempDir Path tempDir) throws Exception { + Path file = Files.writeString(tempDir.resolve("testGetOpenEditorExternalFile.lspt"), "a"); + IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toUri())); + assertNotEquals(Collections.emptySet(), LSPEclipseUtils.findOpenEditorsFor(file.toUri())); } @Test diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/hover/HoverTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/hover/HoverTest.java index 5fb0dd66f..deb3fd2a8 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/hover/HoverTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/hover/HoverTest.java @@ -17,8 +17,9 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.io.File; import java.lang.reflect.Field; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Collections; import java.util.List; import java.util.concurrent.TimeUnit; @@ -52,6 +53,7 @@ import org.eclipse.ui.ide.IDE; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; @SuppressWarnings("restriction") public class HoverTest extends AbstractTestWithProject { @@ -136,14 +138,14 @@ public void testHoverEmptyContentItem() throws CoreException { } @Test - public void testHoverOnExternalFile() throws Exception { + public void testHoverOnExternalFile(@TempDir Path tempDir) throws Exception { final var hoverResponse = new Hover(List.of(Either.forLeft("blah")), new Range(new Position(0, 0), new Position(0, 0))); MockLanguageServer.INSTANCE.setHover(hoverResponse); - File file = TestUtils.createTempFile("testHoverOnExternalfile", ".lspt"); + Path file = Files.createFile(tempDir.resolve("testHoverOnExternalfile.lspt")); ITextViewer viewer = LSPEclipseUtils - .getTextViewer(IDE.openInternalEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toURI()))); + .getTextViewer(IDE.openInternalEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toUri()))); String html = hover.getHoverInfoFuture(viewer, new Region(0, 0)).get(2, TimeUnit.SECONDS); assertTrue(html != null && html.contains("blah")); } diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/outline/OutlineContentTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/outline/OutlineContentTest.java index 4cff2fe4b..a03e5b9ac 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/outline/OutlineContentTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/outline/OutlineContentTest.java @@ -12,8 +12,9 @@ import static org.eclipse.lsp4e.test.utils.TestUtils.waitForCondition; import static org.junit.jupiter.api.Assertions.assertFalse; -import java.io.FileWriter; import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.Arrays; import java.util.List; @@ -45,16 +46,13 @@ import org.eclipse.ui.texteditor.ITextEditor; import org.eclipse.ui.views.contentoutline.IContentOutlinePage; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; public class OutlineContentTest extends AbstractTestWithProject { @Test - public void testExternalFile() throws CoreException, IOException { - var testFile = TestUtils.createTempFile("test" + System.currentTimeMillis(), ".lspt"); - - try (FileWriter fileWriter = new FileWriter(testFile)) { - fileWriter.write("content\n does\n not\n matter\n but needs to cover the ranges described below"); - } + public void testExternalFile(@TempDir Path tempDir) throws CoreException, IOException { + var testFile = Files.writeString(tempDir.resolve("test.lspt"), "content\n does\n not\n matter\n but needs to cover the ranges described below"); final var symbolCow = new DocumentSymbol("cow", SymbolKind.Constant, new Range(new Position(0, 0), new Position(0, 2)), @@ -62,7 +60,7 @@ public void testExternalFile() throws CoreException, IOException { MockLanguageServer.INSTANCE.setDocumentSymbols(symbolCow); - final var editor = (ITextEditor) TestUtils.openExternalFileInEditor(testFile); + final var editor = (ITextEditor) TestUtils.openExternalFileInEditor(testFile.toFile()); final var outlinePage = (CNFOutlinePage) new EditorToOutlineAdapterFactory().getAdapter(editor, IContentOutlinePage.class); final var shell = new Shell(editor.getEditorSite().getWorkbenchWindow().getShell()); @@ -83,12 +81,8 @@ public void testExternalFile() throws CoreException, IOException { } @Test - public void testExternalFileOpenedOnFileStore() throws CoreException, IOException { - var testFile = TestUtils.createTempFile("test" + System.currentTimeMillis(), ".lspt"); - - try (FileWriter fileWriter = new FileWriter(testFile)) { - fileWriter.write("content\n does\n not\n matter\n but needs to cover the ranges described below"); - } + public void testExternalFileOpenedOnFileStore(@TempDir Path tempDir) throws CoreException, IOException { + var testFile = Files.writeString(tempDir.resolve("test.lspt"), "content\n does\n not\n matter\n but needs to cover the ranges described below"); final var symbolCow = new DocumentSymbol("cow", SymbolKind.Constant, new Range(new Position(0, 0), new Position(0, 2)), @@ -96,7 +90,7 @@ public void testExternalFileOpenedOnFileStore() throws CoreException, IOExceptio MockLanguageServer.INSTANCE.setDocumentSymbols(symbolCow); - final var editor = (ITextEditor) TestUtils.openExternalFileOnFileStore(testFile); + final var editor = (ITextEditor) TestUtils.openExternalFileOnFileStore(testFile.toFile()); final var outlinePage = (CNFOutlinePage) new EditorToOutlineAdapterFactory().getAdapter(editor, IContentOutlinePage.class); final var shell = new Shell(editor.getEditorSite().getWorkbenchWindow().getShell()); diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/outline/OutlineViewerInputTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/outline/OutlineViewerInputTest.java index ef9660d36..964a36f03 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/outline/OutlineViewerInputTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/outline/OutlineViewerInputTest.java @@ -14,9 +14,10 @@ import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.io.FileWriter; import java.io.IOException; import java.net.URI; +import java.nio.file.Files; +import java.nio.file.Path; import org.eclipse.core.runtime.CoreException; import org.eclipse.lsp4e.LSPEclipseUtils; @@ -26,6 +27,7 @@ import org.eclipse.lsp4e.test.utils.TestUtils; import org.eclipse.ui.texteditor.ITextEditor; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; /** * Tests to verify that documentURI always contains absolute paths in the file system. @@ -55,15 +57,12 @@ public void testDocumentURIAbsolutePathForWorkspaceFile() throws CoreException, } @Test - public void testDocumentURIAbsolutePathForExternalFile() throws IOException, CoreException { + public void testDocumentURIAbsolutePathForExternalFile(@TempDir Path tempDir) throws IOException, CoreException { // Create a temporary file outside the workspace - var tempFile = TestUtils.createTempFile("externalTest" + System.currentTimeMillis(), ".lspt"); - - try (var fileWriter = new FileWriter(tempFile)) { - fileWriter.write("external file content for testing absolute paths"); - } + var tempFile = Files.writeString(tempDir.resolve("externalTest.lspt"), "external file content for testing absolute paths"); + // Open the external file in an editor - var editor = (ITextEditor) TestUtils.openExternalFileInEditor(tempFile); + var editor = (ITextEditor) TestUtils.openExternalFileInEditor(tempFile.toFile()); var document = LSPEclipseUtils.getDocument(editor); // Create OutlineViewerInput and verify documentURI @@ -78,7 +77,7 @@ public void testDocumentURIAbsolutePathForExternalFile() throws IOException, Cor // Verify it points to the same file we created // replace '\' with '/' on Windows and remove leading '/' from documentURI path on Windows: - assertTrue(documentURI.toString().contains(tempFile.getAbsolutePath().replace("\\","/")), + assertTrue(documentURI.toString().contains(tempFile.toFile().getAbsolutePath().replace("\\","/")), "documentURI should contain the abolute path in the file system"); } diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/rename/LSPTextChangeTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/rename/LSPTextChangeTest.java index 843e7a0d5..6ae995e86 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/rename/LSPTextChangeTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/rename/LSPTextChangeTest.java @@ -14,8 +14,8 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import java.io.File; import java.nio.file.Files; +import java.nio.file.Path; import org.eclipse.core.resources.IFile; import org.eclipse.core.runtime.NullProgressMonitor; @@ -29,6 +29,7 @@ import org.eclipse.lsp4j.TextEdit; import org.eclipse.ltk.core.refactoring.PerformChangeOperation; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; public class LSPTextChangeTest extends AbstractTestWithProject { @@ -53,12 +54,11 @@ public void testRefactoringPreview() throws Exception { } @Test - public void testPerformOperationExternalFile() throws Exception { - File file = TestUtils.createTempFile("testPerformOperationExternalFile", ".lspt"); - Files.write(file.toPath(), "old".getBytes()); + public void testPerformOperationExternalFile(@TempDir Path tempDir) throws Exception { + Path file = Files.writeString(tempDir.resolve("testPerformOperationExternalFile.lspt"), "old"); final var edit = new TextEdit(new Range(new Position(0, 0), new Position(0, 3)), "new"); - final var operation = new PerformChangeOperation(new LSPTextChange("test", LSPEclipseUtils.toUri(file), edit)); + final var operation = new PerformChangeOperation(new LSPTextChange("test", LSPEclipseUtils.toUri(file.toFile()), edit)); operation.run(new NullProgressMonitor()); - assertEquals(edit.getNewText(), new String(Files.readAllBytes(file.toPath()))); + assertEquals(edit.getNewText(), Files.readString(file)); } } diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/rename/RenameTest.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/rename/RenameTest.java index 2a5c3ef41..50bbff04c 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/rename/RenameTest.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/rename/RenameTest.java @@ -23,6 +23,7 @@ import java.lang.reflect.Method; import java.net.URI; import java.nio.file.Files; +import java.nio.file.Path; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -66,6 +67,7 @@ import org.eclipse.ui.handlers.IHandlerService; import org.eclipse.ui.texteditor.ITextEditor; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; public class RenameTest extends AbstractTestWithProject { @@ -163,10 +165,11 @@ public void testPrepareRenameRefactoringError() throws Exception { } @Test - public void testRenameRefactoringExternalFile() throws Exception { - File file = TestUtils.createTempFile("testPerformOperationExternalFile", ".lspt"); - MockLanguageServer.INSTANCE.getTextDocumentService().setRenameEdit(createSimpleMockRenameEdit(file.toURI())); - IFileStore store = EFS.getStore(file.toURI()); + public void testRenameRefactoringExternalFile(@TempDir Path tempDir) throws Exception { + Path file = Files.createFile(tempDir.resolve("testPerformOperationExternalFile.lspt")); + + MockLanguageServer.INSTANCE.getTextDocumentService().setRenameEdit(createSimpleMockRenameEdit(file.toUri())); + IFileStore store = EFS.getStore(file.toUri()); ITextFileBufferManager manager = ITextFileBufferManager.DEFAULT; try { manager.connectFileStore(store, new NullProgressMonitor()); @@ -188,13 +191,13 @@ public void testRenameRefactoringExternalFile() throws Exception { } @Test - public void testRenameChangeAlsoExternalFile() throws Exception { + public void testRenameChangeAlsoExternalFile(@TempDir Path tempDir) throws Exception { IFile workspaceFile = TestUtils.createUniqueTestFile(project, "old"); - File externalFile = TestUtils.createTempFile("testRenameChangeAlsoExternalFile", ".lspt"); - Files.write(externalFile.toPath(), "old".getBytes()); + + Path externalFile = Files.writeString(tempDir.resolve("testRenameChangeAlsoExternalFile.lspt"), "old"); final var edits = new HashMap>(2, 1.f); edits.put(LSPEclipseUtils.toUri(workspaceFile).toString(), List.of(new TextEdit(new Range(new Position(0, 0), new Position(0, 3)), "new"))); - edits.put(LSPEclipseUtils.toUri(externalFile).toString(), List.of(new TextEdit(new Range(new Position(0, 0), new Position(0, 3)), "new"))); + edits.put(LSPEclipseUtils.toUri(externalFile.toFile()).toString(), List.of(new TextEdit(new Range(new Position(0, 0), new Position(0, 3)), "new"))); MockLanguageServer.INSTANCE.getTextDocumentService().setRenameEdit(new WorkspaceEdit(edits)); IDocument document = LSPEclipseUtils.getDocument(workspaceFile); assertNotNull(document); @@ -208,7 +211,7 @@ public void testRenameChangeAlsoExternalFile() throws Exception { e.printStackTrace(); } assertEquals("new", document.get()); - assertEquals("new", new String(Files.readAllBytes(externalFile.toPath()))); + assertEquals("new", Files.readString(externalFile)); } @Test diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/utils/AllCleanExtension.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/utils/AllCleanExtension.java index 85e10b5d0..07ce6cdc0 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/utils/AllCleanExtension.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/utils/AllCleanExtension.java @@ -71,7 +71,6 @@ private void clear() { LanguageServiceAccessor.clearStartedServers(); MockLanguageServer.reset(this.serverConfigurer); MockConnectionProvider.cancellations.clear(); - TestUtils.tearDown(); } } diff --git a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/utils/TestUtils.java b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/utils/TestUtils.java index 05abfb9c3..50d16e17e 100644 --- a/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/utils/TestUtils.java +++ b/org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/utils/TestUtils.java @@ -19,7 +19,6 @@ import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; -import java.util.HashSet; import java.util.List; import java.util.Set; import java.util.concurrent.CountDownLatch; @@ -67,8 +66,6 @@ public interface Condition { boolean isMet() throws Exception; } - private static final Set tempFiles = new HashSet<>(); - private TestUtils() { // this class shouldn't be instantiated } @@ -249,29 +246,6 @@ public static void delete(Path... paths) throws IOException { ArrayUtil.forEach(paths, TestUtils::delete); } - public static File createTempFile(String prefix, String suffix) throws IOException { - File tmp = File.createTempFile(prefix, suffix); - tempFiles.add(tmp); - return tmp; - } - - public static void addManagedTempFile(File file) { - tempFiles.add(file); - } - - public static void tearDown() { - tempFiles.forEach(file -> { - try { - Files.deleteIfExists(file.toPath()); - } catch (IOException e) { - // Trying to have the tests run quieter but I suppose if there's an actual - // problem we'd better find out about it - e.printStackTrace(); - } - }); - tempFiles.clear(); - } - public static ContentTypeToLanguageServerDefinition getDisabledLS() { return LanguageServersRegistry.getInstance().getContentTypeToLSPExtensions().stream() .filter(definition -> "org.eclipse.lsp4e.test.server.disable".equals(definition.getValue().id)