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,16 @@ 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-
142- @ SuppressWarnings ("AddressSelection" ) // We actually expect zero addresses!
143- Object unused =
144- NettyChannelBuilder .forAddress (new InetSocketAddress ("invalid_authority" , 1234 ));
134+ IllegalArgumentException e = assertThrows (IllegalArgumentException .class ,
135+ () -> NettyChannelBuilder .forAddress (new InetSocketAddress ("invalid_authority" , 1234 )));
136+ assertThat (e ).hasMessageThat ().isEqualTo ("Invalid host or port: invalid_authority 1234" );
145137 }
146138
147139 @ Test
@@ -155,32 +147,32 @@ public void failIfSslContextIsNotClient() {
155147 SslContext sslContext = mock (SslContext .class );
156148 NettyChannelBuilder builder = new NettyChannelBuilder (getTestSocketAddress ());
157149
158- thrown . expect (IllegalArgumentException .class );
159- thrown . expectMessage ( "Server SSL context can not be used for client channel" );
160-
161- builder . sslContext ( sslContext );
150+ IllegalArgumentException e = assertThrows (IllegalArgumentException .class ,
151+ () -> builder . sslContext ( sslContext ) );
152+ assertThat ( e ). hasMessageThat ()
153+ . isEqualTo ( "Server SSL context can not be used for client channel" );
162154 }
163155
164156 @ Test
165157 public void failNegotiationTypeWithChannelCredentials_target () {
166158 NettyChannelBuilder builder = NettyChannelBuilder .forTarget (
167159 "fakeTarget" , InsecureChannelCredentials .create ());
168160
169- thrown . expect (IllegalStateException .class );
170- thrown . expectMessage ( "Cannot change security when using ChannelCredentials" );
171-
172- builder . negotiationType ( NegotiationType . TLS );
161+ IllegalStateException e = assertThrows (IllegalStateException .class ,
162+ () -> builder . negotiationType ( NegotiationType . TLS ) );
163+ assertThat ( e ). hasMessageThat ()
164+ . isEqualTo ( "Cannot change security when using ChannelCredentials" );
173165 }
174166
175167 @ Test
176168 public void failNegotiationTypeWithChannelCredentials_socketAddress () {
177169 NettyChannelBuilder builder = NettyChannelBuilder .forAddress (
178170 getTestSocketAddress (), InsecureChannelCredentials .create ());
179171
180- thrown . expect (IllegalStateException .class );
181- thrown . expectMessage ( "Cannot change security when using ChannelCredentials" );
182-
183- builder . negotiationType ( NegotiationType . TLS );
172+ IllegalStateException e = assertThrows (IllegalStateException .class ,
173+ () -> builder . negotiationType ( NegotiationType . TLS ) );
174+ assertThat ( e ). hasMessageThat ()
175+ . isEqualTo ( "Cannot change security when using ChannelCredentials" );
184176 }
185177
186178 @ Test
@@ -205,10 +197,9 @@ public void createProtocolNegotiatorByType_plaintextUpgrade() {
205197
206198 @ Test
207199 public void createProtocolNegotiatorByType_tlsWithNoContext () {
208- thrown .expect (NullPointerException .class );
209- NettyChannelBuilder .createProtocolNegotiatorByType (
210- NegotiationType .TLS ,
211- noSslContext , null );
200+ assertThrows (NullPointerException .class ,
201+ () -> NettyChannelBuilder .createProtocolNegotiatorByType (
202+ NegotiationType .TLS , noSslContext , null ));
212203 }
213204
214205 @ Test
@@ -245,38 +236,40 @@ public void createProtocolNegotiatorByType_tlsWithAuthorityFallback() throws SSL
245236 public void negativeKeepAliveTime () {
246237 NettyChannelBuilder builder = NettyChannelBuilder .forTarget ("fakeTarget" );
247238
248- thrown . expect (IllegalArgumentException .class );
249- thrown . expectMessage ( "keepalive time must be positive" );
250- builder . keepAliveTime (- 1L , TimeUnit . HOURS );
239+ IllegalArgumentException e = assertThrows (IllegalArgumentException .class ,
240+ () -> builder . keepAliveTime (- 1L , TimeUnit . HOURS ) );
241+ assertThat ( e ). hasMessageThat (). isEqualTo ( "keepalive time must be positive" );
251242 }
252243
253244 @ Test
254245 public void negativeKeepAliveTimeout () {
255246 NettyChannelBuilder builder = NettyChannelBuilder .forTarget ("fakeTarget" );
256247
257- thrown . expect (IllegalArgumentException .class );
258- thrown . expectMessage ( "keepalive timeout must be positive" );
259- builder . keepAliveTimeout (- 1L , TimeUnit . HOURS );
248+ IllegalArgumentException e = assertThrows (IllegalArgumentException .class ,
249+ () -> builder . keepAliveTimeout (- 1L , TimeUnit . HOURS ) );
250+ assertThat ( e ). hasMessageThat (). isEqualTo ( "keepalive timeout must be positive" );
260251 }
261252
262253 @ Test
263254 public void assertEventLoopAndChannelType_onlyGroupProvided () {
264255 NettyChannelBuilder builder = NettyChannelBuilder .forTarget ("fakeTarget" );
265256 builder .eventLoopGroup (mock (EventLoopGroup .class ));
266- thrown .expect (IllegalStateException .class );
267- thrown .expectMessage ("Both EventLoopGroup and ChannelType should be provided" );
268257
269- builder .assertEventLoopAndChannelType ();
258+ IllegalStateException e = assertThrows (IllegalStateException .class ,
259+ builder ::assertEventLoopAndChannelType );
260+ assertThat (e ).hasMessageThat ()
261+ .isEqualTo ("Both EventLoopGroup and ChannelType should be provided or neither should be" );
270262 }
271263
272264 @ Test
273265 public void assertEventLoopAndChannelType_onlyTypeProvided () {
274266 NettyChannelBuilder builder = NettyChannelBuilder .forTarget ("fakeTarget" );
275267 builder .channelType (LocalChannel .class , LocalAddress .class );
276- thrown .expect (IllegalStateException .class );
277- thrown .expectMessage ("Both EventLoopGroup and ChannelType should be provided" );
278268
279- builder .assertEventLoopAndChannelType ();
269+ IllegalStateException e = assertThrows (IllegalStateException .class ,
270+ builder ::assertEventLoopAndChannelType );
271+ assertThat (e ).hasMessageThat ()
272+ .isEqualTo ("Both EventLoopGroup and ChannelType should be provided or neither should be" );
280273 }
281274
282275 @ Test
@@ -288,10 +281,11 @@ public Channel newChannel() {
288281 return null ;
289282 }
290283 });
291- thrown .expect (IllegalStateException .class );
292- thrown .expectMessage ("Both EventLoopGroup and ChannelType should be provided" );
293284
294- builder .assertEventLoopAndChannelType ();
285+ IllegalStateException e = assertThrows (IllegalStateException .class ,
286+ builder ::assertEventLoopAndChannelType );
287+ assertThat (e ).hasMessageThat ()
288+ .isEqualTo ("Both EventLoopGroup and ChannelType should be provided or neither should be" );
295289 }
296290
297291 @ Test
0 commit comments