diff --git a/hutool-core/src/main/java/cn/hutool/core/lang/Assert.java b/hutool-core/src/main/java/cn/hutool/core/lang/Assert.java index 350b9bb65..e51a04738 100644 --- a/hutool-core/src/main/java/cn/hutool/core/lang/Assert.java +++ b/hutool-core/src/main/java/cn/hutool/core/lang/Assert.java @@ -144,7 +144,6 @@ public class Assert { /** * 断言对象是否为{@code null} ,如果不为{@code null} 抛出{@link IllegalArgumentException} 异常 - * *
 	 * Assert.isNull(value, "The value must be null");
 	 * 
@@ -160,7 +159,6 @@ public class Assert { /** * 断言对象是否为{@code null} ,如果不为{@code null} 抛出{@link IllegalArgumentException} 异常 - * *
 	 * Assert.isNull(value);
 	 * 
@@ -201,7 +199,6 @@ public class Assert { /** * 断言对象是否不为{@code null} ,如果为{@code null} 抛出{@link IllegalArgumentException} 异常 Assert that an object is not {@code null} . - * *
 	 * Assert.notNull(clazz, "The class must not be null");
 	 * 
@@ -219,7 +216,6 @@ public class Assert { /** * 断言对象是否不为{@code null} ,如果为{@code null} 抛出{@link IllegalArgumentException} 异常 - * *
 	 * Assert.notNull(clazz);
 	 * 
@@ -358,8 +354,8 @@ public class Assert { } /** - * 断言给定字符串是否不被另一个字符串包含(即是否为子串) - * 并使用指定的函数获取错误信息返回 + * 断言给定字符串是否不被另一个字符串包含(即是否为子串),并使用指定的函数获取错误信息返回
+ * 如果非子串,返回子串,如果是子串,则抛出{@link IllegalArgumentException}异常。 *
 	 * Assert.notContain(name, "rod", ()->{
 	 *      // to query relation message
@@ -385,43 +381,42 @@ public class Assert {
 	}
 
 	/**
-	 * 断言给定字符串是否不被另一个字符串包含(即是否为子串)
-	 *
+	 * 断言给定字符串是否不被另一个字符串包含(即是否为子串)
+ * 如果非子串,返回子串,如果是子串,则抛出{@link IllegalArgumentException}异常。 *
 	 * Assert.notContain(name, "rod", "Name must not contain 'rod'");
 	 * 
* * @param textToSearch 被搜索的字符串 - * @param substring 被检查的子串 + * @param subString 被检查的子串 * @param errorMsgTemplate 异常时的消息模板 * @param params 参数列表 * @return 被检查的子串 * @throws IllegalArgumentException 非子串抛出异常 */ - public static String notContain(final String textToSearch, final String substring, final String errorMsgTemplate, final Object... params) throws IllegalArgumentException { - return notContain(textToSearch, substring, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params))); + public static String notContain(final String textToSearch, final String subString, final String errorMsgTemplate, final Object... params) throws IllegalArgumentException { + return notContain(textToSearch, subString, () -> new IllegalArgumentException(StrUtil.format(errorMsgTemplate, params))); } /** - * 断言给定字符串是否不被另一个字符串包含(即是否为子串) - * + * 断言给定字符串是否不被另一个字符串包含(即是否为子串),即subString是否不是textToSearch的子串。
+ * 如果非子串,返回子串,如果是子串,则抛出{@link IllegalArgumentException}异常。 *
 	 * Assert.notContain(name, "rod");
 	 * 
* * @param textToSearch 被搜索的字符串 - * @param substring 被检查的子串 + * @param subString 被检查的子串 * @return 被检查的子串 * @throws IllegalArgumentException 非子串抛出异常 */ - public static String notContain(final String textToSearch, final String substring) throws IllegalArgumentException { - return notContain(textToSearch, substring, "[Assertion failed] - this String argument must not contain the substring [{}]", substring); + public static String notContain(final String textToSearch, final String subString) throws IllegalArgumentException { + return notContain(textToSearch, subString, "[Assertion failed] - this String argument must not contain the substring [{}]", subString); } /** * 断言给定数组是否包含元素,数组必须不为 {@code null} 且至少包含一个元素 * 并使用指定的函数获取错误信息返回 - * *
 	 * Assert.notEmpty(array, ()->{
 	 *      // to query relation message
@@ -447,7 +442,6 @@ public class Assert {
 
 	/**
 	 * 断言给定数组是否包含元素,数组必须不为 {@code null} 且至少包含一个元素
-	 *
 	 * 
 	 * Assert.notEmpty(array, "The array must have elements");
 	 * 
@@ -465,7 +459,6 @@ public class Assert { /** * 断言给定数组是否包含元素,数组必须不为 {@code null} 且至少包含一个元素 - * *
 	 * Assert.notEmpty(array, "The array must have elements");
 	 * 
@@ -507,7 +500,6 @@ public class Assert { /** * 断言给定数组是否不包含{@code null}元素,如果数组为空或 {@code null}将被认为不包含 - * *
 	 * Assert.noNullElements(array, "The array must not have null elements");
 	 * 
@@ -525,7 +517,6 @@ public class Assert { /** * 断言给定数组是否不包含{@code null}元素,如果数组为空或 {@code null}将被认为不包含 - * *
 	 * Assert.noNullElements(array);
 	 * 
@@ -568,7 +559,6 @@ public class Assert { /** * 断言给定集合非空 - * *
 	 * Assert.notEmpty(collection, "Collection must have elements");
 	 * 
@@ -587,7 +577,6 @@ public class Assert { /** * 断言给定集合非空 - * *
 	 * Assert.notEmpty(collection);
 	 * 
@@ -632,7 +621,6 @@ public class Assert { /** * 断言给定Map非空 - * *
 	 * Assert.notEmpty(map, "Map must have entries");
 	 * 
@@ -652,7 +640,6 @@ public class Assert { /** * 断言给定Map非空 - * *
 	 * Assert.notEmpty(map, "Map must have entries");
 	 * 
@@ -670,7 +657,6 @@ public class Assert { /** * 断言给定对象是否是给定类的实例 - * *
 	 * Assert.instanceOf(Foo.class, foo);
 	 * 
@@ -688,7 +674,6 @@ public class Assert { /** * 断言给定对象是否是给定类的实例 - * *
 	 * Assert.instanceOf(Foo.class, foo, "foo must be an instance of class Foo");
 	 * 
@@ -712,7 +697,6 @@ public class Assert { /** * 断言 {@code superType.isAssignableFrom(subType)} 是否为 {@code true}. - * *
 	 * Assert.isAssignable(Number.class, myClass);
 	 * 
@@ -727,7 +711,6 @@ public class Assert { /** * 断言 {@code superType.isAssignableFrom(subType)} 是否为 {@code true}. - * *
 	 * Assert.isAssignable(Number.class, myClass, "myClass must can be assignable to class Number");
 	 * 
@@ -767,7 +750,6 @@ public class Assert { /** * 检查boolean表达式,当检查结果为false时抛出 {@code IllegalStateException}。 - * *
 	 * Assert.state(id == null, "The id property must not already be initialized");
 	 * 
@@ -785,7 +767,6 @@ public class Assert { /** * 检查boolean表达式,当检查结果为false时抛出 {@code IllegalStateException}。 - * *
 	 * Assert.state(id == null);
 	 * 
diff --git a/hutool-core/src/main/java/cn/hutool/core/reflect/LookupFactory.java b/hutool-core/src/main/java/cn/hutool/core/reflect/LookupFactory.java index a40b47137..5c284e18b 100644 --- a/hutool-core/src/main/java/cn/hutool/core/reflect/LookupFactory.java +++ b/hutool-core/src/main/java/cn/hutool/core/reflect/LookupFactory.java @@ -11,7 +11,7 @@ import java.lang.reflect.Method; * {@link MethodHandles.Lookup}工厂,用于创建{@link MethodHandles.Lookup}对象
* jdk8中如果直接调用{@link MethodHandles#lookup()}获取到的{@link MethodHandles.Lookup}在调用findSpecial和unreflectSpecial * 时会出现权限不够问题,抛出"no private access for invokespecial"异常,因此针对JDK8及JDK9+分别封装lookup方法。 - * + * *

* 参考: *

https://blog.csdn.net/u013202238/article/details/108687086

diff --git a/hutool-core/src/test/java/cn/hutool/core/lang/AssertTest.java b/hutool-core/src/test/java/cn/hutool/core/lang/AssertTest.java index cdb4507d3..bba79a832 100755 --- a/hutool-core/src/test/java/cn/hutool/core/lang/AssertTest.java +++ b/hutool-core/src/test/java/cn/hutool/core/lang/AssertTest.java @@ -1,21 +1,97 @@ package cn.hutool.core.lang; +import cn.hutool.core.collection.ListUtil; +import cn.hutool.core.map.MapUtil; import cn.hutool.core.text.StrUtil; import org.junit.Test; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + public class AssertTest { @Test - public void isNullTest(){ + public void isNullTest() { final String a = null; Assert.isNull(a); } + @Test - public void notNullTest(){ + public void notNullTest() { final String a = null; Assert.isNull(a); } + @Test + public void notEmptyTest() { + final String a = " "; + final String s = Assert.notEmpty(a); + org.junit.Assert.assertSame(a, s); + } + + @Test + public void notEmptyForArrayTest() { + // 虽然包含空字符串或者null,但是这大概数组由于有元素,则认定为非empty + String[] a = new String[]{""}; + String[] s = Assert.notEmpty(a); + org.junit.Assert.assertSame(a, s); + + a = new String[]{null}; + s = Assert.notEmpty(a); + org.junit.Assert.assertSame(a, s); + } + + @Test + public void notEmptyForCollectionTest() { + // 虽然包含空字符串或者null,但是这大概数组由于有元素,则认定为非empty + List a = ListUtil.of(""); + List s = Assert.notEmpty(a); + org.junit.Assert.assertSame(a, s); + + a = ListUtil.of((String)null); + s = Assert.notEmpty(a); + org.junit.Assert.assertSame(a, s); + } + + @Test + public void notEmptyForMapTest() { + // 虽然包含空字符串或者null,但是这大概数组由于有元素,则认定为非empty + Map a = MapUtil.of("", ""); + Map s = Assert.notEmpty(a); + org.junit.Assert.assertSame(a, s); + + a = MapUtil.of(null, null); + s = Assert.notEmpty(a); + org.junit.Assert.assertSame(a, s); + } + + @Test + public void noNullElementsTest() { + final String[] a = new String[]{""}; + final String[] s = Assert.noNullElements(a); + org.junit.Assert.assertSame(a, s); + } + + @Test(expected = IllegalArgumentException.class) + public void noNullElementsTest2() { + final String[] a = new String[]{null}; + Assert.noNullElements(a); + } + + @Test(expected = IllegalArgumentException.class) + public void noNullElementsTest3() { + final String[] a = new String[]{"a", null}; + Assert.noNullElements(a); + } + + @Test + public void notBlankTest() { + final String a = "a"; + final String s = Assert.notBlank(a); + org.junit.Assert.assertSame(a, s); + } + @Test(expected = IllegalArgumentException.class) public void isTrueTest() { final int i = 0; @@ -34,7 +110,7 @@ public class AssertTest { public void isTrueTest3() { final int i = -1; //noinspection ConstantConditions - Assert.isTrue(i > 0, ()-> new IndexOutOfBoundsException("relation message to return")); + Assert.isTrue(i > 0, () -> new IndexOutOfBoundsException("relation message to return")); } @Test @@ -59,4 +135,64 @@ public class AssertTest { Assert.notEquals(c, d, () -> new RuntimeException(StrUtil.format("{}和{}相等", c, d))); } + + @Test + public void notEqualsTest2() { + final Object c = null; + final Object d = "null"; + Assert.notEquals(c, d); + } + + @Test + public void checkBetweenTest() { + final int a = 12; + final int i = Assert.checkBetween(a, 1, 12); + org.junit.Assert.assertSame(a, i); + } + + @Test + public void checkBetweenTest2() { + final double a = 12; + final double i = Assert.checkBetween(a, 1, 12); + org.junit.Assert.assertSame(a, i); + } + + @Test + public void checkBetweenTest3() { + final Number a = 12; + final Number i = Assert.checkBetween(a, 1, 12); + org.junit.Assert.assertSame(a, i); + } + + @Test(expected = IllegalArgumentException.class) + public void notContainTest() { + final String sub = "a"; + final String a = Assert.notContain("abc", sub); + org.junit.Assert.assertSame(sub, a); + } + + @Test + public void notContainTest2() { + final String sub = "d"; + final String a = Assert.notContain("abc", sub); + org.junit.Assert.assertSame(sub, a); + } + + @Test + public void isInstanceOfTest() { + final String a = "a"; + final String s = Assert.isInstanceOf(String.class, a); + org.junit.Assert.assertSame(a, s); + } + + @Test + public void isAssignableTest() { + Assert.isAssignable(Map.class, HashMap.class); + } + + @Test + public void checkIndexTest() { + final int i = Assert.checkIndex(1, 10); + org.junit.Assert.assertEquals(1, i); + } }