32
32
package com .google .auth .oauth2 ;
33
33
34
34
import static java .util .concurrent .TimeUnit .HOURS ;
35
- import static org .junit .Assert .assertEquals ;
36
- import static org .junit .Assert .assertFalse ;
37
- import static org .junit .Assert .assertNotNull ;
38
- import static org .junit .Assert .assertNull ;
39
- import static org .junit .Assert .assertSame ;
40
- import static org .junit .Assert .assertThrows ;
41
- import static org .junit .Assert .assertTrue ;
42
- import static org .junit .Assert .fail ;
35
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
36
+ import static org .junit .jupiter .api .Assertions .assertFalse ;
37
+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
38
+ import static org .junit .jupiter .api .Assertions .assertNull ;
39
+ import static org .junit .jupiter .api .Assertions .assertSame ;
40
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
41
+ import static org .junit .jupiter .api .Assertions .assertTrue ;
43
42
44
43
import com .google .api .client .util .Clock ;
45
44
import com .google .auth .TestClock ;
69
68
import java .util .concurrent .TimeoutException ;
70
69
import java .util .concurrent .atomic .AtomicInteger ;
71
70
import java .util .concurrent .atomic .AtomicReference ;
72
- import org .junit .After ;
73
- import org .junit .Before ;
74
- import org .junit .Test ;
75
- import org .junit .function .ThrowingRunnable ;
76
- import org .junit .runner .RunWith ;
77
- import org .junit .runners .JUnit4 ;
71
+ import org .junit .jupiter .api .AfterEach ;
72
+ import org .junit .jupiter .api .BeforeEach ;
73
+ import org .junit .jupiter .api .Test ;
78
74
79
75
/** Test case for {@link OAuth2Credentials}. */
80
- @ RunWith (JUnit4 .class )
81
- public class OAuth2CredentialsTest extends BaseSerializationTest {
76
+ class OAuth2CredentialsTest extends BaseSerializationTest {
82
77
83
78
private static final String CLIENT_SECRET = "jakuaL9YyieakhECKL2SwZcu" ;
84
79
private static final String CLIENT_ID = "ya29.1.AADtN_UtlxN3PuGAxrN2XQnZTVRvDyVWnYq4I6dws" ;
@@ -88,25 +83,25 @@ public class OAuth2CredentialsTest extends BaseSerializationTest {
88
83
89
84
private ExecutorService realExecutor ;
90
85
91
- @ Before
92
- public void setUp () {
86
+ @ BeforeEach
87
+ void setUp () {
93
88
realExecutor = Executors .newCachedThreadPool ();
94
89
}
95
90
96
- @ After
97
- public void tearDown () {
91
+ @ AfterEach
92
+ void tearDown () {
98
93
realExecutor .shutdown ();
99
94
}
100
95
101
96
@ Test
102
- public void constructor_storesAccessToken () {
97
+ void constructor_storesAccessToken () {
103
98
OAuth2Credentials credentials =
104
99
OAuth2Credentials .newBuilder ().setAccessToken (new AccessToken (ACCESS_TOKEN , null )).build ();
105
- assertEquals (credentials .getAccessToken ().getTokenValue (), ACCESS_TOKEN );
100
+ assertEquals (ACCESS_TOKEN , credentials .getAccessToken ().getTokenValue ());
106
101
}
107
102
108
103
@ Test
109
- public void constructor_overrideMargin () throws Throwable {
104
+ void constructor_overrideMargin () throws Throwable {
110
105
Duration staleMargin = Duration .ofMinutes (3 );
111
106
Duration expirationMargin = Duration .ofMinutes (2 );
112
107
@@ -187,18 +182,18 @@ public AccessToken refreshAccessToken() throws IOException {
187
182
}
188
183
189
184
@ Test
190
- public void getAuthenticationType_returnsOAuth2 () {
185
+ void getAuthenticationType_returnsOAuth2 () {
191
186
OAuth2Credentials credentials =
192
187
UserCredentials .newBuilder ()
193
188
.setClientId (CLIENT_ID )
194
189
.setClientSecret (CLIENT_SECRET )
195
190
.setRefreshToken (REFRESH_TOKEN )
196
191
.build ();
197
- assertEquals (credentials .getAuthenticationType (), "OAuth2" );
192
+ assertEquals ("OAuth2" , credentials .getAuthenticationType ());
198
193
}
199
194
200
195
@ Test
201
- public void hasRequestMetadata_returnsTrue () {
196
+ void hasRequestMetadata_returnsTrue () {
202
197
OAuth2Credentials credentials =
203
198
UserCredentials .newBuilder ()
204
199
.setClientId (CLIENT_ID )
@@ -209,7 +204,7 @@ public void hasRequestMetadata_returnsTrue() {
209
204
}
210
205
211
206
@ Test
212
- public void hasRequestMetadataOnly_returnsTrue () {
207
+ void hasRequestMetadataOnly_returnsTrue () {
213
208
OAuth2Credentials credentials =
214
209
UserCredentials .newBuilder ()
215
210
.setClientId (CLIENT_ID )
@@ -220,7 +215,7 @@ public void hasRequestMetadataOnly_returnsTrue() {
220
215
}
221
216
222
217
@ Test
223
- public void addChangeListener_notifiesOnRefresh () throws IOException {
218
+ void addChangeListener_notifiesOnRefresh () throws IOException {
224
219
final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
225
220
final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2" ;
226
221
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory ();
@@ -258,7 +253,7 @@ public void addChangeListener_notifiesOnRefresh() throws IOException {
258
253
}
259
254
260
255
@ Test
261
- public void removeChangeListener_unregisters_observer () throws IOException {
256
+ void removeChangeListener_unregisters_observer () throws IOException {
262
257
final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
263
258
final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2" ;
264
259
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory ();
@@ -295,7 +290,7 @@ public void removeChangeListener_unregisters_observer() throws IOException {
295
290
}
296
291
297
292
@ Test
298
- public void getRequestMetadata_blocking_cachesExpiringToken () throws IOException {
293
+ void getRequestMetadata_blocking_cachesExpiringToken () throws IOException {
299
294
final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
300
295
final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2" ;
301
296
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory ();
@@ -333,13 +328,11 @@ public void getRequestMetadata_blocking_cachesExpiringToken() throws IOException
333
328
clock .addToCurrentTime (60 * 60 * 1000 );
334
329
assertEquals (0 , transportFactory .transport .buildRequestCount );
335
330
336
- try {
337
- credentials .getRequestMetadata (CALL_URI );
338
- fail ("Should throw" );
339
- } catch (IOException e ) {
340
- assertSame (error , e );
341
- assertEquals (1 , transportFactory .transport .buildRequestCount --);
342
- }
331
+ IOException exception =
332
+ assertThrows (
333
+ IOException .class , () -> credentials .getRequestMetadata (CALL_URI ), "Should throw" );
334
+ assertSame (error , exception );
335
+ assertEquals (1 , transportFactory .transport .buildRequestCount --);
343
336
344
337
// Reset the error and try again
345
338
transportFactory .transport .setError (null );
@@ -349,7 +342,7 @@ public void getRequestMetadata_blocking_cachesExpiringToken() throws IOException
349
342
}
350
343
351
344
@ Test
352
- public void getRequestMetadata_async () throws IOException {
345
+ void getRequestMetadata_async () {
353
346
final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
354
347
final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2" ;
355
348
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory ();
@@ -419,8 +412,7 @@ public void getRequestMetadata_async() throws IOException {
419
412
}
420
413
421
414
@ Test
422
- public void getRequestMetadata_async_refreshRace ()
423
- throws ExecutionException , InterruptedException {
415
+ void getRequestMetadata_async_refreshRace () throws ExecutionException , InterruptedException {
424
416
final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
425
417
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory ();
426
418
transportFactory .transport .addClient (CLIENT_ID , CLIENT_SECRET );
@@ -478,7 +470,7 @@ public Map<String, List<String>> call() throws Exception {
478
470
}
479
471
480
472
@ Test
481
- public void getRequestMetadata_temporaryToken_hasToken () throws IOException {
473
+ void getRequestMetadata_temporaryToken_hasToken () throws IOException {
482
474
OAuth2Credentials credentials =
483
475
OAuth2Credentials .newBuilder ().setAccessToken (new AccessToken (ACCESS_TOKEN , null )).build ();
484
476
@@ -488,7 +480,7 @@ public void getRequestMetadata_temporaryToken_hasToken() throws IOException {
488
480
}
489
481
490
482
@ Test
491
- public void getRequestMetadata_staleTemporaryToken () throws IOException , InterruptedException {
483
+ void getRequestMetadata_staleTemporaryToken () throws IOException , InterruptedException {
492
484
Instant actualExpiration = Instant .now ();
493
485
Instant clientStale = actualExpiration .minus (OAuth2Credentials .DEFAULT_REFRESH_MARGIN );
494
486
@@ -558,7 +550,7 @@ public AccessToken refreshAccessToken() {
558
550
}
559
551
560
552
@ Test
561
- public void getRequestMetadata_staleTemporaryToken_expirationWaits () throws Throwable {
553
+ void getRequestMetadata_staleTemporaryToken_expirationWaits () throws Throwable {
562
554
Instant actualExpiration = Instant .now ();
563
555
Instant clientStale = actualExpiration .minus (OAuth2Credentials .DEFAULT_REFRESH_MARGIN );
564
556
Instant clientExpired = actualExpiration .minus (OAuth2Credentials .DEFAULT_EXPIRATION_MARGIN );
@@ -627,7 +619,7 @@ public AccessToken refreshAccessToken() {
627
619
}
628
620
629
621
@ Test
630
- public void getRequestMetadata_singleFlightErrorSharing () {
622
+ void getRequestMetadata_singleFlightErrorSharing () {
631
623
Instant actualExpiration = Instant .now ();
632
624
Instant clientStale = actualExpiration .minus (OAuth2Credentials .DEFAULT_REFRESH_MARGIN );
633
625
Instant clientExpired = actualExpiration .minus (OAuth2Credentials .DEFAULT_EXPIRATION_MARGIN );
@@ -671,32 +663,17 @@ public Map<String, List<String>> call() throws Exception {
671
663
672
664
// Get the error that getRequestMetadata(uri) created
673
665
Throwable actualBlockingError =
674
- assertThrows (
675
- ExecutionException .class ,
676
- new ThrowingRunnable () {
677
- @ Override
678
- public void run () throws Throwable {
679
- blockingCall .get ();
680
- }
681
- })
682
- .getCause ();
666
+ assertThrows (ExecutionException .class , blockingCall ::get ).getCause ();
683
667
684
668
assertEquals (error , actualBlockingError );
685
669
686
670
RuntimeException actualAsyncError =
687
- assertThrows (
688
- RuntimeException .class ,
689
- new ThrowingRunnable () {
690
- @ Override
691
- public void run () throws Throwable {
692
- callback1 .awaitResult ();
693
- }
694
- });
671
+ assertThrows (RuntimeException .class , callback1 ::awaitResult );
695
672
assertEquals (error , actualAsyncError );
696
673
}
697
674
698
675
@ Test
699
- public void getRequestMetadata_syncErrorsIncludeCallingStackframe () {
676
+ void getRequestMetadata_syncErrorsIncludeCallingStackframe () {
700
677
final OAuth2Credentials creds =
701
678
new OAuth2Credentials () {
702
679
@ Override
@@ -726,7 +703,7 @@ public AccessToken refreshAccessToken() {
726
703
}
727
704
728
705
@ Test
729
- public void refresh_refreshesToken () throws IOException {
706
+ void refresh_refreshesToken () throws IOException {
730
707
final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
731
708
final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2" ;
732
709
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory ();
@@ -762,7 +739,7 @@ public void refresh_refreshesToken() throws IOException {
762
739
}
763
740
764
741
@ Test
765
- public void refreshIfExpired_refreshesToken () throws IOException {
742
+ void refreshIfExpired_refreshesToken () throws IOException {
766
743
final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
767
744
final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2" ;
768
745
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory ();
@@ -805,15 +782,21 @@ public void refreshIfExpired_refreshesToken() throws IOException {
805
782
assertEquals (1 , transportFactory .transport .buildRequestCount --);
806
783
}
807
784
808
- @ Test (expected = IllegalStateException .class )
809
- public void refresh_temporaryToken_throws () throws IOException {
810
- OAuth2Credentials credentials =
811
- OAuth2Credentials .newBuilder ().setAccessToken (new AccessToken (ACCESS_TOKEN , null )).build ();
812
- credentials .refresh ();
785
+ @ Test
786
+ void refresh_temporaryToken_throws () {
787
+ assertThrows (
788
+ IllegalStateException .class ,
789
+ () -> {
790
+ OAuth2Credentials credentials =
791
+ OAuth2Credentials .newBuilder ()
792
+ .setAccessToken (new AccessToken (ACCESS_TOKEN , null ))
793
+ .build ();
794
+ credentials .refresh ();
795
+ });
813
796
}
814
797
815
798
@ Test
816
- public void equals_true () throws IOException {
799
+ void equals_true () {
817
800
final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
818
801
OAuth2Credentials credentials =
819
802
OAuth2Credentials .newBuilder ().setAccessToken (new AccessToken (accessToken1 , null )).build ();
@@ -824,7 +807,7 @@ public void equals_true() throws IOException {
824
807
}
825
808
826
809
@ Test
827
- public void equals_false_accessToken () throws IOException {
810
+ void equals_false_accessToken () {
828
811
final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
829
812
final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2" ;
830
813
OAuth2Credentials credentials =
@@ -836,7 +819,7 @@ public void equals_false_accessToken() throws IOException {
836
819
}
837
820
838
821
@ Test
839
- public void toString_containsFields () throws IOException {
822
+ void toString_containsFields () {
840
823
AccessToken accessToken = new AccessToken ("1/MkSJoj1xsli0AccessToken_NKPY2" , null );
841
824
OAuth2Credentials credentials =
842
825
OAuth2Credentials .newBuilder ().setAccessToken (accessToken ).build ();
@@ -851,7 +834,7 @@ public void toString_containsFields() throws IOException {
851
834
}
852
835
853
836
@ Test
854
- public void hashCode_equals () throws IOException {
837
+ void hashCode_equals () throws IOException {
855
838
final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
856
839
OAuth2Credentials credentials =
857
840
OAuth2Credentials .newBuilder ().setAccessToken (new AccessToken (accessToken , null )).build ();
@@ -861,7 +844,7 @@ public void hashCode_equals() throws IOException {
861
844
}
862
845
863
846
@ Test
864
- public void serialize () throws IOException , ClassNotFoundException {
847
+ void serialize () throws IOException , ClassNotFoundException {
865
848
final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
866
849
OAuth2Credentials credentials =
867
850
OAuth2Credentials .newBuilder ().setAccessToken (new AccessToken (accessToken , null )).build ();
0 commit comments