19
19
import static com .google .common .truth .Truth .assertThat ;
20
20
import static org .junit .Assert .assertEquals ;
21
21
import static org .junit .Assert .assertNotNull ;
22
+ import static org .junit .Assert .assertThrows ;
22
23
import static org .junit .Assert .assertTrue ;
23
24
import static org .mockito .Mockito .mock ;
24
25
39
40
import java .net .SocketAddress ;
40
41
import java .util .concurrent .TimeUnit ;
41
42
import javax .net .ssl .SSLException ;
42
- import org .junit .Rule ;
43
43
import org .junit .Test ;
44
- import org .junit .rules .ExpectedException ;
45
44
import org .junit .runner .RunWith ;
46
45
import org .junit .runners .JUnit4 ;
47
46
48
47
@ RunWith (JUnit4 .class )
49
48
public class NettyChannelBuilderTest {
50
49
51
- @ SuppressWarnings ("deprecation" ) // https://github.com/grpc/grpc-java/issues/7467
52
- @ Rule public final ExpectedException thrown = ExpectedException .none ();
53
50
private final SslContext noSslContext = null ;
54
51
55
52
private void shutdown (ManagedChannel mc ) throws Exception {
@@ -107,10 +104,9 @@ private void overrideAuthorityIsReadableHelper(NettyChannelBuilder builder,
107
104
public void failOverrideInvalidAuthority () {
108
105
NettyChannelBuilder builder = new NettyChannelBuilder (getTestSocketAddress ());
109
106
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" );
114
110
}
115
111
116
112
@ Test
@@ -128,20 +124,16 @@ public void enableCheckAuthorityFailOverrideInvalidAuthority() {
128
124
NettyChannelBuilder builder = new NettyChannelBuilder (getTestSocketAddress ())
129
125
.disableCheckAuthority ()
130
126
.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" );
135
130
}
136
131
137
132
@ Test
138
133
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" );
145
137
}
146
138
147
139
@ Test
@@ -155,32 +147,32 @@ public void failIfSslContextIsNotClient() {
155
147
SslContext sslContext = mock (SslContext .class );
156
148
NettyChannelBuilder builder = new NettyChannelBuilder (getTestSocketAddress ());
157
149
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" );
162
154
}
163
155
164
156
@ Test
165
157
public void failNegotiationTypeWithChannelCredentials_target () {
166
158
NettyChannelBuilder builder = NettyChannelBuilder .forTarget (
167
159
"fakeTarget" , InsecureChannelCredentials .create ());
168
160
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" );
173
165
}
174
166
175
167
@ Test
176
168
public void failNegotiationTypeWithChannelCredentials_socketAddress () {
177
169
NettyChannelBuilder builder = NettyChannelBuilder .forAddress (
178
170
getTestSocketAddress (), InsecureChannelCredentials .create ());
179
171
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" );
184
176
}
185
177
186
178
@ Test
@@ -205,10 +197,9 @@ public void createProtocolNegotiatorByType_plaintextUpgrade() {
205
197
206
198
@ Test
207
199
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 ));
212
203
}
213
204
214
205
@ Test
@@ -245,38 +236,40 @@ public void createProtocolNegotiatorByType_tlsWithAuthorityFallback() throws SSL
245
236
public void negativeKeepAliveTime () {
246
237
NettyChannelBuilder builder = NettyChannelBuilder .forTarget ("fakeTarget" );
247
238
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" );
251
242
}
252
243
253
244
@ Test
254
245
public void negativeKeepAliveTimeout () {
255
246
NettyChannelBuilder builder = NettyChannelBuilder .forTarget ("fakeTarget" );
256
247
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" );
260
251
}
261
252
262
253
@ Test
263
254
public void assertEventLoopAndChannelType_onlyGroupProvided () {
264
255
NettyChannelBuilder builder = NettyChannelBuilder .forTarget ("fakeTarget" );
265
256
builder .eventLoopGroup (mock (EventLoopGroup .class ));
266
- thrown .expect (IllegalStateException .class );
267
- thrown .expectMessage ("Both EventLoopGroup and ChannelType should be provided" );
268
257
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" );
270
262
}
271
263
272
264
@ Test
273
265
public void assertEventLoopAndChannelType_onlyTypeProvided () {
274
266
NettyChannelBuilder builder = NettyChannelBuilder .forTarget ("fakeTarget" );
275
267
builder .channelType (LocalChannel .class , LocalAddress .class );
276
- thrown .expect (IllegalStateException .class );
277
- thrown .expectMessage ("Both EventLoopGroup and ChannelType should be provided" );
278
268
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" );
280
273
}
281
274
282
275
@ Test
@@ -288,10 +281,11 @@ public Channel newChannel() {
288
281
return null ;
289
282
}
290
283
});
291
- thrown .expect (IllegalStateException .class );
292
- thrown .expectMessage ("Both EventLoopGroup and ChannelType should be provided" );
293
284
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" );
295
289
}
296
290
297
291
@ Test
0 commit comments