Skip to content

Commit 44ad6d9

Browse files
committed
Migrate tests to JUnit 5
Main changes: * all tests use JUnit Jupiter APIs and assertions * remove public modifiers from test classes and test methods because they are unnecessary with JUnit Jupiter * replace JVM version assumptions with `@EnabledOnJre` annotations that support version ranges * replace OS assumptions with `@EnabledOnOs` and `@DisabledOnOs` * enable JUnit Jupiter in Gradle build scripts for the test task with `useJUnitPlatform()`
1 parent c8bcf01 commit 44ad6d9

File tree

147 files changed

+1433
-1494
lines changed

Some content is hidden

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

147 files changed

+1433
-1494
lines changed

_ext/eclipse-base/src/test/java/com/diffplug/spotless/extra/eclipse/base/SpotlessEclipseFrameworkTest.java

+21-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 DiffPlug
2+
* Copyright 2016-2021 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -35,11 +35,11 @@
3535
import org.eclipse.core.runtime.Status;
3636
import org.eclipse.equinox.log.ExtendedLogReaderService;
3737
import org.eclipse.equinox.log.ExtendedLogService;
38-
import org.junit.After;
39-
import org.junit.AfterClass;
40-
import org.junit.Before;
41-
import org.junit.BeforeClass;
42-
import org.junit.Test;
38+
import org.junit.jupiter.api.AfterAll;
39+
import org.junit.jupiter.api.AfterEach;
40+
import org.junit.jupiter.api.BeforeAll;
41+
import org.junit.jupiter.api.BeforeEach;
42+
import org.junit.jupiter.api.Test;
4343
import org.osgi.framework.Bundle;
4444
import org.osgi.framework.BundleContext;
4545
import org.osgi.framework.BundleException;
@@ -52,7 +52,7 @@
5252
import com.diffplug.spotless.extra.eclipse.base.service.SingleSlf4JService;
5353

5454
/** Integration tests */
55-
public class SpotlessEclipseFrameworkTest {
55+
class SpotlessEclipseFrameworkTest {
5656

5757
private final static String TEST_LOGGER_NAME = SpotlessEclipseFrameworkTest.class.getSimpleName();
5858
private final static String CUSTOM_PREFIX = "prefix\t";
@@ -62,8 +62,8 @@ public class SpotlessEclipseFrameworkTest {
6262

6363
private static boolean TREAT_ERROR_AS_EXCEPTION = false;
6464

65-
@BeforeClass
66-
public static void frameworkTestSetup() throws BundleException {
65+
@BeforeAll
66+
static void frameworkTestSetup() throws BundleException {
6767
//Prepare interception of SLF4J messages to System.out
6868
SLF4J_RECEIVER = new Slf4JMesssageListener();
6969

@@ -87,29 +87,29 @@ public void registerServices(SpotlessEclipseServiceConfig config) {
8787
});
8888
}
8989

90-
@AfterClass
91-
public static void deregisterSlf4JReceiver() {
90+
@AfterAll
91+
static void deregisterSlf4JReceiver() {
9292
SLF4J_RECEIVER.deregister();
9393
}
9494

9595
private EclipseMessageListener eclipseMessageListener;
9696

97-
@Before
98-
public void eclipseReceiverSetup() {
97+
@BeforeEach
98+
void eclipseReceiverSetup() {
9999
ExtendedLogReaderService service = getService(ExtendedLogReaderService.class);
100100
eclipseMessageListener = new EclipseMessageListener();
101101
service.addLogListener(eclipseMessageListener);
102102
SLF4J_RECEIVER.clear();
103103
}
104104

105-
@After
106-
public void logReceiverTearDown() {
105+
@AfterEach
106+
void logReceiverTearDown() {
107107
ExtendedLogReaderService service = getService(ExtendedLogReaderService.class);
108108
service.removeLogListener(eclipseMessageListener);
109109
}
110110

111111
@Test
112-
public void testCustomizedLogMessage() {
112+
void testCustomizedLogMessage() {
113113
ResourcesPlugin.getPlugin().getLog().log(new Status(IStatus.ERROR, "Some plugin", "Hello World!"));
114114
assertSlf4J()
115115
.as("Error message logged.").received("Hello World!").contains(TEST_LOGGER_NAME)
@@ -121,7 +121,7 @@ public void testCustomizedLogMessage() {
121121
}
122122

123123
@Test
124-
public void testCustomizedLogException() {
124+
void testCustomizedLogException() {
125125
assertThatExceptionOfType(IllegalArgumentException.class)
126126
.isThrownBy(() -> {
127127
try {
@@ -135,7 +135,7 @@ public void testCustomizedLogException() {
135135
}
136136

137137
@Test
138-
public void testPluginLog() {
138+
void testPluginLog() {
139139
List<Integer> logLevels = Arrays.asList(IStatus.CANCEL, IStatus.ERROR, IStatus.INFO, IStatus.OK, IStatus.WARNING);
140140
List<Integer> enabledLogLevels = Arrays.asList(IStatus.ERROR, IStatus.WARNING);
141141
List<Integer> disabledLogLevels = logLevels.stream().filter(level -> !enabledLogLevels.contains(level)).collect(Collectors.toList());
@@ -153,7 +153,7 @@ public void testPluginLog() {
153153
}
154154

155155
@Test
156-
public void testLogServiceLevel() {
156+
void testLogServiceLevel() {
157157
ExtendedLogService service = getService(ExtendedLogService.class);
158158
assertThat(service.isErrorEnabled()).as("Error log level is enabled").isTrue();
159159
assertThat(service.isWarnEnabled()).as("Warning log level is enabled").isTrue();
@@ -163,7 +163,7 @@ public void testLogServiceLevel() {
163163
}
164164

165165
@Test
166-
public void testLogServiceLog() {
166+
void testLogServiceLog() {
167167
ExtendedLogService service = getService(ExtendedLogService.class);
168168
service.info("Log Info");
169169
service.warn("Log Warn");
@@ -174,7 +174,7 @@ public void testLogServiceLog() {
174174

175175
@Test
176176
@Deprecated
177-
public void testLogServiceLog_3_12() {
177+
void testLogServiceLog_3_12() {
178178
ExtendedLogService service = getService(ExtendedLogService.class);
179179
service.log(SingleSlf4JService.LOG_INFO, "Log Info");
180180
try {
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 DiffPlug
2+
* Copyright 2016-2021 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,75 +16,63 @@
1616
package com.diffplug.spotless.extra.eclipse.base.osgi;
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
19-
import static org.junit.Assert.*;
19+
import static org.junit.jupiter.api.Assertions.*;
2020

2121
import java.util.Arrays;
2222

23-
import org.junit.Before;
24-
import org.junit.Test;
23+
import org.junit.jupiter.api.BeforeEach;
24+
import org.junit.jupiter.api.Test;
2525
import org.osgi.framework.Bundle;
2626
import org.osgi.framework.BundleException;
2727

28-
public class BundleSetTest {
28+
class BundleSetTest {
2929

3030
BundleSet instance;
3131

32-
@Before
33-
public void initialize() {
32+
@BeforeEach
33+
void initialize() {
3434
instance = new BundleSet();
3535
}
3636

3737
@Test
38-
public void testAddGet() throws BundleException {
38+
void testAddGet() throws BundleException {
3939
Bundle testBundle1 = new TestBundle(1, "a");
4040
Bundle testBundle2 = new TestBundle(2, "b");
4141
instance.add(testBundle1);
4242
instance.add(testBundle2);
43-
assertEquals("Get by ID 1", testBundle1, instance.get(1));
44-
assertEquals("Get by ID 2", testBundle2, instance.get(2));
45-
assertEquals("Get by symbolic name 'a'", testBundle1, instance.get("a"));
46-
assertEquals("Get by symbolic name 'b'", testBundle2, instance.get("b"));
47-
assertTrue("Contains all", instance.getAll().containsAll(Arrays.asList(testBundle1, testBundle2)));
43+
assertEquals(testBundle1, instance.get(1), "Get by ID 1");
44+
assertEquals(testBundle2, instance.get(2), "Get by ID 2");
45+
assertEquals(testBundle1, instance.get("a"), "Get by symbolic name 'a'");
46+
assertEquals(testBundle2, instance.get("b"), "Get by symbolic name 'b'");
47+
assertTrue(instance.getAll().containsAll(Arrays.asList(testBundle1, testBundle2)), "Contains all");
4848
}
4949

5050
@Test
51-
public void testSameSymbolicName() throws BundleException {
51+
void testSameSymbolicName() throws BundleException {
5252
final String symbolicName = "sym.a";
5353
final long id1 = 12345;
5454
final long id2 = 23456;
5555
Bundle testBundle1 = new TestBundle(id1, symbolicName);
5656
Bundle testBundle2 = new TestBundle(id2, symbolicName);
5757
instance.add(testBundle1);
58-
boolean exceptionCaught = false;
59-
try {
60-
instance.add(testBundle2);
61-
} catch (BundleException e) {
62-
exceptionCaught = true;
63-
assertThat(e.getMessage()).as("BundleException does not contain symbolic name.").contains(symbolicName);
64-
assertThat(e.getMessage()).as("BundleException does not contain ID of exisiting bundle.").contains(Long.toString(id1));
65-
assertThat(e.getMessage()).as("BundleException does not contain ID of new bundle.").contains(Long.toString(id2));
66-
}
67-
assertThat(exceptionCaught).as("No BundleException thrown for duplicate symblic name.").isTrue();
58+
BundleException e = assertThrows(BundleException.class, () -> instance.add(testBundle2));
59+
assertThat(e.getMessage()).as("BundleException does not contain symbolic name.").contains(symbolicName);
60+
assertThat(e.getMessage()).as("BundleException does not contain ID of exisiting bundle.").contains(Long.toString(id1));
61+
assertThat(e.getMessage()).as("BundleException does not contain ID of new bundle.").contains(Long.toString(id2));
6862
}
6963

7064
@Test
71-
public void testSameID() throws BundleException {
65+
void testSameID() throws BundleException {
7266
final String symbolicName1 = "sym.a";
7367
final String symbolicName2 = "sym.b";
7468
final long id = 12345;
7569
Bundle testBundle1 = new TestBundle(id, symbolicName1);
7670
Bundle testBundle2 = new TestBundle(id, symbolicName2);
7771
instance.add(testBundle1);
78-
boolean exceptionCaught = false;
79-
try {
80-
instance.add(testBundle2);
81-
} catch (BundleException e) {
82-
exceptionCaught = true;
83-
assertThat(e.getMessage()).as("BundleException does not contain ID.").contains(Long.toString(id));
84-
assertThat(e.getMessage()).as("BundleException does not contain symbolic name of exisiting bundle.").contains(symbolicName1);
85-
assertThat(e.getMessage()).as("BundleException does not contain symbolic name of new bundle.").contains(symbolicName2);
86-
}
87-
assertThat(exceptionCaught).as("No BundleException thrown for duplicate symblic name.").isTrue();
72+
BundleException e = assertThrows(BundleException.class, () -> instance.add(testBundle2));
73+
assertThat(e.getMessage()).as("BundleException does not contain ID.").contains(Long.toString(id));
74+
assertThat(e.getMessage()).as("BundleException does not contain symbolic name of exisiting bundle.").contains(symbolicName1);
75+
assertThat(e.getMessage()).as("BundleException does not contain symbolic name of new bundle.").contains(symbolicName2);
8876
}
8977

9078
}

_ext/eclipse-base/src/test/java/com/diffplug/spotless/extra/eclipse/base/osgi/ServiceCollectionTest.java

+11-17
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 DiffPlug
2+
* Copyright 2016-2021 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,28 +16,29 @@
1616
package com.diffplug.spotless.extra.eclipse.base.osgi;
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
19+
import static org.junit.jupiter.api.Assertions.*;
1920

2021
import java.util.HashMap;
2122

2223
import org.assertj.core.api.AbstractAssert;
23-
import org.junit.Before;
24-
import org.junit.Test;
24+
import org.junit.jupiter.api.BeforeEach;
25+
import org.junit.jupiter.api.Test;
2526
import org.osgi.framework.Bundle;
2627
import org.osgi.framework.ServiceException;
2728
import org.osgi.framework.ServiceReference;
2829

29-
public class ServiceCollectionTest {
30+
class ServiceCollectionTest {
3031

3132
ServiceCollection instance;
3233

33-
@Before
34-
public void initialize() {
34+
@BeforeEach
35+
void initialize() {
3536
Bundle systemBundle = new TestBundle(0, "test.system");
3637
instance = new ServiceCollection(systemBundle, new HashMap<String, String>());
3738
}
3839

3940
@Test
40-
public void testAddGet() {
41+
void testAddGet() {
4142
Service1 service1 = new Service1();
4243
Service2 service2 = new Service2();
4344
instance.add(Interf1.class, service1);
@@ -49,19 +50,12 @@ public void testAddGet() {
4950
}
5051

5152
@Test
52-
public void testMultipleServicesPerInterface() {
53+
void testMultipleServicesPerInterface() {
5354
Service1 serviceX = new Service1();
5455
Service1 serviceY = new Service1();
5556
instance.add(Interf1.class, serviceX);
56-
boolean exceptionCaught = false;
57-
try {
58-
instance.add(Interf1.class, serviceY);
59-
} catch (ServiceException e) {
60-
exceptionCaught = true;
61-
assertThat(e.getMessage()).as("ServiceException does not contain interface class name.").contains(Interf1.class.getName());
62-
}
63-
assertThat(exceptionCaught).as("No ServiceException thrown for duplicate interfaces.").isTrue();
64-
57+
ServiceException e = assertThrows(ServiceException.class, () -> instance.add(Interf1.class, serviceY));
58+
assertThat(e.getMessage()).as("ServiceException does not contain interface class name.").contains(Interf1.class.getName());
6559
}
6660

6761
private static class ServiceReferenceAssert extends AbstractAssert<ServiceReferenceAssert, ServiceCollection> {

_ext/eclipse-cdt/src/test/java/com/diffplug/spotless/extra/eclipse/cdt/EclipseCdtFormatterStepImplTest.java

+19-19
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016 DiffPlug
2+
* Copyright 2016-2021 DiffPlug
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,17 +16,17 @@
1616
package com.diffplug.spotless.extra.eclipse.cdt;
1717

1818
import static com.diffplug.spotless.extra.eclipse.base.SpotlessEclipseFramework.LINE_DELIMITER;
19-
import static org.junit.Assert.*;
19+
import static org.junit.jupiter.api.Assertions.*;
2020

2121
import java.util.Properties;
2222
import java.util.function.Consumer;
2323

2424
import org.eclipse.cdt.core.CCorePlugin;
2525
import org.eclipse.cdt.core.formatter.DefaultCodeFormatterConstants;
26-
import org.junit.Test;
26+
import org.junit.jupiter.api.Test;
2727

2828
/** Eclipse CDT wrapper integration tests */
29-
public class EclipseCdtFormatterStepImplTest {
29+
class EclipseCdtFormatterStepImplTest {
3030

3131
private final static String CPP_UNFORMATTED = "#include <iostream>\n" +
3232
"using namespace std;\n" +
@@ -49,45 +49,45 @@ public class EclipseCdtFormatterStepImplTest {
4949
private final static String FUNCT_PTR_FORMATTED = "void (* getFunc(void)) (int);";
5050

5151
@Test
52-
public void defaultFormat() throws Throwable {
52+
void defaultFormat() throws Throwable {
5353
String output = format(CPP_UNFORMATTED, config -> {});
54-
assertEquals("Unexpected formatting with default preferences.",
55-
CPP_FORMATTED, output);
54+
assertEquals(CPP_FORMATTED,
55+
output, "Unexpected formatting with default preferences.");
5656
}
5757

5858
@Test
59-
public void invalidFormat() throws Throwable {
59+
void invalidFormat() throws Throwable {
6060
String output = format(CPP_FORMATTED.replace("int main() {", "int main() "), config -> {});
61-
assertTrue("Incomplete CPP not formatted on best effort basis.", output.contains("int main()" + LINE_DELIMITER));
61+
assertTrue(output.contains("int main()" + LINE_DELIMITER), "Incomplete CPP not formatted on best effort basis.");
6262
}
6363

6464
@Test
65-
public void invalidCharater() throws Throwable {
65+
void invalidCharater() throws Throwable {
6666
String output = format(CPP_FORMATTED.replace("int main() {", "int main()" + ILLEGAL_CHAR + " {"), config -> {});
67-
assertTrue("Invalid charater not formatted on best effort basis.", output.contains("int main()" + LINE_DELIMITER));
67+
assertTrue(output.contains("int main()" + LINE_DELIMITER), "Invalid charater not formatted on best effort basis.");
6868
}
6969

7070
@Test
71-
public void invalidConfiguration() throws Throwable {
71+
void invalidConfiguration() throws Throwable {
7272
String output = format(CPP_FORMATTED, config -> {
7373
config.setProperty(DefaultCodeFormatterConstants.FORMATTER_TAB_CHAR, CCorePlugin.SPACE);
7474
config.setProperty(DefaultCodeFormatterConstants.FORMATTER_TAB_SIZE, "noInteger");
7575
});
76-
assertEquals("Invalid indentation configuration not replaced by default value (4 spaces)",
77-
CPP_FORMATTED.replace("\t", " "), output);
76+
assertEquals(CPP_FORMATTED.replace("\t", " "),
77+
output, "Invalid indentation configuration not replaced by default value (4 spaces)");
7878
}
7979

8080
@Test
81-
public void htmlCommentFormat() throws Throwable {
81+
void htmlCommentFormat() throws Throwable {
8282
String output = format(DOXYGEN_HTML + CPP_FORMATTED, config -> {});
83-
assertEquals("HTML comments not ignored by formatter.",
84-
DOXYGEN_HTML + CPP_FORMATTED, output);
83+
assertEquals(DOXYGEN_HTML + CPP_FORMATTED,
84+
output, "HTML comments not ignored by formatter.");
8585
}
8686

8787
@Test
88-
public void regionWarning() throws Throwable {
88+
void regionWarning() throws Throwable {
8989
String output = format(FUNCT_PTR_UNFORMATTED, config -> {});
90-
assertEquals("Code not formatted at all due to regional error.", FUNCT_PTR_FORMATTED, output);
90+
assertEquals(FUNCT_PTR_FORMATTED, output, "Code not formatted at all due to regional error.");
9191
}
9292

9393
private static String format(final String input, final Consumer<Properties> config) throws Exception {

0 commit comments

Comments
 (0)