blob: dd651e95b43b334520fdcaf7036509641c2ab885 [file] [log] [blame]
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../../jacoco-resources/report.gif" type="image/gif"/><title>Assert.java</title><link rel="stylesheet" href="../../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../../index.html" class="el_report">Apache Shiro :: Test Coverage</a> &gt; <a href="../index.html" class="el_bundle">shiro-lang</a> &gt; <a href="index.source.html" class="el_package">org.apache.shiro.util</a> &gt; <span class="el_source">Assert.java</span></div><h1>Assert.java</h1><pre class="source lang-java linenums">/*
* 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
* &quot;License&quot;); 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
* &quot;AS IS&quot; 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.shiro.util;
import java.util.Collection;
import java.util.Map;
/**
* Assertion utility class that assists in validating arguments.
* Useful for identifying programmer errors early and clearly at runtime.
* Usage also reduces a program's
* &lt;a href=&quot;http://en.wikipedia.org/wiki/Cyclomatic_complexity&quot;&gt;cyclomatic complexity&lt;/a&gt;.
*
* &lt;p&gt;For example, if the contract of a public method states it does not
* allow &lt;code&gt;null&lt;/code&gt; arguments, Assert can be used to validate that
* contract. Doing this clearly indicates a contract violation when it
* occurs and protects the class's invariants.
*
* &lt;p&gt;Typically used to validate method arguments rather than configuration
* properties, to check for cases that are usually programmer errors rather than
* configuration errors. In contrast to config initialization code, there is
* usally no point in falling back to defaults in such methods.
*
* &lt;p&gt;This class is similar to JUnit's assertion library. If an argument value is
* deemed invalid, an {@link IllegalArgumentException} is thrown (typically).
* For example:
*
* &lt;pre class=&quot;code&quot;&gt;
* Assert.notNull(clazz, &quot;The class must not be null&quot;);
* Assert.isTrue(i &gt; 0, &quot;The value must be greater than zero&quot;);&lt;/pre&gt;
*
* Mainly for internal use within the framework; consider Jakarta's Commons Lang
* &gt;= 2.0 for a more comprehensive suite of assertion utilities.
* &lt;p/&gt;
* &lt;em&gt;Gratefully borrowed from the Spring Framework, also Apache 2.0 licensed&lt;/em&gt;
*
* @author Keith Donald
* @author Juergen Hoeller
* @author Colin Sampaleanu
* @author Rob Harrop
* @since 1.3
*/
<span class="nc" id="L59">public abstract class Assert {</span>
/**
* Assert a boolean expression, throwing &lt;code&gt;IllegalArgumentException&lt;/code&gt;
* if the test result is &lt;code&gt;false&lt;/code&gt;.
* &lt;pre class=&quot;code&quot;&gt;Assert.isTrue(i &amp;gt; 0, &quot;The value must be greater than zero&quot;);&lt;/pre&gt;
* @param expression a boolean expression
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if expression is &lt;code&gt;false&lt;/code&gt;
*/
public static void isTrue(boolean expression, String message) {
<span class="nc bnc" id="L70" title="All 2 branches missed."> if (!expression) {</span>
<span class="nc" id="L71"> throw new IllegalArgumentException(message);</span>
}
<span class="nc" id="L73"> }</span>
/**
* Assert a boolean expression, throwing &lt;code&gt;IllegalArgumentException&lt;/code&gt;
* if the test result is &lt;code&gt;false&lt;/code&gt;.
* &lt;pre class=&quot;code&quot;&gt;Assert.isTrue(i &amp;gt; 0);&lt;/pre&gt;
* @param expression a boolean expression
* @throws IllegalArgumentException if expression is &lt;code&gt;false&lt;/code&gt;
*/
public static void isTrue(boolean expression) {
<span class="nc" id="L83"> isTrue(expression, &quot;[Assertion failed] - this expression must be true&quot;);</span>
<span class="nc" id="L84"> }</span>
/**
* Assert that an object is &lt;code&gt;null&lt;/code&gt; .
* &lt;pre class=&quot;code&quot;&gt;Assert.isNull(value, &quot;The value must be null&quot;);&lt;/pre&gt;
* @param object the object to check
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the object is not &lt;code&gt;null&lt;/code&gt;
*/
public static void isNull(Object object, String message) {
<span class="nc bnc" id="L94" title="All 2 branches missed."> if (object != null) {</span>
<span class="nc" id="L95"> throw new IllegalArgumentException(message);</span>
}
<span class="nc" id="L97"> }</span>
/**
* Assert that an object is &lt;code&gt;null&lt;/code&gt; .
* &lt;pre class=&quot;code&quot;&gt;Assert.isNull(value);&lt;/pre&gt;
* @param object the object to check
* @throws IllegalArgumentException if the object is not &lt;code&gt;null&lt;/code&gt;
*/
public static void isNull(Object object) {
<span class="nc" id="L106"> isNull(object, &quot;[Assertion failed] - the object argument must be null&quot;);</span>
<span class="nc" id="L107"> }</span>
/**
* Assert that an object is not &lt;code&gt;null&lt;/code&gt; .
* &lt;pre class=&quot;code&quot;&gt;Assert.notNull(clazz, &quot;The class must not be null&quot;);&lt;/pre&gt;
* @param object the object to check
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the object is &lt;code&gt;null&lt;/code&gt;
*/
public static void notNull(Object object, String message) {
<span class="pc bpc" id="L117" title="1 of 2 branches missed."> if (object == null) {</span>
<span class="nc" id="L118"> throw new IllegalArgumentException(message);</span>
}
<span class="fc" id="L120"> }</span>
/**
* Assert that an object is not &lt;code&gt;null&lt;/code&gt; .
* &lt;pre class=&quot;code&quot;&gt;Assert.notNull(clazz);&lt;/pre&gt;
* @param object the object to check
* @throws IllegalArgumentException if the object is &lt;code&gt;null&lt;/code&gt;
*/
public static void notNull(Object object) {
<span class="fc" id="L129"> notNull(object, &quot;[Assertion failed] - this argument is required; it must not be null&quot;);</span>
<span class="fc" id="L130"> }</span>
/**
* Assert that the given String is not empty; that is,
* it must not be &lt;code&gt;null&lt;/code&gt; and not the empty String.
* &lt;pre class=&quot;code&quot;&gt;Assert.hasLength(name, &quot;Name must not be empty&quot;);&lt;/pre&gt;
* @param text the String to check
* @param message the exception message to use if the assertion fails
* @see StringUtils#hasLength
*/
public static void hasLength(String text, String message) {
<span class="nc bnc" id="L141" title="All 2 branches missed."> if (!StringUtils.hasLength(text)) {</span>
<span class="nc" id="L142"> throw new IllegalArgumentException(message);</span>
}
<span class="nc" id="L144"> }</span>
/**
* Assert that the given String is not empty; that is,
* it must not be &lt;code&gt;null&lt;/code&gt; and not the empty String.
* &lt;pre class=&quot;code&quot;&gt;Assert.hasLength(name);&lt;/pre&gt;
* @param text the String to check
* @see StringUtils#hasLength
*/
public static void hasLength(String text) {
<span class="nc" id="L154"> hasLength(text,</span>
&quot;[Assertion failed] - this String argument must have length; it must not be null or empty&quot;);
<span class="nc" id="L156"> }</span>
/**
* Assert that the given String has valid text content; that is, it must not
* be &lt;code&gt;null&lt;/code&gt; and must contain at least one non-whitespace character.
* &lt;pre class=&quot;code&quot;&gt;Assert.hasText(name, &quot;'name' must not be empty&quot;);&lt;/pre&gt;
* @param text the String to check
* @param message the exception message to use if the assertion fails
* @see StringUtils#hasText
*/
public static void hasText(String text, String message) {
<span class="nc bnc" id="L167" title="All 2 branches missed."> if (!StringUtils.hasText(text)) {</span>
<span class="nc" id="L168"> throw new IllegalArgumentException(message);</span>
}
<span class="nc" id="L170"> }</span>
/**
* Assert that the given String has valid text content; that is, it must not
* be &lt;code&gt;null&lt;/code&gt; and must contain at least one non-whitespace character.
* &lt;pre class=&quot;code&quot;&gt;Assert.hasText(name, &quot;'name' must not be empty&quot;);&lt;/pre&gt;
* @param text the String to check
* @see StringUtils#hasText
*/
public static void hasText(String text) {
<span class="nc" id="L180"> hasText(text,</span>
&quot;[Assertion failed] - this String argument must have text; it must not be null, empty, or blank&quot;);
<span class="nc" id="L182"> }</span>
/**
* Assert that the given text does not contain the given substring.
* &lt;pre class=&quot;code&quot;&gt;Assert.doesNotContain(name, &quot;rod&quot;, &quot;Name must not contain 'rod'&quot;);&lt;/pre&gt;
* @param textToSearch the text to search
* @param substring the substring to find within the text
* @param message the exception message to use if the assertion fails
*/
public static void doesNotContain(String textToSearch, String substring, String message) {
<span class="nc bnc" id="L192" title="All 4 branches missed."> if (StringUtils.hasLength(textToSearch) &amp;&amp; StringUtils.hasLength(substring) &amp;&amp;</span>
<span class="nc bnc" id="L193" title="All 2 branches missed."> textToSearch.indexOf(substring) != -1) {</span>
<span class="nc" id="L194"> throw new IllegalArgumentException(message);</span>
}
<span class="nc" id="L196"> }</span>
/**
* Assert that the given text does not contain the given substring.
* &lt;pre class=&quot;code&quot;&gt;Assert.doesNotContain(name, &quot;rod&quot;);&lt;/pre&gt;
* @param textToSearch the text to search
* @param substring the substring to find within the text
*/
public static void doesNotContain(String textToSearch, String substring) {
<span class="nc" id="L205"> doesNotContain(textToSearch, substring,</span>
&quot;[Assertion failed] - this String argument must not contain the substring [&quot; + substring + &quot;]&quot;);
<span class="nc" id="L207"> }</span>
/**
* Assert that an array has elements; that is, it must not be
* &lt;code&gt;null&lt;/code&gt; and must have at least one element.
* &lt;pre class=&quot;code&quot;&gt;Assert.notEmpty(array, &quot;The array must have elements&quot;);&lt;/pre&gt;
* @param array the array to check
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the object array is &lt;code&gt;null&lt;/code&gt; or has no elements
*/
public static void notEmpty(Object[] array, String message) {
<span class="nc bnc" id="L219" title="All 4 branches missed."> if (array == null || array.length == 0) {</span>
<span class="nc" id="L220"> throw new IllegalArgumentException(message);</span>
}
<span class="nc" id="L222"> }</span>
/**
* Assert that an array has elements; that is, it must not be
* &lt;code&gt;null&lt;/code&gt; and must have at least one element.
* &lt;pre class=&quot;code&quot;&gt;Assert.notEmpty(array);&lt;/pre&gt;
* @param array the array to check
* @throws IllegalArgumentException if the object array is &lt;code&gt;null&lt;/code&gt; or has no elements
*/
public static void notEmpty(Object[] array) {
<span class="nc" id="L232"> notEmpty(array, &quot;[Assertion failed] - this array must not be empty: it must contain at least 1 element&quot;);</span>
<span class="nc" id="L233"> }</span>
/**
* Assert that an array has no null elements.
* Note: Does not complain if the array is empty!
* &lt;pre class=&quot;code&quot;&gt;Assert.noNullElements(array, &quot;The array must have non-null elements&quot;);&lt;/pre&gt;
* @param array the array to check
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the object array contains a &lt;code&gt;null&lt;/code&gt; element
*/
public static void noNullElements(Object[] array, String message) {
<span class="nc bnc" id="L244" title="All 2 branches missed."> if (array != null) {</span>
<span class="nc bnc" id="L245" title="All 2 branches missed."> for (int i = 0; i &lt; array.length; i++) {</span>
<span class="nc bnc" id="L246" title="All 2 branches missed."> if (array[i] == null) {</span>
<span class="nc" id="L247"> throw new IllegalArgumentException(message);</span>
}
}
}
<span class="nc" id="L251"> }</span>
/**
* Assert that an array has no null elements.
* Note: Does not complain if the array is empty!
* &lt;pre class=&quot;code&quot;&gt;Assert.noNullElements(array);&lt;/pre&gt;
* @param array the array to check
* @throws IllegalArgumentException if the object array contains a &lt;code&gt;null&lt;/code&gt; element
*/
public static void noNullElements(Object[] array) {
<span class="nc" id="L261"> noNullElements(array, &quot;[Assertion failed] - this array must not contain any null elements&quot;);</span>
<span class="nc" id="L262"> }</span>
/**
* Assert that a collection has elements; that is, it must not be
* &lt;code&gt;null&lt;/code&gt; and must have at least one element.
* &lt;pre class=&quot;code&quot;&gt;Assert.notEmpty(collection, &quot;Collection must have elements&quot;);&lt;/pre&gt;
* @param collection the collection to check
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the collection is &lt;code&gt;null&lt;/code&gt; or has no elements
*/
public static void notEmpty(Collection collection, String message) {
<span class="nc bnc" id="L273" title="All 2 branches missed."> if (isEmpty(collection)) {</span>
<span class="nc" id="L274"> throw new IllegalArgumentException(message);</span>
}
<span class="nc" id="L276"> }</span>
/**
* Assert that a collection has elements; that is, it must not be
* &lt;code&gt;null&lt;/code&gt; and must have at least one element.
* &lt;pre class=&quot;code&quot;&gt;Assert.notEmpty(collection, &quot;Collection must have elements&quot;);&lt;/pre&gt;
* @param collection the collection to check
* @throws IllegalArgumentException if the collection is &lt;code&gt;null&lt;/code&gt; or has no elements
*/
public static void notEmpty(Collection collection) {
<span class="nc" id="L286"> notEmpty(collection,</span>
&quot;[Assertion failed] - this collection must not be empty: it must contain at least 1 element&quot;);
<span class="nc" id="L288"> }</span>
/**
* Assert that a Map has entries; that is, it must not be &lt;code&gt;null&lt;/code&gt;
* and must have at least one entry.
* &lt;pre class=&quot;code&quot;&gt;Assert.notEmpty(map, &quot;Map must have entries&quot;);&lt;/pre&gt;
* @param map the map to check
* @param message the exception message to use if the assertion fails
* @throws IllegalArgumentException if the map is &lt;code&gt;null&lt;/code&gt; or has no entries
*/
public static void notEmpty(Map map, String message) {
<span class="nc bnc" id="L299" title="All 2 branches missed."> if (isEmpty(map)) {</span>
<span class="nc" id="L300"> throw new IllegalArgumentException(message);</span>
}
<span class="nc" id="L302"> }</span>
/**
* Assert that a Map has entries; that is, it must not be &lt;code&gt;null&lt;/code&gt;
* and must have at least one entry.
* &lt;pre class=&quot;code&quot;&gt;Assert.notEmpty(map);&lt;/pre&gt;
* @param map the map to check
* @throws IllegalArgumentException if the map is &lt;code&gt;null&lt;/code&gt; or has no entries
*/
public static void notEmpty(Map map) {
<span class="nc" id="L312"> notEmpty(map, &quot;[Assertion failed] - this map must not be empty; it must contain at least one entry&quot;);</span>
<span class="nc" id="L313"> }</span>
/**
* Assert that the provided object is an instance of the provided class.
* &lt;pre class=&quot;code&quot;&gt;Assert.instanceOf(Foo.class, foo);&lt;/pre&gt;
* @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) {
<span class="nc" id="L325"> isInstanceOf(clazz, obj, &quot;&quot;);</span>
<span class="nc" id="L326"> }</span>
/**
* Assert that the provided object is an instance of the provided class.
* &lt;pre class=&quot;code&quot;&gt;Assert.instanceOf(Foo.class, foo);&lt;/pre&gt;
* @param type the type to check against
* @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 &quot;: &quot; or &quot;. &quot; 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 type, Object obj, String message) {
<span class="nc" id="L341"> notNull(type, &quot;Type to check against must not be null&quot;);</span>
<span class="nc bnc" id="L342" title="All 2 branches missed."> if (!type.isInstance(obj)) {</span>
<span class="nc bnc" id="L343" title="All 2 branches missed."> throw new IllegalArgumentException(message +</span>
<span class="nc" id="L344"> &quot;Object of class [&quot; + (obj != null ? obj.getClass().getName() : &quot;null&quot;) +</span>
&quot;] must be an instance of &quot; + type);
}
<span class="nc" id="L347"> }</span>
/**
* Assert that &lt;code&gt;superType.isAssignableFrom(subType)&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt;.
* &lt;pre class=&quot;code&quot;&gt;Assert.isAssignable(Number.class, myClass);&lt;/pre&gt;
* @param superType the super type to check
* @param subType the sub type to check
* @throws IllegalArgumentException if the classes are not assignable
*/
public static void isAssignable(Class superType, Class subType) {
<span class="nc" id="L357"> isAssignable(superType, subType, &quot;&quot;);</span>
<span class="nc" id="L358"> }</span>
/**
* Assert that &lt;code&gt;superType.isAssignableFrom(subType)&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt;.
* &lt;pre class=&quot;code&quot;&gt;Assert.isAssignable(Number.class, myClass);&lt;/pre&gt;
* @param superType the super type to check against
* @param subType the sub type 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 &quot;: &quot; or &quot;. &quot; so that the function generate message looks
* ok when prepended to it.
* @throws IllegalArgumentException if the classes are not assignable
*/
public static void isAssignable(Class superType, Class subType, String message) {
<span class="nc" id="L372"> notNull(superType, &quot;Type to check against must not be null&quot;);</span>
<span class="nc bnc" id="L373" title="All 4 branches missed."> if (subType == null || !superType.isAssignableFrom(subType)) {</span>
<span class="nc" id="L374"> throw new IllegalArgumentException(message + subType + &quot; is not assignable to &quot; + superType);</span>
}
<span class="nc" id="L376"> }</span>
/**
* Assert a boolean expression, throwing &lt;code&gt;IllegalStateException&lt;/code&gt;
* if the test result is &lt;code&gt;false&lt;/code&gt;. Call isTrue if you wish to
* throw IllegalArgumentException on an assertion failure.
* &lt;pre class=&quot;code&quot;&gt;Assert.state(id == null, &quot;The id property must not already be initialized&quot;);&lt;/pre&gt;
* @param expression a boolean expression
* @param message the exception message to use if the assertion fails
* @throws IllegalStateException if expression is &lt;code&gt;false&lt;/code&gt;
*/
public static void state(boolean expression, String message) {
<span class="pc bpc" id="L389" title="1 of 2 branches missed."> if (!expression) {</span>
<span class="nc" id="L390"> throw new IllegalStateException(message);</span>
}
<span class="fc" id="L392"> }</span>
/**
* Assert a boolean expression, throwing {@link IllegalStateException}
* if the test result is &lt;code&gt;false&lt;/code&gt;.
* &lt;p&gt;Call {@link #isTrue(boolean)} if you wish to
* throw {@link IllegalArgumentException} on an assertion failure.
* &lt;pre class=&quot;code&quot;&gt;Assert.state(id == null);&lt;/pre&gt;
* @param expression a boolean expression
* @throws IllegalStateException if the supplied expression is &lt;code&gt;false&lt;/code&gt;
*/
public static void state(boolean expression) {
<span class="nc" id="L404"> state(expression, &quot;[Assertion failed] - this state invariant must be true&quot;);</span>
<span class="nc" id="L405"> }</span>
//////////////////////////
// From CollectionUtils //
//////////////////////////
// CollectionUtils cannot be removed from shiro-core until 2.0 as it has a dependency on PrincipalCollection
private static boolean isEmpty(Map m) {
<span class="nc bnc" id="L414" title="All 4 branches missed."> return m == null || m.isEmpty();</span>
}
private static boolean isEmpty(Collection c) {
<span class="nc bnc" id="L418" title="All 4 branches missed."> return c == null || c.isEmpty();</span>
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.eclemma.org/jacoco">JaCoCo</a> 0.7.7.201606060606</span></div></body></html>