mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
add test
This commit is contained in:
parent
56aed3ba3a
commit
4842974b0d
@ -144,7 +144,6 @@ public class Assert {
|
||||
|
||||
/**
|
||||
* 断言对象是否为{@code null} ,如果不为{@code null} 抛出{@link IllegalArgumentException} 异常
|
||||
*
|
||||
* <pre class="code">
|
||||
* Assert.isNull(value, "The value must be null");
|
||||
* </pre>
|
||||
@ -160,7 +159,6 @@ public class Assert {
|
||||
|
||||
/**
|
||||
* 断言对象是否为{@code null} ,如果不为{@code null} 抛出{@link IllegalArgumentException} 异常
|
||||
*
|
||||
* <pre class="code">
|
||||
* Assert.isNull(value);
|
||||
* </pre>
|
||||
@ -201,7 +199,6 @@ public class Assert {
|
||||
|
||||
/**
|
||||
* 断言对象是否不为{@code null} ,如果为{@code null} 抛出{@link IllegalArgumentException} 异常 Assert that an object is not {@code null} .
|
||||
*
|
||||
* <pre class="code">
|
||||
* Assert.notNull(clazz, "The class must not be null");
|
||||
* </pre>
|
||||
@ -219,7 +216,6 @@ public class Assert {
|
||||
|
||||
/**
|
||||
* 断言对象是否不为{@code null} ,如果为{@code null} 抛出{@link IllegalArgumentException} 异常
|
||||
*
|
||||
* <pre class="code">
|
||||
* Assert.notNull(clazz);
|
||||
* </pre>
|
||||
@ -358,8 +354,8 @@ public class Assert {
|
||||
}
|
||||
|
||||
/**
|
||||
* 断言给定字符串是否不被另一个字符串包含(即是否为子串)
|
||||
* 并使用指定的函数获取错误信息返回
|
||||
* 断言给定字符串是否不被另一个字符串包含(即是否为子串),并使用指定的函数获取错误信息返回<br>
|
||||
* 如果非子串,返回子串,如果是子串,则抛出{@link IllegalArgumentException}异常。
|
||||
* <pre class="code">
|
||||
* Assert.notContain(name, "rod", ()->{
|
||||
* // to query relation message
|
||||
@ -385,43 +381,42 @@ public class Assert {
|
||||
}
|
||||
|
||||
/**
|
||||
* 断言给定字符串是否不被另一个字符串包含(即是否为子串)
|
||||
*
|
||||
* 断言给定字符串是否不被另一个字符串包含(即是否为子串)<br>
|
||||
* 如果非子串,返回子串,如果是子串,则抛出{@link IllegalArgumentException}异常。
|
||||
* <pre class="code">
|
||||
* Assert.notContain(name, "rod", "Name must not contain 'rod'");
|
||||
* </pre>
|
||||
*
|
||||
* @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的子串。<br>
|
||||
* 如果非子串,返回子串,如果是子串,则抛出{@link IllegalArgumentException}异常。
|
||||
* <pre class="code">
|
||||
* Assert.notContain(name, "rod");
|
||||
* </pre>
|
||||
*
|
||||
* @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} 且至少包含一个元素
|
||||
* 并使用指定的函数获取错误信息返回
|
||||
*
|
||||
* <pre class="code">
|
||||
* Assert.notEmpty(array, ()->{
|
||||
* // to query relation message
|
||||
@ -447,7 +442,6 @@ public class Assert {
|
||||
|
||||
/**
|
||||
* 断言给定数组是否包含元素,数组必须不为 {@code null} 且至少包含一个元素
|
||||
*
|
||||
* <pre class="code">
|
||||
* Assert.notEmpty(array, "The array must have elements");
|
||||
* </pre>
|
||||
@ -465,7 +459,6 @@ public class Assert {
|
||||
|
||||
/**
|
||||
* 断言给定数组是否包含元素,数组必须不为 {@code null} 且至少包含一个元素
|
||||
*
|
||||
* <pre class="code">
|
||||
* Assert.notEmpty(array, "The array must have elements");
|
||||
* </pre>
|
||||
@ -507,7 +500,6 @@ public class Assert {
|
||||
|
||||
/**
|
||||
* 断言给定数组是否不包含{@code null}元素,如果数组为空或 {@code null}将被认为不包含
|
||||
*
|
||||
* <pre class="code">
|
||||
* Assert.noNullElements(array, "The array must not have null elements");
|
||||
* </pre>
|
||||
@ -525,7 +517,6 @@ public class Assert {
|
||||
|
||||
/**
|
||||
* 断言给定数组是否不包含{@code null}元素,如果数组为空或 {@code null}将被认为不包含
|
||||
*
|
||||
* <pre class="code">
|
||||
* Assert.noNullElements(array);
|
||||
* </pre>
|
||||
@ -568,7 +559,6 @@ public class Assert {
|
||||
|
||||
/**
|
||||
* 断言给定集合非空
|
||||
*
|
||||
* <pre class="code">
|
||||
* Assert.notEmpty(collection, "Collection must have elements");
|
||||
* </pre>
|
||||
@ -587,7 +577,6 @@ public class Assert {
|
||||
|
||||
/**
|
||||
* 断言给定集合非空
|
||||
*
|
||||
* <pre class="code">
|
||||
* Assert.notEmpty(collection);
|
||||
* </pre>
|
||||
@ -632,7 +621,6 @@ public class Assert {
|
||||
|
||||
/**
|
||||
* 断言给定Map非空
|
||||
*
|
||||
* <pre class="code">
|
||||
* Assert.notEmpty(map, "Map must have entries");
|
||||
* </pre>
|
||||
@ -652,7 +640,6 @@ public class Assert {
|
||||
|
||||
/**
|
||||
* 断言给定Map非空
|
||||
*
|
||||
* <pre class="code">
|
||||
* Assert.notEmpty(map, "Map must have entries");
|
||||
* </pre>
|
||||
@ -670,7 +657,6 @@ public class Assert {
|
||||
|
||||
/**
|
||||
* 断言给定对象是否是给定类的实例
|
||||
*
|
||||
* <pre class="code">
|
||||
* Assert.instanceOf(Foo.class, foo);
|
||||
* </pre>
|
||||
@ -688,7 +674,6 @@ public class Assert {
|
||||
|
||||
/**
|
||||
* 断言给定对象是否是给定类的实例
|
||||
*
|
||||
* <pre class="code">
|
||||
* Assert.instanceOf(Foo.class, foo, "foo must be an instance of class Foo");
|
||||
* </pre>
|
||||
@ -712,7 +697,6 @@ public class Assert {
|
||||
|
||||
/**
|
||||
* 断言 {@code superType.isAssignableFrom(subType)} 是否为 {@code true}.
|
||||
*
|
||||
* <pre class="code">
|
||||
* Assert.isAssignable(Number.class, myClass);
|
||||
* </pre>
|
||||
@ -727,7 +711,6 @@ public class Assert {
|
||||
|
||||
/**
|
||||
* 断言 {@code superType.isAssignableFrom(subType)} 是否为 {@code true}.
|
||||
*
|
||||
* <pre class="code">
|
||||
* Assert.isAssignable(Number.class, myClass, "myClass must can be assignable to class Number");
|
||||
* </pre>
|
||||
@ -767,7 +750,6 @@ public class Assert {
|
||||
|
||||
/**
|
||||
* 检查boolean表达式,当检查结果为false时抛出 {@code IllegalStateException}。
|
||||
*
|
||||
* <pre class="code">
|
||||
* Assert.state(id == null, "The id property must not already be initialized");
|
||||
* </pre>
|
||||
@ -785,7 +767,6 @@ public class Assert {
|
||||
|
||||
/**
|
||||
* 检查boolean表达式,当检查结果为false时抛出 {@code IllegalStateException}。
|
||||
*
|
||||
* <pre class="code">
|
||||
* Assert.state(id == null);
|
||||
* </pre>
|
||||
|
@ -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<String> a = ListUtil.of("");
|
||||
List<String> 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<String, String> a = MapUtil.of("", "");
|
||||
Map<String, String> 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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user