Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

migrate from JUnit 4 to JUnit 5 #1338

Merged
merged 8 commits into from
Oct 14, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ subprojects {
ext {
libraries = [
guava: "com.google.guava:guava:31.1-jre",
junit: "junit:junit:4.13.2",
jupiterApi: 'org.junit.jupiter:junit-jupiter-api:5.+',
jupiterEngine: 'org.junit.jupiter:junit-jupiter-engine:5.+',
jupiterMockito: 'org.mockito:mockito-junit-jupiter:4.+',
mockito: 'org.mockito:mockito-core:4.+',
slf4j: "org.slf4j:slf4j-api:1.7.36",
truth: 'com.google.truth:truth:1.1.3',
Expand All @@ -74,6 +76,7 @@ subprojects {
}

test {
useJUnitPlatform()
testLogging {
showStandardStreams = true
}
Expand Down
2 changes: 1 addition & 1 deletion zuul-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ dependencies {
implementation 'io.perfmark:perfmark-api:0.25.0'
implementation 'javax.inject:javax.inject:1'

testImplementation libraries.junit,
testImplementation libraries.jupiterApi, libraries.jupiterEngine, libraries.jupiterMockito,
libraries.mockito,
libraries.truth,
libraries.awaitility
Expand Down
36 changes: 27 additions & 9 deletions zuul-core/dependencies.lock
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,6 @@
"javax.inject:javax.inject": {
"locked": "1"
},
"junit:junit": {
"locked": "4.13.2"
},
"org.awaitility:awaitility": {
"locked": "4.2.0"
},
Expand All @@ -303,9 +300,18 @@
"org.bouncycastle:bcprov-jdk15on": {
"locked": "1.70"
},
"org.junit.jupiter:junit-jupiter-api": {
"locked": "5.9.1"
},
"org.junit.jupiter:junit-jupiter-engine": {
"locked": "5.9.1"
},
"org.mockito:mockito-core": {
"locked": "4.8.0"
},
"org.mockito:mockito-junit-jupiter": {
"locked": "4.8.0"
},
"org.openjdk.jmh:jmh-core": {
"locked": "1.35"
},
Expand Down Expand Up @@ -517,9 +523,6 @@
"javax.inject:javax.inject": {
"locked": "1"
},
"junit:junit": {
"locked": "4.13.2"
},
"org.awaitility:awaitility": {
"locked": "4.2.0"
},
Expand All @@ -529,9 +532,18 @@
"org.bouncycastle:bcprov-jdk15on": {
"locked": "1.70"
},
"org.junit.jupiter:junit-jupiter-api": {
"locked": "5.9.1"
},
"org.junit.jupiter:junit-jupiter-engine": {
"locked": "5.9.1"
},
"org.mockito:mockito-core": {
"locked": "4.8.0"
},
"org.mockito:mockito-junit-jupiter": {
"locked": "4.8.0"
},
"org.slf4j:slf4j-api": {
"locked": "1.7.36"
}
Expand Down Expand Up @@ -642,9 +654,6 @@
"javax.inject:javax.inject": {
"locked": "1"
},
"junit:junit": {
"locked": "4.13.2"
},
"org.awaitility:awaitility": {
"locked": "4.2.0"
},
Expand All @@ -654,9 +663,18 @@
"org.bouncycastle:bcprov-jdk15on": {
"locked": "1.70"
},
"org.junit.jupiter:junit-jupiter-api": {
"locked": "5.9.1"
},
"org.junit.jupiter:junit-jupiter-engine": {
"locked": "5.9.1"
},
"org.mockito:mockito-core": {
"locked": "4.8.0"
},
"org.mockito:mockito-junit-jupiter": {
"locked": "4.8.0"
},
"org.slf4j:slf4j-api": {
"firstLevelTransitive": [
"com.netflix.zuul:zuul-discovery"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,32 +17,30 @@
package com.netflix.netty.common;

import static io.netty.handler.timeout.IdleStateEvent.ALL_IDLE_STATE_EVENT;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import com.netflix.spectator.api.Counter;
import com.netflix.spectator.api.DefaultRegistry;
import com.netflix.spectator.api.Id;
import com.netflix.spectator.api.Registry;
import com.netflix.zuul.netty.server.http2.DummyChannelHandler;
import io.netty.channel.embedded.EmbeddedChannel;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

@RunWith(JUnit4.class)
public class CloseOnIdleStateHandlerTest {

private Registry registry = new DefaultRegistry();
private Id counterId;
private final String listener = "test-idle-state";

@Before
@BeforeEach
public void setup() {
counterId = registry.createId("server.connections.idle.timeout").withTags("id", listener);
}

@Test
public void incrementCounterOnIdleStateEvent() {
void incrementCounterOnIdleStateEvent() {
final EmbeddedChannel channel = new EmbeddedChannel();
channel.pipeline().addLast(new DummyChannelHandler());
channel.pipeline().addLast(new CloseOnIdleStateHandler(registry, listener));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.netflix.netty.common;

import static com.netflix.netty.common.HttpLifecycleChannelHandler.ATTR_HTTP_PIPELINE_REJECT;
import static org.junit.Assert.fail;
import com.google.common.truth.Truth;
import com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteEvent;
import com.netflix.netty.common.HttpLifecycleChannelHandler.CompleteReason;
Expand All @@ -34,7 +33,7 @@
import io.netty.handler.codec.http.HttpMethod;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.ReferenceCountUtil;
import org.junit.Test;
import org.junit.jupiter.api.Test;

public class HttpServerLifecycleChannelHandlerTest {

Expand All @@ -54,7 +53,7 @@ public CompleteEvent getCompleteEvent() {
}

@Test
public void completionEventReasonIsUpdatedOnPipelineReject() {
void completionEventReasonIsUpdatedOnPipelineReject() {

final EmbeddedChannel channel = new EmbeddedChannel(new HttpServerLifecycleOutboundChannelHandler());
final AssertReasonHandler reasonHandler = new AssertReasonHandler();
Expand All @@ -70,7 +69,7 @@ public void completionEventReasonIsUpdatedOnPipelineReject() {
}

@Test
public void completionEventReasonIsCloseByDefault() {
void completionEventReasonIsCloseByDefault() {

final EmbeddedChannel channel = new EmbeddedChannel(new HttpServerLifecycleOutboundChannelHandler());
final AssertReasonHandler reasonHandler = new AssertReasonHandler();
Expand All @@ -84,7 +83,7 @@ public void completionEventReasonIsCloseByDefault() {
}

@Test
public void pipelineRejectReleasesIfNeeded() {
void pipelineRejectReleasesIfNeeded() {

EmbeddedChannel channel = new EmbeddedChannel(new HttpServerLifecycleInboundChannelHandler());

Expand All @@ -99,7 +98,7 @@ public void pipelineRejectReleasesIfNeeded() {
Truth.assertThat(channel.attr(ATTR_HTTP_PIPELINE_REJECT).get()).isEqualTo(Boolean.TRUE);
Truth.assertThat(buffer.refCnt()).isEqualTo(0);
} finally {
if(buffer.refCnt() != 0) {
if (buffer.refCnt() != 0) {
ReferenceCountUtil.release(buffer);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@

package com.netflix.netty.common;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.*;

import java.net.Inet4Address;
import java.net.Inet6Address;
Expand All @@ -29,22 +27,19 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.junit.Assume;
import org.junit.AssumptionViolatedException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.Test;

/**
* Unit tests for {@link SourceAddressChannelHandler}.
*/
@RunWith(JUnit4.class)
public class SourceAddressChannelHandlerTest {

@Test
public void ipv6AddressScopeIdRemoved() throws Exception {
void ipv6AddressScopeIdRemoved() throws Exception {
Inet6Address address =
Inet6Address.getByAddress("localhost", new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 2);
Inet6Address.getByAddress("localhost", new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, 2);
assertEquals(2, address.getScopeId());

String addressString = SourceAddressChannelHandler.getHostAddress(new InetSocketAddress(address, 8080));
Expand All @@ -53,16 +48,16 @@ public void ipv6AddressScopeIdRemoved() throws Exception {
}

@Test
public void ipv4AddressString() throws Exception {
InetAddress address = Inet4Address.getByAddress("localhost", new byte[] {127, 0, 0, 1});
void ipv4AddressString() throws Exception {
InetAddress address = Inet4Address.getByAddress("localhost", new byte[]{127, 0, 0, 1});

String addressString = SourceAddressChannelHandler.getHostAddress(new InetSocketAddress(address, 8080));

assertEquals("127.0.0.1", addressString);
}

@Test
public void failsOnUnresolved() {
void failsOnUnresolved() {
InetSocketAddress address = InetSocketAddress.createUnresolved("localhost", 8080);

String addressString = SourceAddressChannelHandler.getHostAddress(address);
Expand All @@ -71,11 +66,11 @@ public void failsOnUnresolved() {
}

@Test
public void mapsIpv4AddressFromIpv6Address() throws Exception {
void mapsIpv4AddressFromIpv6Address() throws Exception {
// Can't think of a reason why this would ever come up, but testing it just in case.
// ::ffff:127.0.0.1
Inet6Address address = Inet6Address.getByAddress(
"localhost", new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte) 0xFF, (byte) 0xFF, 127, 0, 0, 1}, -1);
"localhost", new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, (byte) 0xFF, (byte) 0xFF, 127, 0, 0, 1}, -1);
assertEquals(0, address.getScopeId());

String addressString = SourceAddressChannelHandler.getHostAddress(new InetSocketAddress(address, 8080));
Expand All @@ -84,24 +79,24 @@ public void mapsIpv4AddressFromIpv6Address() throws Exception {
}

@Test
public void ipv6AddressScopeNameRemoved() throws Exception {
void ipv6AddressScopeNameRemoved() throws Exception {
List<NetworkInterface> nics = Collections.list(NetworkInterface.getNetworkInterfaces());
Assume.assumeTrue("No network interfaces", !nics.isEmpty());
Assumptions.assumeTrue(!nics.isEmpty(), "No network interfaces");


List<Throwable> failures = new ArrayList<>();
for (NetworkInterface nic : nics) {
Inet6Address address;
try {
address = Inet6Address.getByAddress(
"localhost", new byte[] {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, nic);
"localhost", new byte[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, nic);
} catch (UnknownHostException e) {
// skip, the nic doesn't match
failures.add(e);
continue;
}

assertTrue(address.toString(), address.toString().contains("%"));
assertTrue(address.toString().contains("%"), address.toString());

String addressString = SourceAddressChannelHandler.getHostAddress(new InetSocketAddress(address, 8080));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@

package com.netflix.netty.common.metrics;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;

import io.netty.buffer.ByteBuf;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.junit.MockitoJUnitRunner;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class InstrumentedResourceLeakDetectorTest {

InstrumentedResourceLeakDetector<Object> leakDetector;

@Before
@BeforeEach
public void setup() {
leakDetector = new InstrumentedResourceLeakDetector<>(ByteBuf.class, 1);
}

@Test
public void test() {
void test() {
leakDetector.reportTracedLeak("test", "test");
assertEquals(leakDetector.leakCounter.get(), 1);

Expand Down
Loading