Skip to content

Commit 4721b99

Browse files
refactor(tests): Upgrade tests to JUnit 5 (#1403)
1 parent a284e36 commit 4721b99

74 files changed

Lines changed: 543 additions & 459 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

org.eclipse.lsp4e.test/META-INF/MANIFEST.MF

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ Bundle-Vendor: Eclipse LSP4E
88
Bundle-RequiredExecutionEnvironment: JavaSE-21
99
Require-Bundle: org.eclipse.core.runtime,
1010
org.eclipse.core.resources,
11-
org.junit,
1211
org.eclipse.ui,
1312
org.eclipse.ui.workbench,
1413
org.eclipse.ui.ide,
@@ -29,5 +28,8 @@ Require-Bundle: org.eclipse.core.runtime,
2928
org.eclipse.e4.ui.model.workbench,
3029
org.eclipse.e4.ui.workbench,
3130
org.eclipse.tm4e.ui,
32-
org.eclipse.core.filesystem
31+
org.eclipse.core.filesystem,
32+
junit-jupiter-api,
33+
org.hamcrest,
34+
org.opentest4j
3335
Automatic-Module-Name: org.eclipse.lsp4e.test

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
*******************************************************************************/
1212
package org.eclipse.lsp4e.test;
1313

14-
import static org.junit.Assert.assertFalse;
15-
import static org.junit.Assert.assertTrue;
14+
import static org.junit.jupiter.api.Assertions.assertFalse;
15+
import static org.junit.jupiter.api.Assertions.assertTrue;
1616

1717
import java.util.List;
1818

@@ -21,22 +21,22 @@
2121
import org.eclipse.lsp4e.LanguageServersRegistry;
2222
import org.eclipse.lsp4e.test.utils.AbstractTest;
2323
import org.eclipse.lsp4e.test.utils.TestUtils;
24-
import org.junit.AfterClass;
25-
import org.junit.BeforeClass;
26-
import org.junit.Test;
24+
import org.junit.jupiter.api.AfterAll;
25+
import org.junit.jupiter.api.BeforeAll;
26+
import org.junit.jupiter.api.Test;
2727

2828
public class ContentTypeToLanguageServerDefinitionTest extends AbstractTest {
2929

3030
public static final String SERVER_TO_DISABLE = "org.eclipse.lsp4e.test.server.disable";
3131
public static final String DISABLED_CONTENT_TYPE = "org.eclipse.lsp4e.test.content-type-disabled";
3232
public static final String DISABLED_SERVER_PREF = SERVER_TO_DISABLE + "/" + DISABLED_CONTENT_TYPE;
3333

34-
@BeforeClass
34+
@BeforeAll
3535
public static void setup() {
3636
LanguageServerPlugin.getDefault().getPreferenceStore().setValue(DISABLED_SERVER_PREF, "false");
3737
}
3838

39-
@AfterClass
39+
@AfterAll
4040
public static void cleanup() {
4141
TestUtils.getDisabledLS().setUserEnabled(false);
4242
}

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
import static org.eclipse.lsp4e.test.utils.TestUtils.waitForAndAssertCondition;
1515
import static org.hamcrest.MatcherAssert.assertThat;
1616
import static org.hamcrest.Matchers.matchesPattern;
17-
import static org.junit.Assert.*;
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
1819

1920
import java.util.Collection;
2021
import java.util.concurrent.CompletableFuture;
@@ -31,14 +32,14 @@
3132
import org.eclipse.lsp4e.test.utils.TestUtils;
3233
import org.eclipse.lsp4e.tests.mock.MockConnectionProviderMultiRootFolders;
3334
import org.eclipse.ui.IEditorPart;
34-
import org.junit.Before;
35-
import org.junit.Test;
35+
import org.junit.jupiter.api.BeforeEach;
36+
import org.junit.jupiter.api.Test;
3637

3738
public class LanguageServerWrapperTest extends AbstractTestWithProject {
3839

3940
private IProject project2;
4041

41-
@Before
42+
@BeforeEach
4243
public void setUp() throws Exception {
4344
project2 = TestUtils.createProject("LanguageServerWrapperTestProject2" + System.currentTimeMillis());
4445
}
@@ -134,7 +135,7 @@ public void testStartStopAndActive() throws CoreException, AssertionError {
134135
Integer cpStartCount= MockConnectionProviderMultiRootFolders.getStartCount();
135136
Integer cpStopCount= MockConnectionProviderMultiRootFolders.getStopCount();
136137

137-
assertEquals("startCount == stopCount", cpStartCount, cpStopCount);
138+
assertEquals(cpStartCount, cpStopCount, "startCount == stopCount");
138139
}
139140

140141
}

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

Lines changed: 41 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,16 @@
1313
package org.eclipse.lsp4e.test;
1414

1515
import static org.eclipse.lsp4e.LanguageServiceAccessor.hasActiveLanguageServers;
16-
import static org.eclipse.lsp4e.test.utils.TestUtils.*;
17-
import static org.junit.Assert.*;
16+
import static org.eclipse.lsp4e.test.utils.TestUtils.createUniqueTestFile;
17+
import static org.eclipse.lsp4e.test.utils.TestUtils.openEditor;
18+
import static org.eclipse.lsp4e.test.utils.TestUtils.waitForCondition;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
22+
import static org.junit.jupiter.api.Assertions.assertNull;
23+
import static org.junit.jupiter.api.Assertions.assertThrows;
24+
import static org.junit.jupiter.api.Assertions.assertTrue;
25+
import static org.junit.jupiter.api.Assumptions.assumeTrue;
1826

1927
import java.util.Collections;
2028
import java.util.List;
@@ -55,8 +63,7 @@
5563
import org.eclipse.swt.custom.StyledText;
5664
import org.eclipse.swt.widgets.Display;
5765
import org.eclipse.ui.tests.harness.util.DisplayHelper;
58-
import org.junit.Assume;
59-
import org.junit.Test;
66+
import org.junit.jupiter.api.Test;
6067

6168
public class LanguageServersTest extends AbstractTestWithProject {
6269

@@ -172,11 +179,11 @@ public synchronized CompletableFuture<Hover> hover(HoverParams position) {
172179
.withCapability(ServerCapabilities::getHoverProvider)
173180
.computeAll(ls -> ls.getTextDocumentService().hover(params).thenApply(h -> h.getContents().getLeft().get(0).getLeft()));
174181

175-
assertEquals("Should have had two responses", 2, result.size());
182+
assertEquals(2, result.size(), "Should have had two responses");
176183

177184
final Object first = CompletableFuture.anyOf(result.get(0), result.get(1)).join();
178185

179-
assertEquals("HoverContent1 should have returned first, independently", "HoverContent1", first);
186+
assertEquals("HoverContent1", first, "HoverContent1 should have returned first, independently");
180187

181188
List<String> hovers = result.stream().map(CompletableFuture::join).toList();
182189

@@ -195,7 +202,7 @@ public synchronized CompletableFuture<Hover> hover(HoverParams position) {
195202
@Test
196203
public void testCollectAllUserCannotBlockListener() throws Exception {
197204
// This test will only work if a minimum of two tasks can be run in the common pool without blocking!
198-
Assume.assumeTrue("Test skipped as common thread pool does not have multiple executors", ForkJoinPool.commonPool().getParallelism() >= 2);
205+
assumeTrue(ForkJoinPool.commonPool().getParallelism() >= 2, "Test skipped as common thread pool does not have multiple executors");
199206
final var hoverResponse = new Hover(List.of(Either.forLeft("HoverContent")), new Range(new Position(0, 0), new Position(0, 10)));
200207
MockLanguageServer.INSTANCE.setHover(hoverResponse);
201208

@@ -235,8 +242,8 @@ public void testCollectAllUserCannotBlockListener() throws Exception {
235242

236243
final String resultThread = resultThreadFuture.join();
237244

238-
assertTrue("Second hover response should not have been blocked by the first but took " + secondResponseTime + " ms", secondResponseTime < 1000);
239-
assertTrue("Result should not have run on an LS listener thread but ran on " + resultThread, !resultThread.startsWith("LS"));
245+
assertTrue(secondResponseTime < 1000, "Second hover response should not have been blocked by the first but took " + secondResponseTime + " ms");
246+
assertTrue(!resultThread.startsWith("LS"), "Result should not have run on an LS listener thread but ran on " + resultThread);
240247
}
241248

242249
@Test
@@ -283,7 +290,7 @@ public synchronized CompletableFuture<Hover> hover(HoverParams position) {
283290
Optional<String> result = response.join();
284291
assertTrue(result.isPresent());
285292

286-
assertEquals("HoverContent1 should have arrived first", "HoverContent1", result.get());
293+
assertEquals("HoverContent1", result.get(), "HoverContent1 should have arrived first");
287294

288295
// It won't *normally) matter in production but because the tests run quickly, make sure the test teardown doesn't
289296
// occur before the slower, ignored result has completed, otherwise will get a load of console noise
@@ -331,9 +338,9 @@ public synchronized CompletableFuture<Hover> hover(HoverParams position) {
331338
.computeFirst(ls -> ls.getTextDocumentService().hover(params).thenApply(h -> h == null ? null : h.getContents().getLeft().get(0).getLeft()));
332339

333340
Optional<String> result = response.join();
334-
assertTrue("Should have returned a result", result.isPresent());
341+
assertTrue(result.isPresent(), "Should have returned a result");
335342

336-
assertEquals("HoverContent2 should have been the result", "HoverContent2", result.get());
343+
assertEquals("HoverContent2", result.get(), "HoverContent2 should have been the result");
337344

338345
}
339346

@@ -366,7 +373,7 @@ public synchronized CompletableFuture<Hover> hover(HoverParams position) {
366373
.computeFirst(ls -> ls.getTextDocumentService().hover(params).thenApply(h -> h == null ? null : h.getContents().getLeft().get(0).getLeft()));
367374

368375
Optional<String> result = response.join();
369-
assertTrue("Should not have returned a result", result.isEmpty());
376+
assertTrue(result.isEmpty(), "Should not have returned a result");
370377
}
371378

372379
@Test
@@ -410,9 +417,9 @@ public synchronized CompletableFuture<Hover> hover(HoverParams position) {
410417
.computeFirst(ls -> ls.getTextDocumentService().hover(params).thenApply(h -> h == null ? Collections.emptyList() : List.of(h.getContents().getLeft().get(0).getLeft())));
411418

412419
Optional<List<String>> result = response.join();
413-
assertTrue("Should have returned a result", result.isPresent());
420+
assertTrue(result.isPresent(), "Should have returned a result");
414421

415-
assertEquals("HoverContent2 should have been the result", "HoverContent2", result.get().get(0));
422+
assertEquals("HoverContent2", result.get().get(0), "HoverContent2 should have been the result");
416423
}
417424

418425
/**
@@ -492,7 +499,7 @@ public synchronized CompletableFuture<Hover> hover(HoverParams position) {
492499
message.append(" Too Late " );message.append(i);
493500
message.append(System.lineSeparator());
494501
});
495-
assertTrue(message.toString(), tooEarlyHover.isEmpty() && tooLateHover.isEmpty());
502+
assertTrue(tooEarlyHover.isEmpty() && tooLateHover.isEmpty(), message.toString());
496503
}
497504

498505
/**
@@ -580,8 +587,8 @@ public synchronized CompletableFuture<Hover> hover(HoverParams position) {
580587

581588
final long finishTime = System.currentTimeMillis() - startTime;
582589

583-
assertTrue(String.format("Dispatch should not have blocked but took %d ms vs overall test time of %d ms", dispatchTime, finishTime), dispatchTime < 1000);
584-
assertEquals("Should not have been any messages dispatched on UI thread", 0, uiDispatchCount.get());
590+
assertTrue(dispatchTime < 1000, String.format("Dispatch should not have blocked but took %d ms vs overall test time of %d ms", dispatchTime, finishTime));
591+
assertEquals(0, uiDispatchCount.get(), "Should not have been any messages dispatched on UI thread");
585592
}
586593

587594
@Test
@@ -590,7 +597,7 @@ public void testAnyMatchingIsNonBlocking() throws Exception {
590597
long start = System.currentTimeMillis();
591598
assertFalse(LanguageServers.forProject(project).anyMatching());
592599
long duration = System.currentTimeMillis() - start;
593-
assertTrue("LanguageServers.anyMatching() took too long: " + duration + "ms", duration < 100);
600+
assertTrue(duration < 100, "LanguageServers.anyMatching() took too long: " + duration + "ms");
594601

595602
// test with one slow LS available
596603
MockLanguageServer.INSTANCE.setTimeToProceedQueries(5_000);
@@ -600,7 +607,7 @@ public void testAnyMatchingIsNonBlocking() throws Exception {
600607
try {
601608
assertTrue(LanguageServers.forProject(project).anyMatching());
602609
duration = System.currentTimeMillis() - start;
603-
assertTrue("LanguageServers.anyMatching() took too long: " + duration + "ms", duration < 100);
610+
assertTrue(duration < 100, "LanguageServers.anyMatching() took too long: " + duration + "ms");
604611
} finally {
605612
editor1.getSite().getPage().closeEditor(editor1, false);
606613
}
@@ -617,7 +624,7 @@ public void testNoMatchingServers() throws Exception {
617624

618625
LanguageServerDocumentExecutor executor = LanguageServers.forDocument(document).withFilter(sc -> false);
619626

620-
assertFalse("Should not have been any valid LS", executor.anyMatching());
627+
assertFalse(executor.anyMatching(), "Should not have been any valid LS");
621628

622629
final var params = new HoverParams();
623630
final var position = new Position();
@@ -626,10 +633,10 @@ public void testNoMatchingServers() throws Exception {
626633
params.setPosition(position);
627634

628635
Optional<?> result = executor.computeFirst(ls -> ls.getTextDocumentService().hover(params)).get(10, TimeUnit.SECONDS);
629-
assertFalse("Should not have had a result", result.isPresent());
636+
assertFalse(result.isPresent(), "Should not have had a result");
630637

631638
List<?> collectedResult = executor.collectAll(ls -> ls.getTextDocumentService().hover(params)).get(10, TimeUnit.SECONDS);
632-
assertTrue("Should not have had a result", collectedResult.isEmpty());
639+
assertTrue(collectedResult.isEmpty(), "Should not have had a result");
633640

634641
List<CompletableFuture<Hover>> allResults = executor.computeAll(ls -> ls.getTextDocumentService().hover(params));
635642
for (CompletableFuture<Hover> f : allResults) {
@@ -638,7 +645,7 @@ public void testNoMatchingServers() throws Exception {
638645
}
639646
}
640647

641-
@Test(expected=CompletionException.class)
648+
@Test
642649
public void testComputeFirstBubblesException() throws Exception {
643650
MockLanguageServer.INSTANCE.setTextDocumentService(new MockTextDocumentService(MockLanguageServer.INSTANCE::buildMaybeDelayedFuture) {
644651
@Override
@@ -664,11 +671,14 @@ public synchronized CompletableFuture<Hover> hover(HoverParams position) {
664671
position.setLine(0);
665672
params.setPosition(position);
666673

667-
CompletableFuture<Optional<String>> response = LanguageServers.forDocument(document)
668-
.withCapability(ServerCapabilities::getHoverProvider)
669-
.computeFirst(ls -> ls.getTextDocumentService().hover(params).thenApply(h -> h == null ? null : h.getContents().getLeft().get(0).getLeft()));
674+
assertThrows(CompletionException.class, () -> {
675+
CompletableFuture<Optional<String>> response = LanguageServers.forDocument(document)
676+
.withCapability(ServerCapabilities::getHoverProvider)
677+
.computeFirst(ls -> ls.getTextDocumentService().hover(params)
678+
.thenApply(h -> h == null ? null : h.getContents().getLeft().get(0).getLeft()));
670679

671-
response.join();
680+
response.join();
681+
});
672682
}
673683

674684
/**
@@ -700,8 +710,8 @@ public void testWrapperWrapsSameLS() throws Exception {
700710

701711
final var matching = new AtomicInteger();
702712

703-
assertEquals("Should have had two responses", 2, result.size());
704-
assertNotEquals("LS should have been different proxies", result.get(0).second(), result.get(1).second());
713+
assertEquals(2, result.size(), "Should have had two responses");
714+
assertNotEquals(result.get(0).second(), result.get(1).second(), "LS should have been different proxies");
705715
result.forEach(p -> {
706716
p.first().execute(ls -> {
707717
if (ls == p.second()) {
@@ -711,7 +721,7 @@ public void testWrapperWrapsSameLS() throws Exception {
711721
}).join();
712722
});
713723

714-
assertEquals("Wrapper should have used same LS", 2, matching.get());
724+
assertEquals(2, matching.get(), "Wrapper should have used same LS");
715725
}
716726

717727
/**

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

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,26 @@
1313
*******************************************************************************/
1414
package org.eclipse.lsp4e.test;
1515

16-
import static org.eclipse.lsp4e.LSPEclipseUtils.*;
17-
import static org.eclipse.lsp4e.LanguageServiceAccessor.*;
18-
import static org.eclipse.lsp4e.test.utils.TestUtils.*;
19-
import static org.junit.Assert.*;
16+
import static org.eclipse.lsp4e.LSPEclipseUtils.getDocument;
17+
import static org.eclipse.lsp4e.LSPEclipseUtils.getTextViewer;
18+
import static org.eclipse.lsp4e.LanguageServiceAccessor.getLSPDocumentInfosFor;
19+
import static org.eclipse.lsp4e.LanguageServiceAccessor.getLSWrapper;
20+
import static org.eclipse.lsp4e.LanguageServiceAccessor.getLSWrappers;
21+
import static org.eclipse.lsp4e.LanguageServiceAccessor.hasActiveLanguageServers;
22+
import static org.eclipse.lsp4e.test.utils.TestUtils.createFile;
23+
import static org.eclipse.lsp4e.test.utils.TestUtils.createProject;
24+
import static org.eclipse.lsp4e.test.utils.TestUtils.createTempFile;
25+
import static org.eclipse.lsp4e.test.utils.TestUtils.createUniqueTestFile;
26+
import static org.eclipse.lsp4e.test.utils.TestUtils.createUniqueTestFileMultiLS;
27+
import static org.eclipse.lsp4e.test.utils.TestUtils.openEditor;
28+
import static org.eclipse.lsp4e.test.utils.TestUtils.openTextViewer;
29+
import static org.eclipse.lsp4e.test.utils.TestUtils.waitForAndAssertCondition;
30+
import static org.eclipse.lsp4e.test.utils.TestUtils.waitForCondition;
31+
import static org.junit.jupiter.api.Assertions.assertEquals;
32+
import static org.junit.jupiter.api.Assertions.assertFalse;
33+
import static org.junit.jupiter.api.Assertions.assertNotNull;
34+
import static org.junit.jupiter.api.Assertions.assertSame;
35+
import static org.junit.jupiter.api.Assertions.assertTrue;
2036

2137
import java.util.ArrayList;
2238
import java.util.concurrent.CompletableFuture;
@@ -41,7 +57,7 @@
4157
import org.eclipse.lsp4j.ServerCapabilities;
4258
import org.eclipse.ui.ide.IDE;
4359
import org.eclipse.ui.texteditor.ITextEditor;
44-
import org.junit.Test;
60+
import org.junit.jupiter.api.Test;
4561

4662
public class LanguageServiceAccessorTest extends AbstractTestWithProject {
4763

@@ -130,8 +146,8 @@ public void testReuseSameLSforMultiContentType() throws Exception {
130146
var file1LS = file1LanguageServers.get(0).serverDefinition;
131147
assertTrue(file2LanguageServers.contains(file1LS)); // LS from file1 is reused
132148

133-
assertEquals("Not right amount of language servers bound to project", 2,
134-
LanguageServers.forProject(project).computeAll(ls -> CompletableFuture.completedFuture(null)).size());
149+
assertEquals(2, LanguageServers.forProject(project).computeAll(ls -> CompletableFuture.completedFuture(null)).size(),
150+
"Not right amount of language servers bound to project");
135151
}
136152

137153
@Test
@@ -317,7 +333,7 @@ public void testLanguageServerHierarchy_parentContentTypeUsed() throws Exception
317333
var servers = getLSWrappers(testFile, MATCH_ALL);
318334
var iterator = servers.iterator();
319335
assertEquals("org.eclipse.lsp4e.test.server", iterator.next().serverDefinition.id);
320-
assertFalse("Should only be a single LS", iterator.hasNext());
336+
assertFalse(iterator.hasNext(), "Should only be a single LS");
321337
}
322338

323339
@Test

0 commit comments

Comments
 (0)