Skip to content

Commit b345e1f

Browse files
committed
fix test
1 parent 70a819f commit b345e1f

3 files changed

Lines changed: 12 additions & 68 deletions

File tree

java-bigquery/google-cloud-bigquery-jdbc/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,11 @@
307307
<artifactId>mockito-core</artifactId>
308308
<scope>test</scope>
309309
</dependency>
310+
<dependency>
311+
<groupId>org.mockito</groupId>
312+
<artifactId>mockito-inline</artifactId>
313+
<scope>test</scope>
314+
</dependency>
310315
<dependency>
311316
<groupId>org.mockito</groupId>
312317
<artifactId>mockito-junit-jupiter</artifactId>

java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryCallableStatementTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ public void testSetDateCalParamByName() throws SQLException {
914914
Calendar expectedCal = mock(Calendar.class);
915915

916916
doReturn(1L).when(expectedDate).getTime();
917-
doReturn(1L).when(expectedCal).getTime();
917+
doReturn(new java.util.Date(1L)).when(expectedCal).getTime();
918918
doReturn(1L).when(expectedCal).getTimeInMillis();
919919
statement.setDate(PARAM_KEY, expectedDate, expectedCal);
920920
Date actual = statement.getDate(PARAM_KEY);
@@ -1033,7 +1033,7 @@ public void testSetTimeCalParamByName() throws SQLException {
10331033
Calendar expectedCal = mock(Calendar.class);
10341034

10351035
doReturn(1L).when(expectedTime).getTime();
1036-
doReturn(1L).when(expectedCal).getTime();
1036+
doReturn(new java.util.Date(1L)).when(expectedCal).getTime();
10371037
doReturn(1L).when(expectedCal).getTimeInMillis();
10381038
statement.setTime(PARAM_KEY, expectedTime, expectedCal);
10391039
Time actual = statement.getTime(PARAM_KEY);
@@ -1062,7 +1062,7 @@ public void testSetTimestampCalParamByName() throws SQLException {
10621062
Calendar expectedCal = mock(Calendar.class);
10631063

10641064
doReturn(1L).when(expectedTimestamp).getTime();
1065-
doReturn(1L).when(expectedCal).getTime();
1065+
doReturn(new java.util.Date(1L)).when(expectedCal).getTime();
10661066
doReturn(1L).when(expectedCal).getTimeInMillis();
10671067
statement.setTimestamp(PARAM_KEY, expectedTimestamp, expectedCal);
10681068
Timestamp actual = statement.getTimestamp(PARAM_KEY);

java-bigquery/google-cloud-bigquery-jdbc/src/test/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcOAuthUtilityTest.java

Lines changed: 4 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)