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,18 @@ 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
134
@ 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" );
145
139
}
146
140
147
141
@ Test
@@ -155,32 +149,32 @@ public void failIfSslContextIsNotClient() {
155
149
SslContext sslContext = mock (SslContext .class );
156
150
NettyChannelBuilder builder = new NettyChannelBuilder (getTestSocketAddress ());
157
151
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" );
162
156
}
163
157
164
158
@ Test
165
159
public void failNegotiationTypeWithChannelCredentials_target () {
166
160
NettyChannelBuilder builder = NettyChannelBuilder .forTarget (
167
161
"fakeTarget" , InsecureChannelCredentials .create ());
168
162
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" );
173
167
}
174
168
175
169
@ Test
176
170
public void failNegotiationTypeWithChannelCredentials_socketAddress () {
177
171
NettyChannelBuilder builder = NettyChannelBuilder .forAddress (
178
172
getTestSocketAddress (), InsecureChannelCredentials .create ());
179
173
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" );
184
178
}
185
179
186
180
@ Test
@@ -205,10 +199,9 @@ public void createProtocolNegotiatorByType_plaintextUpgrade() {
205
199
206
200
@ Test
207
201
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 ));
212
205
}
213
206
214
207
@ Test
@@ -245,38 +238,40 @@ public void createProtocolNegotiatorByType_tlsWithAuthorityFallback() throws SSL
245
238
public void negativeKeepAliveTime () {
246
239
NettyChannelBuilder builder = NettyChannelBuilder .forTarget ("fakeTarget" );
247
240
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" );
251
244
}
252
245
253
246
@ Test
254
247
public void negativeKeepAliveTimeout () {
255
248
NettyChannelBuilder builder = NettyChannelBuilder .forTarget ("fakeTarget" );
256
249
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" );
260
253
}
261
254
262
255
@ Test
263
256
public void assertEventLoopAndChannelType_onlyGroupProvided () {
264
257
NettyChannelBuilder builder = NettyChannelBuilder .forTarget ("fakeTarget" );
265
258
builder .eventLoopGroup (mock (EventLoopGroup .class ));
266
- thrown .expect (IllegalStateException .class );
267
- thrown .expectMessage ("Both EventLoopGroup and ChannelType should be provided" );
268
259
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" );
270
264
}
271
265
272
266
@ Test
273
267
public void assertEventLoopAndChannelType_onlyTypeProvided () {
274
268
NettyChannelBuilder builder = NettyChannelBuilder .forTarget ("fakeTarget" );
275
269
builder .channelType (LocalChannel .class , LocalAddress .class );
276
- thrown .expect (IllegalStateException .class );
277
- thrown .expectMessage ("Both EventLoopGroup and ChannelType should be provided" );
278
270
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" );
280
275
}
281
276
282
277
@ Test
@@ -288,10 +283,11 @@ public Channel newChannel() {
288
283
return null ;
289
284
}
290
285
});
291
- thrown .expect (IllegalStateException .class );
292
- thrown .expectMessage ("Both EventLoopGroup and ChannelType should be provided" );
293
286
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" );
295
291
}
296
292
297
293
@ Test
0 commit comments