diff --git a/temporal-sdk/src/main/java/io/temporal/worker/WorkerFactory.java b/temporal-sdk/src/main/java/io/temporal/worker/WorkerFactory.java index 70bcf28c76..0d23890283 100644 --- a/temporal-sdk/src/main/java/io/temporal/worker/WorkerFactory.java +++ b/temporal-sdk/src/main/java/io/temporal/worker/WorkerFactory.java @@ -181,7 +181,8 @@ public Worker newWorker(String taskQueue) { */ public synchronized Worker newWorker(String taskQueue, WorkerOptions options) { Preconditions.checkArgument( - !Strings.isNullOrEmpty(taskQueue), "taskQueue should not be an empty string"); + !Strings.isNullOrEmpty(taskQueue) && !taskQueue.trim().isEmpty(), + "taskQueue should not be an empty or blank string"); Preconditions.checkState( state == State.Initial, String.format(statusErrorMessage, "create new worker", state.name(), State.Initial.name())); diff --git a/temporal-sdk/src/test/java/io/temporal/worker/WorkerFactoryValidationTest.java b/temporal-sdk/src/test/java/io/temporal/worker/WorkerFactoryValidationTest.java new file mode 100644 index 0000000000..f093b2f1f0 --- /dev/null +++ b/temporal-sdk/src/test/java/io/temporal/worker/WorkerFactoryValidationTest.java @@ -0,0 +1,38 @@ +package io.temporal.worker; + +import static org.junit.Assert.assertThrows; + +import io.temporal.testing.TestWorkflowEnvironment; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class WorkerFactoryValidationTest { + + private TestWorkflowEnvironment env; + + @Before + public void setUp() { + env = TestWorkflowEnvironment.newInstance(); + } + + @After + public void tearDown() { + env.close(); + } + + @Test + public void newWorkerRejectsNullTaskQueue() { + assertThrows(IllegalArgumentException.class, () -> env.getWorkerFactory().newWorker(null)); + } + + @Test + public void newWorkerRejectsEmptyTaskQueue() { + assertThrows(IllegalArgumentException.class, () -> env.getWorkerFactory().newWorker("")); + } + + @Test + public void newWorkerRejectsBlankTaskQueue() { + assertThrows(IllegalArgumentException.class, () -> env.getWorkerFactory().newWorker(" ")); + } +}