Skip to content

Commit 94e7307

Browse files
committed
test: tests for BranchUtil
1 parent 3f9dc06 commit 94e7307

File tree

2 files changed

+161
-14
lines changed

2 files changed

+161
-14
lines changed

devkit-utils/src/main/java/com/onixbyte/devkit/utils/BranchUtil.java

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -206,18 +206,4 @@ public void then(Runnable trueHandler) {
206206
then(trueHandler, null);
207207
}
208208

209-
/**
210-
* Get the boolean result.
211-
* <p>
212-
* <b>Note:</b> {@link BranchUtil} is not responsible for getting a raw boolean result, consider use
213-
* {@link BoolUtil} to replace.
214-
*
215-
* @return the result
216-
* @see BoolUtil
217-
*/
218-
@Deprecated(forRemoval = true)
219-
public boolean getResult() {
220-
return result;
221-
}
222-
223209
}
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
* Copyright (C) 2024-2025 OnixByte.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
*
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package com.onixbyte.devkit.utils;
19+
20+
import org.junit.jupiter.api.Test;
21+
22+
import java.util.concurrent.atomic.AtomicBoolean;
23+
import java.util.function.BooleanSupplier;
24+
25+
import static org.junit.jupiter.api.Assertions.*;
26+
27+
class BranchUtilTest {
28+
29+
// Test the static methods or(Boolean... values) and and(Boolean... values)
30+
@Test
31+
void testOrWithBooleanValues() {
32+
BranchUtil trueResult = BranchUtil.or(true, false, false);
33+
assertNotNull(trueResult);
34+
35+
BranchUtil falseResult = BranchUtil.or(false, false, false);
36+
assertNotNull(falseResult);
37+
}
38+
39+
@Test
40+
void testAndWithBooleanValues() {
41+
BranchUtil trueResult = BranchUtil.and(true, true, true);
42+
assertNotNull(trueResult);
43+
44+
BranchUtil falseResult = BranchUtil.and(true, false, true);
45+
assertNotNull(falseResult);
46+
}
47+
48+
// Test the static methods or(BooleanSupplier... valueSuppliers) and and(BooleanSupplier... valueSuppliers)
49+
@Test
50+
void testOrWithBooleanSuppliers() {
51+
BooleanSupplier trueSupplier = () -> true;
52+
BooleanSupplier falseSupplier = () -> false;
53+
54+
BranchUtil trueResult = BranchUtil.or(falseSupplier, trueSupplier);
55+
56+
BranchUtil falseResult = BranchUtil.or(falseSupplier, falseSupplier);
57+
}
58+
59+
@Test
60+
void testAndWithBooleanSuppliers() {
61+
BooleanSupplier trueSupplier = () -> true;
62+
BooleanSupplier falseSupplier = () -> false;
63+
64+
BranchUtil trueResult = BranchUtil.and(trueSupplier, trueSupplier);
65+
66+
BranchUtil falseResult = BranchUtil.and(trueSupplier, falseSupplier);
67+
}
68+
69+
// Test thenSupply(T, T)
70+
@Test
71+
void testThenSupplyBothSuppliers_ResultTrue() {
72+
BranchUtil b = BranchUtil.and(true);
73+
String trueVal = "yes";
74+
String falseVal = "no";
75+
76+
String result = b.thenSupply(() -> trueVal, () -> falseVal);
77+
assertEquals(trueVal, result);
78+
}
79+
80+
@Test
81+
void testThenSupplyBothSuppliers_ResultFalse_WithFalseSupplier() {
82+
BranchUtil b = BranchUtil.and(false);
83+
String trueVal = "yes";
84+
String falseVal = "no";
85+
86+
String result = b.thenSupply(() -> trueVal, () -> falseVal);
87+
assertEquals(falseVal, result);
88+
}
89+
90+
@Test
91+
void testThenSupplyBothSuppliers_ResultFalse_NoFalseSupplier() {
92+
BranchUtil b = BranchUtil.and(false);
93+
String trueVal = "yes";
94+
95+
String result = b.thenSupply(() -> trueVal, null);
96+
assertNull(result);
97+
}
98+
99+
@Test
100+
void testThenSupplySingleTrueSupplier_ResultTrue() {
101+
BranchUtil b = BranchUtil.and(true);
102+
String trueVal = "success";
103+
104+
String result = b.thenSupply(() -> trueVal);
105+
assertEquals(trueVal, result);
106+
}
107+
108+
@Test
109+
void testThenSupplySingleTrueSupplier_ResultFalse() {
110+
BranchUtil b = BranchUtil.and(false);
111+
String trueVal = "success";
112+
113+
String result = b.thenSupply(() -> trueVal);
114+
assertNull(result);
115+
}
116+
117+
// Test then(Runnable, Runnable)
118+
@Test
119+
void testThenWithBothHandlers_ResultTrue() {
120+
BranchUtil b = BranchUtil.and(true);
121+
AtomicBoolean trueRun = new AtomicBoolean(false);
122+
AtomicBoolean falseRun = new AtomicBoolean(false);
123+
124+
b.then(() -> trueRun.set(true), () -> falseRun.set(true));
125+
126+
assertTrue(trueRun.get());
127+
assertFalse(falseRun.get());
128+
}
129+
130+
@Test
131+
void testThenWithBothHandlers_ResultFalse() {
132+
BranchUtil b = BranchUtil.and(false);
133+
AtomicBoolean trueRun = new AtomicBoolean(false);
134+
AtomicBoolean falseRun = new AtomicBoolean(false);
135+
136+
b.then(() -> trueRun.set(true), () -> falseRun.set(true));
137+
138+
assertFalse(trueRun.get());
139+
assertTrue(falseRun.get());
140+
}
141+
142+
@Test
143+
void testThenWithOnlyTrueHandler_ResultTrue() {
144+
BranchUtil b = BranchUtil.and(true);
145+
AtomicBoolean trueRun = new AtomicBoolean(false);
146+
147+
b.then(() -> trueRun.set(true));
148+
149+
assertTrue(trueRun.get());
150+
}
151+
152+
@Test
153+
void testThenWithOnlyTrueHandler_ResultFalse() {
154+
BranchUtil b = BranchUtil.and(false);
155+
AtomicBoolean trueRun = new AtomicBoolean(false);
156+
157+
b.then(() -> trueRun.set(true));
158+
159+
assertFalse(trueRun.get());
160+
}
161+
}

0 commit comments

Comments
 (0)