From 79b8b812209de66e3e48e9037356fb0a1351e1c4 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Wed, 22 Jan 2025 21:57:53 +0800 Subject: [PATCH] =?UTF-8?q?test:=20=E6=B5=8B=E8=AF=95=E5=B7=A5=E5=85=B7?= =?UTF-8?q?=E7=B1=BB=E7=9A=84=E7=A7=81=E6=9C=89=E6=9E=84=E9=80=A0=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../collection/CollectionToolsTests.java | 20 +++++++++- .../commons/constant/PatternConstsTests.java | 38 ++++++++++++++++++- .../commons/function/FunctionTests.java | 21 +++++++++- .../plusone/commons/util/ArrayToolsTests.java | 26 ++++++++++++- .../commons/util/AssertToolsTests.java | 25 +++++++++++- .../commons/util/BigDecimalsTests.java | 17 ++++++++- .../commons/util/DateTimeToolsTests.java | 28 +++++++++++++- .../plusone/commons/util/EnumToolsTests.java | 21 +++++++++- .../commons/util/IdGeneratorTests.java | 20 +++++++++- .../plusone/commons/util/NumbersTests.java | 23 ++++++++++- .../commons/util/OptionalToolsTests.java | 19 +++++++++- .../commons/util/RandomToolsTests.java | 20 +++++++++- .../plusone/commons/util/RegexToolsTests.java | 18 ++++++++- .../commons/util/StringToolsTests.java | 20 +++++++++- 14 files changed, 302 insertions(+), 14 deletions(-) diff --git a/src/test/java/xyz/zhouxy/plusone/commons/collection/CollectionToolsTests.java b/src/test/java/xyz/zhouxy/plusone/commons/collection/CollectionToolsTests.java index 97fdef5..7fa7165 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/collection/CollectionToolsTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/collection/CollectionToolsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2024 the original author or authors. + * Copyright 2024-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,10 +18,14 @@ package xyz.zhouxy.plusone.commons.collection; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertSame; +import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.lang.reflect.Constructor; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.HashMap; import java.util.List; @@ -69,4 +73,18 @@ public class CollectionToolsTests { assertSame(map, CollectionTools.nullToEmptyMap(map)); assertEquals(Collections.emptyMap(), CollectionTools.nullToEmptyMap(null)); } + + @Test + void test_constructor_isNotAccessible_ThrowsIllegalStateException() { + Constructor[] constructors = CollectionTools.class.getDeclaredConstructors(); + Arrays.stream(constructors) + .forEach(constructor -> { + assertFalse(constructor.isAccessible()); + constructor.setAccessible(true); + Throwable cause = assertThrows(Exception.class, constructor::newInstance) + .getCause(); + assertInstanceOf(IllegalStateException.class, cause); + assertEquals("Utility class", cause.getMessage()); + }); + } } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/constant/PatternConstsTests.java b/src/test/java/xyz/zhouxy/plusone/commons/constant/PatternConstsTests.java index 88304d6..5a216a4 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/constant/PatternConstsTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/constant/PatternConstsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2024 the original author or authors. + * Copyright 2024-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,8 @@ package xyz.zhouxy.plusone.commons.constant; import static org.junit.jupiter.api.Assertions.*; +import java.lang.reflect.Constructor; +import java.util.Arrays; import java.util.regex.Matcher; import org.junit.jupiter.api.Test; @@ -232,4 +234,38 @@ class PatternConstsTests { // ================================ // #endregion - Chinese2ndIdCardNumber // ================================ + + // ================================ + // #region - invoke constructor + // ================================ + + @Test + void test_constructor_isNotAccessible_ThrowsIllegalStateException() { + Constructor[] constructors; + constructors = RegexConsts.class.getDeclaredConstructors(); + Arrays.stream(constructors) + .forEach(constructor -> { + assertFalse(constructor.isAccessible()); + constructor.setAccessible(true); + Throwable cause = assertThrows(Exception.class, constructor::newInstance) + .getCause(); + assertInstanceOf(IllegalStateException.class, cause); + assertEquals("Utility class", cause.getMessage()); + }); + + constructors = PatternConsts.class.getDeclaredConstructors(); + Arrays.stream(constructors) + .forEach(constructor -> { + assertFalse(constructor.isAccessible()); + constructor.setAccessible(true); + Throwable cause = assertThrows(Exception.class, constructor::newInstance) + .getCause(); + assertInstanceOf(IllegalStateException.class, cause); + assertEquals("Utility class", cause.getMessage()); + }); + } + + // ================================ + // #endregion - invoke constructor + // ================================ } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/function/FunctionTests.java b/src/test/java/xyz/zhouxy/plusone/commons/function/FunctionTests.java index 17d6aca..501235a 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/function/FunctionTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/function/FunctionTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * Copyright 2023-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,8 +16,13 @@ package xyz.zhouxy.plusone.commons.function; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; +import java.lang.reflect.Constructor; +import java.util.Arrays; import java.util.Objects; import java.util.function.Predicate; @@ -33,4 +38,18 @@ class FunctionTests { .and(StringUtils::isNotBlank); assertFalse(predicate.test(str), "校验应是不通过"); } + + @Test + void test_constructorOfPredicateTools_isNotAccessible_ThrowsIllegalStateException() { + Constructor[] constructors = PredicateTools.class.getDeclaredConstructors(); + Arrays.stream(constructors) + .forEach(constructor -> { + assertFalse(constructor.isAccessible()); + constructor.setAccessible(true); + Throwable cause = assertThrows(Exception.class, constructor::newInstance) + .getCause(); + assertInstanceOf(IllegalStateException.class, cause); + assertEquals("Utility class", cause.getMessage()); + }); + } } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/ArrayToolsTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/ArrayToolsTests.java index 32423a1..3521906 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/ArrayToolsTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/ArrayToolsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2024 the original author or authors. + * Copyright 2024-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,9 +20,11 @@ import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.lang.reflect.Constructor; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -1087,4 +1089,26 @@ public class ArrayToolsTests { // #endregion // ================================ + // ================================ + // #region - invoke constructor + // ================================ + + @Test + void test_constructor_isNotAccessible_ThrowsIllegalStateException() { + Constructor[] constructors = ArrayTools.class.getDeclaredConstructors(); + Arrays.stream(constructors) + .forEach(constructor -> { + assertFalse(constructor.isAccessible()); + constructor.setAccessible(true); + Throwable cause = assertThrows(Exception.class, constructor::newInstance) + .getCause(); + assertInstanceOf(IllegalStateException.class, cause); + assertEquals("Utility class", cause.getMessage()); + }); + } + + // ================================ + // #endregion - invoke constructor + // ================================ + } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/AssertToolsTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/AssertToolsTests.java index b6ea20e..5127592 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/AssertToolsTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/AssertToolsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2024 the original author or authors. + * Copyright 2024-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,9 @@ package xyz.zhouxy.plusone.commons.util; import static org.junit.jupiter.api.Assertions.*; +import java.lang.reflect.Constructor; import java.time.LocalDate; +import java.util.Arrays; import java.util.Optional; import java.util.function.Supplier; @@ -939,4 +941,25 @@ public class AssertToolsTests { // #endregion - Condition + // ================================ + // #region - invoke constructor + // ================================ + + @Test + void test_constructor_isNotAccessible_ThrowsIllegalStateException() { + Constructor[] constructors = AssertTools.class.getDeclaredConstructors(); + Arrays.stream(constructors) + .forEach(constructor -> { + assertFalse(constructor.isAccessible()); + constructor.setAccessible(true); + Throwable cause = assertThrows(Exception.class, constructor::newInstance) + .getCause(); + assertInstanceOf(IllegalStateException.class, cause); + assertEquals("Utility class", cause.getMessage()); + }); + } + + // ================================ + // #endregion - invoke constructor + // ================================ } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/BigDecimalsTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/BigDecimalsTests.java index 2fc6a0c..61e2b98 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/BigDecimalsTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/BigDecimalsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2024 the original author or authors. + * Copyright 2024-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,7 +18,9 @@ package xyz.zhouxy.plusone.commons.util; import static org.junit.jupiter.api.Assertions.*; +import java.lang.reflect.Constructor; import java.math.BigDecimal; +import java.util.Arrays; import org.junit.jupiter.api.Test; @@ -202,4 +204,17 @@ public class BigDecimalsTests { assertEquals(bd, BigDecimals.of("10")); } + @Test + void test_constructor_isNotAccessible_ThrowsIllegalStateException() { + Constructor[] constructors = BigDecimals.class.getDeclaredConstructors(); + Arrays.stream(constructors) + .forEach(constructor -> { + assertFalse(constructor.isAccessible()); + constructor.setAccessible(true); + Throwable cause = assertThrows(Exception.class, constructor::newInstance) + .getCause(); + assertInstanceOf(IllegalStateException.class, cause); + assertEquals("Utility class", cause.getMessage()); + }); + } } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/DateTimeToolsTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/DateTimeToolsTests.java index a1796bf..03005ae 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/DateTimeToolsTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/DateTimeToolsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2023-2024 the original author or authors. + * Copyright 2023-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,10 +18,12 @@ package xyz.zhouxy.plusone.commons.util; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.lang.reflect.Constructor; import java.time.DateTimeException; import java.time.Instant; import java.time.LocalDate; @@ -32,6 +34,7 @@ import java.time.Year; import java.time.YearMonth; import java.time.ZoneId; import java.time.ZonedDateTime; +import java.util.Arrays; import java.util.Calendar; import java.util.Date; import java.util.TimeZone; @@ -493,4 +496,27 @@ class DateTimeToolsTests { // ================================ // #endregion - toString // ================================ + + // ================================ + // #region - invoke constructor + // ================================ + + @Test + void test_constructor_isNotAccessible_ThrowsIllegalStateException() { + Constructor[] constructors = DateTimeTools.class.getDeclaredConstructors(); + Arrays.stream(constructors) + .forEach(constructor -> { + assertFalse(constructor.isAccessible()); + constructor.setAccessible(true); + Throwable cause = assertThrows(Exception.class, constructor::newInstance) + .getCause(); + assertInstanceOf(IllegalStateException.class, cause); + assertEquals("Utility class", cause.getMessage()); + }); + } + + // ================================ + // #endregion - invoke constructor + // ================================ + } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/EnumToolsTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/EnumToolsTests.java index d4442cd..9a695af 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/EnumToolsTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/EnumToolsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2024 the original author or authors. + * Copyright 2024-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,10 +17,15 @@ package xyz.zhouxy.plusone.commons.util; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; +import java.lang.reflect.Constructor; +import java.util.Arrays; + import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -222,4 +227,18 @@ class EnumToolsTests { assertSame(MyEnum.VALUE_0, EnumTools.valueOf(MyEnum.class, null, MyEnum.VALUE_0)); assertNull(EnumTools.valueOf(MyEnum.class, null, null)); } + + @Test + void test_constructor_isNotAccessible_ThrowsIllegalStateException() { + Constructor[] constructors = EnumTools.class.getDeclaredConstructors(); + Arrays.stream(constructors) + .forEach(constructor -> { + assertFalse(constructor.isAccessible()); + constructor.setAccessible(true); + Throwable cause = assertThrows(Exception.class, constructor::newInstance) + .getCause(); + assertInstanceOf(IllegalStateException.class, cause); + assertEquals("Utility class", cause.getMessage()); + }); + } } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/IdGeneratorTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/IdGeneratorTests.java index ccc3f59..f970df1 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/IdGeneratorTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/IdGeneratorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2024 the original author or authors. + * Copyright 2024-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,12 @@ package xyz.zhouxy.plusone.commons.util; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; +import java.lang.reflect.Constructor; +import java.util.Arrays; import java.util.Set; import java.util.UUID; import java.util.concurrent.LinkedBlockingQueue; @@ -83,4 +88,17 @@ public class IdGeneratorTests { IdGenerator.toSimpleString(id)); } + @Test + void test_constructor_isNotAccessible_ThrowsIllegalStateException() { + Constructor[] constructors = IdGenerator.class.getDeclaredConstructors(); + Arrays.stream(constructors) + .forEach(constructor -> { + assertFalse(constructor.isAccessible()); + constructor.setAccessible(true); + Throwable cause = assertThrows(Exception.class, constructor::newInstance) + .getCause(); + assertInstanceOf(IllegalStateException.class, cause); + assertEquals("Utility class", cause.getMessage()); + }); + } } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/NumbersTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/NumbersTests.java index 1e2435f..0f8b9b1 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/NumbersTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/NumbersTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2024 the original author or authors. + * Copyright 2024-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,9 +17,16 @@ package xyz.zhouxy.plusone.commons.util; import org.junit.jupiter.api.Test; + +import java.lang.reflect.Constructor; import java.math.BigDecimal; import java.math.BigInteger; +import java.util.Arrays; + import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; public class NumbersTests { @@ -184,4 +191,18 @@ class NumbersTests { BigDecimal result = Numbers.nullToZero(value); assertEquals(BigDecimal.ZERO, result); } + + @Test + void test_constructor_isNotAccessible_ThrowsIllegalStateException() { + Constructor[] constructors = Numbers.class.getDeclaredConstructors(); + Arrays.stream(constructors) + .forEach(constructor -> { + assertFalse(constructor.isAccessible()); + constructor.setAccessible(true); + Throwable cause = assertThrows(Exception.class, constructor::newInstance) + .getCause(); + assertInstanceOf(IllegalStateException.class, cause); + assertEquals("Utility class", cause.getMessage()); + }); + } } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/OptionalToolsTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/OptionalToolsTests.java index 79199e4..6569fca 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/OptionalToolsTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/OptionalToolsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2024 the original author or authors. + * Copyright 2024-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,10 +18,13 @@ package xyz.zhouxy.plusone.commons.util; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.lang.reflect.Constructor; +import java.util.Arrays; import java.util.Optional; import java.util.OptionalDouble; import java.util.OptionalInt; @@ -184,4 +187,18 @@ class OptionalToolsTests { Double result = OptionalTools.toDouble(OptionalDouble.of(10.0)); assertEquals(10.0, result, 0.0001); } + + @Test + void test_constructor_isNotAccessible_ThrowsIllegalStateException() { + Constructor[] constructors = OptionalTools.class.getDeclaredConstructors(); + Arrays.stream(constructors) + .forEach(constructor -> { + assertFalse(constructor.isAccessible()); + constructor.setAccessible(true); + Throwable cause = assertThrows(Exception.class, constructor::newInstance) + .getCause(); + assertInstanceOf(IllegalStateException.class, cause); + assertEquals("Utility class", cause.getMessage()); + }); + } } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/RandomToolsTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/RandomToolsTests.java index 2f63bc6..7a05601 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/RandomToolsTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/RandomToolsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2024 the original author or authors. + * Copyright 2024-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,9 +18,13 @@ package xyz.zhouxy.plusone.commons.util; import static org.junit.jupiter.api.Assertions.assertAll; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertThrows; +import java.lang.reflect.Constructor; import java.security.SecureRandom; +import java.util.Arrays; import java.util.Random; import org.junit.jupiter.api.BeforeAll; @@ -102,4 +106,18 @@ public class RandomToolsTests { String result = RandomTools.secureRandomStr(sourceCharactersString, 5); assertEquals(5, result.length()); } + + @Test + void test_constructor_isNotAccessible_ThrowsIllegalStateException() { + Constructor[] constructors = RandomTools.class.getDeclaredConstructors(); + Arrays.stream(constructors) + .forEach(constructor -> { + assertFalse(constructor.isAccessible()); + constructor.setAccessible(true); + Throwable cause = assertThrows(Exception.class, constructor::newInstance) + .getCause(); + assertInstanceOf(IllegalStateException.class, cause); + assertEquals("Utility class", cause.getMessage()); + }); + } } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/RegexToolsTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/RegexToolsTests.java index 0d3fb8d..7ad974f 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/RegexToolsTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/RegexToolsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2024 the original author or authors. + * Copyright 2024-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,6 +18,8 @@ package xyz.zhouxy.plusone.commons.util; import static org.junit.jupiter.api.Assertions.*; +import java.lang.reflect.Constructor; +import java.util.Arrays; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -162,4 +164,18 @@ class RegexToolsTests { RegexTools.getMatcher("abc", pattern); }); } + + @Test + void test_constructor_isNotAccessible_ThrowsIllegalStateException() { + Constructor[] constructors = RegexTools.class.getDeclaredConstructors(); + Arrays.stream(constructors) + .forEach(constructor -> { + assertFalse(constructor.isAccessible()); + constructor.setAccessible(true); + Throwable cause = assertThrows(Exception.class, constructor::newInstance) + .getCause(); + assertInstanceOf(IllegalStateException.class, cause); + assertEquals("Utility class", cause.getMessage()); + }); + } } diff --git a/src/test/java/xyz/zhouxy/plusone/commons/util/StringToolsTests.java b/src/test/java/xyz/zhouxy/plusone/commons/util/StringToolsTests.java index b383ee6..94d88a5 100644 --- a/src/test/java/xyz/zhouxy/plusone/commons/util/StringToolsTests.java +++ b/src/test/java/xyz/zhouxy/plusone/commons/util/StringToolsTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2024 the original author or authors. + * Copyright 2024-2025 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -18,10 +18,14 @@ package xyz.zhouxy.plusone.commons.util; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; +import java.lang.reflect.Constructor; +import java.util.Arrays; + import org.junit.jupiter.api.Test; public @@ -84,4 +88,18 @@ class StringToolsTests { void repeat_ZeroTimes_ReturnsEmptyString() { assertEquals("", StringTools.repeat("Hello", 0)); } + + @Test + void test_constructor_isNotAccessible_ThrowsIllegalStateException() { + Constructor[] constructors = StringTools.class.getDeclaredConstructors(); + Arrays.stream(constructors) + .forEach(constructor -> { + assertFalse(constructor.isAccessible()); + constructor.setAccessible(true); + Throwable cause = assertThrows(Exception.class, constructor::newInstance) + .getCause(); + assertInstanceOf(IllegalStateException.class, cause); + assertEquals("Utility class", cause.getMessage()); + }); + } }