RATIS-1972. Add junit 5 dependencies in ratis-server. (#1003)
diff --git a/ratis-common/src/test/java/org/apache/ratis/BaseTest.java b/ratis-common/src/test/java/org/apache/ratis/BaseTest.java
index 8213694..fb34d64 100644
--- a/ratis-common/src/test/java/org/apache/ratis/BaseTest.java
+++ b/ratis-common/src/test/java/org/apache/ratis/BaseTest.java
@@ -27,10 +27,13 @@
import org.apache.ratis.util.TimeDuration;
import org.apache.ratis.util.function.CheckedRunnable;
import org.junit.After;
-import org.junit.Assert;
import org.junit.Rule;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.TestInfo;
+import org.junit.jupiter.api.Timeout;
import org.junit.rules.TestName;
-import org.junit.rules.Timeout;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.event.Level;
@@ -47,6 +50,7 @@
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
+@Timeout(value = 100)
public abstract class BaseTest {
public final Logger LOG = LoggerFactory.getLogger(getClass());
@@ -80,7 +84,24 @@
return peersWithPriority;
}
+
+ /*
+ * Junit 4 reference will be removed and the code will be refactored once
+ * all the unit tests are migrated to Junit 5.
+ */
+
+ private String testCaseName;
+
+ @BeforeEach
+ public void setup(TestInfo testInfo) {
+ testCaseName = testInfo.getTestMethod()
+ .orElseThrow(() -> new RuntimeException("Exception while getting test name."))
+ .getName();
+ }
+
+ // @After annotation is retained to support junit 4 tests.
@After
+ @AfterEach
public void assertNoFailures() {
final Throwable e = firstException.get();
if (e != null) {
@@ -90,9 +111,12 @@
ExitUtils.assertNotTerminated();
}
+ // Retained to support junit 4 tests.
@Rule
- public final Timeout globalTimeout = new Timeout(getGlobalTimeoutSeconds(), TimeUnit.SECONDS );
+ public final org.junit.rules.Timeout globalTimeout = new org.junit.rules.Timeout(
+ getGlobalTimeoutSeconds(), TimeUnit.SECONDS );
+ // Retained to support junit 4 tests.
@Rule
public final TestName testName = new TestName();
@@ -122,7 +146,9 @@
}
public File getTestDir() {
- return new File(getClassTestDir(), testName.getMethodName());
+ // This will work for both junit 4 and 5.
+ final String name = testCaseName != null ? testCaseName : testName.getMethodName();
+ return new File(getClassTestDir(), name);
}
@SafeVarargs
@@ -135,13 +161,13 @@
description, expectedThrowableClass.getSimpleName(),
StringUtils.array2String(expectedCauseClasses, Class::getSimpleName));
}
- Assert.assertEquals(expectedThrowableClass, t.getClass());
+ Assertions.assertEquals(expectedThrowableClass, t.getClass());
for (Class<? extends Throwable> expectedCause : expectedCauseClasses) {
final Throwable previous = t;
t = Objects.requireNonNull(previous.getCause(),
() -> "previous.getCause() == null for previous=" + previous);
- Assert.assertEquals(expectedCause, t.getClass());
+ Assertions.assertEquals(expectedCause, t.getClass());
}
}
diff --git a/ratis-examples/pom.xml b/ratis-examples/pom.xml
index 4078f62..54691d6 100644
--- a/ratis-examples/pom.xml
+++ b/ratis-examples/pom.xml
@@ -122,6 +122,11 @@
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-api</artifactId>
+ <scope>test</scope>
+ </dependency>
</dependencies>
<build>
<plugins>
diff --git a/ratis-server/pom.xml b/ratis-server/pom.xml
index 3756e3e..38af72d 100644
--- a/ratis-server/pom.xml
+++ b/ratis-server/pom.xml
@@ -71,6 +71,31 @@
<scope>test</scope>
</dependency>
<dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-api</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-engine</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.vintage</groupId>
+ <artifactId>junit-vintage-engine</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.platform</groupId>
+ <artifactId>junit-platform-launcher</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-params</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>
diff --git a/ratis-server/src/test/java/org/apache/ratis/server/impl/TestLogAppenderMetrics.java b/ratis-server/src/test/java/org/apache/ratis/server/impl/TestLogAppenderMetrics.java
index 5c78db4..4bd075e 100644
--- a/ratis-server/src/test/java/org/apache/ratis/server/impl/TestLogAppenderMetrics.java
+++ b/ratis-server/src/test/java/org/apache/ratis/server/impl/TestLogAppenderMetrics.java
@@ -27,11 +27,11 @@
import org.apache.ratis.protocol.RaftPeerId;
import org.apache.ratis.server.metrics.LogAppenderMetrics;
import org.apache.ratis.util.Timestamp;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
import org.apache.ratis.thirdparty.com.codahale.metrics.Gauge;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
public class TestLogAppenderMetrics {
@@ -39,7 +39,7 @@
private RaftPeerId raftPeerId;
private MyFollowerInfo followerInfo;
- @Before
+ @BeforeEach
public void setup() {
RaftGroupId raftGroupId = RaftGroupId.randomId();
raftPeerId = RaftPeerId.valueOf("TestId");
@@ -55,19 +55,19 @@
public void testLogAppenderGauges() {
Gauge nextIndex = ratisMetricRegistry.getGauges((s, metric) ->
s.contains(String.format(FOLLOWER_NEXT_INDEX, raftPeerId.toString()))).values().iterator().next();
- Assert.assertEquals(100L, nextIndex.getValue());
+ Assertions.assertEquals(100L, nextIndex.getValue());
Gauge matchIndex = ratisMetricRegistry.getGauges((s, metric) ->
s.contains(String.format(FOLLOWER_MATCH_INDEX, raftPeerId.toString()))).values().iterator().next();
- Assert.assertEquals(0L, matchIndex.getValue());
+ Assertions.assertEquals(0L, matchIndex.getValue());
Gauge rpcTime = ratisMetricRegistry.getGauges((s, metric) ->
s.contains(String.format(FOLLOWER_RPC_RESP_TIME, raftPeerId.toString()))).values().iterator().next();
- Assert.assertTrue(((Long) rpcTime.getValue()) > 0);
+ Assertions.assertTrue(((Long) rpcTime.getValue()) > 0);
followerInfo.updateNextIndex(200L);
followerInfo.updateMatchIndex(100L);
followerInfo.updateLastRpcResponseTime();
- Assert.assertEquals(200L, nextIndex.getValue());
- Assert.assertEquals(100L, matchIndex.getValue());
- Assert.assertNotNull(rpcTime.getValue());
+ Assertions.assertEquals(200L, nextIndex.getValue());
+ Assertions.assertEquals(100L, matchIndex.getValue());
+ Assertions.assertNotNull(rpcTime.getValue());
}
private static class MyFollowerInfo {
diff --git a/ratis-server/src/test/java/org/apache/ratis/server/impl/TestRetryCacheMetrics.java b/ratis-server/src/test/java/org/apache/ratis/server/impl/TestRetryCacheMetrics.java
index b25a50b..0d779a2 100644
--- a/ratis-server/src/test/java/org/apache/ratis/server/impl/TestRetryCacheMetrics.java
+++ b/ratis-server/src/test/java/org/apache/ratis/server/impl/TestRetryCacheMetrics.java
@@ -19,7 +19,7 @@
package org.apache.ratis.server.impl;
import static org.apache.ratis.server.metrics.RaftServerMetricsImpl.*;
-import static org.junit.Assert.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import org.apache.ratis.metrics.impl.RatisMetricRegistryImpl;
import org.apache.ratis.thirdparty.com.codahale.metrics.Gauge;
@@ -30,9 +30,9 @@
import org.apache.ratis.protocol.RaftPeerId;
import org.apache.ratis.server.RaftServerConfigKeys;
import org.apache.ratis.server.metrics.RaftServerMetricsImpl;
-import org.junit.After;
-import org.junit.BeforeClass;
-import org.junit.Test;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
import java.util.Map;
@@ -43,7 +43,7 @@
private static RatisMetricRegistryImpl ratisMetricRegistry;
private static RetryCacheImpl retryCache;
- @BeforeClass
+ @BeforeAll
public static void setUp() {
RaftGroupId raftGroupId = RaftGroupId.randomId();
RaftPeerId raftPeerId = RaftPeerId.valueOf("TestId");
@@ -56,7 +56,7 @@
ratisMetricRegistry = (RatisMetricRegistryImpl) raftServerMetrics.getRegistry();
}
- @After
+ @AfterEach
public void tearDown() {
retryCache.close();
checkEntryCount(0);
diff --git a/ratis-server/src/test/java/org/apache/ratis/server/metrics/TestLeaderElectionMetrics.java b/ratis-server/src/test/java/org/apache/ratis/server/metrics/TestLeaderElectionMetrics.java
index a39612c..bba5d92 100644
--- a/ratis-server/src/test/java/org/apache/ratis/server/metrics/TestLeaderElectionMetrics.java
+++ b/ratis-server/src/test/java/org/apache/ratis/server/metrics/TestLeaderElectionMetrics.java
@@ -20,8 +20,8 @@
import static org.apache.ratis.server.metrics.LeaderElectionMetrics.LAST_LEADER_ELECTION_ELAPSED_TIME;
import static org.apache.ratis.server.metrics.LeaderElectionMetrics.LEADER_ELECTION_TIMEOUT_COUNT_METRIC;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import org.apache.ratis.metrics.impl.RatisMetricRegistryImpl;
import org.apache.ratis.thirdparty.com.codahale.metrics.Gauge;
@@ -29,8 +29,8 @@
import org.apache.ratis.protocol.RaftGroupId;
import org.apache.ratis.protocol.RaftGroupMemberId;
import org.apache.ratis.protocol.RaftPeerId;
-import org.junit.BeforeClass;
-import org.junit.Test;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
import java.util.SortedMap;
@@ -42,7 +42,7 @@
private static LeaderElectionMetrics leaderElectionMetrics;
private static RatisMetricRegistryImpl ratisMetricRegistry;
- @BeforeClass
+ @BeforeAll
public static void setUp() {
RaftGroupId raftGroupId = RaftGroupId.randomId();
RaftPeerId raftPeerId = RaftPeerId.valueOf("TestId");
@@ -58,14 +58,14 @@
(s, metric) -> s.contains(LAST_LEADER_ELECTION_ELAPSED_TIME));
LOG.info("{} gauges: {}", LAST_LEADER_ELECTION_ELAPSED_TIME, gauges);
final Long leaderElectionLatency = (Long)gauges.values().iterator().next().getValue();
- assertTrue("leaderElectionLatency = " + leaderElectionLatency, leaderElectionLatency >= 0L);
+ assertTrue(leaderElectionLatency >= 0L, "leaderElectionLatency = " + leaderElectionLatency);
}
@Test
public void testOnLeaderElectionTimeout() throws Exception {
long numLeaderElectionTimeouts = ratisMetricRegistry.counter(
LEADER_ELECTION_TIMEOUT_COUNT_METRIC).getCount();
- assertTrue(numLeaderElectionTimeouts == 0);
+ assertEquals(0, numLeaderElectionTimeouts);
leaderElectionMetrics.onLeaderElectionTimeout();
numLeaderElectionTimeouts = ratisMetricRegistry.counter(LEADER_ELECTION_TIMEOUT_COUNT_METRIC).getCount();
assertEquals(1, numLeaderElectionTimeouts);
diff --git a/ratis-test/pom.xml b/ratis-test/pom.xml
index 40b6b2e..2021e06 100644
--- a/ratis-test/pom.xml
+++ b/ratis-test/pom.xml
@@ -137,6 +137,11 @@
<scope>test</scope>
</dependency>
<dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-api</artifactId>
+ <scope>test</scope>
+ </dependency>
+ <dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<scope>test</scope>