Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.spanner.ErrorCode;
import com.google.cloud.spanner.SpannerException;
import java.io.FileInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand All @@ -35,9 +36,9 @@
@RunWith(JUnit4.class)
public class CredentialsServiceTest {
private static final String FILE_TEST_PATH =
CredentialsServiceTest.class.getResource("test-key.json").getFile();
private static final String APP_DEFAULT_FILE_TEST_PATH =
CredentialsServiceTest.class.getResource("test-key-app-default.json").getFile();
CredentialsServiceTest.class.getResource("test-key.json").getPath();
private static final String SA_APP_DEFAULT_FILE_TEST_PATH =
CredentialsServiceTest.class.getResource("test-key-app-default.json").getPath();

private static final String TEST_PROJECT_ID = "test-project";
private static final String APP_DEFAULT_PROJECT_ID = "app-default-test-project";
Expand All @@ -49,7 +50,11 @@ public class CredentialsServiceTest {
GoogleCredentials internalGetApplicationDefault() throws IOException {
// Read application default credentials directly from a specific file instead of actually
// fetching the default from the environment.
return GoogleCredentials.fromStream(new FileInputStream(APP_DEFAULT_FILE_TEST_PATH));
return ServiceAccountCredentials.fromStream(
// Calling `getResource().getPath()` on Windows returns a string that might start with
// something like `/C:/...`. Paths.get() interprets the leading / as part of the path
// and would be invalid. Use `new File().toPath()` to read from these files.
Files.newInputStream(new File(SA_APP_DEFAULT_FILE_TEST_PATH).toPath()));
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
import com.google.common.base.Strings;
import com.google.longrunning.OperationsClient;
import com.google.longrunning.OperationsSettings;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -352,9 +351,7 @@ public void deniedListBackupOperations() throws IOException {
.setTransportChannelProvider(InstantiatingGrpcChannelProvider.newBuilder().build())
.setEndpoint("spanner.googleapis.com:443")
.setCredentialsProvider(
FixedCredentialsProvider.create(
GoogleCredentials.fromStream(
new FileInputStream(System.getenv("GOOGLE_APPLICATION_CREDENTIALS")))))
FixedCredentialsProvider.create(GoogleCredentials.getApplicationDefault()))
.build())) {
client.listOperations(backupId.getName() + "/operations", "");
fail("Expected PermissionDeniedException");
Expand Down