This is an automated email from the ASF dual-hosted git repository.
delei pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/fesod.git
The following commit(s) were added to refs/heads/main by this push:
new d7131316 test: Add unit tests for utility classes (#756)
d7131316 is described below
commit d7131316707d4b8aeae4729a6af7ad38f9016f03
Author: 蹦蹦巴拉巴拉蹦 <[email protected]>
AuthorDate: Mon Jan 5 22:44:05 2026 +0800
test: Add unit tests for utility classes (#756)
* test: add unit tests for utility classes
* test: apply suggestions and clean up test code
* style(test): normalize comment formatting
---------
Co-authored-by: DeleiGuo <[email protected]>
---
.../apache/fesod/common/util/PositionUtils.java | 6 +-
.../apache/fesod/common/util/BooleanUtilsTest.java | 64 +++++++
.../org/apache/fesod/common/util/IntUtilsTest.java | 42 +++++
.../org/apache/fesod/common/util/IoUtilsTest.java | 111 ++++++++++++
.../apache/fesod/common/util/ListUtilsTest.java | 150 ++++++++++++++++
.../org/apache/fesod/common/util/MapUtilsTest.java | 109 ++++++++++++
.../apache/fesod/common/util/MemberUtilsTest.java | 103 +++++++++++
.../fesod/common/util/PositionUtilsTest.java | 83 +++++++++
.../apache/fesod/common/util/StringUtilsTest.java | 189 ++++++++++++++++++++-
.../fesod/common/util/ValidateUtilsTest.java | 95 +++++++++++
10 files changed, 948 insertions(+), 4 deletions(-)
diff --git
a/fesod-common/src/main/java/org/apache/fesod/common/util/PositionUtils.java
b/fesod-common/src/main/java/org/apache/fesod/common/util/PositionUtils.java
index fce21da6..d3f4aa64 100644
--- a/fesod-common/src/main/java/org/apache/fesod/common/util/PositionUtils.java
+++ b/fesod-common/src/main/java/org/apache/fesod/common/util/PositionUtils.java
@@ -33,10 +33,10 @@ public class PositionUtils {
private PositionUtils() {}
- public static int getRowByRowTagt(String rowTagt, Integer before) {
+ public static int getRowByRowTag(String rowTag, Integer before) {
int row;
- if (rowTagt != null) {
- row = Integer.parseInt(rowTagt) - 1;
+ if (rowTag != null) {
+ row = Integer.parseInt(rowTag) - 1;
return row;
} else {
if (before == null) {
diff --git
a/fesod-common/src/test/java/org/apache/fesod/common/util/BooleanUtilsTest.java
b/fesod-common/src/test/java/org/apache/fesod/common/util/BooleanUtilsTest.java
new file mode 100644
index 00000000..33373cc6
--- /dev/null
+++
b/fesod-common/src/test/java/org/apache/fesod/common/util/BooleanUtilsTest.java
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.fesod.common.util;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link BooleanUtils}
+ */
+class BooleanUtilsTest {
+
+ @Test
+ void test_valueOf() {
+ Assertions.assertTrue(BooleanUtils.valueOf("1"));
+ Assertions.assertFalse(BooleanUtils.valueOf(""));
+ Assertions.assertFalse(BooleanUtils.valueOf(null));
+ }
+
+ @Test
+ void test_isTrue() {
+ Assertions.assertTrue(BooleanUtils.isTrue(Boolean.TRUE));
+ Assertions.assertFalse(BooleanUtils.isTrue(Boolean.FALSE));
+ Assertions.assertFalse(BooleanUtils.isTrue(null));
+ }
+
+ @Test
+ void test_isNotTrue() {
+ Assertions.assertFalse(BooleanUtils.isNotTrue(Boolean.TRUE));
+ Assertions.assertTrue(BooleanUtils.isNotTrue(Boolean.FALSE));
+ Assertions.assertTrue(BooleanUtils.isNotTrue(null));
+ }
+
+ @Test
+ void test_isFalse() {
+ Assertions.assertFalse(BooleanUtils.isFalse(Boolean.TRUE));
+ Assertions.assertTrue(BooleanUtils.isFalse(Boolean.FALSE));
+ Assertions.assertFalse(BooleanUtils.isFalse(null));
+ }
+
+ @Test
+ void test_isNotFalse() {
+ Assertions.assertTrue(BooleanUtils.isNotFalse(Boolean.TRUE));
+ Assertions.assertFalse(BooleanUtils.isNotFalse(Boolean.FALSE));
+ Assertions.assertTrue(BooleanUtils.isNotFalse(null));
+ }
+}
diff --git
a/fesod-common/src/test/java/org/apache/fesod/common/util/IntUtilsTest.java
b/fesod-common/src/test/java/org/apache/fesod/common/util/IntUtilsTest.java
new file mode 100644
index 00000000..10fa1f46
--- /dev/null
+++ b/fesod-common/src/test/java/org/apache/fesod/common/util/IntUtilsTest.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.fesod.common.util;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link IntUtils}
+ */
+class IntUtilsTest {
+
+ @Test
+ void test_saturatedCast() {
+ Assertions.assertEquals(Integer.MIN_VALUE,
IntUtils.saturatedCast(Long.MIN_VALUE));
+ Assertions.assertEquals(Integer.MAX_VALUE,
IntUtils.saturatedCast(Long.MAX_VALUE));
+ Assertions.assertEquals(Integer.MAX_VALUE,
IntUtils.saturatedCast((long) Integer.MAX_VALUE));
+ Assertions.assertEquals(Integer.MAX_VALUE,
IntUtils.saturatedCast((long) Integer.MAX_VALUE + 1));
+ Assertions.assertEquals(Integer.MIN_VALUE,
IntUtils.saturatedCast((long) Integer.MIN_VALUE));
+ Assertions.assertEquals(Integer.MIN_VALUE,
IntUtils.saturatedCast((long) Integer.MIN_VALUE - 1));
+ Assertions.assertEquals(0, IntUtils.saturatedCast(0L));
+ Assertions.assertEquals(12345, IntUtils.saturatedCast(12345L));
+ Assertions.assertEquals(-12345, IntUtils.saturatedCast(-12345L));
+ }
+}
diff --git
a/fesod-common/src/test/java/org/apache/fesod/common/util/IoUtilsTest.java
b/fesod-common/src/test/java/org/apache/fesod/common/util/IoUtilsTest.java
new file mode 100644
index 00000000..a84863bb
--- /dev/null
+++ b/fesod-common/src/test/java/org/apache/fesod/common/util/IoUtilsTest.java
@@ -0,0 +1,111 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.fesod.common.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Arrays;
+import java.util.Random;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link IoUtils}
+ */
+class IoUtilsTest {
+
+ private byte[] data;
+ private static final int DATA_SIZE = 1024 * 4 + 10;
+
+ @BeforeEach
+ void setup() {
+ data = new byte[DATA_SIZE];
+ new Random().nextBytes(data);
+ }
+
+ @Test
+ void test_toByteArray() throws IOException {
+ InputStream inputStream = new ByteArrayInputStream(data);
+ byte[] actual = IoUtils.toByteArray(inputStream);
+
+ Assertions.assertArrayEquals(data, actual);
+ }
+
+ @Test
+ void test_toByteArray_with_size() throws IOException {
+ int size = DATA_SIZE - 10;
+ InputStream inputStream = new ByteArrayInputStream(data);
+
+ byte[] actual = IoUtils.toByteArray(inputStream, size);
+
+ byte[] expected = Arrays.copyOf(data, size);
+ Assertions.assertEquals(size, actual.length);
+ Assertions.assertArrayEquals(expected, actual);
+ }
+
+ @Test
+ void test_toByteArray_with_zero_size() throws IOException {
+ InputStream inputStream = new ByteArrayInputStream(data);
+ byte[] actual = IoUtils.toByteArray(inputStream, 0);
+ Assertions.assertEquals(0, actual.length);
+ }
+
+ @Test
+ void test_toByteArray_size_mismatch_exception() {
+ byte[] smallData = new byte[10];
+ InputStream inputStream = new ByteArrayInputStream(smallData);
+
+ IOException exception = Assertions.assertThrows(IOException.class, ()
-> {
+ IoUtils.toByteArray(inputStream, 20);
+ });
+ Assertions.assertTrue(exception.getMessage().contains("Unexpected read
size"));
+ }
+
+ @Test
+ void test_toByteArray_negative_size() {
+ InputStream inputStream = new ByteArrayInputStream(data);
+ Assertions.assertThrows(IllegalArgumentException.class, () ->
IoUtils.toByteArray(inputStream, -1));
+ }
+
+ @Test
+ void test_copy_stream() throws IOException {
+ ByteArrayInputStream inputStream = new ByteArrayInputStream(data);
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+ int bytesCopied = IoUtils.copy(inputStream, out);
+
+ Assertions.assertEquals(data.length, bytesCopied);
+ Assertions.assertArrayEquals(data, out.toByteArray());
+ }
+
+ @Test
+ void test_copy_empty_stream() throws IOException {
+ ByteArrayInputStream inputStream = new ByteArrayInputStream(new
byte[0]);
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+
+ int bytesCopied = IoUtils.copy(inputStream, out);
+
+ Assertions.assertEquals(0, bytesCopied);
+ Assertions.assertEquals(0, out.size());
+ }
+}
diff --git
a/fesod-common/src/test/java/org/apache/fesod/common/util/ListUtilsTest.java
b/fesod-common/src/test/java/org/apache/fesod/common/util/ListUtilsTest.java
new file mode 100644
index 00000000..e1cc581c
--- /dev/null
+++ b/fesod-common/src/test/java/org/apache/fesod/common/util/ListUtilsTest.java
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.fesod.common.util;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link ListUtils}
+ */
+class ListUtilsTest {
+
+ @Test
+ void test_newArrayList() {
+ ArrayList<String> list = ListUtils.newArrayList();
+ Assertions.assertNotNull(list);
+ Assertions.assertTrue(list.isEmpty());
+ Assertions.assertEquals(0, list.size());
+ }
+
+ @Test
+ void test_newArrayList_varargs() {
+ ArrayList<String> list = ListUtils.newArrayList("a", "b", "c");
+ Assertions.assertEquals(3, list.size());
+ Assertions.assertEquals(Arrays.asList("a", "b", "c"), list);
+
+ ArrayList<Integer> emptyList = ListUtils.newArrayList();
+ Assertions.assertTrue(emptyList.isEmpty());
+ }
+
+ @Test
+ void test_newArrayList_varargs_null() {
+ Assertions.assertThrows(NullPointerException.class, () -> {
+ ListUtils.newArrayList((Object[]) null);
+ });
+ }
+
+ @Test
+ void test_newArrayList_iterator() {
+ List<Integer> source = Arrays.asList(1, 2, 3);
+ ArrayList<Integer> list = ListUtils.newArrayList(source.iterator());
+
+ Assertions.assertEquals(3, list.size());
+ Assertions.assertEquals(source, list);
+ }
+
+ @Test
+ void test_newArrayList_iterator_null() {
+ ArrayList<Object> list = ListUtils.newArrayList((Iterator<Object>)
null);
+ Assertions.assertNotNull(list);
+ Assertions.assertTrue(list.isEmpty());
+ }
+
+ @Test
+ void test_newArrayList_iterable_collection() {
+ List<String> source = Arrays.asList("apple", "banana");
+ ArrayList<String> list = ListUtils.newArrayList(source);
+
+ Assertions.assertEquals(2, list.size());
+ Assertions.assertEquals(source, list);
+ }
+
+ @Test
+ void test_newArrayList_iterable_plain() {
+ Iterable<Integer> iterable = () -> Arrays.asList(1, 2).iterator();
+
+ ArrayList<Integer> list = ListUtils.newArrayList(iterable);
+ Assertions.assertEquals(2, list.size());
+ }
+
+ @Test
+ void test_newArrayList_iterable_null() {
+ Assertions.assertThrows(NullPointerException.class, () -> {
+ ListUtils.newArrayList((Iterable<Object>) null);
+ });
+ }
+
+ @Test
+ void test_newArrayListWithCapacity() {
+ ArrayList<Integer> list = ListUtils.newArrayListWithCapacity(10);
+ Assertions.assertNotNull(list);
+ Assertions.assertTrue(list.isEmpty());
+
+ Assertions.assertThrows(IllegalArgumentException.class, () -> {
+ ListUtils.newArrayListWithCapacity(-1);
+ });
+ }
+
+ @Test
+ void test_newArrayListWithExpectedSize() {
+ ArrayList<Integer> list = ListUtils.newArrayListWithExpectedSize(100);
+ Assertions.assertNotNull(list);
+ Assertions.assertTrue(list.isEmpty());
+
+ Assertions.assertThrows(IllegalArgumentException.class, () -> {
+ ListUtils.newArrayListWithExpectedSize(-5);
+ });
+ }
+
+ @Test
+ void test_computeArrayListCapacity() {
+ Assertions.assertEquals(5, ListUtils.computeArrayListCapacity(0));
+ Assertions.assertEquals(16, ListUtils.computeArrayListCapacity(10));
+ Assertions.assertEquals(115, ListUtils.computeArrayListCapacity(100));
+ }
+
+ @Test
+ void test_checkNotNull() {
+ String ref = "hello";
+ String result = ListUtils.checkNotNull(ref);
+ Assertions.assertEquals("hello", result);
+
+ Assertions.assertThrows(NullPointerException.class, () -> {
+ ListUtils.checkNotNull(null);
+ });
+ }
+
+ @Test
+ void test_checkNonnegative() {
+ Assertions.assertEquals(0, ListUtils.checkNonnegative(0, "test"));
+ Assertions.assertEquals(100, ListUtils.checkNonnegative(100, "test"));
+
+ IllegalArgumentException exception =
Assertions.assertThrows(IllegalArgumentException.class, () -> {
+ ListUtils.checkNonnegative(-1, "myField");
+ });
+ Assertions.assertTrue(exception.getMessage().contains("myField"));
+ Assertions.assertTrue(exception.getMessage().contains("-1"));
+ }
+}
diff --git
a/fesod-common/src/test/java/org/apache/fesod/common/util/MapUtilsTest.java
b/fesod-common/src/test/java/org/apache/fesod/common/util/MapUtilsTest.java
new file mode 100644
index 00000000..0940d9fa
--- /dev/null
+++ b/fesod-common/src/test/java/org/apache/fesod/common/util/MapUtilsTest.java
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.fesod.common.util;
+
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.TreeMap;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link MapUtils}
+ */
+class MapUtilsTest {
+
+ @Test
+ void test_newHashMap() {
+ HashMap<String, Integer> map = MapUtils.newHashMap();
+ Assertions.assertNotNull(map);
+ Assertions.assertInstanceOf(HashMap.class, map);
+ Assertions.assertTrue(map.isEmpty());
+ }
+
+ @Test
+ void test_newTreeMap() {
+ TreeMap<String, String> map = MapUtils.newTreeMap();
+ Assertions.assertNotNull(map);
+ Assertions.assertInstanceOf(TreeMap.class, map);
+
+ map.put("b", "2");
+ map.put("a", "1");
+ Assertions.assertEquals("a", map.firstKey());
+ }
+
+ @Test
+ void test_newLinkedHashMap() {
+ LinkedHashMap<Integer, String> map = MapUtils.newLinkedHashMap();
+ Assertions.assertNotNull(map);
+ Assertions.assertInstanceOf(LinkedHashMap.class, map);
+
+ map.put(2, "two");
+ map.put(1, "one");
+ map.put(3, "three");
+
+ Integer[] keys = map.keySet().toArray(new Integer[0]);
+ Assertions.assertArrayEquals(new Integer[] {2, 1, 3}, keys);
+ }
+
+ @Test
+ void test_newHashMapWithExpectedSize() {
+ HashMap<String, String> map = MapUtils.newHashMapWithExpectedSize(10);
+ Assertions.assertNotNull(map);
+ Assertions.assertTrue(map.isEmpty());
+ }
+
+ @Test
+ void test_newHashMapWithExpectedSize_negative() {
+ Assertions.assertThrows(IllegalArgumentException.class, () -> {
+ MapUtils.newHashMapWithExpectedSize(-5);
+ });
+ }
+
+ @Test
+ void test_newLinkedHashMapWithExpectedSize() {
+ LinkedHashMap<String, String> map =
MapUtils.newLinkedHashMapWithExpectedSize(20);
+ Assertions.assertNotNull(map);
+ Assertions.assertTrue(map.isEmpty());
+ }
+
+ @Test
+ void test_capacity_logic() {
+ Assertions.assertEquals(1, MapUtils.capacity(0));
+ Assertions.assertEquals(2, MapUtils.capacity(1));
+ Assertions.assertEquals(3, MapUtils.capacity(2));
+
+ Assertions.assertEquals(5, MapUtils.capacity(3));
+ Assertions.assertEquals(17, MapUtils.capacity(12));
+
+ int veryLargeSize = Integer.MAX_VALUE - 100;
+ Assertions.assertEquals(Integer.MAX_VALUE,
MapUtils.capacity(veryLargeSize));
+ }
+
+ @Test
+ void test_generics() {
+ Map<String, Long> map = MapUtils.newHashMap();
+ HashMap<String, Long> hashMap = MapUtils.newHashMap();
+
+ Assertions.assertNotNull(map);
+ Assertions.assertNotNull(hashMap);
+ }
+}
diff --git
a/fesod-common/src/test/java/org/apache/fesod/common/util/MemberUtilsTest.java
b/fesod-common/src/test/java/org/apache/fesod/common/util/MemberUtilsTest.java
new file mode 100644
index 00000000..6acb649f
--- /dev/null
+++
b/fesod-common/src/test/java/org/apache/fesod/common/util/MemberUtilsTest.java
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.fesod.common.util;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link MemberUtils}
+ */
+class MemberUtilsTest {
+
+ public static class TestPublicClass {
+ public void publicMethod() {}
+ }
+
+ static class TestPrivateClass {
+ public void publicMethod() {}
+
+ void packageMethod() {}
+
+ private void privateMethod() {}
+ }
+
+ @Test
+ void test_setAccessibleWorkaround_null() {
+ Assertions.assertFalse(MemberUtils.setAccessibleWorkaround(null));
+ }
+
+ @Test
+ void test_setAccessibleWorkaround_PublicClass_PublicMethod() throws
NoSuchMethodException {
+ Method method = TestPublicClass.class.getMethod("publicMethod");
+
+ boolean result = MemberUtils.setAccessibleWorkaround(method);
+ Assertions.assertFalse(result);
+ }
+
+ @Test
+ void test_setAccessibleWorkaround_PackagePrivateClass_PrivateMethod()
throws NoSuchMethodException {
+ Method method =
TestPrivateClass.class.getDeclaredMethod("privateMethod");
+
+ boolean result = MemberUtils.setAccessibleWorkaround(method);
+ Assertions.assertFalse(result);
+ }
+
+ @Test
+ void test_setAccessibleWorkaround_PackagePrivateClass_PublicMethod()
throws NoSuchMethodException {
+ Method method =
TestPrivateClass.class.getDeclaredMethod("publicMethod");
+ method.setAccessible(false);
+
+ boolean result = MemberUtils.setAccessibleWorkaround(method);
+ Assertions.assertTrue(result);
+ Assertions.assertTrue(method.isAccessible());
+ }
+
+ @Test
+ void test_setAccessibleWorkaround_AlreadyAccessible() throws
NoSuchMethodException {
+ Method method =
TestPrivateClass.class.getDeclaredMethod("publicMethod");
+ method.setAccessible(true);
+
+ boolean result = MemberUtils.setAccessibleWorkaround(method);
+ Assertions.assertFalse(result);
+ }
+
+ @Test
+ void test_setAccessibleWorkaround_PackageMethod() throws
NoSuchMethodException {
+ Method method =
TestPrivateClass.class.getDeclaredMethod("packageMethod");
+
+ boolean result = MemberUtils.setAccessibleWorkaround(method);
+ Assertions.assertFalse(result);
+ }
+
+ @Test
+ void test_isPackageAccess() {
+ Assertions.assertTrue(MemberUtils.isPackageAccess(0));
+ Assertions.assertTrue(MemberUtils.isPackageAccess(Modifier.STATIC));
+ Assertions.assertTrue(MemberUtils.isPackageAccess(Modifier.FINAL));
+
+ Assertions.assertFalse(MemberUtils.isPackageAccess(Modifier.PUBLIC));
+
Assertions.assertFalse(MemberUtils.isPackageAccess(Modifier.PROTECTED));
+ Assertions.assertFalse(MemberUtils.isPackageAccess(Modifier.PRIVATE));
+ Assertions.assertFalse(MemberUtils.isPackageAccess(Modifier.PUBLIC |
Modifier.STATIC));
+ }
+}
diff --git
a/fesod-common/src/test/java/org/apache/fesod/common/util/PositionUtilsTest.java
b/fesod-common/src/test/java/org/apache/fesod/common/util/PositionUtilsTest.java
new file mode 100644
index 00000000..ee7ce409
--- /dev/null
+++
b/fesod-common/src/test/java/org/apache/fesod/common/util/PositionUtilsTest.java
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.fesod.common.util;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link PositionUtils}
+ */
+class PositionUtilsTest {
+
+ @Test
+ void test_getRowByRowTag() {
+ Assertions.assertEquals(0, PositionUtils.getRowByRowTag("1", null));
+ Assertions.assertEquals(9, PositionUtils.getRowByRowTag("10", 5));
+ Assertions.assertEquals(6, PositionUtils.getRowByRowTag(null, 5));
+ Assertions.assertEquals(0, PositionUtils.getRowByRowTag(null, null));
+ }
+
+ @Test
+ void test_getRow() {
+ Assertions.assertEquals(0, PositionUtils.getRow("A1"));
+ Assertions.assertEquals(9, PositionUtils.getRow("B10"));
+ Assertions.assertEquals(99, PositionUtils.getRow("AA100"));
+
+ Assertions.assertEquals(4, PositionUtils.getRow("$A$5"));
+ Assertions.assertEquals(19, PositionUtils.getRow("C$20"));
+
+ Assertions.assertEquals(-1, PositionUtils.getRow(null));
+ }
+
+ @Test
+ void test_getCol() {
+ // A -> 0
+ Assertions.assertEquals(0, PositionUtils.getCol("A1", null));
+ // B -> 1
+ Assertions.assertEquals(1, PositionUtils.getCol("B10", null));
+ // Z -> 25
+ Assertions.assertEquals(25, PositionUtils.getCol("Z1", null));
+
+ // AA -> 26
+ Assertions.assertEquals(26, PositionUtils.getCol("AA1", null));
+ // AZ -> 51
+ Assertions.assertEquals(51, PositionUtils.getCol("AZ1", null));
+
+ Assertions.assertEquals(0, PositionUtils.getCol("$A1", null));
+ Assertions.assertEquals(1, PositionUtils.getCol("$B$2", null));
+
+ Assertions.assertEquals(5, PositionUtils.getCol(null, 4));
+ Assertions.assertEquals(0, PositionUtils.getCol(null, null)); // -1 + 1
+ }
+
+ @Test
+ void test_getRow_invalid_format() {
+ Assertions.assertThrows(NumberFormatException.class, () -> {
+ PositionUtils.getRow("ABC");
+ });
+ }
+
+ @Test
+ void test_getCol_case_insensitivity() {
+ Assertions.assertEquals(0, PositionUtils.getCol("a1", null));
+ Assertions.assertEquals(27, PositionUtils.getCol("ab1", null));
+ }
+}
diff --git
a/fesod-common/src/test/java/org/apache/fesod/common/util/StringUtilsTest.java
b/fesod-common/src/test/java/org/apache/fesod/common/util/StringUtilsTest.java
index bea5fde6..0f4ec3d0 100644
---
a/fesod-common/src/test/java/org/apache/fesod/common/util/StringUtilsTest.java
+++
b/fesod-common/src/test/java/org/apache/fesod/common/util/StringUtilsTest.java
@@ -22,7 +22,194 @@ package org.apache.fesod.common.util;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
-public class StringUtilsTest {
+/**
+ * Tests {@link StringUtils}
+ */
+class StringUtilsTest {
+
+ private abstract static class RunTest {
+
+ abstract boolean invoke();
+
+ void run(final TestData data, final String id) {
+ if (data.throwable != null) {
+ Assertions.assertThrows(data.throwable, this::invoke, id + "
Expected " + data.throwable);
+ } else {
+ final boolean stringCheck = invoke();
+ Assertions.assertEquals(data.expected, stringCheck, id + "
Failed test " + data);
+ }
+ }
+ }
+
+ private static class TestData {
+ final String source;
+ final boolean ignoreCase;
+ final int toffset;
+ final String other;
+ final int ooffset;
+ final int len;
+ final boolean expected;
+ final Class<? extends Throwable> throwable;
+
+ TestData(
+ final String source,
+ final boolean ignoreCase,
+ final int toffset,
+ final String other,
+ final int ooffset,
+ final int len,
+ final boolean expected) {
+ this.source = source;
+ this.ignoreCase = ignoreCase;
+ this.toffset = toffset;
+ this.other = other;
+ this.ooffset = ooffset;
+ this.len = len;
+ this.expected = expected;
+ this.throwable = null;
+ }
+
+ TestData(
+ final String source,
+ final boolean ignoreCase,
+ final int toffset,
+ final String other,
+ final int ooffset,
+ final int len,
+ final Class<? extends Throwable> throwable) {
+ this.source = source;
+ this.ignoreCase = ignoreCase;
+ this.toffset = toffset;
+ this.other = other;
+ this.ooffset = ooffset;
+ this.len = len;
+ this.expected = false;
+ this.throwable = throwable;
+ }
+
+ @Override
+ public String toString() {
+ final StringBuilder sb = new StringBuilder();
+ sb.append(source).append("[").append(toffset).append("]");
+ sb.append(ignoreCase ? " caseblind " : " samecase ");
+ sb.append(other).append("[").append(ooffset).append("]");
+ sb.append(" ").append(len).append(" => ");
+ if (throwable != null) {
+ sb.append(throwable);
+ } else {
+ sb.append(expected);
+ }
+ return sb.toString();
+ }
+ }
+
+ private static final TestData[] TEST_DATA = {
+ // @formatter:off
+ // Source IgnoreCase Offset Other Offset Length Result
+ new TestData("", true, -1, "", -1, -1, false),
+ new TestData("", true, 0, "", 0, 1, false),
+ new TestData("a", true, 0, "abc", 0, 0, true),
+ new TestData("a", true, 0, "abc", 0, 1, true),
+ new TestData("a", true, 0, null, 0, 0, NullPointerException.class),
+ new TestData(null, true, 0, null, 0, 0, NullPointerException.class),
+ new TestData(null, true, 0, "", 0, 0, NullPointerException.class),
+ new TestData("Abc", true, 0, "abc", 0, 3, true),
+ new TestData("Abc", false, 0, "abc", 0, 3, false),
+ new TestData("Abc", true, 1, "abc", 1, 2, true),
+ new TestData("Abc", false, 1, "abc", 1, 2, true),
+ new TestData("Abcd", true, 1, "abcD", 1, 2, true),
+ new TestData("Abcd", false, 1, "abcD", 1, 2, true),
+ // @formatter:on
+ };
+
+ @Test
+ void test_isEmpty() {
+ Assertions.assertTrue(StringUtils.isEmpty(null));
+ Assertions.assertTrue(StringUtils.isEmpty(""));
+ Assertions.assertFalse(StringUtils.isEmpty(" "));
+ Assertions.assertFalse(StringUtils.isEmpty("bob"));
+ Assertions.assertFalse(StringUtils.isEmpty(" bob "));
+ }
+
+ @Test
+ void test_isBlank() {
+ Assertions.assertTrue(StringUtils.isBlank(null));
+ Assertions.assertTrue(StringUtils.isBlank(""));
+ Assertions.assertTrue(StringUtils.isBlank(" "));
+ Assertions.assertFalse(StringUtils.isBlank("bob"));
+ Assertions.assertFalse(StringUtils.isBlank(" bob "));
+ }
+
+ @Test
+ void test_isNotBlank() {
+ Assertions.assertFalse(StringUtils.isNotBlank(null));
+ Assertions.assertFalse(StringUtils.isNotBlank(""));
+ Assertions.assertFalse(StringUtils.isNotBlank(" "));
+ Assertions.assertTrue(StringUtils.isNotBlank("bob"));
+ Assertions.assertTrue(StringUtils.isNotBlank(" bob "));
+ }
+
+ @Test
+ void test_equals() {
+ Assertions.assertTrue(StringUtils.equals(null, null));
+ Assertions.assertFalse(StringUtils.equals(null, "abc"));
+ Assertions.assertFalse(StringUtils.equals("abc", null));
+ Assertions.assertTrue(StringUtils.equals("abc", "abc"));
+ Assertions.assertFalse(StringUtils.equals("abc", "ABC"));
+ }
+
+ @Test
+ void test_isNumeric() {
+ Assertions.assertFalse(StringUtils.isNumeric(null));
+ Assertions.assertFalse(StringUtils.isNumeric(""));
+ Assertions.assertFalse(StringUtils.isNumeric(" "));
+ Assertions.assertFalse(StringUtils.isNumeric("a"));
+ Assertions.assertFalse(StringUtils.isNumeric("A"));
+
Assertions.assertFalse(StringUtils.isNumeric("kgKgKgKgkgkGkjkjlJlOKLgHdGdHgl"));
+ Assertions.assertFalse(StringUtils.isNumeric("ham kso"));
+ Assertions.assertTrue(StringUtils.isNumeric("1"));
+ Assertions.assertTrue(StringUtils.isNumeric("1000"));
+ Assertions.assertTrue(StringUtils.isNumeric("\u0967\u0968\u0969"));
+ Assertions.assertFalse(StringUtils.isNumeric("\u0967\u0968 \u0969"));
+ Assertions.assertFalse(StringUtils.isNumeric("2.3"));
+ Assertions.assertFalse(StringUtils.isNumeric("10 00"));
+
Assertions.assertFalse(StringUtils.isNumeric("hkHKHik6iUGHKJgU7tUJgKJGI87GIkug"));
+ Assertions.assertFalse(StringUtils.isNumeric("_"));
+ Assertions.assertFalse(StringUtils.isNumeric("hkHKHik*khbkuh"));
+ Assertions.assertFalse(StringUtils.isNumeric("+123"));
+ Assertions.assertFalse(StringUtils.isNumeric("-123"));
+ }
+
+ @Test
+ void test_regionMatches() {
+ for (final TestData data : TEST_DATA) {
+ new RunTest() {
+ @Override
+ boolean invoke() {
+ return data.source.regionMatches(data.ignoreCase,
data.toffset, data.other, data.ooffset, data.len);
+ }
+ }.run(data, "String");
+ new RunTest() {
+ @Override
+ boolean invoke() {
+ return StringUtils.regionMatches(
+ data.source, data.ignoreCase, data.toffset,
data.other, data.ooffset, data.len);
+ }
+ }.run(data, "CSString");
+ new RunTest() {
+ @Override
+ boolean invoke() {
+ return StringUtils.regionMatches(
+ new StringBuilder(data.source),
+ data.ignoreCase,
+ data.toffset,
+ data.other,
+ data.ooffset,
+ data.len);
+ }
+ }.run(data, "CSNonString");
+ }
+ }
@Test
void stripTest() {
diff --git
a/fesod-common/src/test/java/org/apache/fesod/common/util/ValidateUtilsTest.java
b/fesod-common/src/test/java/org/apache/fesod/common/util/ValidateUtilsTest.java
new file mode 100644
index 00000000..a7b59af6
--- /dev/null
+++
b/fesod-common/src/test/java/org/apache/fesod/common/util/ValidateUtilsTest.java
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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 org.apache.fesod.common.util;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests {@link ValidateUtils}
+ */
+class ValidateUtilsTest {
+
+ @Test
+ void test_isTrue_long() {
+ Assertions.assertDoesNotThrow(() -> ValidateUtils.isTrue(true, "Error
count: %d", 100L));
+
+ IllegalArgumentException exception =
Assertions.assertThrows(IllegalArgumentException.class, () -> {
+ ValidateUtils.isTrue(false, "Error count: %d", 50L);
+ });
+ Assertions.assertEquals("Error count: 50", exception.getMessage());
+ }
+
+ @Test
+ void test_isTrue_double() {
+ Assertions.assertDoesNotThrow(() -> ValidateUtils.isTrue(true, "Value:
%.1f", 1.5));
+
+ IllegalArgumentException exception =
Assertions.assertThrows(IllegalArgumentException.class, () -> {
+ ValidateUtils.isTrue(false, "Value: %.2f", 3.1415);
+ });
+ Assertions.assertEquals("Value: 3.14", exception.getMessage());
+ }
+
+ @Test
+ void test_isTrue_objects() {
+ Assertions.assertDoesNotThrow(() -> ValidateUtils.isTrue(true, "Failed
for %s and %s", "A", "B"));
+
+ IllegalArgumentException exception =
Assertions.assertThrows(IllegalArgumentException.class, () -> {
+ ValidateUtils.isTrue(false, "User %s has id %d", "Admin", 123);
+ });
+ Assertions.assertEquals("User Admin has id 123",
exception.getMessage());
+ }
+
+ @Test
+ void test_isTrue() {
+ Assertions.assertDoesNotThrow(() -> ValidateUtils.isTrue(1 + 1 == 2));
+
+ IllegalArgumentException exception =
Assertions.assertThrows(IllegalArgumentException.class, () -> {
+ ValidateUtils.isTrue(false);
+ });
+ Assertions.assertEquals("The validated expression is false",
exception.getMessage());
+ }
+
+ @Test
+ void test_notNull() {
+ String testObj = "NotNull";
+
+ String result = ValidateUtils.notNull(testObj);
+ Assertions.assertEquals(testObj, result);
+
+ NullPointerException exception =
Assertions.assertThrows(NullPointerException.class, () -> {
+ ValidateUtils.notNull(null);
+ });
+ Assertions.assertEquals("The validated object is null",
exception.getMessage());
+ }
+
+ @Test
+ void test_notNull_withMessage() {
+ String testObj = "NotNull";
+
+ String result = ValidateUtils.notNull(testObj, "Must not be null");
+ Assertions.assertEquals(testObj, result);
+
+ NullPointerException exception =
Assertions.assertThrows(NullPointerException.class, () -> {
+ ValidateUtils.notNull(null, "The field %s cannot be null",
"username");
+ });
+ Assertions.assertEquals("The field username cannot be null",
exception.getMessage());
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]