Skip to content

Commit 7152873

Browse files
KarboniteKreamminwoox
authored andcommitted
Implement support for JUnit 5 (#467)
Resolves #443. This PR implements `CentralDogmaExtension` to support JUnit 5. line/armeria#1736 was used as a base example. #### Changes: - Move logic from `CentralDogmaRule` to `CentralDogmaRuleDelegate` in `testing:common` - Implement `CentralDogmaRule` (`testing:junit4`) and `CentralDogmaExtension` (`testing:junit`) - Migrate some existing tests to JUnit 5 #### Notes: - JUnit 5 doesn't support extensions inside extensions yet, so I couldn't use `@TempDir` for the data directory - `@TempDir` has issues deleting read-only files on Windows at the moment, so some tests cannot be migrated until JUnit 5.6 is released
1 parent bdb4e48 commit 7152873

File tree

36 files changed

+998
-280
lines changed

36 files changed

+998
-280
lines changed

build.gradle

+7-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ ext {
2626
':client:java-spring-boot-autoconfigure': "${rootProject.name}-client-spring-boot-autoconfigure",
2727
':client:java-spring-boot-starter': "${rootProject.name}-client-spring-boot-starter",
2828
':client:java-spring-boot1-autoconfigure': "${rootProject.name}-client-spring-boot1-autoconfigure",
29-
':client:java-spring-boot1-starter': "${rootProject.name}-client-spring-boot1-starter"
29+
':client:java-spring-boot1-starter': "${rootProject.name}-client-spring-boot1-starter",
30+
// Set the correct artifactId of 'testing-common'.
31+
':testing:testing-common': "${rootProject.name}-testing-common"
3032
]
3133
}
3234

@@ -89,6 +91,10 @@ configure(projectsWithFlags('java')) {
8991
testCompile 'org.hamcrest:hamcrest-library'
9092
testCompile 'org.assertj:assertj-core'
9193
testCompile 'org.mockito:mockito-core'
94+
testCompile 'org.junit.jupiter:junit-jupiter-api'
95+
testRuntime 'org.junit.jupiter:junit-jupiter-engine'
96+
testRuntime 'org.junit.platform:junit-platform-launcher'
97+
testRuntime 'org.junit.vintage:junit-vintage-engine'
9298
}
9399

94100
// Target Java 8.

dependencies.yml

+14
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,20 @@ junit:
150150
javadocs:
151151
- https://junit.org/junit4/javadoc/4.12/
152152

153+
org.junit.jupiter:
154+
junit-jupiter-api:
155+
version: &JUNIT_JUPITER_VERSION '5.5.2'
156+
javadocs:
157+
- https://junit.org/junit5/docs/5.5.2/api/
158+
junit-jupiter-engine:
159+
version: *JUNIT_JUPITER_VERSION
160+
org.junit.platform:
161+
junit-platform-launcher:
162+
version: '1.5.2'
163+
org.junit.vintage:
164+
junit-vintage-engine:
165+
version: '5.5.2'
166+
153167
kr.motd.gradle:
154168
sphinx-gradle-plugin: { version: '2.6.1' }
155169

it/src/test/java/com/linecorp/centraldogma/it/AbstractMultiClientTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.junit.runners.Parameterized.Parameters;
2828

2929
import com.linecorp.centraldogma.client.CentralDogma;
30-
import com.linecorp.centraldogma.testing.CentralDogmaRule;
30+
import com.linecorp.centraldogma.testing.junit4.CentralDogmaRule;
3131

3232
@RunWith(Parameterized.class)
3333
public abstract class AbstractMultiClientTest {

it/src/test/java/com/linecorp/centraldogma/it/CacheTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
import com.linecorp.centraldogma.common.PushResult;
3737
import com.linecorp.centraldogma.common.Query;
3838
import com.linecorp.centraldogma.common.Revision;
39-
import com.linecorp.centraldogma.testing.CentralDogmaRule;
39+
import com.linecorp.centraldogma.testing.junit4.CentralDogmaRule;
4040

4141
public class CacheTest extends AbstractMultiClientTest {
4242

it/src/test/java/com/linecorp/centraldogma/it/CentralDogmaEndpointGroupTest.java

+19-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 LINE Corporation
2+
* Copyright 2020 LINE Corporation
33
*
44
* LINE Corporation licenses this file to you under the Apache License,
55
* version 2.0 (the "License"); you may not use this file except in compliance
@@ -25,8 +25,9 @@
2525
import java.util.concurrent.TimeUnit;
2626
import java.util.concurrent.TimeoutException;
2727

28-
import org.junit.Rule;
29-
import org.junit.Test;
28+
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.Timeout;
30+
import org.junit.jupiter.api.extension.RegisterExtension;
3031

3132
import com.fasterxml.jackson.core.JsonProcessingException;
3233
import com.fasterxml.jackson.databind.JsonNode;
@@ -41,9 +42,10 @@
4142
import com.linecorp.centraldogma.common.Change;
4243
import com.linecorp.centraldogma.common.Query;
4344
import com.linecorp.centraldogma.common.Revision;
44-
import com.linecorp.centraldogma.testing.CentralDogmaRule;
45+
import com.linecorp.centraldogma.testing.junit.CentralDogmaExtension;
46+
47+
class CentralDogmaEndpointGroupTest {
4548

46-
public class CentralDogmaEndpointGroupTest {
4749
private static final List<String> HOST_AND_PORT_LIST = ImmutableList.of(
4850
"1.2.3.4:5678",
4951
"centraldogma-sample001.com:1234");
@@ -62,8 +64,8 @@ public class CentralDogmaEndpointGroupTest {
6264
Endpoint.of("1.2.3.4", 5678),
6365
Endpoint.of("centraldogma-sample001.com", 1234));
6466

65-
@Rule
66-
public final CentralDogmaRule dogma = new CentralDogmaRule() {
67+
@RegisterExtension
68+
final CentralDogmaExtension dogma = new CentralDogmaExtension() {
6769
@Override
6870
protected void scaffold(CentralDogma client) {
6971
client.createProject("directory").join();
@@ -78,10 +80,15 @@ protected void scaffold(CentralDogma client) {
7880
String.join("\n", HOST_AND_PORT_LIST)))
7981
.join();
8082
}
83+
84+
@Override
85+
protected boolean runForEachTest() {
86+
return true;
87+
}
8188
};
8289

8390
@Test
84-
public void json() throws Exception {
91+
void json() throws Exception {
8592
try (Watcher<JsonNode> watcher = dogma.client().fileWatcher("directory", "my-service",
8693
Query.ofJson("/endpoint.json"))) {
8794
final CentralDogmaEndpointGroup<JsonNode> endpointGroup = CentralDogmaEndpointGroup.ofWatcher(
@@ -91,8 +98,9 @@ public void json() throws Exception {
9198
}
9299
}
93100

94-
@Test(timeout = 10000)
95-
public void text() throws Exception {
101+
@Test
102+
@Timeout(10)
103+
void text() throws Exception {
96104
try (Watcher<String> watcher = dogma.client().fileWatcher("directory", "my-service",
97105
Query.ofText("/endpoints.txt"))) {
98106
final CountDownLatch latch = new CountDownLatch(2);
@@ -114,7 +122,7 @@ public void text() throws Exception {
114122
}
115123

116124
@Test
117-
public void recoverFromNotFound() throws Exception {
125+
void recoverFromNotFound() throws Exception {
118126
try (Watcher<String> watcher = dogma.client().fileWatcher("directory",
119127
"new-service",
120128
Query.ofText("/endpoints.txt"))) {

it/src/test/java/com/linecorp/centraldogma/it/CentralDogmaRuleWithScaffolding.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import com.linecorp.centraldogma.client.CentralDogma;
2828
import com.linecorp.centraldogma.common.Change;
2929
import com.linecorp.centraldogma.common.Revision;
30-
import com.linecorp.centraldogma.testing.CentralDogmaRule;
30+
import com.linecorp.centraldogma.testing.junit4.CentralDogmaRule;
3131

3232
public class CentralDogmaRuleWithScaffolding extends CentralDogmaRule {
3333

it/src/test/java/com/linecorp/centraldogma/it/ClientType.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import java.util.function.Function;
2020

2121
import com.linecorp.centraldogma.client.CentralDogma;
22-
import com.linecorp.centraldogma.testing.CentralDogmaRule;
22+
import com.linecorp.centraldogma.testing.junit4.CentralDogmaRule;
2323

2424
enum ClientType {
2525
DEFAULT(CentralDogmaRule::client),

it/src/test/java/com/linecorp/centraldogma/it/MergeFileTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
import com.linecorp.centraldogma.common.MergedEntry;
3636
import com.linecorp.centraldogma.common.QueryExecutionException;
3737
import com.linecorp.centraldogma.common.Revision;
38-
import com.linecorp.centraldogma.testing.CentralDogmaRule;
38+
import com.linecorp.centraldogma.testing.junit4.CentralDogmaRule;
3939

4040
public class MergeFileTest extends AbstractMultiClientTest {
4141

it/src/test/java/com/linecorp/centraldogma/it/mirror/git/GitMirrorAuthTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
import com.linecorp.centraldogma.server.CentralDogmaBuilder;
5353
import com.linecorp.centraldogma.server.MirroringService;
5454
import com.linecorp.centraldogma.server.storage.project.Project;
55-
import com.linecorp.centraldogma.testing.CentralDogmaRule;
55+
import com.linecorp.centraldogma.testing.junit4.CentralDogmaRule;
5656

5757
@RunWith(Parameterized.class)
5858
public class GitMirrorAuthTest {

it/src/test/java/com/linecorp/centraldogma/it/mirror/git/GitMirrorTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
import com.linecorp.centraldogma.server.MirrorException;
6262
import com.linecorp.centraldogma.server.MirroringService;
6363
import com.linecorp.centraldogma.server.storage.project.Project;
64-
import com.linecorp.centraldogma.testing.CentralDogmaRule;
64+
import com.linecorp.centraldogma.testing.junit4.CentralDogmaRule;
6565

6666
public class GitMirrorTest {
6767

it/src/test/java/com/linecorp/centraldogma/it/updater/CentralDogmaBeanTest.java

+14-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 LINE Corporation
2+
* Copyright 2020 LINE Corporation
33
*
44
* LINE Corporation licenses this file to you under the Apache License,
55
* version 2.0 (the "License"); you may not use this file except in compliance
@@ -29,9 +29,9 @@
2929
import javax.annotation.Nullable;
3030

3131
import org.awaitility.core.ConditionTimeoutException;
32-
import org.junit.Before;
33-
import org.junit.ClassRule;
34-
import org.junit.Test;
32+
import org.junit.jupiter.api.BeforeEach;
33+
import org.junit.jupiter.api.Test;
34+
import org.junit.jupiter.api.extension.RegisterExtension;
3535

3636
import com.fasterxml.jackson.databind.ObjectMapper;
3737
import com.google.common.collect.ImmutableList;
@@ -43,12 +43,12 @@
4343
import com.linecorp.centraldogma.common.Change;
4444
import com.linecorp.centraldogma.common.PushResult;
4545
import com.linecorp.centraldogma.common.Revision;
46-
import com.linecorp.centraldogma.testing.CentralDogmaRule;
46+
import com.linecorp.centraldogma.testing.junit.CentralDogmaExtension;
4747

48-
public class CentralDogmaBeanTest {
48+
class CentralDogmaBeanTest {
4949

50-
@ClassRule
51-
public static final CentralDogmaRule dogma = new CentralDogmaRule() {
50+
@RegisterExtension
51+
static final CentralDogmaExtension dogma = new CentralDogmaExtension() {
5252
@Override
5353
protected void scaffold(CentralDogma client) {
5454
client.createProject("a").join();
@@ -64,13 +64,13 @@ protected void scaffold(CentralDogma client) {
6464

6565
private CentralDogmaBeanFactory factory;
6666

67-
@Before
68-
public void setup() {
67+
@BeforeEach
68+
void setUp() {
6969
factory = new CentralDogmaBeanFactory(dogma.client(), objectMapper);
7070
}
7171

7272
@Test
73-
public void stayDefault() {
73+
void stayDefault() {
7474
final TestPropertyDefault property = factory.get(new TestPropertyDefault(), TestPropertyDefault.class);
7575

7676
// Delay to detect if data for this bean has already been written to the server.
@@ -82,7 +82,7 @@ public void stayDefault() {
8282
}
8383

8484
@Test
85-
public void test() throws Exception {
85+
void test() {
8686
final int[] called = new int[1];
8787
final Consumer<TestProperty> listener = testProperty -> called[0] = 1;
8888
final CentralDogma client = dogma.client();
@@ -142,7 +142,7 @@ public void test() throws Exception {
142142
}
143143

144144
@Test
145-
public void overrideSettings() throws Exception {
145+
void overrideSettings() {
146146
final CentralDogma client = dogma.client();
147147

148148
client.push("alice", "bob", Revision.HEAD, "Add charlie.json",
@@ -173,7 +173,7 @@ public void overrideSettings() throws Exception {
173173
}
174174

175175
@Test
176-
public void updateListenerIgnoreDefault() {
176+
void updateListenerIgnoreDefault() {
177177
final CentralDogma client = dogma.client();
178178
final AtomicReference<TestProperty> update = new AtomicReference<>();
179179

server-auth/saml/src/test/java/com/linecorp/centraldogma/server/auth/saml/SamlAuthTest.java

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 LINE Corporation
2+
* Copyright 2020 LINE Corporation
33
*
44
* LINE Corporation licenses this file to you under the Apache License,
55
* version 2.0 (the "License"); you may not use this file except in compliance
@@ -17,8 +17,8 @@
1717

1818
import static org.assertj.core.api.Assertions.assertThat;
1919

20-
import org.junit.ClassRule;
21-
import org.junit.Test;
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.extension.RegisterExtension;
2222

2323
import com.linecorp.armeria.common.AggregatedHttpResponse;
2424
import com.linecorp.armeria.common.HttpHeaderNames;
@@ -27,9 +27,9 @@
2727
import com.linecorp.centraldogma.internal.Jackson;
2828
import com.linecorp.centraldogma.server.CentralDogmaBuilder;
2929
import com.linecorp.centraldogma.server.auth.AuthProvider;
30-
import com.linecorp.centraldogma.testing.CentralDogmaRule;
30+
import com.linecorp.centraldogma.testing.junit.CentralDogmaExtension;
3131

32-
public class SamlAuthTest {
32+
class SamlAuthTest {
3333

3434
private static final SamlAuthProperties PROPERTIES;
3535

@@ -60,8 +60,8 @@ public class SamlAuthTest {
6060
}
6161
}
6262

63-
@ClassRule
64-
public static final CentralDogmaRule rule = new CentralDogmaRule() {
63+
@RegisterExtension
64+
static final CentralDogmaExtension dogma = new CentralDogmaExtension() {
6565
@Override
6666
protected void configure(CentralDogmaBuilder builder) {
6767
builder.authProviderFactory(new SamlAuthProviderFactory());
@@ -71,32 +71,32 @@ protected void configure(CentralDogmaBuilder builder) {
7171
};
7272

7373
@Test
74-
public void shouldUseBuiltinWebPageOnlyForLogout() throws Exception {
74+
void shouldUseBuiltinWebPageOnlyForLogout() {
7575
AggregatedHttpResponse resp;
7676

7777
// Receive HTML which submits SAMLRequest to IdP.
78-
resp = rule.httpClient().get(AuthProvider.LOGIN_PATH).aggregate().join();
78+
resp = dogma.httpClient().get(AuthProvider.LOGIN_PATH).aggregate().join();
7979
assertThat(resp.status()).isEqualTo(HttpStatus.OK);
8080
assertThat(resp.headers().contentType()).isEqualTo(MediaType.HTML_UTF_8);
8181
assertThat(resp.contentUtf8()).contains("<input type=\"hidden\" name=\"SAMLRequest\"");
8282

8383
// Redirect to built-in web logout page.
84-
resp = rule.httpClient().get(AuthProvider.LOGOUT_PATH).aggregate().join();
84+
resp = dogma.httpClient().get(AuthProvider.LOGOUT_PATH).aggregate().join();
8585
assertThat(resp.status()).isEqualTo(HttpStatus.MOVED_PERMANENTLY);
8686
assertThat(resp.headers().get(HttpHeaderNames.LOCATION))
8787
.isEqualTo(AuthProvider.BUILTIN_WEB_LOGOUT_PATH);
8888
}
8989

9090
@Test
91-
public void shouldReturnMetadata() {
92-
final AggregatedHttpResponse resp = rule.httpClient().get("/saml/metadata").aggregate().join();
91+
void shouldReturnMetadata() {
92+
final AggregatedHttpResponse resp = dogma.httpClient().get("/saml/metadata").aggregate().join();
9393
assertThat(resp.status()).isEqualTo(HttpStatus.OK);
9494
assertThat(resp.headers().contentType()).isEqualTo(MediaType.parse("application/samlmetadata+xml"));
9595
assertThat(resp.headers().get(HttpHeaderNames.CONTENT_DISPOSITION))
9696
.contains("attachment; filename=\"saml_metadata.xml\"");
9797

9898
// Check ACS URLs for the service provider.
99-
final int port = rule.serverAddress().getPort();
99+
final int port = dogma.serverAddress().getPort();
100100
assertThat(resp.contentUtf8())
101101
.contains("entityID=\"test-sp\"")
102102
.contains("<md:AssertionConsumerService " +

0 commit comments

Comments
 (0)