Skip to content

Replace ExpectedException #12103

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

Merged
merged 3 commits into from
May 30, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
92 changes: 44 additions & 48 deletions netty/src/test/java/io/grpc/netty/NettyChannelBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;

Expand All @@ -39,17 +40,13 @@
import java.net.SocketAddress;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLException;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class NettyChannelBuilderTest {

@SuppressWarnings("deprecation") // https://github.com/grpc/grpc-java/issues/7467
@Rule public final ExpectedException thrown = ExpectedException.none();
private final SslContext noSslContext = null;

private void shutdown(ManagedChannel mc) throws Exception {
Expand Down Expand Up @@ -107,10 +104,9 @@ private void overrideAuthorityIsReadableHelper(NettyChannelBuilder builder,
public void failOverrideInvalidAuthority() {
NettyChannelBuilder builder = new NettyChannelBuilder(getTestSocketAddress());

thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Invalid authority:");

builder.overrideAuthority("[invalidauthority");
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
() -> builder.overrideAuthority("[invalidauthority"));
assertThat(e).hasMessageThat().isEqualTo("Invalid authority: [invalidauthority");
}

@Test
Expand All @@ -128,20 +124,18 @@ public void enableCheckAuthorityFailOverrideInvalidAuthority() {
NettyChannelBuilder builder = new NettyChannelBuilder(getTestSocketAddress())
.disableCheckAuthority()
.enableCheckAuthority();

thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Invalid authority:");
builder.overrideAuthority("[invalidauthority");
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
() -> builder.overrideAuthority("[invalidauthority"));
assertThat(e).hasMessageThat().isEqualTo("Invalid authority: [invalidauthority");
}

@Test
public void failInvalidAuthority() {
thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Invalid host or port");

@SuppressWarnings("AddressSelection") // We actually expect zero addresses!
Object unused =
NettyChannelBuilder.forAddress(new InetSocketAddress("invalid_authority", 1234));
InetSocketAddress address = new InetSocketAddress("invalid_authority", 1234);
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
() -> NettyChannelBuilder.forAddress(address));
assertThat(e).hasMessageThat().isEqualTo("Invalid host or port: invalid_authority 1234");
}

@Test
Expand All @@ -155,32 +149,32 @@ public void failIfSslContextIsNotClient() {
SslContext sslContext = mock(SslContext.class);
NettyChannelBuilder builder = new NettyChannelBuilder(getTestSocketAddress());

thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("Server SSL context can not be used for client channel");

builder.sslContext(sslContext);
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
() -> builder.sslContext(sslContext));
assertThat(e).hasMessageThat()
.isEqualTo("Server SSL context can not be used for client channel");
}

@Test
public void failNegotiationTypeWithChannelCredentials_target() {
NettyChannelBuilder builder = NettyChannelBuilder.forTarget(
"fakeTarget", InsecureChannelCredentials.create());

thrown.expect(IllegalStateException.class);
thrown.expectMessage("Cannot change security when using ChannelCredentials");

builder.negotiationType(NegotiationType.TLS);
IllegalStateException e = assertThrows(IllegalStateException.class,
() -> builder.negotiationType(NegotiationType.TLS));
assertThat(e).hasMessageThat()
.isEqualTo("Cannot change security when using ChannelCredentials");
}

@Test
public void failNegotiationTypeWithChannelCredentials_socketAddress() {
NettyChannelBuilder builder = NettyChannelBuilder.forAddress(
getTestSocketAddress(), InsecureChannelCredentials.create());

thrown.expect(IllegalStateException.class);
thrown.expectMessage("Cannot change security when using ChannelCredentials");

builder.negotiationType(NegotiationType.TLS);
IllegalStateException e = assertThrows(IllegalStateException.class,
() -> builder.negotiationType(NegotiationType.TLS));
assertThat(e).hasMessageThat()
.isEqualTo("Cannot change security when using ChannelCredentials");
}

@Test
Expand All @@ -205,10 +199,9 @@ public void createProtocolNegotiatorByType_plaintextUpgrade() {

@Test
public void createProtocolNegotiatorByType_tlsWithNoContext() {
thrown.expect(NullPointerException.class);
NettyChannelBuilder.createProtocolNegotiatorByType(
NegotiationType.TLS,
noSslContext, null);
assertThrows(NullPointerException.class,
() -> NettyChannelBuilder.createProtocolNegotiatorByType(
NegotiationType.TLS, noSslContext, null));
}

@Test
Expand Down Expand Up @@ -245,38 +238,40 @@ public void createProtocolNegotiatorByType_tlsWithAuthorityFallback() throws SSL
public void negativeKeepAliveTime() {
NettyChannelBuilder builder = NettyChannelBuilder.forTarget("fakeTarget");

thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("keepalive time must be positive");
builder.keepAliveTime(-1L, TimeUnit.HOURS);
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
() -> builder.keepAliveTime(-1L, TimeUnit.HOURS));
assertThat(e).hasMessageThat().isEqualTo("keepalive time must be positive");
}

@Test
public void negativeKeepAliveTimeout() {
NettyChannelBuilder builder = NettyChannelBuilder.forTarget("fakeTarget");

thrown.expect(IllegalArgumentException.class);
thrown.expectMessage("keepalive timeout must be positive");
builder.keepAliveTimeout(-1L, TimeUnit.HOURS);
IllegalArgumentException e = assertThrows(IllegalArgumentException.class,
() -> builder.keepAliveTimeout(-1L, TimeUnit.HOURS));
assertThat(e).hasMessageThat().isEqualTo("keepalive timeout must be positive");
}

@Test
public void assertEventLoopAndChannelType_onlyGroupProvided() {
NettyChannelBuilder builder = NettyChannelBuilder.forTarget("fakeTarget");
builder.eventLoopGroup(mock(EventLoopGroup.class));
thrown.expect(IllegalStateException.class);
thrown.expectMessage("Both EventLoopGroup and ChannelType should be provided");

builder.assertEventLoopAndChannelType();
IllegalStateException e = assertThrows(IllegalStateException.class,
builder::assertEventLoopAndChannelType);
assertThat(e).hasMessageThat()
.isEqualTo("Both EventLoopGroup and ChannelType should be provided or neither should be");
}

@Test
public void assertEventLoopAndChannelType_onlyTypeProvided() {
NettyChannelBuilder builder = NettyChannelBuilder.forTarget("fakeTarget");
builder.channelType(LocalChannel.class, LocalAddress.class);
thrown.expect(IllegalStateException.class);
thrown.expectMessage("Both EventLoopGroup and ChannelType should be provided");

builder.assertEventLoopAndChannelType();
IllegalStateException e = assertThrows(IllegalStateException.class,
builder::assertEventLoopAndChannelType);
assertThat(e).hasMessageThat()
.isEqualTo("Both EventLoopGroup and ChannelType should be provided or neither should be");
}

@Test
Expand All @@ -288,10 +283,11 @@ public Channel newChannel() {
return null;
}
});
thrown.expect(IllegalStateException.class);
thrown.expectMessage("Both EventLoopGroup and ChannelType should be provided");

builder.assertEventLoopAndChannelType();
IllegalStateException e = assertThrows(IllegalStateException.class,
builder::assertEventLoopAndChannelType);
assertThat(e).hasMessageThat()
.isEqualTo("Both EventLoopGroup and ChannelType should be provided or neither should be");
}

@Test
Expand Down
Loading