`

Spring Assert(来自Spring源码)

阅读更多

这个Spring的通用类在代码中使用起来非常简洁!

Spring的工具类,在平时项目中完全可以重新打包使用

 

public abstract class Assert {

	/**
	 * Assert a boolean expression, throwing
	 * <code>IllegalArgumentException</code> if the test result is
	 * <code>false</code>.
	 * <pre>
	 * Assert.isTrue(i > 0, "The value must be greater than zero");</pre>
	 * @param expression a boolean expression
	 * @param message the exception message to use if the assertion fails
	 * @throws IllegalArgumentException if expression is <code>false</code>
	 */
	public static void isTrue(boolean expression, String message) {
		if (!expression) {
			throw new IllegalArgumentException(message);
		}
	}

	/**
	 * Assert a boolean expression, throwing
	 * <code>IllegalArgumentException</code> if the test result is
	 * <code>false</code>.
	 * <pre>
	 * Assert.isTrue(i > 0);</pre>
	 * @param expression a boolean expression
	 * @throws IllegalArgumentException if expression is <code>false</code>
	 */
	public static void isTrue(boolean expression) {
		isTrue(expression, "[Assertion failed] - this expression must be true");
	}

	/**
	 * Assert that an object is not null.
	 * <pre>
	 * Assert.notNull(clazz, "The class must not be null");</pre>
	 * @param object the object to check
	 * @param message the exception message to use if the assertion fails
	 * @throws IllegalArgumentException if the object is <code>null</code>
	 */
	public static void notNull(Object object, String message) {
		if (object == null) {
			throw new IllegalArgumentException(message);
		}
	}

	/**
	 * Assert that an object is not null.
	 * <pre>
	 * Assert.notNull(clazz);</pre>
	 * @param object the object to check
	 * @throws IllegalArgumentException if the object is <code>null</code>
	 */
	public static void notNull(Object object) {
		notNull(object, "[Assertion failed] - this argument is required; it cannot be null");
	}

	/**
	 * Assert that a string is not empty; that is, it must not be null and not empty.
	 * <pre>
	 * Assert.hasLength(name, "Name must not be empty");</pre>
	 * @param text the string to check
	 * @see StringUtils#hasLength
	 */
	public static void hasLength(String text, String message) {
		if (!StringUtils.hasLength(text)) {
			throw new IllegalArgumentException(message);
		}
	}

	/**
	 * Assert that a string is not empty; that is, it must not be null and not empty.
	 * <pre>
	 * Assert.hasLength(name);</pre>
	 * @param text the string to check
	 * @see StringUtils#hasLength
	 */
	public static void hasLength(String text) {
		hasLength(text, "[Assertion failed] - this string argument must have length; it cannot be null or empty");
	}

	/**
	 * Assert that a string has valid text content; that is, it must not be null
	 * and must contain at least one non-whitespace character.
	 * <pre>
	 * Assert.hasText(name, "Name must not be empty");</pre>
	 * @param text the string to check
	 * @see StringUtils#hasText
	 */
	public static void hasText(String text, String message) {
		if (!StringUtils.hasText(text)) {
			throw new IllegalArgumentException(message);
		}
	}

	/**
	 * Assert that a string has valid text content; that is, it must not be null
	 * and must contain at least one non-whitespace character.
	 * <pre>
	 * Assert.hasText(name, "Name must not be empty");</pre>
	 * @param text the string to check
	 * @see StringUtils#hasText
	 */
	public static void hasText(String text) {
		hasText(text,
				"[Assertion failed] - this string argument must have text; it cannot be null, empty, or blank");
	}

	/**
	 * Assert that an array has elements; that is, it must not be
	 * <code>null</code> and must have at least one element.
	 * <pre>
	 * Assert.notEmpty(array, "The array must have elements");</pre>
	 * @param array the array to check
	 * @param message the exception message to use if the assertion fails
	 * @throws IllegalArgumentException if the object array is <code>null</code> or has no elements
	 */
	public static void notEmpty(Object[] array, String message) {
		if (array == null || array.length == 0) {
			throw new IllegalArgumentException(message);
		}
	}

	/**
	 * Assert that an array has elements; that is, it must not be
	 * <code>null</code> and must have at least one element.
	 * <pre>
	 * Assert.notEmpty(array);</pre>
	 * @param array the array to check
	 * @throws IllegalArgumentException if the object array is <code>null</code> or has no elements
	 */
	public static void notEmpty(Object[] array) {
		notEmpty(array, "[Assertion failed] - this array must not be empty: it must contain at least 1 element");
	}

	/**
	 * Assert that a collection has elements; that is, it must not be
	 * <code>null</code> and must have at least one element.
	 * <pre>
	 * Assert.notEmpty(collection, "Collection must have elements");</pre>
	 * @param collection the collection to check
	 * @param message the exception message to use if the assertion fails
	 * @throws IllegalArgumentException if the collection is <code>null</code> or has no elements
	 */
	public static void notEmpty(Collection<?> collection, String message) {
		if (collection == null || collection.isEmpty()) {
			throw new IllegalArgumentException(message);
		}
	}

	/**
	 * Assert that a collection has elements; that is, it must not be
	 * <code>null</code> and must have at least one element.
	 * <pre>
	 * Assert.notEmpty(collection, "Collection must have elements");</pre>
	 * @param collection the collection to check
	 * @throws IllegalArgumentException if the collection is <code>null</code> or has no elements
	 */
	public static void notEmpty(Collection<?> collection) {
		notEmpty(collection,
				"[Assertion failed] - this collection must not be empty: it must contain at least 1 element");
	}

	/**
	 * Assert that a Map has entries; that is, it must not be <code>null</code>
	 * and must have at least one entry.
	 * <pre>
	 * Assert.notEmpty(map, "Map must have entries");</pre>
	 * @param map the map to check
	 * @param message the exception message to use if the assertion fails
	 * @throws IllegalArgumentException if the map is <code>null</code> or has no entries
	 */
	public static void notEmpty(Map<?,?> map, String message) {
		if (map == null || map.isEmpty()) {
			throw new IllegalArgumentException(message);
		}
	}

	/**
	 * Assert that a Map has entries; that is, it must not be <code>null</code>
	 * and must have at least one entry.
	 * <pre>
	 * Assert.notEmpty(map);</pre>
	 * @param map the map to check
	 * @throws IllegalArgumentException if the map is <code>null</code> or has no entries
	 */
	public static void notEmpty(Map<?,?> map) {
		notEmpty(map, "[Assertion failed] - this map must not be empty; it must contain at least one entry");
	}

	/**
	 * Assert that the provided object is an instance of the provided class.
	 * <pre>
	 * Assert.instanceOf(Foo.class, foo);</pre>
	 * @param clazz the required class
	 * @param obj the object to check
	 * @throws IllegalArgumentException if the object is not an instance of clazz
	 * @see Class#isInstance
	 */
	public static void isInstanceOf(Class<?> clazz, Object obj) {
		isInstanceOf(clazz, obj, "");
	}

	/**
	 * Assert that the provided object is an instance of the provided class.
	 * <pre>
	 * Assert.instanceOf(Foo.class, foo);</pre>
	 * @param clazz the required class
	 * @param obj the object to check
	 * @param message a message which will be prepended to the message produced by
	 * the function itself, and which may be used to provide context. It should
	 * normally end in a ":" or ". " so that the function generate message looks
	 * ok when prepended to it.
	 * @throws IllegalArgumentException if the object is not an instance of clazz
	 * @see Class#isInstance
	 */
	public static void isInstanceOf(Class<?> clazz, Object obj, String message) {
		Assert.notNull(clazz, "The clazz to perform the instanceof assertion cannot be null");
		Assert.isTrue(clazz.isInstance(obj), message +
				"Object of class '" + (obj != null ? obj.getClass().getName() : "[null]") +
				"' must be an instance of '" + clazz.getName() + "'");
	}


	/**
	 * Assert a boolean expression, throwing <code>IllegalStateException</code>
	 * if the test result is <code>false</code>. Call isTrue if you wish to
	 * throw IllegalArgumentException on an assertion failure.
	 * <pre>
	 * Assert.state(id == null, "The id property must not already be initialized");</pre>
	 * @param expression a boolean expression
	 * @param message the exception message to use if the assertion fails
	 * @throws IllegalStateException if expression is <code>false</code>
	 */
	public static void state(boolean expression, String message) {
		if (!expression) {
			throw new IllegalStateException(message);
		}
	}

	/**
	 * Assert a boolean expression, throwing <code>IllegalStateException</code>
	 * if the test result is <code>false</code>. Call isTrue if you wish to
	 * throw IllegalArgumentException on an assertion failure.
	 * <pre>
	 * Assert.state(id == null);</pre>
	 * @param expression a boolean expression
	 * @throws IllegalStateException if expression is <code>false</code>
	 */
	public static void state(boolean expression) {
		state(expression, "[Assertion failed] - this state invariant must be true");
	}

}
分享到:
评论

相关推荐

    spring源代码

    import junit.framework.Assert; import org.jboss.vfs.VFS; import org.jboss.vfs.VirtualFile; import org.jboss.vfs.spi.RealFileSystem; import org.junit.Test; import org.springframework.core.io.Resource;...

    spring 5.2.9 07 源码分析-spring的bean工厂准备工作 测试用例

    assert message.equals("Hello, Spring!"); } } ``` 在这个测试用例中,`@SpringBootTest`注解启动了一个应用上下文,`@Autowired`注解注入了`MessageService` Bean,然后在`testMessage`方法中验证了Bean的正确...

    Spring源码学习五:BeanDefinition装载1

    Spring 源码学习五:BeanDefinition 装载 1 在 Spring 框架中,BeanDefinition 是一个核心概念,它描述了一个 Bean 的定义,包括其依赖项、作用域、生命周期等信息。在本篇文章中,我们将深入探讨 Spring 的源码,...

    Springboot事件监听源码

    本文将深入探讨Spring Boot事件监听的源码实现,并通过一个案例示例来阐述其工作原理。 Spring Boot使用Spring框架中的ApplicationEvent和ApplicationListener接口来实现事件发布和监听。首先,`ApplicationEvent`...

    spring3学习笔记(2)-集成ibatis3进行单元测试

    4. 断言(Assertion):使用JUnit提供的assert方法,如assertEquals,assertNull等,来检查测试的结果是否符合预期。 5. 数据库初始化:在测试前,可能需要对数据库进行预设数据,以满足特定测试场景。可以使用诸如...

    基于SpringBoot的仿牛客论坛源码.zip

    3. `assert` - 这个目录可能包含测试相关的资源,如单元测试或集成测试的代码,通常位于`src/test/java`和`src/test/resources`下。 4. `README.md` - 这是一个Markdown格式的文件,通常包含项目介绍、如何运行、...

    Junit 单元测试完整案例

    通过分析和运行源码,你可以了解如何将这些概念应用于实际项目,提高代码质量。 总结,Junit单元测试对于确保代码的正确性和可维护性至关重要。Spring和Struts框架的测试涉及到特定的策略和工具,理解并熟练运用...

    JUnit3.8的初步学习

    测试方法内部,可以使用断言(assert)方法来验证预期结果是否与实际结果相符,如`assertEquals()`和`assertTrue()`等。 在实际使用中,我们通常会使用注解(Annotation)来标记测试类和方法。然而,JUnit3.8并不...

    使用junit测试ssh中的dao

    5. **使用断言**:使用JUnit提供的assert系列方法,比如assertEquals,来验证测试结果是否符合预期。 6. **异常测试**:如果DAO方法可能会抛出异常,确保测试用例涵盖这些情况,检查异常类型和消息是否正确。 ...

    java面试题

    64. 什么时候用assert。 47 65. 什么是java序列化,如何实现java序列化? 48 65.1. java序列化、反序列化 48 65.2. 对象的序列化主要有两种用途: 48 65.3. 对象序列化包括如下步骤: 49 65.4. 对象反序列化的步骤...

    意识测试

    Spring Boot框架提供了Spring Boot Test支持,可以方便地进行集成测试,确保各个组件协同工作的正确性。 3. 性能测试(Performance Testing): 对于Java应用,性能测试旨在评估系统在高负载或大数据量情况下的...

    Junit的使用

    例如,JUnit4引入了`@RunWith`注解,允许使用自定义的测试运行器,如Spring的`@RunWith(SpringRunner.class)`,这在进行依赖注入的测试中非常有用。 在实际开发中,为了实现更灵活的测试,我们还可以使用参数化测试...

    maven的环境搭建、配置和测试

    import static org.junit.Assert.*; public class MyTest { @Test public void testExample() { assertEquals(4, 2 + 2); } } ``` 执行`mvn test`命令,Maven会自动运行所有标记为`@Test`的测试方法。 总结,...

Global site tag (gtag.js) - Google Analytics