View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.juneau.commons.utils;
18  
19  import static org.apache.juneau.commons.utils.PredicateUtils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import java.io.ByteArrayOutputStream;
23  import java.io.PrintStream;
24  import java.util.*;
25  import java.util.function.*;
26  
27  import org.junit.jupiter.api.*;
28  
29  class PredicateUtils_Test {
30  
31  	//====================================================================================================
32  	// Constructor (line 24)
33  	//====================================================================================================
34  	@Test
35  	void a00_constructor() {
36  		// Test line 24: class declaration
37  		// PredicateUtils has a private constructor, so it cannot be instantiated.
38  		// Line 24 (class declaration) is covered by using the class's static methods.
39  	}
40  
41  	//====================================================================================================
42  	// consumeIf(Predicate<T>, Consumer<T>, T)
43  	//====================================================================================================
44  	@Test
45  	void a001_consumeIf() {
46  		List<String> consumed = new ArrayList<>();
47  		Consumer<String> consumer = consumed::add;
48  		
49  		// When predicate is null, should consume
50  		consumeIf(null, consumer, "test");
51  		assertEquals(1, consumed.size());
52  		assertEquals("test", consumed.get(0));
53  		
54  		// When predicate matches, should consume
55  		consumed.clear();
56  		Predicate<String> matches = s -> s.equals("match");
57  		consumeIf(matches, consumer, "match");
58  		assertEquals(1, consumed.size());
59  		assertEquals("match", consumed.get(0));
60  		
61  		// When predicate doesn't match, should not consume
62  		consumed.clear();
63  		Predicate<String> noMatch = s -> s.equals("match");
64  		consumeIf(noMatch, consumer, "nomatch");
65  		assertTrue(consumed.isEmpty());
66  		
67  		// Test with different types
68  		List<Integer> intConsumed = new ArrayList<>();
69  		Consumer<Integer> intConsumer = intConsumed::add;
70  		Predicate<Integer> even = i -> i % 2 == 0;
71  		consumeIf(even, intConsumer, 2);
72  		assertEquals(1, intConsumed.size());
73  		assertEquals(2, intConsumed.get(0));
74  		
75  		consumeIf(even, intConsumer, 3);
76  		assertEquals(1, intConsumed.size()); // Should not add 3
77  	}
78  
79  	//====================================================================================================
80  	// peek()
81  	//====================================================================================================
82  	@Test
83  	void a002_peek() {
84  		// Capture stderr output
85  		PrintStream originalErr = System.err;
86  		ByteArrayOutputStream errCapture = new ByteArrayOutputStream();
87  		System.setErr(new PrintStream(errCapture));
88  		
89  		try {
90  			// Test peek() function
91  			Function<String, String> peekFunc = peek();
92  			String result = peekFunc.apply("test value");
93  			
94  			// Should return the value unchanged
95  			assertEquals("test value", result);
96  			
97  			// Should have printed to stderr
98  			String output = errCapture.toString();
99  			assertTrue(output.contains("test value"), "Output should contain 'test value', but was: " + output);
100 			
101 			// Test with null
102 			errCapture.reset();
103 			Function<Object, Object> peekFunc2 = peek();
104 			Object result2 = peekFunc2.apply(null);
105 			assertNull(result2);
106 			String output2 = errCapture.toString();
107 			assertTrue(output2.contains("null"), "Output should contain 'null', but was: " + output2);
108 			
109 			// Test with different types
110 			errCapture.reset();
111 			Function<Integer, Integer> peekInt = peek();
112 			Integer result3 = peekInt.apply(123);
113 			assertEquals(123, result3);
114 			String output3 = errCapture.toString();
115 			assertTrue(output3.contains("123"), "Output should contain '123', but was: " + output3);
116 		} finally {
117 			System.setErr(originalErr);
118 		}
119 	}
120 
121 	//====================================================================================================
122 	// peek(String, Function<T,?>)
123 	//====================================================================================================
124 	@Test
125 	void a003_peek_withMessage() {
126 		// Capture stderr output
127 		PrintStream originalErr = System.err;
128 		ByteArrayOutputStream errCapture = new ByteArrayOutputStream();
129 		System.setErr(new PrintStream(errCapture));
130 		
131 		try {
132 			// Test peek() with message and formatter
133 			Function<String, String> peekFunc = peek("Processing: {0}", s -> s.toUpperCase());
134 			String result = peekFunc.apply("test");
135 			
136 			// Should return the value unchanged
137 			assertEquals("test", result);
138 			
139 			// Should have printed formatted message to stderr
140 			String output = errCapture.toString();
141 			assertTrue(output.contains("Processing: TEST"), "Output should contain 'Processing: TEST', but was: " + output);
142 			
143 			// Test with different formatter
144 			errCapture.reset();
145 			Function<Integer, Integer> peekInt = peek("Value: {0}", i -> i * 2);
146 			Integer result2 = peekInt.apply(5);
147 			assertEquals(5, result2);
148 			String output2 = errCapture.toString();
149 			assertTrue(output2.contains("Value: 10"), "Output should contain 'Value: 10', but was: " + output2);
150 			
151 			// Test with null value
152 			errCapture.reset();
153 			Function<String, String> peekNull = peek("Null value: {0}", s -> s == null ? "null" : s);
154 			String result3 = peekNull.apply(null);
155 			assertNull(result3);
156 			String output3 = errCapture.toString();
157 			assertTrue(output3.contains("Null value: null"), "Output should contain 'Null value: null', but was: " + output3);
158 			
159 			// Test with complex formatter
160 			errCapture.reset();
161 			class Person {
162 				String name;
163 				Person(String name) { this.name = name; }
164 			}
165 			Function<Person, Person> peekPerson = peek("Person: {0}", p -> p.name);
166 			Person person = new Person("John");
167 			Person result4 = peekPerson.apply(person);
168 			assertSame(person, result4);
169 			String output4 = errCapture.toString();
170 			assertTrue(output4.contains("Person: John"), "Output should contain 'Person: John', but was: " + output4);
171 		} finally {
172 			System.setErr(originalErr);
173 		}
174 	}
175 
176 	//====================================================================================================
177 	// test(Predicate<T>, T)
178 	//====================================================================================================
179 	@Test
180 	void a004_test() {
181 		// When predicate is null, should return true
182 		assertTrue(test(null, "any value"));
183 		assertTrue(test(null, null));
184 		assertTrue(test(null, 123));
185 		
186 		// When predicate matches, should return true
187 		Predicate<String> matches = s -> s.equals("match");
188 		assertTrue(test(matches, "match"));
189 		
190 		// When predicate doesn't match, should return false
191 		assertFalse(test(matches, "nomatch"));
192 		
193 		// Test with different types
194 		Predicate<Integer> even = i -> i % 2 == 0;
195 		assertTrue(test(even, 2));
196 		assertTrue(test(even, 4));
197 		assertFalse(test(even, 3));
198 		assertFalse(test(even, 5));
199 		
200 		// Test with null value
201 		Predicate<String> notNull = s -> s != null;
202 		assertTrue(test(notNull, "test"));
203 		assertFalse(test(notNull, null));
204 		
205 		// Test with always true predicate
206 		Predicate<Object> alwaysTrue = o -> true;
207 		assertTrue(test(alwaysTrue, "anything"));
208 		assertTrue(test(alwaysTrue, null));
209 		assertTrue(test(alwaysTrue, 123));
210 		
211 		// Test with always false predicate
212 		Predicate<Object> alwaysFalse = o -> false;
213 		assertFalse(test(alwaysFalse, "anything"));
214 		assertFalse(test(alwaysFalse, null));
215 		assertFalse(test(alwaysFalse, 123));
216 	}
217 }