Skip to content

Commit 5f840f4

Browse files
committed
fix(tests): Use JUnit @tempdir to create temporary files
1 parent 3ff7ca7 commit 5f840f4

18 files changed

Lines changed: 134 additions & 156 deletions

org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/LanguageServiceAccessorTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import static org.eclipse.lsp4e.LanguageServiceAccessor.hasActiveLanguageServers;
2222
import static org.eclipse.lsp4e.test.utils.TestUtils.createFile;
2323
import static org.eclipse.lsp4e.test.utils.TestUtils.createProject;
24-
import static org.eclipse.lsp4e.test.utils.TestUtils.createTempFile;
2524
import static org.eclipse.lsp4e.test.utils.TestUtils.createUniqueTestFile;
2625
import static org.eclipse.lsp4e.test.utils.TestUtils.createUniqueTestFileMultiLS;
2726
import static org.eclipse.lsp4e.test.utils.TestUtils.openEditor;
@@ -34,6 +33,8 @@
3433
import static org.junit.jupiter.api.Assertions.assertSame;
3534
import static org.junit.jupiter.api.Assertions.assertTrue;
3635

36+
import java.nio.file.Files;
37+
import java.nio.file.Path;
3738
import java.util.ArrayList;
3839
import java.util.concurrent.CompletableFuture;
3940
import java.util.function.Predicate;
@@ -58,6 +59,7 @@
5859
import org.eclipse.ui.ide.IDE;
5960
import org.eclipse.ui.texteditor.ITextEditor;
6061
import org.junit.jupiter.api.Test;
62+
import org.junit.jupiter.api.io.TempDir;
6163

6264
public class LanguageServiceAccessorTest extends AbstractTestWithProject {
6365

@@ -368,10 +370,10 @@ public void testLanguageServerEnablementTester() throws Exception {
368370
}
369371

370372
@Test
371-
public void testLSforExternalThenLocalFile() throws Exception {
373+
public void testLSforExternalThenLocalFile(@TempDir Path tempDir) throws Exception {
372374
var wb = UI.getActiveWindow();
373-
var local = createTempFile("testLSforExternalThenLocalFile", ".lspt");
374-
var editor = (ITextEditor) IDE.openEditorOnFileStore(wb.getActivePage(), EFS.getStore(local.toURI()));
375+
var local = Files.createFile(tempDir.resolve("testLSforExternalThenLocalFile.lspt"));
376+
var editor = (ITextEditor) IDE.openEditorOnFileStore(wb.getActivePage(), EFS.getStore(local.toUri()));
375377

376378
Predicate<ServerCapabilities> hasHoverCapabilities = capabilities -> {
377379
var hoverProvider = capabilities.getHoverProvider();

org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/color/ColorTest.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
import static org.eclipse.lsp4e.test.utils.TestUtils.waitForAndAssertCondition;
1515

16-
import java.io.File;
17-
import java.io.FileOutputStream;
16+
import java.nio.file.Files;
17+
import java.nio.file.Path;
1818
import java.util.List;
1919

2020
import org.eclipse.core.filesystem.EFS;
@@ -37,6 +37,7 @@
3737
import org.eclipse.ui.ide.IDE;
3838
import org.junit.jupiter.api.BeforeEach;
3939
import org.junit.jupiter.api.Test;
40+
import org.junit.jupiter.api.io.TempDir;
4041

4142
public class ColorTest extends AbstractTestWithProject {
4243

@@ -56,12 +57,9 @@ public void testColorProvider() throws Exception {
5657
}
5758

5859
@Test
59-
public void testColorProviderExternalFile() throws Exception {
60-
File file = TestUtils.createTempFile("testColorProviderExternalFile", ".lspt");
61-
try (var out = new FileOutputStream(file)) {
62-
out.write("\u2588\u2588\u2588\u2588\u2588".getBytes());
63-
}
64-
ITextViewer viewer = LSPEclipseUtils.getTextViewer(IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toURI())));
60+
public void testColorProviderExternalFile(@TempDir Path tempDir) throws Exception {
61+
Path file = Files.write(tempDir.resolve("testColorProviderExternalFile.lspt"), "\u2588\u2588\u2588\u2588\u2588".getBytes());
62+
ITextViewer viewer = LSPEclipseUtils.getTextViewer(IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toUri())));
6563
StyledText widget = viewer.getTextWidget();
6664
waitForAndAssertCondition(3_000, widget.getDisplay(), () -> containsColor(widget, color, 10));
6765
}

org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/completion/IncompleteCompletionTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818
import static org.junit.jupiter.api.Assertions.assertNotNull;
1919
import static org.junit.jupiter.api.Assertions.assertTrue;
2020

21-
import java.io.File;
2221
import java.net.URI;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
2324
import java.util.ArrayList;
2425
import java.util.List;
2526

@@ -56,6 +57,7 @@
5657
import org.eclipse.ui.ide.IDE;
5758
import org.eclipse.ui.texteditor.ITextEditor;
5859
import org.junit.jupiter.api.Test;
60+
import org.junit.jupiter.api.io.TempDir;
5961

6062
public class IncompleteCompletionTest extends AbstractCompletionTest {
6163
/*
@@ -566,13 +568,13 @@ public void testCompletionWithAdditionalTextEditInsertion() throws Exception {
566568
}
567569

568570
@Test
569-
public void testCompletionExternalFile() throws Exception {
571+
public void testCompletionExternalFile(@TempDir Path tempDir) throws Exception {
570572
final var items = new ArrayList<CompletionItem>();
571573
items.add(createCompletionItem("FirstClassExternal", CompletionItemKind.Class));
572574
MockLanguageServer.INSTANCE.setCompletionList(new CompletionList(true, items));
573575

574-
File file = TestUtils.createTempFile("testCompletionExternalFile", ".lspt");
575-
final var editor = (ITextEditor) IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toURI()));
576+
Path file = Files.createFile(tempDir.resolve("testCompletionExternalFile.lspt"));
577+
final var editor = (ITextEditor) IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toUri()));
576578
ITextViewer viewer = LSPEclipseUtils.getTextViewer(editor);
577579
ICompletionProposal[] proposals = contentAssistProcessor.computeCompletionProposals(viewer, 0);
578580
assertEquals(1, proposals.length);

org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/definition/DefinitionTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
import static org.junit.jupiter.api.Assertions.assertEquals;
1515
import static org.junit.jupiter.api.Assertions.assertTrue;
1616

17-
import java.io.File;
17+
import java.nio.file.Files;
18+
import java.nio.file.Path;
1819
import java.util.ArrayList;
1920
import java.util.Arrays;
2021
import java.util.Collections;
@@ -41,6 +42,7 @@
4142
import org.eclipse.ui.ide.IDE;
4243
import org.eclipse.ui.texteditor.ITextEditor;
4344
import org.junit.jupiter.api.Test;
45+
import org.junit.jupiter.api.io.TempDir;
4446

4547
public class DefinitionTest extends AbstractTestWithProject {
4648

@@ -82,12 +84,12 @@ public void testDefinitionAndTypeDefinition() throws Exception {
8284
}
8385

8486
@Test
85-
public void testDefinitionOneLocationExternalFile() throws Exception {
87+
public void testDefinitionOneLocationExternalFile(@TempDir Path tempDir) throws Exception {
8688
final var location = new Location("file://test", new Range(new Position(0, 0), new Position(0, 10)));
8789
MockLanguageServer.INSTANCE.setDefinition(List.of(location));
8890

89-
File file = TestUtils.createTempFile("testDocumentLinkExternalFile", ".lspt");
90-
final var editor = (ITextEditor) IDE.openInternalEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toURI()));
91+
Path file = Files.createFile(tempDir.resolve("testDocumentLinkExternalFile.lspt"));
92+
final var editor = (ITextEditor) IDE.openInternalEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toUri()));
9193
ITextViewer viewer = LSPEclipseUtils.getTextViewer(editor);
9294

9395
IHyperlink[] hyperlinks = hyperlinkDetector.detectHyperlinks(viewer, new Region(0, 0), true);

org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/diagnostics/DiagnosticsTest.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import static org.junit.jupiter.api.Assertions.assertNull;
2121
import static org.junit.jupiter.api.Assertions.assertTrue;
2222

23-
import java.io.File;
24-
import java.io.FileOutputStream;
23+
import java.nio.file.Files;
24+
import java.nio.file.Path;
2525
import java.util.ArrayList;
2626
import java.util.Collections;
2727
import java.util.List;
@@ -62,6 +62,7 @@
6262
import org.eclipse.ui.ide.IDE;
6363
import org.eclipse.ui.texteditor.MarkerUtilities;
6464
import org.junit.jupiter.api.Test;
65+
import org.junit.jupiter.api.io.TempDir;
6566

6667
public class DiagnosticsTest extends AbstractTestWithProject {
6768

@@ -336,15 +337,12 @@ public void testDiagnosticRedrawingCalls() throws CoreException {
336337
}
337338

338339
@Test
339-
public void testDiagnosticsOnExternalFile() throws Exception {
340+
public void testDiagnosticsOnExternalFile(@TempDir Path tempDir) throws Exception {
340341
MockLanguageServer.INSTANCE.setDiagnostics(List.of(new Diagnostic(new Range(new Position(0, 0), new Position(0, 1)), "This is a warning", DiagnosticSeverity.Warning, null)));
341-
File file = TestUtils.createTempFile("testDiagnosticsOnExternalFile", ".lspt");
342+
Path file = Files.writeString(tempDir.resolve("testDiagnosticsOnExternalFile.lspt"), "a");
342343
Font font = null;
343344
try {
344-
try (var out = new FileOutputStream(file);) {
345-
out.write('a');
346-
}
347-
ITextViewer viewer = LSPEclipseUtils.getTextViewer(IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toURI())));
345+
ITextViewer viewer = LSPEclipseUtils.getTextViewer(IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toUri())));
348346
StyledText widget = viewer.getTextWidget();
349347
final var biggerFont = new FontData(); // bigger font to keep color intact in some pixel (not altered by anti-aliasing)
350348
biggerFont.setHeight(40);

org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/documentLink/DocumentLinkTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
1515
import static org.junit.jupiter.api.Assertions.assertEquals;
1616

17-
import java.io.File;
17+
import java.nio.file.Files;
18+
import java.nio.file.Path;
1819
import java.util.ArrayList;
1920

2021
import org.eclipse.core.filesystem.EFS;
@@ -34,6 +35,7 @@
3435
import org.eclipse.ui.ide.IDE;
3536
import org.eclipse.ui.texteditor.ITextEditor;
3637
import org.junit.jupiter.api.Test;
38+
import org.junit.jupiter.api.io.TempDir;
3739

3840
public class DocumentLinkTest extends AbstractTestWithProject {
3941

@@ -63,13 +65,13 @@ public void testDocumentLink() throws Exception {
6365
}
6466

6567
@Test
66-
public void testDocumentLinkExternalFile() throws Exception {
68+
public void testDocumentLinkExternalFile(@TempDir Path tempDir) throws Exception {
6769
final var links = new ArrayList<DocumentLink>();
6870
links.add(new DocumentLink(new Range(new Position(0, 9), new Position(0, 15)), "file://test0"));
6971
MockLanguageServer.INSTANCE.setDocumentLinks(links);
7072

71-
File file = TestUtils.createTempFile("testDocumentLinkExternalFile", ".lspt");
72-
final var editor = (ITextEditor) IDE.openInternalEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toURI()));
73+
Path file = Files.createFile(tempDir.resolve("testDocumentLinkExternalFile.lspt"));
74+
final var editor = (ITextEditor) IDE.openInternalEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toUri()));
7375
ITextViewer viewer = LSPEclipseUtils.getTextViewer(editor);
7476
viewer.getDocument().set("Long enough dummy content to match ranges");
7577

org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/DocumentDidChangeTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
import static org.junit.jupiter.api.Assertions.assertEquals;
1818
import static org.junit.jupiter.api.Assertions.assertNotNull;
1919

20-
import java.io.File;
20+
import java.nio.file.Files;
21+
import java.nio.file.Path;
2122
import java.util.List;
2223
import java.util.function.Predicate;
2324

@@ -39,6 +40,7 @@
3940
import org.eclipse.ui.IEditorPart;
4041
import org.eclipse.ui.ide.IDE;
4142
import org.junit.jupiter.api.Test;
43+
import org.junit.jupiter.api.io.TempDir;
4244

4345
public class DocumentDidChangeTest extends AbstractTestWithProject {
4446

@@ -193,12 +195,12 @@ public boolean test(ServerCapabilities t) {
193195
}
194196

195197
@Test
196-
public void testFullSyncExternalFile() throws Exception {
198+
public void testFullSyncExternalFile(@TempDir Path tempDir) throws Exception {
197199
MockLanguageServer.INSTANCE.getInitializeResult().getCapabilities()
198200
.setTextDocumentSync(TextDocumentSyncKind.Full);
199201

200-
File file = TestUtils.createTempFile("testFullSyncExternalFile", ".lspt");
201-
IEditorPart editor = IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toURI()));
202+
Path file = Files.createFile(tempDir.resolve("testFullSyncExternalFile.lspt"));
203+
IEditorPart editor = IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toUri()));
202204
ITextViewer viewer = LSPEclipseUtils.getTextViewer(editor);
203205
LanguageServers.forDocument(viewer.getDocument()).withFilter(new Predicate<ServerCapabilities>() {
204206
@Override

org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/DocumentDidCloseTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
import static org.junit.jupiter.api.Assertions.assertEquals;
1515
import static org.junit.jupiter.api.Assertions.assertNotNull;
1616

17-
import java.io.File;
17+
import java.nio.file.Files;
18+
import java.nio.file.Path;
1819
import java.util.concurrent.CompletableFuture;
1920
import java.util.concurrent.TimeUnit;
2021

@@ -31,6 +32,7 @@
3132
import org.eclipse.ui.IEditorPart;
3233
import org.eclipse.ui.ide.IDE;
3334
import org.junit.jupiter.api.Test;
35+
import org.junit.jupiter.api.io.TempDir;
3436

3537
public class DocumentDidCloseTest extends AbstractTestWithProject {
3638

@@ -54,9 +56,9 @@ public void testClose() throws Exception {
5456
}
5557

5658
@Test
57-
public void testCloseExternalFile() throws Exception {
58-
File testFile = TestUtils.createTempFile("testCloseExternalFile", ".lspt");
59-
IEditorPart editor = IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(testFile.toURI()));
59+
public void testCloseExternalFile(@TempDir Path tempDir) throws Exception {
60+
Path testFile = Files.createFile(tempDir.resolve("testCloseExternalFile.lspt"));
61+
IEditorPart editor = IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(testFile.toUri()));
6062

6163
// Force LS to initialize and open file
6264
LanguageServers.forDocument(LSPEclipseUtils.getDocument(editor.getEditorInput())).anyMatching();
@@ -68,6 +70,6 @@ public void testCloseExternalFile() throws Exception {
6870

6971
DidCloseTextDocumentParams lastChange = didCloseExpectation.get(1000, TimeUnit.MILLISECONDS);
7072
assertNotNull(lastChange.getTextDocument());
71-
assertEquals(LSPEclipseUtils.toUri(testFile).toString(), lastChange.getTextDocument().getUri());
73+
assertEquals(LSPEclipseUtils.toUri(testFile.toFile()).toString(), lastChange.getTextDocument().getUri());
7274
}
7375
}

org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/DocumentDidOpenTest.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
import static org.junit.jupiter.api.Assertions.assertEquals;
1515
import static org.junit.jupiter.api.Assertions.assertNotNull;
1616

17-
import java.io.File;
17+
import java.nio.file.Files;
18+
import java.nio.file.Path;
1819
import java.util.concurrent.CompletableFuture;
1920
import java.util.concurrent.TimeUnit;
2021

@@ -32,6 +33,7 @@
3233
import org.eclipse.ui.ide.IDE;
3334
import org.eclipse.ui.texteditor.AbstractTextEditor;
3435
import org.junit.jupiter.api.Test;
36+
import org.junit.jupiter.api.io.TempDir;
3537

3638
public class DocumentDidOpenTest extends AbstractTestWithProject {
3739

@@ -55,11 +57,11 @@ public void testOpen() throws Exception {
5557
}
5658

5759
@Test
58-
public void testOpenExternalFile() throws Exception {
59-
File file = TestUtils.createTempFile("testOpenExternalFile", ".lspt");
60+
public void testOpenExternalFile(@TempDir Path tempDir) throws Exception {
61+
Path file = Files.createFile(tempDir.resolve("testOpenExternalFile.lspt"));
6062
final var didOpenExpectation = new CompletableFuture<DidOpenTextDocumentParams>();
6163
MockLanguageServer.INSTANCE.setDidOpenCallback(didOpenExpectation);
62-
IEditorPart editor = IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toURI()));
64+
IEditorPart editor = IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toUri()));
6365
// Force LS to initialize and open file
6466
LanguageServers.forDocument(LSPEclipseUtils.getDocument(editor.getEditorInput())).anyMatching();
6567

org.eclipse.lsp4e.test/src/org/eclipse/lsp4e/test/edit/DocumentDidSaveTest.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616
import static org.junit.jupiter.api.Assertions.assertEquals;
1717
import static org.junit.jupiter.api.Assertions.assertNotNull;
1818

19-
import java.io.File;
19+
import java.nio.file.Files;
20+
import java.nio.file.Path;
2021
import java.util.concurrent.CompletableFuture;
2122
import java.util.concurrent.TimeUnit;
2223

@@ -35,6 +36,7 @@
3536
import org.eclipse.ui.IEditorPart;
3637
import org.eclipse.ui.ide.IDE;
3738
import org.junit.jupiter.api.Test;
39+
import org.junit.jupiter.api.io.TempDir;
3840

3941
public class DocumentDidSaveTest extends AbstractTestWithProject {
4042

@@ -67,9 +69,9 @@ public void testSave() throws Exception {
6769
}
6870

6971
@Test
70-
public void testSaveExternalFile() throws Exception {
71-
File file = TestUtils.createTempFile("testSaveExternalFile", ".lspt");
72-
IEditorPart editor = IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toURI()));
72+
public void testSaveExternalFile(@TempDir Path tempDir) throws Exception {
73+
Path file = Files.createFile(tempDir.resolve("testSaveExternalFile.lspt"));
74+
IEditorPart editor = IDE.openEditorOnFileStore(UI.getActivePage(), EFS.getStore(file.toUri()));
7375
ITextViewer viewer = LSPEclipseUtils.getTextViewer(editor);
7476

7577
// 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 {
8789
waitForAndAssertCondition(2_000, () -> {
8890
DidSaveTextDocumentParams lastChange = didSaveExpectation.get(10, TimeUnit.MILLISECONDS);
8991
assertNotNull(lastChange.getTextDocument());
90-
assertEquals(LSPEclipseUtils.toUri(file).toString(), lastChange.getTextDocument().getUri());
92+
assertEquals(LSPEclipseUtils.toUri(file.toFile()).toString(), lastChange.getTextDocument().getUri());
9193
return true;
9294
});
9395
}

0 commit comments

Comments
 (0)