|
| 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