refactor: rename modules

Closes #75
This commit is contained in:
siujamo
2025-06-17 16:32:57 +08:00
parent 6793e90331
commit 71e7993352
79 changed files with 501 additions and 336 deletions
@@ -0,0 +1,79 @@
/*
* Copyright (C) 2024-2025 OnixByte.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.onixbyte.devkit.utils;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import static org.junit.jupiter.api.Assertions.*;
class AesUtilTest {
@Test
void testEncryptAndDecryptByte() throws GeneralSecurityException {
byte[] secretKey = "43f72073956d4c81".getBytes(StandardCharsets.UTF_8);
byte[] originalData = "Hello World".getBytes(StandardCharsets.UTF_8);
byte[] encryptedData = AesUtil.encrypt(originalData, secretKey);
assertNotNull(encryptedData);
byte[] decryptedData = AesUtil.decrypt(encryptedData, secretKey);
assertNotNull(decryptedData);
assertArrayEquals(originalData, decryptedData);
}
@Test
void testEncryptAndDecryptString() throws GeneralSecurityException {
var secret = "43f72073956d4c81";
var originalData = "Hello World";
var encryptedData = AesUtil.encrypt(originalData, secret);
assertNotNull(encryptedData);
assertNotEquals(originalData, encryptedData);
var decryptedData = AesUtil.decrypt(encryptedData, secret);
assertNotNull(decryptedData);
assertEquals(originalData, decryptedData);
}
@Test
void testEncryptWithWrongKeyFails() throws GeneralSecurityException {
var secret = "43f72073956d4c81";
var wrongSecret = "0000000000000000";
var originalData = "Hello World";
var encryptedData = AesUtil.encrypt(originalData.getBytes(StandardCharsets.UTF_8),
secret.getBytes(StandardCharsets.UTF_8));
assertNotNull(encryptedData);
// When decrypting with the wrong key, a BadPaddingException or IllegalBlockSizeException is expected to be thrown
assertThrows(GeneralSecurityException.class, () -> {
AesUtil.decrypt(encryptedData, wrongSecret.getBytes(StandardCharsets.UTF_8));
});
}
@Test
void testGenerateRandomSecret() {
var randomSecret = AesUtil.generateRandomSecret();
assertNotNull(randomSecret);
assertEquals(16, randomSecret.length());
}
}
@@ -0,0 +1,103 @@
/*
* Copyright (C) 2024-2025 OnixByte.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.onixbyte.devkit.utils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.*;
public class Base64UtilTest {
@Test
void testEncodeAndDecodeWithUtf8() {
var original = "Hello, Base64!";
var encoded = Base64Util.encode(original);
assertNotNull(encoded);
assertNotEquals(original, encoded);
var decoded = Base64Util.decode(encoded);
assertNotNull(decoded);
assertEquals(original, decoded);
}
@Test
void testEncodeAndDecodeWithCharset() {
var original = "编码测试"; // Some unicode characters (Chinese)
var charset = StandardCharsets.UTF_8;
var encoded = Base64Util.encode(original, charset);
assertNotNull(encoded);
assertNotEquals(original, encoded);
var decoded = Base64Util.decode(encoded, charset);
assertNotNull(decoded);
assertEquals(original, decoded);
}
@Test
void testEncodeUrlComponentsAndDecodeWithUtf8() {
var original = "This is a test for URL-safe Base64 encoding+!";
var encodedUrl = Base64Util.encodeUrlComponents(original);
assertNotNull(encodedUrl);
assertNotEquals(original, encodedUrl);
// URL-safe encoding should not contain '+' or '/' characters
assertFalse(encodedUrl.contains("+"));
assertFalse(encodedUrl.contains("/"));
var decodedUrl = Base64Util.decodeUrlComponents(encodedUrl);
assertNotNull(decodedUrl);
assertEquals(original, decodedUrl);
}
@Test
void testEncodeUrlComponentsAndDecodeWithCharset() {
var original = "测试 URL 安全编码"; // Unicode string
var charset = StandardCharsets.UTF_8;
var encodedUrl = Base64Util.encodeUrlComponents(original, charset);
assertNotNull(encodedUrl);
assertNotEquals(original, encodedUrl);
var decodedUrl = Base64Util.decodeUrlComponents(encodedUrl, charset);
assertNotNull(decodedUrl);
assertEquals(original, decodedUrl);
}
@Test
void testEncodeAndDecodeEmptyString() {
var original = "";
var encoded = Base64Util.encode(original);
assertNotNull(encoded);
assertEquals("", Base64Util.decode(encoded));
}
@Test
void testEncodeAndDecodeNullSafety() {
// Since Base64Util does not explicitly handle null, the test expects NPE if null is input
assertThrows(NullPointerException.class, () -> Base64Util.encode(null));
assertThrows(NullPointerException.class, () -> Base64Util.decode(null));
}
}
@@ -0,0 +1,137 @@
/*
* Copyright (C) 2024-2025 OnixByte.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.onixbyte.devkit.utils;
import org.junit.jupiter.api.Test;
import java.util.function.BooleanSupplier;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class BoolUtilTest {
// Tests for and(Boolean... values)
@Test
void and_AllTrueValues_ReturnsTrue() {
assertTrue(BoolUtil.and(true, true, true));
}
@Test
void and_SomeFalseValues_ReturnsFalse() {
assertFalse(BoolUtil.and(true, false, true));
}
@Test
void and_AllFalseValues_ReturnsFalse() {
assertFalse(BoolUtil.and(false, false));
}
@Test
void and_WithNullValues_IgnoresNulls() {
assertTrue(BoolUtil.and(true, null, true));
assertFalse(BoolUtil.and(true, null, false));
}
@Test
void and_AllNullValues_ReturnsTrue() {
// Stream after filtering null is empty, allMatch on empty returns true
assertTrue(BoolUtil.and((Boolean) null, null));
}
// Tests for and(BooleanSupplier... valueSuppliers)
@Test
void and_AllSuppliersTrue_ReturnsTrue() {
BooleanSupplier trueSupplier = () -> true;
BooleanSupplier falseSupplier = () -> false;
assertTrue(BoolUtil.and(trueSupplier, trueSupplier));
assertFalse(BoolUtil.and(trueSupplier, falseSupplier));
}
@Test
void and_WithNullSuppliers_IgnoresNull() {
BooleanSupplier trueSupplier = () -> true;
assertTrue(BoolUtil.and(trueSupplier, null, trueSupplier));
assertFalse(BoolUtil.and(trueSupplier, null, () -> false));
}
@Test
void and_AllNullSuppliers_ReturnsTrue() {
assertTrue(BoolUtil.and((BooleanSupplier) null, null));
}
// Tests for or(Boolean... values)
@Test
void or_AllTrueValues_ReturnsTrue() {
assertTrue(BoolUtil.or(true, true, true));
}
@Test
void or_SomeTrueValues_ReturnsTrue() {
assertTrue(BoolUtil.or(false, true, false));
}
@Test
void or_AllFalseValues_ReturnsFalse() {
assertFalse(BoolUtil.or(false, false));
}
@Test
void or_WithNullValues_IgnoresNull() {
assertTrue(BoolUtil.or(false, null, true));
assertFalse(BoolUtil.or(false, null, false));
}
@Test
void or_AllNullValues_ReturnsFalse() {
// Stream after filtering null is empty, anyMatch on empty returns false
assertFalse(BoolUtil.or((Boolean) null, null));
}
// Tests for or(BooleanSupplier... valueSuppliers)
@Test
void or_AllSuppliersTrue_ReturnsTrue() {
BooleanSupplier trueSupplier = () -> true;
BooleanSupplier falseSupplier = () -> false;
assertTrue(BoolUtil.or(trueSupplier, trueSupplier));
assertTrue(BoolUtil.or(falseSupplier, trueSupplier));
assertFalse(BoolUtil.or(falseSupplier, falseSupplier));
}
@Test
void or_WithNullSuppliers_IgnoresNull() {
BooleanSupplier trueSupplier = () -> true;
BooleanSupplier falseSupplier = () -> false;
assertTrue(BoolUtil.or(falseSupplier, null, trueSupplier));
assertFalse(BoolUtil.or(falseSupplier, null, falseSupplier));
}
@Test
void or_AllNullSuppliers_ReturnsFalse() {
assertFalse(BoolUtil.or((BooleanSupplier) null, null));
}
}
@@ -0,0 +1,161 @@
/*
* Copyright (C) 2024-2025 OnixByte.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.onixbyte.devkit.utils;
import org.junit.jupiter.api.Test;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BooleanSupplier;
import static org.junit.jupiter.api.Assertions.*;
class BranchUtilTest {
// Test the static methods or(Boolean... values) and and(Boolean... values)
@Test
void testOrWithBooleanValues() {
BranchUtil trueResult = BranchUtil.or(true, false, false);
assertNotNull(trueResult);
BranchUtil falseResult = BranchUtil.or(false, false, false);
assertNotNull(falseResult);
}
@Test
void testAndWithBooleanValues() {
BranchUtil trueResult = BranchUtil.and(true, true, true);
assertNotNull(trueResult);
BranchUtil falseResult = BranchUtil.and(true, false, true);
assertNotNull(falseResult);
}
// Test the static methods or(BooleanSupplier... valueSuppliers) and and(BooleanSupplier... valueSuppliers)
@Test
void testOrWithBooleanSuppliers() {
BooleanSupplier trueSupplier = () -> true;
BooleanSupplier falseSupplier = () -> false;
BranchUtil trueResult = BranchUtil.or(falseSupplier, trueSupplier);
BranchUtil falseResult = BranchUtil.or(falseSupplier, falseSupplier);
}
@Test
void testAndWithBooleanSuppliers() {
BooleanSupplier trueSupplier = () -> true;
BooleanSupplier falseSupplier = () -> false;
BranchUtil trueResult = BranchUtil.and(trueSupplier, trueSupplier);
BranchUtil falseResult = BranchUtil.and(trueSupplier, falseSupplier);
}
// Test thenSupply(T, T)
@Test
void testThenSupplyBothSuppliers_ResultTrue() {
BranchUtil b = BranchUtil.and(true);
String trueVal = "yes";
String falseVal = "no";
String result = b.thenSupply(() -> trueVal, () -> falseVal);
assertEquals(trueVal, result);
}
@Test
void testThenSupplyBothSuppliers_ResultFalse_WithFalseSupplier() {
BranchUtil b = BranchUtil.and(false);
String trueVal = "yes";
String falseVal = "no";
String result = b.thenSupply(() -> trueVal, () -> falseVal);
assertEquals(falseVal, result);
}
@Test
void testThenSupplyBothSuppliers_ResultFalse_NoFalseSupplier() {
BranchUtil b = BranchUtil.and(false);
String trueVal = "yes";
String result = b.thenSupply(() -> trueVal, null);
assertNull(result);
}
@Test
void testThenSupplySingleTrueSupplier_ResultTrue() {
BranchUtil b = BranchUtil.and(true);
String trueVal = "success";
String result = b.thenSupply(() -> trueVal);
assertEquals(trueVal, result);
}
@Test
void testThenSupplySingleTrueSupplier_ResultFalse() {
BranchUtil b = BranchUtil.and(false);
String trueVal = "success";
String result = b.thenSupply(() -> trueVal);
assertNull(result);
}
// Test then(Runnable, Runnable)
@Test
void testThenWithBothHandlers_ResultTrue() {
BranchUtil b = BranchUtil.and(true);
AtomicBoolean trueRun = new AtomicBoolean(false);
AtomicBoolean falseRun = new AtomicBoolean(false);
b.then(() -> trueRun.set(true), () -> falseRun.set(true));
assertTrue(trueRun.get());
assertFalse(falseRun.get());
}
@Test
void testThenWithBothHandlers_ResultFalse() {
BranchUtil b = BranchUtil.and(false);
AtomicBoolean trueRun = new AtomicBoolean(false);
AtomicBoolean falseRun = new AtomicBoolean(false);
b.then(() -> trueRun.set(true), () -> falseRun.set(true));
assertFalse(trueRun.get());
assertTrue(falseRun.get());
}
@Test
void testThenWithOnlyTrueHandler_ResultTrue() {
BranchUtil b = BranchUtil.and(true);
AtomicBoolean trueRun = new AtomicBoolean(false);
b.then(() -> trueRun.set(true));
assertTrue(trueRun.get());
}
@Test
void testThenWithOnlyTrueHandler_ResultFalse() {
BranchUtil b = BranchUtil.and(false);
AtomicBoolean trueRun = new AtomicBoolean(false);
b.then(() -> trueRun.set(true));
assertFalse(trueRun.get());
}
}
@@ -0,0 +1,112 @@
/*
* Copyright (C) 2024-2025 OnixByte.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.onixbyte.devkit.utils;
import org.junit.jupiter.api.Test;
import java.util.*;
import java.util.function.Supplier;
import static org.junit.jupiter.api.Assertions.*;
class CollectionUtilTest {
@Test
void chunk_NullOriginalCollection_ThrowsException() {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> CollectionUtil.chunk(null, 3, ArrayList::new));
assertEquals("Collection must not be null.", ex.getMessage());
}
@Test
void chunk_NegativeMaxSize_ThrowsException() {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> CollectionUtil.chunk(List.of(1, 2), -1, ArrayList::new));
assertEquals("maxSize must greater than 0.", ex.getMessage());
}
@Test
void chunk_NullCollectionFactory_ThrowsException() {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> CollectionUtil.chunk(List.of(1, 2), 2, null));
assertEquals("Factory method cannot be null.", ex.getMessage());
}
@Test
void chunk_EmptyCollection_ReturnsOneEmptySubCollection() {
List<List<Integer>> chunks = CollectionUtil.chunk(Collections.emptyList(), 3, ArrayList::new);
assertEquals(1, chunks.size());
assertTrue(chunks.get(0).isEmpty());
}
@Test
void chunk_CollectionSizeLessThanMaxSize_ReturnsOneSubCollectionWithAllElements() {
List<Integer> list = List.of(1, 2);
List<List<Integer>> chunks = CollectionUtil.chunk(list, 5, ArrayList::new);
assertEquals(1, chunks.size());
assertEquals(list, chunks.get(0));
}
@Test
void chunk_CollectionSizeEqualMaxSize_ReturnsOneSubCollectionWithAllElements() {
List<Integer> list = List.of(1, 2, 3);
List<List<Integer>> chunks = CollectionUtil.chunk(list, 3, ArrayList::new);
assertEquals(1, chunks.size());
assertEquals(list, chunks.get(0));
}
@Test
void chunk_CollectionSizeGreaterThanMaxSize_ReturnsMultipleSubCollections() {
List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7);
int maxSize = 3;
List<List<Integer>> chunks = CollectionUtil.chunk(list, maxSize, ArrayList::new);
// Expect 3 subcollections: [1,2,3], [4,5,6], [7]
assertEquals(3, chunks.size());
assertEquals(List.of(1, 2, 3), chunks.get(0));
assertEquals(List.of(4, 5, 6), chunks.get(1));
assertEquals(List.of(7), chunks.get(2));
}
@Test
void chunk_UsesDifferentCollectionTypeAsSubCollections() {
LinkedList<Integer> list = new LinkedList<>(List.of(1, 2, 3, 4));
Supplier<LinkedList<Integer>> factory = LinkedList::new;
List<LinkedList<Integer>> chunks = CollectionUtil.chunk(list, 2, factory);
assertEquals(2, chunks.size());
assertInstanceOf(LinkedList.class, chunks.get(0));
assertInstanceOf(LinkedList.class, chunks.get(1));
assertEquals(List.of(1, 2), chunks.get(0));
assertEquals(List.of(3, 4), chunks.get(1));
}
@Test
void chunk_CollectionWithOneElementAndMaxSizeOne_ReturnsOneSubCollection() {
List<String> list = List.of("a");
List<List<String>> chunks = CollectionUtil.chunk(list, 1, ArrayList::new);
assertEquals(1, chunks.size());
assertEquals(list, chunks.get(0));
}
@Test
void chunk_MaxSizeZero_ThrowsException() {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> CollectionUtil.chunk(List.of(1), 0, ArrayList::new));
assertEquals("maxSize must greater than 0.", ex.getMessage());
}
}
@@ -0,0 +1,128 @@
/*
* Copyright (C) 2024-2025 OnixByte.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.onixbyte.devkit.utils;
import org.junit.jupiter.api.Test;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertEquals;
class HashUtilTest {
// Test MD2 hashing with explicit charset and default charset
@Test
void testMd2() {
String input = "test";
// Known MD2 hash of "test" with UTF-8
String expectedHash = "dd34716876364a02d0195e2fb9ae2d1b";
assertEquals(expectedHash, HashUtil.md2(input, StandardCharsets.UTF_8));
assertEquals(expectedHash, HashUtil.md2(input));
// Test null charset fallback to UTF-8
assertEquals(expectedHash, HashUtil.md2(input, null));
}
// Test MD5 hashing with explicit charset and default charset
@Test
void testMd5() {
String input = "test";
// Known MD5 hash of "test"
String expectedHash = "098f6bcd4621d373cade4e832627b4f6";
assertEquals(expectedHash, HashUtil.md5(input, StandardCharsets.UTF_8));
assertEquals(expectedHash, HashUtil.md5(input));
assertEquals(expectedHash, HashUtil.md5(input, null));
}
// Test SHA-1 hashing with explicit charset and default charset
@Test
void testSha1() {
String input = "test";
// Known SHA-1 hash of "test"
String expectedHash = "a94a8fe5ccb19ba61c4c0873d391e987982fbbd3";
assertEquals(expectedHash, HashUtil.sha1(input, StandardCharsets.UTF_8));
assertEquals(expectedHash, HashUtil.sha1(input));
assertEquals(expectedHash, HashUtil.sha1(input, null));
}
// Test SHA-224 hashing with explicit charset and default charset
@Test
void testSha224() {
String input = "test";
// Known SHA-224 hash of "test"
String expectedHash = "90a3ed9e32b2aaf4c61c410eb925426119e1a9dc53d4286ade99a809";
assertEquals(expectedHash, HashUtil.sha224(input, StandardCharsets.UTF_8));
assertEquals(expectedHash, HashUtil.sha224(input));
assertEquals(expectedHash, HashUtil.sha224(input, null));
}
// Test SHA-256 hashing with explicit charset and default charset
@Test
void testSha256() {
String input = "test";
// Known SHA-256 hash of "test"
String expectedHash = "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08";
assertEquals(expectedHash, HashUtil.sha256(input, StandardCharsets.UTF_8));
assertEquals(expectedHash, HashUtil.sha256(input));
assertEquals(expectedHash, HashUtil.sha256(input, null));
}
// Test SHA-384 hashing with explicit charset and default charset
@Test
void testSha384() {
String input = "test";
// Known SHA-384 hash of "test"
String expectedHash = "768412320f7b0aa5812fce428dc4706b3cae50e02a64caa16a782249bfe8efc4b7ef1ccb126255d196047dfedf17a0a9";
assertEquals(expectedHash, HashUtil.sha384(input, StandardCharsets.UTF_8));
assertEquals(expectedHash, HashUtil.sha384(input));
assertEquals(expectedHash, HashUtil.sha384(input, null));
}
// Test SHA-512 hashing with explicit charset and default charset
@Test
void testSha512() {
String input = "test";
// Known SHA-512 hash of "test"
String expectedHash = "ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff";
// remove all whitespace in expected to match format generated
expectedHash = expectedHash.replaceAll("\\s+", "");
assertEquals(expectedHash, HashUtil.sha512(input, StandardCharsets.UTF_8));
assertEquals(expectedHash, HashUtil.sha512(input));
assertEquals(expectedHash, HashUtil.sha512(input, null));
}
// Test empty string input
@Test
void testEmptyString() {
String input = "";
// MD5 hash of empty string
String expectedMd5 = "d41d8cd98f00b204e9800998ecf8427e";
assertEquals(expectedMd5, HashUtil.md5(input));
// SHA-256 hash of empty string
String expectedSha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855";
assertEquals(expectedSha256, HashUtil.sha256(input));
}
// Test null charset fallback for one algorithm as a sample
@Test
void testNullCharsetFallsBackToUtf8() {
String input = "abc";
String hashWithNull = HashUtil.md5(input, null);
String hashWithUtf8 = HashUtil.md5(input, StandardCharsets.UTF_8);
assertEquals(hashWithUtf8, hashWithNull);
}
}
@@ -0,0 +1,131 @@
/*
* Copyright (C) 2024-2025 OnixByte.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.onixbyte.devkit.utils;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class RangeUtilTest {
/**
* Tests generating ascending range from 0 up to end (exclusive).
*/
@Test
void testRangeEndValid() {
int[] expected = {0, 1, 2, 3, 4};
assertArrayEquals(expected, RangeUtil.range(5).toArray());
}
/**
* Tests that range(end) throws IllegalArgumentException for end less than or equal to zero.
*/
@Test
void testRangeEndInvalidThrows() {
IllegalArgumentException ex1 = assertThrows(IllegalArgumentException.class,
() -> RangeUtil.range(0));
assertTrue(ex1.getMessage().contains("should not be less than or equal to 0"));
IllegalArgumentException ex2 = assertThrows(IllegalArgumentException.class,
() -> RangeUtil.range(-3));
assertTrue(ex2.getMessage().contains("should not be less than or equal to 0"));
}
/**
* Tests ascending range where start is less than end.
*/
@Test
void testRangeStartEndAscending() {
int[] expected = {3, 4, 5, 6, 7};
assertArrayEquals(expected, RangeUtil.range(3, 8).toArray());
}
/**
* Tests descending range where start is greater than end.
*/
@Test
void testRangeStartEndDescending() {
int[] expected = {8, 7, 6, 5, 4};
assertArrayEquals(expected, RangeUtil.range(8, 3).toArray());
}
/**
* Tests empty stream when start equals end.
*/
@Test
void testRangeStartEqualsEndReturnsEmpty() {
assertEquals(0, RangeUtil.range(5, 5).count());
}
/**
* Tests that rangeClosed generates inclusive range in ascending order.
*/
@Test
void testRangeClosedAscending() {
int[] expected = {3, 4, 5, 6, 7, 8};
assertArrayEquals(expected, RangeUtil.rangeClosed(3, 8).toArray());
}
/**
* Tests range method with positive step generating ascending sequence.
*/
@Test
void testRangeWithPositiveStep() {
int[] expected = {2, 4, 6, 8};
assertArrayEquals(expected, RangeUtil.range(2, 10, 2).toArray());
}
/**
* Tests range method with negative step generating descending sequence.
*/
@Test
void testRangeWithNegativeStep() {
int[] expected = {10, 7, 4, 1};
assertArrayEquals(expected, RangeUtil.range(10, 0, -3).toArray());
}
/**
* Tests that passing zero step throws IllegalArgumentException.
*/
@Test
void testRangeStepZeroThrows() {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> RangeUtil.range(0, 10, 0));
assertEquals("Step value must not be zero.", ex.getMessage());
}
/**
* Tests that range with positive step but invalid start/end throws IllegalArgumentException.
*/
@Test
void testRangePositiveStepInvalidRangeThrows() {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> RangeUtil.range(10, 5, 1));
assertEquals("Range parameters are inconsistent with the step value.", ex.getMessage());
}
/**
* Tests that range with negative step but invalid start/end throws IllegalArgumentException.
*/
@Test
void testRangeNegativeStepInvalidRangeThrows() {
IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
() -> RangeUtil.range(5, 10, -1));
assertEquals("Range parameters are inconsistent with the step value.", ex.getMessage());
}
}