Skip to content

Commit

Permalink
Implement support for JUnit 5 (#467)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
KarboniteKream authored and minwoox committed Jan 14, 2020
1 parent bdb4e48 commit 7152873
Show file tree
Hide file tree
Showing 36 changed files with 998 additions and 280 deletions.
8 changes: 7 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ ext {
':client:java-spring-boot-autoconfigure': "${rootProject.name}-client-spring-boot-autoconfigure",
':client:java-spring-boot-starter': "${rootProject.name}-client-spring-boot-starter",
':client:java-spring-boot1-autoconfigure': "${rootProject.name}-client-spring-boot1-autoconfigure",
':client:java-spring-boot1-starter': "${rootProject.name}-client-spring-boot1-starter"
':client:java-spring-boot1-starter': "${rootProject.name}-client-spring-boot1-starter",
// Set the correct artifactId of 'testing-common'.
':testing:testing-common': "${rootProject.name}-testing-common"
]
}

Expand Down Expand Up @@ -89,6 +91,10 @@ configure(projectsWithFlags('java')) {
testCompile 'org.hamcrest:hamcrest-library'
testCompile 'org.assertj:assertj-core'
testCompile 'org.mockito:mockito-core'
testCompile 'org.junit.jupiter:junit-jupiter-api'
testRuntime 'org.junit.jupiter:junit-jupiter-engine'
testRuntime 'org.junit.platform:junit-platform-launcher'
testRuntime 'org.junit.vintage:junit-vintage-engine'
}

// Target Java 8.
Expand Down
14 changes: 14 additions & 0 deletions dependencies.yml
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,20 @@ junit:
javadocs:
- https://junit.org/junit4/javadoc/4.12/

org.junit.jupiter:
junit-jupiter-api:
version: &JUNIT_JUPITER_VERSION '5.5.2'
javadocs:
- https://junit.org/junit5/docs/5.5.2/api/
junit-jupiter-engine:
version: *JUNIT_JUPITER_VERSION
org.junit.platform:
junit-platform-launcher:
version: '1.5.2'
org.junit.vintage:
junit-vintage-engine:
version: '5.5.2'

kr.motd.gradle:
sphinx-gradle-plugin: { version: '2.6.1' }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import org.junit.runners.Parameterized.Parameters;

import com.linecorp.centraldogma.client.CentralDogma;
import com.linecorp.centraldogma.testing.CentralDogmaRule;
import com.linecorp.centraldogma.testing.junit4.CentralDogmaRule;

@RunWith(Parameterized.class)
public abstract class AbstractMultiClientTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
import com.linecorp.centraldogma.common.PushResult;
import com.linecorp.centraldogma.common.Query;
import com.linecorp.centraldogma.common.Revision;
import com.linecorp.centraldogma.testing.CentralDogmaRule;
import com.linecorp.centraldogma.testing.junit4.CentralDogmaRule;

public class CacheTest extends AbstractMultiClientTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 LINE Corporation
* Copyright 2020 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
Expand All @@ -25,8 +25,9 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import org.junit.Rule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.api.extension.RegisterExtension;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
Expand All @@ -41,9 +42,10 @@
import com.linecorp.centraldogma.common.Change;
import com.linecorp.centraldogma.common.Query;
import com.linecorp.centraldogma.common.Revision;
import com.linecorp.centraldogma.testing.CentralDogmaRule;
import com.linecorp.centraldogma.testing.junit.CentralDogmaExtension;

class CentralDogmaEndpointGroupTest {

public class CentralDogmaEndpointGroupTest {
private static final List<String> HOST_AND_PORT_LIST = ImmutableList.of(
"1.2.3.4:5678",
"centraldogma-sample001.com:1234");
Expand All @@ -62,8 +64,8 @@ public class CentralDogmaEndpointGroupTest {
Endpoint.of("1.2.3.4", 5678),
Endpoint.of("centraldogma-sample001.com", 1234));

@Rule
public final CentralDogmaRule dogma = new CentralDogmaRule() {
@RegisterExtension
final CentralDogmaExtension dogma = new CentralDogmaExtension() {
@Override
protected void scaffold(CentralDogma client) {
client.createProject("directory").join();
Expand All @@ -78,10 +80,15 @@ protected void scaffold(CentralDogma client) {
String.join("\n", HOST_AND_PORT_LIST)))
.join();
}

@Override
protected boolean runForEachTest() {
return true;
}
};

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

@Test(timeout = 10000)
public void text() throws Exception {
@Test
@Timeout(10)
void text() throws Exception {
try (Watcher<String> watcher = dogma.client().fileWatcher("directory", "my-service",
Query.ofText("/endpoints.txt"))) {
final CountDownLatch latch = new CountDownLatch(2);
Expand All @@ -114,7 +122,7 @@ public void text() throws Exception {
}

@Test
public void recoverFromNotFound() throws Exception {
void recoverFromNotFound() throws Exception {
try (Watcher<String> watcher = dogma.client().fileWatcher("directory",
"new-service",
Query.ofText("/endpoints.txt"))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.linecorp.centraldogma.client.CentralDogma;
import com.linecorp.centraldogma.common.Change;
import com.linecorp.centraldogma.common.Revision;
import com.linecorp.centraldogma.testing.CentralDogmaRule;
import com.linecorp.centraldogma.testing.junit4.CentralDogmaRule;

public class CentralDogmaRuleWithScaffolding extends CentralDogmaRule {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.function.Function;

import com.linecorp.centraldogma.client.CentralDogma;
import com.linecorp.centraldogma.testing.CentralDogmaRule;
import com.linecorp.centraldogma.testing.junit4.CentralDogmaRule;

enum ClientType {
DEFAULT(CentralDogmaRule::client),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import com.linecorp.centraldogma.common.MergedEntry;
import com.linecorp.centraldogma.common.QueryExecutionException;
import com.linecorp.centraldogma.common.Revision;
import com.linecorp.centraldogma.testing.CentralDogmaRule;
import com.linecorp.centraldogma.testing.junit4.CentralDogmaRule;

public class MergeFileTest extends AbstractMultiClientTest {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
import com.linecorp.centraldogma.server.CentralDogmaBuilder;
import com.linecorp.centraldogma.server.MirroringService;
import com.linecorp.centraldogma.server.storage.project.Project;
import com.linecorp.centraldogma.testing.CentralDogmaRule;
import com.linecorp.centraldogma.testing.junit4.CentralDogmaRule;

@RunWith(Parameterized.class)
public class GitMirrorAuthTest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
import com.linecorp.centraldogma.server.MirrorException;
import com.linecorp.centraldogma.server.MirroringService;
import com.linecorp.centraldogma.server.storage.project.Project;
import com.linecorp.centraldogma.testing.CentralDogmaRule;
import com.linecorp.centraldogma.testing.junit4.CentralDogmaRule;

public class GitMirrorTest {

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017 LINE Corporation
* Copyright 2020 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
Expand Down Expand Up @@ -29,9 +29,9 @@
import javax.annotation.Nullable;

import org.awaitility.core.ConditionTimeoutException;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.ImmutableList;
Expand All @@ -43,12 +43,12 @@
import com.linecorp.centraldogma.common.Change;
import com.linecorp.centraldogma.common.PushResult;
import com.linecorp.centraldogma.common.Revision;
import com.linecorp.centraldogma.testing.CentralDogmaRule;
import com.linecorp.centraldogma.testing.junit.CentralDogmaExtension;

public class CentralDogmaBeanTest {
class CentralDogmaBeanTest {

@ClassRule
public static final CentralDogmaRule dogma = new CentralDogmaRule() {
@RegisterExtension
static final CentralDogmaExtension dogma = new CentralDogmaExtension() {
@Override
protected void scaffold(CentralDogma client) {
client.createProject("a").join();
Expand All @@ -64,13 +64,13 @@ protected void scaffold(CentralDogma client) {

private CentralDogmaBeanFactory factory;

@Before
public void setup() {
@BeforeEach
void setUp() {
factory = new CentralDogmaBeanFactory(dogma.client(), objectMapper);
}

@Test
public void stayDefault() {
void stayDefault() {
final TestPropertyDefault property = factory.get(new TestPropertyDefault(), TestPropertyDefault.class);

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

@Test
public void test() throws Exception {
void test() {
final int[] called = new int[1];
final Consumer<TestProperty> listener = testProperty -> called[0] = 1;
final CentralDogma client = dogma.client();
Expand Down Expand Up @@ -142,7 +142,7 @@ public void test() throws Exception {
}

@Test
public void overrideSettings() throws Exception {
void overrideSettings() {
final CentralDogma client = dogma.client();

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

@Test
public void updateListenerIgnoreDefault() {
void updateListenerIgnoreDefault() {
final CentralDogma client = dogma.client();
final AtomicReference<TestProperty> update = new AtomicReference<>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 LINE Corporation
* Copyright 2020 LINE Corporation
*
* LINE Corporation licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
Expand All @@ -17,8 +17,8 @@

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

import org.junit.ClassRule;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import com.linecorp.armeria.common.AggregatedHttpResponse;
import com.linecorp.armeria.common.HttpHeaderNames;
Expand All @@ -27,9 +27,9 @@
import com.linecorp.centraldogma.internal.Jackson;
import com.linecorp.centraldogma.server.CentralDogmaBuilder;
import com.linecorp.centraldogma.server.auth.AuthProvider;
import com.linecorp.centraldogma.testing.CentralDogmaRule;
import com.linecorp.centraldogma.testing.junit.CentralDogmaExtension;

public class SamlAuthTest {
class SamlAuthTest {

private static final SamlAuthProperties PROPERTIES;

Expand Down Expand Up @@ -60,8 +60,8 @@ public class SamlAuthTest {
}
}

@ClassRule
public static final CentralDogmaRule rule = new CentralDogmaRule() {
@RegisterExtension
static final CentralDogmaExtension dogma = new CentralDogmaExtension() {
@Override
protected void configure(CentralDogmaBuilder builder) {
builder.authProviderFactory(new SamlAuthProviderFactory());
Expand All @@ -71,32 +71,32 @@ protected void configure(CentralDogmaBuilder builder) {
};

@Test
public void shouldUseBuiltinWebPageOnlyForLogout() throws Exception {
void shouldUseBuiltinWebPageOnlyForLogout() {
AggregatedHttpResponse resp;

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

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

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

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

0 comments on commit 7152873

Please sign in to comment.