From e6796a0ec6105f8c87e5330b51c8c9ee75df353f Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Wed, 16 Aug 2023 13:56:26 -0400 Subject: [PATCH] [LANG-1706] ThreadUtils find methods should not return null items #1098 --- src/changes/changes.xml | 1 + .../org/apache/commons/lang3/ThreadUtils.java | 4 ++-- .../apache/commons/lang3/ThreadUtilsTest.java | 17 +++++++++++++++++ 3 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 55e0335dd..aad9a5fa0 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -54,6 +54,7 @@ The type attribute can be add,update,fix,remove. [Javadoc] Fix Incorrect Description in Processor isAarch64() #1093. [Javadoc] Point to right getShortClassName flavor in Javadoc for relevant notes #1097. Improve performance of StringUtils.isMixedCase() #1096. + ThreadUtils find methods should not return null items #1098. Add Functions#function(Function). Add FailableFunction#function(FailableFunction). diff --git a/src/main/java/org/apache/commons/lang3/ThreadUtils.java b/src/main/java/org/apache/commons/lang3/ThreadUtils.java index aa1250b3b..b71457de8 100644 --- a/src/main/java/org/apache/commons/lang3/ThreadUtils.java +++ b/src/main/java/org/apache/commons/lang3/ThreadUtils.java @@ -284,7 +284,7 @@ public static Collection findThreadGroups(final ThreadGroup threadG count = threadGroup.enumerate(threadGroups, recurse); //return value of enumerate() must be strictly less than the array size according to Javadoc } while (count >= threadGroups.length); - return Collections.unmodifiableCollection(Stream.of(threadGroups).filter(predicate).collect(Collectors.toList())); + return Collections.unmodifiableCollection(Stream.of(threadGroups).limit(count).filter(predicate).collect(Collectors.toList())); } /** @@ -376,7 +376,7 @@ public static Collection findThreads(final ThreadGroup threadGroup, fina count = threadGroup.enumerate(threads, recurse); //return value of enumerate() must be strictly less than the array size according to javadoc } while (count >= threads.length); - return Collections.unmodifiableCollection(Stream.of(threads).filter(predicate).collect(Collectors.toList())); + return Collections.unmodifiableCollection(Stream.of(threads).limit(count).filter(predicate).collect(Collectors.toList())); } /** diff --git a/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java b/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java index 89e135cf3..638fb149e 100644 --- a/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java +++ b/src/test/java/org/apache/commons/lang3/ThreadUtilsTest.java @@ -31,7 +31,9 @@ import java.lang.reflect.Modifier; import java.time.Duration; import java.util.Arrays; +import java.util.Collection; import java.util.List; +import java.util.Objects; import java.util.concurrent.CountDownLatch; import java.util.function.Predicate; @@ -148,6 +150,20 @@ public void testConstructor() { assertFalse(Modifier.isFinal(ThreadUtils.class.getModifiers())); } + @Test + public void testGetAllThreadGroupsDoesNotReturnNull() { + // LANG-1706 getAllThreadGroups and findThreadGroups should not return null items + Collection threads = ThreadUtils.getAllThreadGroups(); + assertEquals(0, threads.stream().filter(Objects::isNull).count()); + } + + @Test + public void testGetAllThreadsDoesNotReturnNull() { + // LANG-1706 getAllThreads and findThreads should not return null items + Collection threads = ThreadUtils.getAllThreads(); + assertEquals(0, threads.stream().filter(Objects::isNull).count()); + } + @Test public void testInvalidThreadId() { assertThrows(IllegalArgumentException.class, () -> ThreadUtils.findThreadById(-5L)); @@ -390,4 +406,5 @@ public void testThreadsSameName() throws InterruptedException { alsot1.join(); } } + }