1919import static com .google .common .truth .Truth .assertThat ;
2020import static org .junit .Assert .assertEquals ;
2121import static org .junit .Assert .assertNotNull ;
22+ import static org .junit .Assert .assertThrows ;
2223import static org .junit .Assert .assertTrue ;
2324import static org .mockito .Mockito .mock ;
2425
3940import java .net .SocketAddress ;
4041import java .util .concurrent .TimeUnit ;
4142import javax .net .ssl .SSLException ;
42- import org .junit .Rule ;
4343import org .junit .Test ;
44- import org .junit .rules .ExpectedException ;
4544import org .junit .runner .RunWith ;
4645import org .junit .runners .JUnit4 ;
4746
4847@ RunWith (JUnit4 .class )
4948public class NettyChannelBuilderTest {
5049
51- @ SuppressWarnings ("deprecation" ) // https://github.com/grpc/grpc-java/issues/7467
52- @ Rule public final ExpectedException thrown = ExpectedException .none ();
5350 private final SslContext noSslContext = null ;
5451
5552 private void shutdown (ManagedChannel mc ) throws Exception {
@@ -107,10 +104,9 @@ private void overrideAuthorityIsReadableHelper(NettyChannelBuilder builder,
107104 public void failOverrideInvalidAuthority () {
108105 NettyChannelBuilder builder = new NettyChannelBuilder (getTestSocketAddress ());
109106
110- thrown .expect (IllegalArgumentException .class );
111- thrown .expectMessage ("Invalid authority:" );
112-
113- builder .overrideAuthority ("[invalidauthority" );
107+ IllegalArgumentException e = assertThrows (IllegalArgumentException .class ,
108+ () -> builder .overrideAuthority ("[invalidauthority" ));
109+ assertThat (e ).hasMessageThat ().isEqualTo ("Invalid authority: [invalidauthority" );
114110 }
115111
116112 @ Test
@@ -128,20 +124,18 @@ public void enableCheckAuthorityFailOverrideInvalidAuthority() {
128124 NettyChannelBuilder builder = new NettyChannelBuilder (getTestSocketAddress ())
129125 .disableCheckAuthority ()
130126 .enableCheckAuthority ();
131-
132- thrown .expect (IllegalArgumentException .class );
133- thrown .expectMessage ("Invalid authority:" );
134- builder .overrideAuthority ("[invalidauthority" );
127+ IllegalArgumentException e = assertThrows (IllegalArgumentException .class ,
128+ () -> builder .overrideAuthority ("[invalidauthority" ));
129+ assertThat (e ).hasMessageThat ().isEqualTo ("Invalid authority: [invalidauthority" );
135130 }
136131
137132 @ Test
138133 public void failInvalidAuthority () {
139- thrown .expect (IllegalArgumentException .class );
140- thrown .expectMessage ("Invalid host or port" );
141-
142134 @ SuppressWarnings ("AddressSelection" ) // We actually expect zero addresses!
143- Object unused =
144- NettyChannelBuilder .forAddress (new InetSocketAddress ("invalid_authority" , 1234 ));
135+ InetSocketAddress address = new InetSocketAddress ("invalid_authority" , 1234 );
136+ IllegalArgumentException e = assertThrows (IllegalArgumentException .class ,
137+ () -> NettyChannelBuilder .forAddress (address ));
138+ assertThat (e ).hasMessageThat ().isEqualTo ("Invalid host or port: invalid_authority 1234" );
145139 }
146140
147141 @ Test
@@ -155,32 +149,32 @@ public void failIfSslContextIsNotClient() {
155149 SslContext sslContext = mock (SslContext .class );
156150 NettyChannelBuilder builder = new NettyChannelBuilder (getTestSocketAddress ());
157151
158- thrown . expect (IllegalArgumentException .class );
159- thrown . expectMessage ( "Server SSL context can not be used for client channel" );
160-
161- builder . sslContext ( sslContext );
152+ IllegalArgumentException e = assertThrows (IllegalArgumentException .class ,
153+ () -> builder . sslContext ( sslContext ) );
154+ assertThat ( e ). hasMessageThat ()
155+ . isEqualTo ( "Server SSL context can not be used for client channel" );
162156 }
163157
164158 @ Test
165159 public void failNegotiationTypeWithChannelCredentials_target () {
166160 NettyChannelBuilder builder = NettyChannelBuilder .forTarget (
167161 "fakeTarget" , InsecureChannelCredentials .create ());
168162
169- thrown . expect (IllegalStateException .class );
170- thrown . expectMessage ( "Cannot change security when using ChannelCredentials" );
171-
172- builder . negotiationType ( NegotiationType . TLS );
163+ IllegalStateException e = assertThrows (IllegalStateException .class ,
164+ () -> builder . negotiationType ( NegotiationType . TLS ) );
165+ assertThat ( e ). hasMessageThat ()
166+ . isEqualTo ( "Cannot change security when using ChannelCredentials" );
173167 }
174168
175169 @ Test
176170 public void failNegotiationTypeWithChannelCredentials_socketAddress () {
177171 NettyChannelBuilder builder = NettyChannelBuilder .forAddress (
178172 getTestSocketAddress (), InsecureChannelCredentials .create ());
179173
180- thrown . expect (IllegalStateException .class );
181- thrown . expectMessage ( "Cannot change security when using ChannelCredentials" );
182-
183- builder . negotiationType ( NegotiationType . TLS );
174+ IllegalStateException e = assertThrows (IllegalStateException .class ,
175+ () -> builder . negotiationType ( NegotiationType . TLS ) );
176+ assertThat ( e ). hasMessageThat ()
177+ . isEqualTo ( "Cannot change security when using ChannelCredentials" );
184178 }
185179
186180 @ Test
@@ -205,10 +199,9 @@ public void createProtocolNegotiatorByType_plaintextUpgrade() {
205199
206200 @ Test
207201 public void createProtocolNegotiatorByType_tlsWithNoContext () {
208- thrown .expect (NullPointerException .class );
209- NettyChannelBuilder .createProtocolNegotiatorByType (
210- NegotiationType .TLS ,
211- noSslContext , null );
202+ assertThrows (NullPointerException .class ,
203+ () -> NettyChannelBuilder .createProtocolNegotiatorByType (
204+ NegotiationType .TLS , noSslContext , null ));
212205 }
213206
214207 @ Test
@@ -245,38 +238,40 @@ public void createProtocolNegotiatorByType_tlsWithAuthorityFallback() throws SSL
245238 public void negativeKeepAliveTime () {
246239 NettyChannelBuilder builder = NettyChannelBuilder .forTarget ("fakeTarget" );
247240
248- thrown . expect (IllegalArgumentException .class );
249- thrown . expectMessage ( "keepalive time must be positive" );
250- builder . keepAliveTime (- 1L , TimeUnit . HOURS );
241+ IllegalArgumentException e = assertThrows (IllegalArgumentException .class ,
242+ () -> builder . keepAliveTime (- 1L , TimeUnit . HOURS ) );
243+ assertThat ( e ). hasMessageThat (). isEqualTo ( "keepalive time must be positive" );
251244 }
252245
253246 @ Test
254247 public void negativeKeepAliveTimeout () {
255248 NettyChannelBuilder builder = NettyChannelBuilder .forTarget ("fakeTarget" );
256249
257- thrown . expect (IllegalArgumentException .class );
258- thrown . expectMessage ( "keepalive timeout must be positive" );
259- builder . keepAliveTimeout (- 1L , TimeUnit . HOURS );
250+ IllegalArgumentException e = assertThrows (IllegalArgumentException .class ,
251+ () -> builder . keepAliveTimeout (- 1L , TimeUnit . HOURS ) );
252+ assertThat ( e ). hasMessageThat (). isEqualTo ( "keepalive timeout must be positive" );
260253 }
261254
262255 @ Test
263256 public void assertEventLoopAndChannelType_onlyGroupProvided () {
264257 NettyChannelBuilder builder = NettyChannelBuilder .forTarget ("fakeTarget" );
265258 builder .eventLoopGroup (mock (EventLoopGroup .class ));
266- thrown .expect (IllegalStateException .class );
267- thrown .expectMessage ("Both EventLoopGroup and ChannelType should be provided" );
268259
269- builder .assertEventLoopAndChannelType ();
260+ IllegalStateException e = assertThrows (IllegalStateException .class ,
261+ builder ::assertEventLoopAndChannelType );
262+ assertThat (e ).hasMessageThat ()
263+ .isEqualTo ("Both EventLoopGroup and ChannelType should be provided or neither should be" );
270264 }
271265
272266 @ Test
273267 public void assertEventLoopAndChannelType_onlyTypeProvided () {
274268 NettyChannelBuilder builder = NettyChannelBuilder .forTarget ("fakeTarget" );
275269 builder .channelType (LocalChannel .class , LocalAddress .class );
276- thrown .expect (IllegalStateException .class );
277- thrown .expectMessage ("Both EventLoopGroup and ChannelType should be provided" );
278270
279- builder .assertEventLoopAndChannelType ();
271+ IllegalStateException e = assertThrows (IllegalStateException .class ,
272+ builder ::assertEventLoopAndChannelType );
273+ assertThat (e ).hasMessageThat ()
274+ .isEqualTo ("Both EventLoopGroup and ChannelType should be provided or neither should be" );
280275 }
281276
282277 @ Test
@@ -288,10 +283,11 @@ public Channel newChannel() {
288283 return null ;
289284 }
290285 });
291- thrown .expect (IllegalStateException .class );
292- thrown .expectMessage ("Both EventLoopGroup and ChannelType should be provided" );
293286
294- builder .assertEventLoopAndChannelType ();
287+ IllegalStateException e = assertThrows (IllegalStateException .class ,
288+ builder ::assertEventLoopAndChannelType );
289+ assertThat (e ).hasMessageThat ()
290+ .isEqualTo ("Both EventLoopGroup and ChannelType should be provided or neither should be" );
295291 }
296292
297293 @ Test
0 commit comments