This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-exec.git
commit 130f930d1008ab7ac1d56806f3fa87961f95d727 Author: Gary D. Gregory <[email protected]> AuthorDate: Sun Aug 17 10:31:16 2025 -0400 Watchdog.builder().get() now uses a default timeout of 30 seconds instead of throwing a NullPointerException --- src/changes/changes.xml | 1 + .../java/org/apache/commons/exec/Watchdog.java | 22 +++++++------- .../java/org/apache/commons/exec/WatchdogTest.java | 35 ++++++++++++++++++++++ 3 files changed, 48 insertions(+), 10 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index bd18beff..627924ff 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -25,6 +25,7 @@ <body> <release version="1.6.0" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required."> <!-- FIX --> + <action type="fix" dev="ggregory" due-to="Gary Gregory">Watchdog.builder().get() now uses a default timeout of 30 seconds instead of throwing a NullPointerException.</action> <action type="fix" dev="ggregory" due-to="Gary Gregory">Fix Checkstyle issues.</action> <!-- ADD --> <action type="add" dev="ggregory" due-to="Gary Gregory">TimeoutObserver now extends Consumer<Watchdog>.</action> diff --git a/src/main/java/org/apache/commons/exec/Watchdog.java b/src/main/java/org/apache/commons/exec/Watchdog.java index 31000cde..faf80080 100644 --- a/src/main/java/org/apache/commons/exec/Watchdog.java +++ b/src/main/java/org/apache/commons/exec/Watchdog.java @@ -39,13 +39,15 @@ public class Watchdog implements Runnable { */ public static final class Builder implements Supplier<Watchdog> { + private static final Duration DEFAULT_TIMEOUT = Duration.ofSeconds(30); + /** Thread factory. */ private ThreadFactory threadFactory; /** * Timeout duration. */ - private Duration timeout; + private Duration timeout = DEFAULT_TIMEOUT; /** * Constructs a new instance. @@ -61,7 +63,7 @@ public class Watchdog implements Runnable { */ @Override public Watchdog get() { - return new Watchdog(threadFactory, timeout); + return new Watchdog(this); } /** @@ -78,11 +80,11 @@ public class Watchdog implements Runnable { /** * Sets the timeout duration. * - * @param timeout the timeout duration. + * @param timeout the timeout duration, null resets to the default 30 seconds. * @return {@code this} instance. */ public Builder setTimeout(final Duration timeout) { - this.timeout = timeout; + this.timeout = timeout != null ? timeout : DEFAULT_TIMEOUT; return this; } @@ -126,7 +128,7 @@ public class Watchdog implements Runnable { */ @Deprecated public Watchdog(final long timeoutMillis) { - this(null, Duration.ofMillis(timeoutMillis)); + this(builder().setTimeout(Duration.ofMillis(timeoutMillis))); } /** @@ -135,12 +137,12 @@ public class Watchdog implements Runnable { * @param threadFactory the thread factory. * @param timeout the timeout duration. */ - private Watchdog(final ThreadFactory threadFactory, final Duration timeout) { - if (timeout.isNegative() || Duration.ZERO.equals(timeout)) { - throw new IllegalArgumentException("timeout must not be less than 1."); + private Watchdog(final Builder builder) { + if (builder.timeout.isNegative() || Duration.ZERO.equals(builder.timeout)) { + throw new IllegalArgumentException("Timeout must be positive."); } - this.timeoutMillis = timeout.toMillis(); - this.threadFactory = threadFactory; + this.timeoutMillis = builder.timeout.toMillis(); + this.threadFactory = builder.threadFactory; } /** diff --git a/src/test/java/org/apache/commons/exec/WatchdogTest.java b/src/test/java/org/apache/commons/exec/WatchdogTest.java new file mode 100644 index 00000000..9d155e16 --- /dev/null +++ b/src/test/java/org/apache/commons/exec/WatchdogTest.java @@ -0,0 +1,35 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.commons.exec; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +import org.junit.jupiter.api.Test; + +/** + * Tests {@link Watchdog}. + */ +class WatchdogTest { + + @Test + void testBuilder() { + assertNotNull(Watchdog.builder().get()); + } +}
