Skip to content
This repository was archived by the owner on Apr 7, 2026. It is now read-only.

Commit ce25b66

Browse files
committed
added null checks for arguments in MutableCredentials
1 parent de735ad commit ce25b66

2 files changed

Lines changed: 34 additions & 2 deletions

File tree

google-cloud-spanner/src/main/java/com/google/cloud/spanner/MutableCredentials.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.net.URI;
2424
import java.util.List;
2525
import java.util.Map;
26+
import java.util.Objects;
2627
import java.util.Set;
2728
import java.util.concurrent.Executor;
2829
import javax.annotation.Nonnull;
@@ -49,7 +50,9 @@ public MutableCredentials(ServiceAccountCredentials credentials) {
4950
this(credentials, SpannerOptions.SCOPES);
5051
}
5152

52-
public MutableCredentials(ServiceAccountCredentials credentials, @Nonnull Set<String> scopes) {
53+
public MutableCredentials(@Nonnull ServiceAccountCredentials credentials, @Nonnull Set<String> scopes) {
54+
Objects.requireNonNull(credentials, "credentials must not be null");
55+
Objects.requireNonNull(scopes, "scopes must not be null");
5356
if (scopes.isEmpty()) {
5457
throw new IllegalArgumentException("Scopes must not be empty");
5558
}
@@ -60,13 +63,16 @@ public MutableCredentials(ServiceAccountCredentials credentials, @Nonnull Set<St
6063
/**
6164
* Replaces the current delegate with a newly scoped credentials instance.
6265
*
66+
* Note any in-flight RPC may continue to use the old credentials.
67+
*
6368
* <p>The provided {@link ServiceAccountCredentials} is scoped using the same scopes that were
6469
* supplied when this {@link MutableCredentials} instance was created.
6570
*
6671
* @param credentials the new base service account credentials to scope and use for client
6772
* authorization.
6873
*/
69-
public void updateCredentials(ServiceAccountCredentials credentials) {
74+
public void updateCredentials(@Nonnull ServiceAccountCredentials credentials) {
75+
Objects.requireNonNull(credentials, "credentials must not be null");
7076
delegate = (ServiceAccountCredentials) credentials.createScoped(scopes);
7177
}
7278

google-cloud-spanner/src/test/java/com/google/cloud/spanner/MutableCredentialsTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static org.junit.Assert.assertEquals;
2020
import static org.junit.Assert.assertFalse;
2121
import static org.junit.Assert.assertSame;
22+
import static org.junit.Assert.assertThrows;
2223
import static org.junit.Assert.assertTrue;
2324
import static org.mockito.ArgumentMatchers.any;
2425
import static org.mockito.Mockito.mock;
@@ -134,6 +135,31 @@ public void testCreateMutableCredentialsEmptyScopesThrowsError() {
134135
new MutableCredentials(initialCredentials, Collections.emptySet());
135136
}
136137

138+
@Test
139+
public void testCreateMutableCredentialsNullCredentialsThrowsError() {
140+
NullPointerException exception =
141+
assertThrows(NullPointerException.class, () -> new MutableCredentials(null, scopes));
142+
assertEquals("credentials must not be null", exception.getMessage());
143+
}
144+
145+
@Test
146+
public void testCreateMutableCredentialsNullScopesThrowsError() {
147+
NullPointerException exception =
148+
assertThrows(
149+
NullPointerException.class, () -> new MutableCredentials(initialCredentials, null));
150+
assertEquals("scopes must not be null", exception.getMessage());
151+
}
152+
153+
@Test
154+
public void testUpdateMutableCredentialsNullCredentialsThrowsError() throws IOException {
155+
setupInitialCredentials();
156+
MutableCredentials credentials = new MutableCredentials(initialCredentials, scopes);
157+
158+
NullPointerException exception =
159+
assertThrows(NullPointerException.class, () -> credentials.updateCredentials(null));
160+
assertEquals("credentials must not be null", exception.getMessage());
161+
}
162+
137163
private void validateInitialDelegatedCredentialsAreSet(
138164
MutableCredentials credentials, URI testUri) throws IOException {
139165
assertEquals(initialAuthType, credentials.getAuthenticationType());

0 commit comments

Comments
 (0)