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.lang;
18  
19  import static org.apache.juneau.commons.utils.CollectionUtils.*;
20  import static org.junit.jupiter.api.Assertions.*;
21  
22  import org.apache.juneau.*;
23  import org.junit.jupiter.api.*;
24  
25  class Flag_Test extends TestBase {
26  
27  	//-----------------------------------------------------------------------------------------------------------------
28  	// Basic tests
29  	//-----------------------------------------------------------------------------------------------------------------
30  
31  	@Test
32  	void a01_create() {
33  		var a = Flag.create();
34  		assertFalse(a.isSet());
35  		assertTrue(a.isUnset());
36  	}
37  
38  	@Test
39  	void a02_of_false() {
40  		var a = Flag.of(false);
41  		assertFalse(a.isSet());
42  		assertTrue(a.isUnset());
43  	}
44  
45  	@Test
46  	void a03_of_true() {
47  		var a = Flag.of(true);
48  		assertTrue(a.isSet());
49  		assertFalse(a.isUnset());
50  	}
51  
52  	@Test
53  	void a04_set() {
54  		var a = Flag.create();
55  		var b = a.set();
56  		assertSame(a, b);
57  		assertTrue(a.isSet());
58  		assertFalse(a.isUnset());
59  	}
60  
61  	@Test
62  	void a05_unset() {
63  		var a = Flag.of(true);
64  		var b = a.unset();
65  		assertSame(a, b);
66  		assertFalse(a.isSet());
67  		assertTrue(a.isUnset());
68  	}
69  
70  	@Test
71  	void a06_setIf_false() {
72  		var a = Flag.create();
73  		a.setIf(false);
74  		assertFalse(a.isSet());
75  	}
76  
77  	@Test
78  	void a07_setIf_true() {
79  		var a = Flag.create();
80  		a.setIf(true);
81  		assertTrue(a.isSet());
82  	}
83  
84  	@Test
85  	void a08_setIf_logicalOr() {
86  		// Test that setIf uses logical OR - once set, remains set
87  		var a = Flag.create();
88  		a.setIf(false);
89  		assertFalse(a.isSet());
90  		a.setIf(true);
91  		assertTrue(a.isSet());
92  		a.setIf(false);  // Should remain true
93  		assertTrue(a.isSet());
94  	}
95  
96  	@Test
97  	void a09_getAndSet() {
98  		var a = Flag.create();
99  		assertFalse(a.getAndSet());
100 		assertTrue(a.isSet());
101 		assertTrue(a.getAndSet());
102 		assertTrue(a.isSet());
103 	}
104 
105 	@Test
106 	void a10_getAndUnset() {
107 		var a = Flag.of(true);
108 		assertTrue(a.getAndUnset());
109 		assertFalse(a.isSet());
110 		assertFalse(a.getAndUnset());
111 		assertFalse(a.isSet());
112 	}
113 
114 	//-----------------------------------------------------------------------------------------------------------------
115 	// Conditional execution tests
116 	//-----------------------------------------------------------------------------------------------------------------
117 
118 	@Test
119 	void b01_ifSet_whenSet() {
120 		var a = Flag.of(true);
121 		var b = Flag.create();
122 
123 		a.ifSet(() -> b.set());
124 
125 		assertTrue(b.isSet());
126 	}
127 
128 	@Test
129 	void b02_ifSet_whenUnset() {
130 		var a = Flag.create();
131 		var b = Flag.create();
132 
133 		a.ifSet(() -> b.set());
134 
135 		assertFalse(b.isSet());
136 	}
137 
138 	@Test
139 	void b03_ifNotSet_whenSet() {
140 		var a = Flag.of(true);
141 		var b = Flag.create();
142 
143 		a.ifNotSet(() -> b.set());
144 
145 		assertFalse(b.isSet());
146 	}
147 
148 	@Test
149 	void b04_ifNotSet_whenUnset() {
150 		var a = Flag.create();
151 		var b = Flag.create();
152 
153 		a.ifNotSet(() -> b.set());
154 
155 		assertTrue(b.isSet());
156 	}
157 
158 	@Test
159 	void b05_ifSet_chaining() {
160 		var a = Flag.of(true);
161 		var b = a.ifSet(() -> {});
162 		assertSame(a, b);
163 	}
164 
165 	@Test
166 	void b06_ifNotSet_chaining() {
167 		var a = Flag.create();
168 		var b = a.ifNotSet(() -> {});
169 		assertSame(a, b);
170 	}
171 
172 	//-----------------------------------------------------------------------------------------------------------------
173 	// Use case scenarios
174 	//-----------------------------------------------------------------------------------------------------------------
175 
176 	@Test
177 	void c01_initializeOnce() {
178 		var a = Flag.create();
179 
180 		// Simulate initializing only once
181 		for (var i = 0; i < 5; i++) {
182 			a.ifNotSet(() -> a.set());
183 		}
184 
185 		assertTrue(a.isSet());
186 	}
187 
188 	@Test
189 	void c02_lambdaUsage() {
190 		var a = Flag.create();
191 
192 		// Simulate using flag in a lambda
193 		var list = l("a", "b", "c");
194 		list.forEach(x -> {
195 			if ("b".equals(x)) {
196 				a.set();
197 			}
198 		});
199 
200 		assertTrue(a.isSet());
201 	}
202 
203 	@Test
204 	void c03_getAndSet_togglePattern() {
205 		var a = Flag.create();
206 
207 		// First time - not set, so do something
208 		if (!a.getAndSet()) {
209 			// First execution
210 			assertTrue(true);
211 		}
212 
213 		// Second time - already set, so skip
214 		if (!a.getAndSet()) {
215 			// Should not execute
216 			fail("Should not execute");
217 		}
218 	}
219 
220 	@Test
221 	void c04_multipleConditions() {
222 		var a = Flag.create();
223 		var b = Flag.create();
224 		var c = Flag.create();
225 
226 		a.setIf(true);
227 		b.setIf(false);
228 		c.setIf(true);
229 
230 		assertTrue(a.isSet());
231 		assertFalse(b.isSet());
232 		assertTrue(c.isSet());
233 
234 		// Count how many are set
235 		var d = 0;
236 		if (a.isSet()) d++;
237 		if (b.isSet()) d++;
238 		if (c.isSet()) d++;
239 
240 		assertEquals(2, d);
241 	}
242 
243 	//-----------------------------------------------------------------------------------------------------------------
244 	// toString(), equals(), hashCode()
245 	//-----------------------------------------------------------------------------------------------------------------
246 
247 	@Test
248 	void d01_toString_true() {
249 		var flag = Flag.of(true);
250 		assertEquals("true", flag.toString());
251 	}
252 
253 	@Test
254 	void d02_toString_false() {
255 		var flag = Flag.of(false);
256 		assertEquals("false", flag.toString());
257 	}
258 
259 	@Test
260 	void d03_equals_sameValue() {
261 		var flag1 = Flag.of(true);
262 		var flag2 = Flag.of(true);
263 		assertTrue(flag1.equals(flag2));
264 		assertTrue(flag2.equals(flag1));
265 	}
266 
267 	@Test
268 	void d04_equals_differentValue() {
269 		var flag1 = Flag.of(true);
270 		var flag2 = Flag.of(false);
271 		assertFalse(flag1.equals(flag2));
272 		assertFalse(flag2.equals(flag1));
273 	}
274 
275 	@Test
276 	void d05_equals_sameInstance() {
277 		var flag = Flag.of(true);
278 		assertTrue(flag.equals(flag));
279 	}
280 
281 	@Test
282 	void d06_equals_notAFlag() {
283 		var flag = Flag.of(true);
284 		assertFalse(flag.equals(null));
285 	}
286 
287 	@Test
288 	void d07_hashCode_sameValue() {
289 		var flag1 = Flag.of(true);
290 		var flag2 = Flag.of(true);
291 		assertEquals(flag1.hashCode(), flag2.hashCode());
292 	}
293 
294 	@Test
295 	void d08_hashCode_differentValue() {
296 		var flag1 = Flag.of(true);
297 		var flag2 = Flag.of(false);
298 		// Different values should have different hash codes (though not guaranteed)
299 		assertNotEquals(flag1.hashCode(), flag2.hashCode());
300 	}
301 
302 	@Test
303 	void d09_hashCode_booleanHashCode() {
304 		var flagTrue = Flag.of(true);
305 		var flagFalse = Flag.of(false);
306 		assertEquals(Boolean.hashCode(true), flagTrue.hashCode());
307 		assertEquals(Boolean.hashCode(false), flagFalse.hashCode());
308 	}
309 }