@@ -395,15 +395,11 @@ public void testParseUserImpersonationForADC() {
395395
396396 @ Test
397397 public void testGetServiceAccountImpersonatedCredentialsForADC () throws Exception {
398- java .nio .file .Path tempAdcFile = createTempAdcFile ();
399- String originalEnv = System .getenv ("GOOGLE_APPLICATION_CREDENTIALS" );
398+ GoogleCredentials dummySourceCredentials = GoogleCredentials .newBuilder ().build ();
400399
401- try {
402- if (originalEnv == null ) {
403- setEnvironmentVariable (
404- "GOOGLE_APPLICATION_CREDENTIALS" , tempAdcFile .toAbsolutePath ().toString ());
405- clearDefaultCredentialsCache ();
406- }
400+ try (org .mockito .MockedStatic <GoogleCredentials > mockedCreds =
401+ org .mockito .Mockito .mockStatic (GoogleCredentials .class )) {
402+ mockedCreds .when (GoogleCredentials ::getApplicationDefault ).thenReturn (dummySourceCredentials );
407403
408404 Map <String , String > authProperties =
409405 BigQueryJdbcOAuthUtility .parseOAuthProperties (
@@ -418,11 +414,6 @@ public void testGetServiceAccountImpersonatedCredentialsForADC() throws Exceptio
418414 authProperties , java .util .Collections .EMPTY_MAP , false , null );
419415
420416 assertThat (credentials ).isInstanceOf (ImpersonatedCredentials .class );
421- } finally {
422- if (originalEnv == null ) {
423- removeEnvironmentVariable ("GOOGLE_APPLICATION_CREDENTIALS" );
424- clearDefaultCredentialsCache ();
425- }
426417 }
427418 }
428419
@@ -480,56 +471,4 @@ public void testPrivateKeyFromP12Bytes_wrong_password() {
480471 assertTrue (false );
481472 }
482473 }
483-
484- private static java .nio .file .Path createTempAdcFile () throws Exception {
485- String dummyJson =
486- "{\n "
487- + " \" type\" : \" service_account\" ,\n "
488- + " \" project_id\" : \" dummy-project\" ,\n "
489- + " \" private_key_id\" : \" dummy-key-id\" ,\n "
490- + " \" private_key\" : \" "
491- + fake_pkcs8_key .replace ("\n " , "\\ n" )
492- + "\" ,\n "
493- + " \" client_email\" : \" dummy@email.com\" ,\n "
494- + " \" client_id\" : \" dummy-client-id\" ,\n "
495- + " \" auth_uri\" : \" https://accounts.google.com/o/oauth2/auth\" ,\n "
496- + " \" token_uri\" : \" https://oauth2.googleapis.com/token\" \n "
497- + "}" ;
498-
499- java .nio .file .Path tempFile = java .nio .file .Files .createTempFile ("adc" , ".json" );
500- java .nio .file .Files .write (tempFile , dummyJson .getBytes ());
501- tempFile .toFile ().deleteOnExit ();
502- return tempFile ;
503- }
504-
505- private static void setEnvironmentVariable (String key , String value ) throws Exception {
506- Class <?> processEnvironmentClass = Class .forName ("java.lang.ProcessEnvironment" );
507- java .lang .reflect .Field theEnvironmentField =
508- processEnvironmentClass .getDeclaredField ("theEnvironment" );
509- theEnvironmentField .setAccessible (true );
510- java .util .Map <String , String > env =
511- (java .util .Map <String , String >) theEnvironmentField .get (null );
512- env .put (key , value );
513- }
514-
515- private static void removeEnvironmentVariable (String key ) throws Exception {
516- Class <?> processEnvironmentClass = Class .forName ("java.lang.ProcessEnvironment" );
517- java .lang .reflect .Field theEnvironmentField =
518- processEnvironmentClass .getDeclaredField ("theEnvironment" );
519- theEnvironmentField .setAccessible (true );
520- java .util .Map <String , String > env =
521- (java .util .Map <String , String >) theEnvironmentField .get (null );
522- env .remove (key );
523- }
524-
525- private static void clearDefaultCredentialsCache () throws Exception {
526- Class <?> providerClass = Class .forName ("com.google.auth.oauth2.DefaultCredentialsProvider" );
527- java .lang .reflect .Field defaultField = providerClass .getDeclaredField ("DEFAULT" );
528- defaultField .setAccessible (true );
529- Object provider = defaultField .get (null );
530-
531- java .lang .reflect .Field cachedCredsField = providerClass .getDeclaredField ("cachedCredentials" );
532- cachedCredsField .setAccessible (true );
533- cachedCredsField .set (provider , null );
534- }
535474}
0 commit comments