3232package com .google .auth .oauth2 ;
3333
3434import 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 ;
4342
4443import com .google .api .client .util .Clock ;
4544import com .google .auth .TestClock ;
6968import java .util .concurrent .TimeoutException ;
7069import java .util .concurrent .atomic .AtomicInteger ;
7170import 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 ;
7874
7975/** Test case for {@link OAuth2Credentials}. */
80- @ RunWith (JUnit4 .class )
81- public class OAuth2CredentialsTest extends BaseSerializationTest {
76+ class OAuth2CredentialsTest extends BaseSerializationTest {
8277
8378 private static final String CLIENT_SECRET = "jakuaL9YyieakhECKL2SwZcu" ;
8479 private static final String CLIENT_ID = "ya29.1.AADtN_UtlxN3PuGAxrN2XQnZTVRvDyVWnYq4I6dws" ;
@@ -88,25 +83,25 @@ public class OAuth2CredentialsTest extends BaseSerializationTest {
8883
8984 private ExecutorService realExecutor ;
9085
91- @ Before
92- public void setUp () {
86+ @ BeforeEach
87+ void setUp () {
9388 realExecutor = Executors .newCachedThreadPool ();
9489 }
9590
96- @ After
97- public void tearDown () {
91+ @ AfterEach
92+ void tearDown () {
9893 realExecutor .shutdown ();
9994 }
10095
10196 @ Test
102- public void constructor_storesAccessToken () {
97+ void constructor_storesAccessToken () {
10398 OAuth2Credentials credentials =
10499 OAuth2Credentials .newBuilder ().setAccessToken (new AccessToken (ACCESS_TOKEN , null )).build ();
105- assertEquals (credentials .getAccessToken ().getTokenValue (), ACCESS_TOKEN );
100+ assertEquals (ACCESS_TOKEN , credentials .getAccessToken ().getTokenValue ());
106101 }
107102
108103 @ Test
109- public void constructor_overrideMargin () throws Throwable {
104+ void constructor_overrideMargin () throws Throwable {
110105 Duration staleMargin = Duration .ofMinutes (3 );
111106 Duration expirationMargin = Duration .ofMinutes (2 );
112107
@@ -187,18 +182,18 @@ public AccessToken refreshAccessToken() throws IOException {
187182 }
188183
189184 @ Test
190- public void getAuthenticationType_returnsOAuth2 () {
185+ void getAuthenticationType_returnsOAuth2 () {
191186 OAuth2Credentials credentials =
192187 UserCredentials .newBuilder ()
193188 .setClientId (CLIENT_ID )
194189 .setClientSecret (CLIENT_SECRET )
195190 .setRefreshToken (REFRESH_TOKEN )
196191 .build ();
197- assertEquals (credentials .getAuthenticationType (), "OAuth2" );
192+ assertEquals ("OAuth2" , credentials .getAuthenticationType ());
198193 }
199194
200195 @ Test
201- public void hasRequestMetadata_returnsTrue () {
196+ void hasRequestMetadata_returnsTrue () {
202197 OAuth2Credentials credentials =
203198 UserCredentials .newBuilder ()
204199 .setClientId (CLIENT_ID )
@@ -209,7 +204,7 @@ public void hasRequestMetadata_returnsTrue() {
209204 }
210205
211206 @ Test
212- public void hasRequestMetadataOnly_returnsTrue () {
207+ void hasRequestMetadataOnly_returnsTrue () {
213208 OAuth2Credentials credentials =
214209 UserCredentials .newBuilder ()
215210 .setClientId (CLIENT_ID )
@@ -220,7 +215,7 @@ public void hasRequestMetadataOnly_returnsTrue() {
220215 }
221216
222217 @ Test
223- public void addChangeListener_notifiesOnRefresh () throws IOException {
218+ void addChangeListener_notifiesOnRefresh () throws IOException {
224219 final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
225220 final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2" ;
226221 MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory ();
@@ -258,7 +253,7 @@ public void addChangeListener_notifiesOnRefresh() throws IOException {
258253 }
259254
260255 @ Test
261- public void removeChangeListener_unregisters_observer () throws IOException {
256+ void removeChangeListener_unregisters_observer () throws IOException {
262257 final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
263258 final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2" ;
264259 MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory ();
@@ -295,7 +290,7 @@ public void removeChangeListener_unregisters_observer() throws IOException {
295290 }
296291
297292 @ Test
298- public void getRequestMetadata_blocking_cachesExpiringToken () throws IOException {
293+ void getRequestMetadata_blocking_cachesExpiringToken () throws IOException {
299294 final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
300295 final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2" ;
301296 MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory ();
@@ -333,13 +328,11 @@ public void getRequestMetadata_blocking_cachesExpiringToken() throws IOException
333328 clock .addToCurrentTime (60 * 60 * 1000 );
334329 assertEquals (0 , transportFactory .transport .buildRequestCount );
335330
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 --);
343336
344337 // Reset the error and try again
345338 transportFactory .transport .setError (null );
@@ -349,7 +342,7 @@ public void getRequestMetadata_blocking_cachesExpiringToken() throws IOException
349342 }
350343
351344 @ Test
352- public void getRequestMetadata_async () throws IOException {
345+ void getRequestMetadata_async () {
353346 final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
354347 final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2" ;
355348 MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory ();
@@ -419,8 +412,7 @@ public void getRequestMetadata_async() throws IOException {
419412 }
420413
421414 @ Test
422- public void getRequestMetadata_async_refreshRace ()
423- throws ExecutionException , InterruptedException {
415+ void getRequestMetadata_async_refreshRace () throws ExecutionException , InterruptedException {
424416 final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
425417 MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory ();
426418 transportFactory .transport .addClient (CLIENT_ID , CLIENT_SECRET );
@@ -478,7 +470,7 @@ public Map<String, List<String>> call() throws Exception {
478470 }
479471
480472 @ Test
481- public void getRequestMetadata_temporaryToken_hasToken () throws IOException {
473+ void getRequestMetadata_temporaryToken_hasToken () throws IOException {
482474 OAuth2Credentials credentials =
483475 OAuth2Credentials .newBuilder ().setAccessToken (new AccessToken (ACCESS_TOKEN , null )).build ();
484476
@@ -488,7 +480,7 @@ public void getRequestMetadata_temporaryToken_hasToken() throws IOException {
488480 }
489481
490482 @ Test
491- public void getRequestMetadata_staleTemporaryToken () throws IOException , InterruptedException {
483+ void getRequestMetadata_staleTemporaryToken () throws IOException , InterruptedException {
492484 Instant actualExpiration = Instant .now ();
493485 Instant clientStale = actualExpiration .minus (OAuth2Credentials .DEFAULT_REFRESH_MARGIN );
494486
@@ -558,7 +550,7 @@ public AccessToken refreshAccessToken() {
558550 }
559551
560552 @ Test
561- public void getRequestMetadata_staleTemporaryToken_expirationWaits () throws Throwable {
553+ void getRequestMetadata_staleTemporaryToken_expirationWaits () throws Throwable {
562554 Instant actualExpiration = Instant .now ();
563555 Instant clientStale = actualExpiration .minus (OAuth2Credentials .DEFAULT_REFRESH_MARGIN );
564556 Instant clientExpired = actualExpiration .minus (OAuth2Credentials .DEFAULT_EXPIRATION_MARGIN );
@@ -627,7 +619,7 @@ public AccessToken refreshAccessToken() {
627619 }
628620
629621 @ Test
630- public void getRequestMetadata_singleFlightErrorSharing () {
622+ void getRequestMetadata_singleFlightErrorSharing () {
631623 Instant actualExpiration = Instant .now ();
632624 Instant clientStale = actualExpiration .minus (OAuth2Credentials .DEFAULT_REFRESH_MARGIN );
633625 Instant clientExpired = actualExpiration .minus (OAuth2Credentials .DEFAULT_EXPIRATION_MARGIN );
@@ -671,32 +663,17 @@ public Map<String, List<String>> call() throws Exception {
671663
672664 // Get the error that getRequestMetadata(uri) created
673665 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 ();
683667
684668 assertEquals (error , actualBlockingError );
685669
686670 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 );
695672 assertEquals (error , actualAsyncError );
696673 }
697674
698675 @ Test
699- public void getRequestMetadata_syncErrorsIncludeCallingStackframe () {
676+ void getRequestMetadata_syncErrorsIncludeCallingStackframe () {
700677 final OAuth2Credentials creds =
701678 new OAuth2Credentials () {
702679 @ Override
@@ -726,7 +703,7 @@ public AccessToken refreshAccessToken() {
726703 }
727704
728705 @ Test
729- public void refresh_refreshesToken () throws IOException {
706+ void refresh_refreshesToken () throws IOException {
730707 final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
731708 final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2" ;
732709 MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory ();
@@ -762,7 +739,7 @@ public void refresh_refreshesToken() throws IOException {
762739 }
763740
764741 @ Test
765- public void refreshIfExpired_refreshesToken () throws IOException {
742+ void refreshIfExpired_refreshesToken () throws IOException {
766743 final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
767744 final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2" ;
768745 MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory ();
@@ -805,15 +782,21 @@ public void refreshIfExpired_refreshesToken() throws IOException {
805782 assertEquals (1 , transportFactory .transport .buildRequestCount --);
806783 }
807784
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+ });
813796 }
814797
815798 @ Test
816- public void equals_true () throws IOException {
799+ void equals_true () {
817800 final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
818801 OAuth2Credentials credentials =
819802 OAuth2Credentials .newBuilder ().setAccessToken (new AccessToken (accessToken1 , null )).build ();
@@ -824,7 +807,7 @@ public void equals_true() throws IOException {
824807 }
825808
826809 @ Test
827- public void equals_false_accessToken () throws IOException {
810+ void equals_false_accessToken () {
828811 final String accessToken1 = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
829812 final String accessToken2 = "2/MkSJoj1xsli0AccessToken_NKPY2" ;
830813 OAuth2Credentials credentials =
@@ -836,7 +819,7 @@ public void equals_false_accessToken() throws IOException {
836819 }
837820
838821 @ Test
839- public void toString_containsFields () throws IOException {
822+ void toString_containsFields () {
840823 AccessToken accessToken = new AccessToken ("1/MkSJoj1xsli0AccessToken_NKPY2" , null );
841824 OAuth2Credentials credentials =
842825 OAuth2Credentials .newBuilder ().setAccessToken (accessToken ).build ();
@@ -851,7 +834,7 @@ public void toString_containsFields() throws IOException {
851834 }
852835
853836 @ Test
854- public void hashCode_equals () throws IOException {
837+ void hashCode_equals () throws IOException {
855838 final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
856839 OAuth2Credentials credentials =
857840 OAuth2Credentials .newBuilder ().setAccessToken (new AccessToken (accessToken , null )).build ();
@@ -861,7 +844,7 @@ public void hashCode_equals() throws IOException {
861844 }
862845
863846 @ Test
864- public void serialize () throws IOException , ClassNotFoundException {
847+ void serialize () throws IOException , ClassNotFoundException {
865848 final String accessToken = "1/MkSJoj1xsli0AccessToken_NKPY2" ;
866849 OAuth2Credentials credentials =
867850 OAuth2Credentials .newBuilder ().setAccessToken (new AccessToken (accessToken , null )).build ();
0 commit comments