Skip to content

Commit 017a6d2

Browse files
committed
Add null guards for Exec.ExecutionBuilder
The class `Exec.ExecutionBuilder` has three `private final` properties: `namespace`, `name`, and `command`. While it makes little sense to provide `null` as their values, if this happens, this would throw a `NullPointerException` at a much later time, complicating the debugging process. This commit implements a fail-fast `null` guards for these variables.
1 parent b093525 commit 017a6d2

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

util/src/main/java/io/kubernetes/client/Exec.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import java.util.HashMap;
4040
import java.util.List;
4141
import java.util.Map;
42+
import java.util.Objects;
4243
import java.util.Optional;
4344
import java.util.concurrent.CompletableFuture;
4445
import java.util.concurrent.CountDownLatch;
@@ -359,9 +360,9 @@ public final class ExecutionBuilder {
359360
private Consumer<Throwable> onUnhandledError;
360361

361362
private ExecutionBuilder(String namespace, String name, String[] command) {
362-
this.namespace = namespace;
363-
this.name = name;
364-
this.command = command;
363+
this.namespace = Objects.requireNonNull(namespace, "namespace");
364+
this.name = Objects.requireNonNull(name, "name");
365+
this.command = Objects.requireNonNull(command, "command");
365366
this.stdin = true;
366367
this.stdout = true;
367368
this.stderr = true;

util/src/test/java/io/kubernetes/client/ExecTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo;
2020
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
2121
import static org.junit.Assert.assertEquals;
22+
import static org.junit.Assert.assertThrows;
2223
import static org.mockito.ArgumentMatchers.any;
2324
import static org.mockito.Mockito.mock;
2425
import static org.mockito.Mockito.times;
@@ -281,4 +282,18 @@ public void testNonZeroBadIntExit() {
281282
int exitCode = Exec.parseExitCode(client, inputStream);
282283
assertEquals(-1, exitCode);
283284
}
285+
286+
@Test
287+
public void testExecutionBuilderNull() {
288+
Exec exec = new Exec(null);
289+
assertThrows(NullPointerException.class, () -> {
290+
exec.newExecutionBuilder(null, null, null);
291+
});
292+
assertThrows(NullPointerException.class, () -> {
293+
exec.newExecutionBuilder("", null, null);
294+
});
295+
assertThrows(NullPointerException.class, () -> {
296+
exec.newExecutionBuilder("", "", null);
297+
});
298+
}
284299
}

0 commit comments

Comments
 (0)