This is an automated email from the ASF dual-hosted git repository.
jamesbognar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/juneau.git
The following commit(s) were added to refs/heads/master by this push:
new 40073d7844 Unit tests
40073d7844 is described below
commit 40073d78440c781c65e5d260440cfd37f1047a34
Author: James Bognar <[email protected]>
AuthorDate: Wed Dec 3 13:01:03 2025 -0800
Unit tests
---
.../juneau/commons/utils/CollectionUtils.java | 2 +-
.../org/apache/juneau/commons/utils/Console.java | 6 +
.../juneau/commons/utils/PredicateUtils.java | 144 +-------
.../juneau/commons/utils/WeightedAverage.java | 12 +-
.../juneau/commons/utils/CollectionUtils_Test.java | 38 ++
.../apache/juneau/commons/utils/Console_Test.java | 11 +
.../juneau/commons/utils/PredicateUtils_Test.java | 393 ++++++++++-----------
7 files changed, 244 insertions(+), 362 deletions(-)
diff --git
a/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/CollectionUtils.java
b/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/CollectionUtils.java
index acd8c58bd1..2b8c68e726 100644
---
a/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/CollectionUtils.java
+++
b/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/CollectionUtils.java
@@ -432,7 +432,7 @@ public class CollectionUtils {
for (var value : arr) {
result.add(value);
}
- } else if (componentType == short.class) {
+ } else /* (componentType == short.class) */ {
var arr = (short[])array;
for (var value : arr) {
result.add(value);
diff --git
a/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/Console.java
b/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/Console.java
index 70b4f2dc31..eeaf864edd 100644
---
a/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/Console.java
+++
b/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/Console.java
@@ -28,6 +28,12 @@ import java.text.*;
*/
public class Console {
+ /**
+ * Constructor.
+ */
+ public Console() {
+ }
+
/**
* Prints a message with arguments to {@link System#err}.
*
diff --git
a/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/PredicateUtils.java
b/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/PredicateUtils.java
index c5ef37c3d4..542ade7fb4 100644
---
a/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/PredicateUtils.java
+++
b/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/PredicateUtils.java
@@ -16,57 +16,13 @@
*/
package org.apache.juneau.commons.utils;
-import static org.apache.juneau.commons.utils.Utils.*;
-
import java.util.function.*;
-import org.apache.juneau.commons.reflect.*;
-
/**
* Utility methods for composing {@link Predicate} instances.
*/
public final class PredicateUtils {
- /**
- * Returns a predicate that is the short-circuiting AND of the provided
predicates.
- *
- * <p>
- * {@code null} entries are ignored. If all entries are {@code null} or
no predicates are provided,
- * the returned predicate always returns {@code true}.
- *
- * @param <T> The input type of the predicate.
- * @param predicates The predicates to combine.
- * @return A composed predicate representing the logical AND.
- */
- @SafeVarargs
- public static <T> Predicate<T> and(Predicate<T>...predicates) {
- Predicate<T> result = t -> true;
- if (nn(predicates)) {
- for (var p : predicates) {
- if (nn(p))
- result = result.and(p);
- }
- }
- return result;
- }
-
- /**
- * Returns a predicate that evaluates to true only when the value is an
instance of the given type and the provided
- * predicate also returns true for the cast value.
- *
- * @param <T> The target type to test and cast to.
- * @param type The target class.
- * @param predicate The predicate to apply to the cast value. Can be
null (treated as always-true after type check).
- * @return A predicate over Object that performs an instanceof check
AND the provided predicate.
- */
- public static <T> Predicate<Object> andType(Class<T> type, Predicate<?
super T> predicate) {
- Predicate<Object> p = type::isInstance;
- if (nn(predicate)) {
- p = p.and(o -> predicate.test(type.cast(o)));
- }
- return p;
- }
-
/**
* Consumes the specified value if the predicate is <jk>null</jk> or
matches the specified value.
*
@@ -80,104 +36,6 @@ public final class PredicateUtils {
consumer.accept(value);
}
- /**
- * Returns a predicate that tests whether an {@link ElementInfo} has
the specified flag.
- *
- * <p>
- * Useful for filtering streams of reflection elements.
- *
- * <h5 class='section'>Example:</h5>
- * <p class='bjava'>
- * List<ClassInfo> classes = ...;
- * ClassInfo publicClass = classes.stream()
- * .filter(is(PUBLIC))
- * .findFirst()
- * .orElse(<jk>null</jk>);
- * </p>
- *
- * @param flag The flag to test for.
- * @return A predicate that returns {@code true} if the element has the
specified flag.
- */
- public static Predicate<ElementInfo> is(ElementFlag flag) {
- return ei -> ei.is(flag);
- }
-
- /**
- * Returns a predicate that tests whether an {@link ElementInfo} has
all of the specified flags.
- *
- * <p>
- * Useful for filtering streams of reflection elements.
- *
- * <h5 class='section'>Example:</h5>
- * <p class='bjava'>
- * List<ClassInfo> classes = ...;
- * ClassInfo publicNonDeprecated = classes.stream()
- * .filter(isAll(PUBLIC, NOT_DEPRECATED))
- * .findFirst()
- * .orElse(<jk>null</jk>);
- * </p>
- *
- * @param flags The flags to test for.
- * @return A predicate that returns {@code true} if the element has all
of the specified flags.
- */
- public static Predicate<ElementInfo> isAll(ElementFlag...flags) {
- return ei -> ei.isAll(flags);
- }
-
- /**
- * Returns a predicate that tests whether an {@link ElementInfo} has
any of the specified flags.
- *
- * <p>
- * Useful for filtering streams of reflection elements.
- *
- * <h5 class='section'>Example:</h5>
- * <p class='bjava'>
- * List<ClassInfo> classes = ...;
- * List<ClassInfo> visibleClasses = classes.stream()
- * .filter(isAny(PUBLIC, PROTECTED))
- * .collect(Collectors.toList());
- * </p>
- *
- * @param flags The flags to test for.
- * @return A predicate that returns {@code true} if the element has any
of the specified flags.
- */
- public static Predicate<ElementInfo> isAny(ElementFlag...flags) {
- return ei -> ei.isAny(flags);
- }
-
- /**
- * Returns a predicate that tests whether the input value is an
instance of the specified type.
- *
- * @param type The class object representing the target type.
- * @return A predicate that returns {@code true} if the value is an
instance of {@code type}.
- */
- public static Predicate<?> isType(Class<?> type) {
- return v -> nn(type) && type.isInstance(v);
- }
-
- /**
- * Returns a predicate that is the short-circuiting OR of the provided
predicates.
- *
- * <p>
- * {@code null} entries are ignored. If all entries are {@code null} or
no predicates are provided,
- * the returned predicate always returns {@code false}.
- *
- * @param <T> The input type of the predicate.
- * @param predicates The predicates to combine.
- * @return A composed predicate representing the logical OR.
- */
- @SafeVarargs
- public static <T> Predicate<T> or(Predicate<T>...predicates) {
- Predicate<T> result = t -> false;
- if (nn(predicates)) {
- for (var p : predicates) {
- if (nn(p))
- result = result.or(p);
- }
- }
- return result;
- }
-
/**
* Returns a function that prints the input value to stderr and returns
it unchanged.
*
@@ -230,7 +88,7 @@ public final class PredicateUtils {
/**
* Returns <jk>true</jk> if the specified predicate is <jk>null</jk> or
matches the specified value.
-
+
* @param <T> The type being tested.
* @param predicate The predicate.
* @param value The value to test.
diff --git
a/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/WeightedAverage.java
b/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/WeightedAverage.java
index 38994baf0f..23f1b2342a 100644
---
a/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/WeightedAverage.java
+++
b/juneau-core/juneau-commons/src/main/java/org/apache/juneau/commons/utils/WeightedAverage.java
@@ -35,14 +35,10 @@ public class WeightedAverage {
*/
public WeightedAverage add(int w, Number v) {
if (nn(v)) {
- try {
- double w1 = weight, w2 = w;
- weight = Math.addExact(weight, w);
- if (weight != 0) {
- value = (value * (w1 / weight)) +
(v.floatValue() * (w2 / weight));
- }
- } catch (@SuppressWarnings("unused")
ArithmeticException ae) {
- throw new ArithmeticException("Weight
overflow.");
+ double w1 = weight, w2 = w;
+ weight = Math.addExact(weight, w);
+ if (weight != 0) {
+ value = (value * (w1 / weight)) +
(v.floatValue() * (w2 / weight));
}
}
return this;
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/commons/utils/CollectionUtils_Test.java
b/juneau-utest/src/test/java/org/apache/juneau/commons/utils/CollectionUtils_Test.java
index 5336cc0e0b..e43863bd5d 100644
---
a/juneau-utest/src/test/java/org/apache/juneau/commons/utils/CollectionUtils_Test.java
+++
b/juneau-utest/src/test/java/org/apache/juneau/commons/utils/CollectionUtils_Test.java
@@ -1334,6 +1334,11 @@ class CollectionUtils_Test extends TestBase {
List<?> result7 = toList(Optional.of("test"));
assertEquals(1, result7.size());
assertEquals("test", result7.get(0));
+
+ // Test line 2009: unsupported type throws exception
+ assertThrows(RuntimeException.class, () -> {
+ toList(new Object() {}); // Unsupported type
+ });
}
//====================================================================================================
@@ -1349,6 +1354,26 @@ class CollectionUtils_Test extends TestBase {
assertThrows(UnsupportedOperationException.class, () ->
result.add("d"));
assertThrows(IllegalArgumentException.class, () ->
toSet((String[])null));
+
+ // Test lines 2085, 2093: Iterator behavior
+ Iterator<String> it = result.iterator();
+ assertTrue(it.hasNext());
+ assertEquals("a", it.next());
+ assertEquals("b", it.next());
+ assertEquals("c", it.next());
+ assertFalse(it.hasNext());
+
+ // Test line 2085: NoSuchElementException when calling next()
after exhausted
+ assertThrows(java.util.NoSuchElementException.class, () -> {
+ it.next();
+ });
+
+ // Test line 2093: UnsupportedOperationException when calling
remove()
+ Iterator<String> it2 = result.iterator();
+ it2.next(); // Move to first element
+ assertThrows(UnsupportedOperationException.class, () -> {
+ it2.remove();
+ });
}
//====================================================================================================
@@ -1380,6 +1405,19 @@ class CollectionUtils_Test extends TestBase {
assertTrue(result.contains("a"));
assertTrue(result.contains("b"));
assertTrue(result.contains("c"));
+
+ // Test line 2189: null object returns early
+ List<Object> result2 = new ArrayList<>();
+ traverse(null, result2::add);
+ assertTrue(result2.isEmpty());
+
+ // Test line 2194: Stream handling
+ List<Object> result3 = new ArrayList<>();
+ traverse(java.util.stream.Stream.of("x", "y", "z"),
result3::add);
+ assertEquals(3, result3.size());
+ assertTrue(result3.contains("x"));
+ assertTrue(result3.contains("y"));
+ assertTrue(result3.contains("z"));
}
//====================================================================================================
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/commons/utils/Console_Test.java
b/juneau-utest/src/test/java/org/apache/juneau/commons/utils/Console_Test.java
index 57a97b60c8..e7543fd62d 100644
---
a/juneau-utest/src/test/java/org/apache/juneau/commons/utils/Console_Test.java
+++
b/juneau-utest/src/test/java/org/apache/juneau/commons/utils/Console_Test.java
@@ -49,6 +49,17 @@ class Console_Test extends TestBase {
System.setErr(originalErr);
}
+
//====================================================================================================
+ // Constructor (line 29)
+
//====================================================================================================
+ @Test
+ void a00_constructor() {
+ // Test line 29: class instantiation
+ // Console has an implicit public no-arg constructor
+ var instance = new Console();
+ assertNotNull(instance);
+ }
+
//====================================================================================================
// out(String, Object...) tests
//====================================================================================================
diff --git
a/juneau-utest/src/test/java/org/apache/juneau/commons/utils/PredicateUtils_Test.java
b/juneau-utest/src/test/java/org/apache/juneau/commons/utils/PredicateUtils_Test.java
index e2884070ff..440a993bdb 100644
---
a/juneau-utest/src/test/java/org/apache/juneau/commons/utils/PredicateUtils_Test.java
+++
b/juneau-utest/src/test/java/org/apache/juneau/commons/utils/PredicateUtils_Test.java
@@ -16,229 +16,202 @@
*/
package org.apache.juneau.commons.utils;
-import static org.apache.juneau.commons.reflect.ElementFlag.*;
import static org.apache.juneau.commons.utils.PredicateUtils.*;
import static org.junit.jupiter.api.Assertions.*;
+import java.io.ByteArrayOutputStream;
+import java.io.PrintStream;
import java.util.*;
import java.util.function.*;
-import java.util.stream.*;
-import org.apache.juneau.commons.reflect.*;
import org.junit.jupiter.api.*;
-public class PredicateUtils_Test {
+class PredicateUtils_Test {
//====================================================================================================
- // Constructor (line 28)
+ // Constructor (line 24)
//====================================================================================================
- // Note: PredicateUtils has a private constructor, so it cannot be
instantiated.
- // Line 28 (class declaration) is covered by using the class's static
methods.
+ @Test
+ void a00_constructor() {
+ // Test line 24: class declaration
+ // PredicateUtils has a private constructor, so it cannot be
instantiated.
+ // Line 24 (class declaration) is covered by using the class's
static methods.
+ }
- @Test
- void and_allNull_returnsTrue() {
- Predicate<String> p = and(null, null);
- assertTrue(p.test("anything"));
- assertTrue(p.test(null));
- }
-
- @Test
- void and_empty_returnsTrue() {
- Predicate<String> p = and();
- assertTrue(p.test("x"));
- }
-
- @Test
- void and_single_predicate() {
- Predicate<Integer> isEven = x -> x != null && x % 2 == 0;
- Predicate<Integer> p = and(isEven);
- assertTrue(p.test(2));
- assertFalse(p.test(3));
- assertFalse(p.test(null));
- }
-
- @Test
- void and_multiple_predicates() {
- Predicate<Integer> isEven = x -> x != null && x % 2 == 0;
- Predicate<Integer> gt10 = x -> x != null && x > 10;
- Predicate<Integer> p = and(isEven, gt10);
- assertTrue(p.test(12));
- assertFalse(p.test(11));
- assertFalse(p.test(10));
- assertFalse(p.test(9));
- assertFalse(p.test(null));
- }
-
- @Test
- void and_ignoresNullEntries() {
- Predicate<String> startsA = s -> s != null && s.startsWith("A");
- Predicate<String> p = and(null, startsA, null);
- assertTrue(p.test("Alpha"));
- assertFalse(p.test("Beta"));
- }
-
- @Test
- void or_allNull_returnsFalse() {
- Predicate<String> p = or(null, null);
- assertFalse(p.test("anything"));
- assertFalse(p.test(null));
- }
-
- @Test
- void or_empty_returnsFalse() {
- Predicate<String> p = or();
- assertFalse(p.test("x"));
- }
-
- @Test
- void or_single_predicate() {
- Predicate<Integer> isEven = x -> x != null && x % 2 == 0;
- Predicate<Integer> p = or(isEven);
- assertTrue(p.test(2));
- assertFalse(p.test(3));
- assertFalse(p.test(null));
- }
-
- @Test
- void or_multiple_predicates() {
- Predicate<Integer> isEven = x -> x != null && x % 2 == 0;
- Predicate<Integer> gt10 = x -> x != null && x > 10;
- Predicate<Integer> p = or(isEven, gt10);
- assertTrue(p.test(12));
- assertTrue(p.test(11));
- assertTrue(p.test(10));
- assertFalse(p.test(9));
- assertFalse(p.test(null));
- }
-
- @Test
- void or_ignoresNullEntries() {
- Predicate<String> startsA = s -> s != null && s.startsWith("A");
- Predicate<String> p = or(null, startsA, null);
- assertTrue(p.test("Alpha"));
- assertFalse(p.test("Beta"));
- }
-
- // Test classes for ElementInfo filtering
- public static class PublicClass {}
- private static class PrivateClass {}
- public static final class PublicFinalClass {}
- @Deprecated
- public static class DeprecatedPublicClass {}
-
- @Test
- void is_singleFlag_matches() {
- List<ClassInfo> classes = Stream.of(PublicClass.class,
PrivateClass.class)
- .map(ClassInfo::of)
- .collect(Collectors.toList());
-
- ClassInfo result = classes.stream()
- .filter(is(PUBLIC))
- .findFirst()
- .orElse(null);
-
- assertNotNull(result);
- assertEquals(PublicClass.class.getName(), result.getName());
- }
-
- @Test
- void is_singleFlag_noMatch() {
- List<ClassInfo> classes = Stream.of(PublicClass.class)
- .map(ClassInfo::of)
- .collect(Collectors.toList());
-
- ClassInfo result = classes.stream()
- .filter(is(PRIVATE))
- .findFirst()
- .orElse(null);
-
- assertNull(result);
- }
-
- @Test
- void isAll_multipleFlags_matches() {
- List<ClassInfo> classes = Stream.of(PublicClass.class,
PrivateClass.class, PublicFinalClass.class, DeprecatedPublicClass.class)
- .map(ClassInfo::of)
- .collect(Collectors.toList());
-
- ClassInfo result = classes.stream()
- .filter(isAll(PUBLIC, NOT_DEPRECATED))
- .findFirst()
- .orElse(null);
-
- assertNotNull(result);
- assertEquals(PublicClass.class.getName(), result.getName());
- }
-
- @Test
- void isAll_multipleFlags_collectAll() {
- List<ClassInfo> classes = Stream.of(PublicClass.class,
PrivateClass.class, PublicFinalClass.class)
- .map(ClassInfo::of)
- .collect(Collectors.toList());
-
- List<ClassInfo> results = classes.stream()
- .filter(isAll(PUBLIC, NOT_DEPRECATED))
- .collect(Collectors.toList());
-
- assertEquals(2, results.size());
- assertTrue(results.stream().anyMatch(c ->
c.getName().equals(PublicClass.class.getName())));
- assertTrue(results.stream().anyMatch(c ->
c.getName().equals(PublicFinalClass.class.getName())));
- }
-
- @Test
- void isAll_noMatch() {
- List<ClassInfo> classes = Stream.of(PublicClass.class,
PrivateClass.class)
- .map(ClassInfo::of)
- .collect(Collectors.toList());
-
- ClassInfo result = classes.stream()
- .filter(isAll(PUBLIC, FINAL))
- .findFirst()
- .orElse(null);
-
- assertNull(result);
- }
-
- @Test
- void isAny_multipleFlags_matchesFirst() {
- List<ClassInfo> classes = Stream.of(PublicClass.class,
PrivateClass.class)
- .map(ClassInfo::of)
- .collect(Collectors.toList());
-
- List<ClassInfo> results = classes.stream()
- .filter(isAny(PUBLIC, PROTECTED))
- .collect(Collectors.toList());
-
- assertEquals(1, results.size());
- assertEquals(PublicClass.class.getName(), results.get(0).getName());
- }
-
- @Test
- void isAny_multipleFlags_matchesMultiple() {
- List<ClassInfo> classes = Stream.of(PublicClass.class,
PrivateClass.class, PublicFinalClass.class)
- .map(ClassInfo::of)
- .collect(Collectors.toList());
-
- List<ClassInfo> results = classes.stream()
- .filter(isAny(PUBLIC, PRIVATE))
- .collect(Collectors.toList());
-
- assertEquals(3, results.size());
- }
+
//====================================================================================================
+ // consumeIf(Predicate<T>, Consumer<T>, T)
+
//====================================================================================================
+ @Test
+ void a001_consumeIf() {
+ List<String> consumed = new ArrayList<>();
+ Consumer<String> consumer = consumed::add;
+
+ // When predicate is null, should consume
+ consumeIf(null, consumer, "test");
+ assertEquals(1, consumed.size());
+ assertEquals("test", consumed.get(0));
+
+ // When predicate matches, should consume
+ consumed.clear();
+ Predicate<String> matches = s -> s.equals("match");
+ consumeIf(matches, consumer, "match");
+ assertEquals(1, consumed.size());
+ assertEquals("match", consumed.get(0));
+
+ // When predicate doesn't match, should not consume
+ consumed.clear();
+ Predicate<String> noMatch = s -> s.equals("match");
+ consumeIf(noMatch, consumer, "nomatch");
+ assertTrue(consumed.isEmpty());
+
+ // Test with different types
+ List<Integer> intConsumed = new ArrayList<>();
+ Consumer<Integer> intConsumer = intConsumed::add;
+ Predicate<Integer> even = i -> i % 2 == 0;
+ consumeIf(even, intConsumer, 2);
+ assertEquals(1, intConsumed.size());
+ assertEquals(2, intConsumed.get(0));
+
+ consumeIf(even, intConsumer, 3);
+ assertEquals(1, intConsumed.size()); // Should not add 3
+ }
- @Test
- void isAny_noMatch() {
- List<ClassInfo> classes = Stream.of(PublicClass.class)
- .map(ClassInfo::of)
- .collect(Collectors.toList());
+
//====================================================================================================
+ // peek()
+
//====================================================================================================
+ @Test
+ void a002_peek() {
+ // Capture stderr output
+ PrintStream originalErr = System.err;
+ ByteArrayOutputStream errCapture = new ByteArrayOutputStream();
+ System.setErr(new PrintStream(errCapture));
+
+ try {
+ // Test peek() function
+ Function<String, String> peekFunc = peek();
+ String result = peekFunc.apply("test value");
+
+ // Should return the value unchanged
+ assertEquals("test value", result);
+
+ // Should have printed to stderr
+ String output = errCapture.toString();
+ assertTrue(output.contains("test value"), "Output
should contain 'test value', but was: " + output);
+
+ // Test with null
+ errCapture.reset();
+ Function<Object, Object> peekFunc2 = peek();
+ Object result2 = peekFunc2.apply(null);
+ assertNull(result2);
+ String output2 = errCapture.toString();
+ assertTrue(output2.contains("null"), "Output should
contain 'null', but was: " + output2);
+
+ // Test with different types
+ errCapture.reset();
+ Function<Integer, Integer> peekInt = peek();
+ Integer result3 = peekInt.apply(123);
+ assertEquals(123, result3);
+ String output3 = errCapture.toString();
+ assertTrue(output3.contains("123"), "Output should
contain '123', but was: " + output3);
+ } finally {
+ System.setErr(originalErr);
+ }
+ }
- ClassInfo result = classes.stream()
- .filter(isAny(PRIVATE, PROTECTED))
- .findFirst()
- .orElse(null);
+
//====================================================================================================
+ // peek(String, Function<T,?>)
+
//====================================================================================================
+ @Test
+ void a003_peek_withMessage() {
+ // Capture stderr output
+ PrintStream originalErr = System.err;
+ ByteArrayOutputStream errCapture = new ByteArrayOutputStream();
+ System.setErr(new PrintStream(errCapture));
+
+ try {
+ // Test peek() with message and formatter
+ Function<String, String> peekFunc = peek("Processing:
{0}", s -> s.toUpperCase());
+ String result = peekFunc.apply("test");
+
+ // Should return the value unchanged
+ assertEquals("test", result);
+
+ // Should have printed formatted message to stderr
+ String output = errCapture.toString();
+ assertTrue(output.contains("Processing: TEST"), "Output
should contain 'Processing: TEST', but was: " + output);
+
+ // Test with different formatter
+ errCapture.reset();
+ Function<Integer, Integer> peekInt = peek("Value: {0}",
i -> i * 2);
+ Integer result2 = peekInt.apply(5);
+ assertEquals(5, result2);
+ String output2 = errCapture.toString();
+ assertTrue(output2.contains("Value: 10"), "Output
should contain 'Value: 10', but was: " + output2);
+
+ // Test with null value
+ errCapture.reset();
+ Function<String, String> peekNull = peek("Null value:
{0}", s -> s == null ? "null" : s);
+ String result3 = peekNull.apply(null);
+ assertNull(result3);
+ String output3 = errCapture.toString();
+ assertTrue(output3.contains("Null value: null"),
"Output should contain 'Null value: null', but was: " + output3);
+
+ // Test with complex formatter
+ errCapture.reset();
+ class Person {
+ String name;
+ Person(String name) { this.name = name; }
+ }
+ Function<Person, Person> peekPerson = peek("Person:
{0}", p -> p.name);
+ Person person = new Person("John");
+ Person result4 = peekPerson.apply(person);
+ assertSame(person, result4);
+ String output4 = errCapture.toString();
+ assertTrue(output4.contains("Person: John"), "Output
should contain 'Person: John', but was: " + output4);
+ } finally {
+ System.setErr(originalErr);
+ }
+ }
- assertNull(result);
- }
+
//====================================================================================================
+ // test(Predicate<T>, T)
+
//====================================================================================================
+ @Test
+ void a004_test() {
+ // When predicate is null, should return true
+ assertTrue(test(null, "any value"));
+ assertTrue(test(null, null));
+ assertTrue(test(null, 123));
+
+ // When predicate matches, should return true
+ Predicate<String> matches = s -> s.equals("match");
+ assertTrue(test(matches, "match"));
+
+ // When predicate doesn't match, should return false
+ assertFalse(test(matches, "nomatch"));
+
+ // Test with different types
+ Predicate<Integer> even = i -> i % 2 == 0;
+ assertTrue(test(even, 2));
+ assertTrue(test(even, 4));
+ assertFalse(test(even, 3));
+ assertFalse(test(even, 5));
+
+ // Test with null value
+ Predicate<String> notNull = s -> s != null;
+ assertTrue(test(notNull, "test"));
+ assertFalse(test(notNull, null));
+
+ // Test with always true predicate
+ Predicate<Object> alwaysTrue = o -> true;
+ assertTrue(test(alwaysTrue, "anything"));
+ assertTrue(test(alwaysTrue, null));
+ assertTrue(test(alwaysTrue, 123));
+
+ // Test with always false predicate
+ Predicate<Object> alwaysFalse = o -> false;
+ assertFalse(test(alwaysFalse, "anything"));
+ assertFalse(test(alwaysFalse, null));
+ assertFalse(test(alwaysFalse, 123));
+ }
}
-
-