001package com.basilv.core; 002 003 004/** 005 * Provides assertion support. A failed assertion will throw an AssertionError. 006 * <P> 007 * The methods are generally of the form: methodXXX(message, expression). If the 008 * expression fails the assertion, then the AssertionException is created using 009 * the specified message and then thrown. 010 */ 011public class Assert 012{ 013 /** 014 * Fails the assertion if the specified expression is false. 015 */ 016 public static void isTrue(String expressionDescription, boolean expression) { 017 if (!expression) { 018 throwException(expressionDescription + " is not true."); 019 } 020 } 021 022 /** 023 * Fails the assertion if the specified expression is false. 024 */ 025 public static void isTrue(boolean expression) { 026 isTrue("Expression asserted as true", expression); 027 } 028 029 /** 030 * Fails the assertion if the specified expression is true. 031 */ 032 public static void isFalse(String expressionDescription, boolean expression) { 033 if (expression) { 034 throwException(expressionDescription + " is not false."); 035 } 036 } 037 038 /** 039 * Fails the assertion if the specified expression is true. 040 */ 041 public static void isFalse(boolean expression) { 042 isFalse("Expression asserted as false", expression); 043 } 044 045 /** 046 * Fails the assertion if the specified object is null. 047 */ 048 public static void notNull(Object evalObj) { 049 notNull("Object asserted as not null", evalObj); 050 } 051 052 /** 053 * Fails the assertion if the specified object is null. 054 */ 055 public static void notNull(String objDescription, Object evalObj) { 056 if (evalObj == null) { 057 throwException(objDescription + " is null."); 058 } 059 } 060 061 /** 062 * Fails the assertion if the specified string is null or empty. 063 */ 064 public static void notNullOrEmpty(String evalString) { 065 notNullOrEmpty("String asserted as not null or empty", evalString); 066 } 067 068 /** 069 * Fails the assertion if the specified string is null or empty. 070 * 071 * @param stringDescription A description of the string being evaluated. 072 * @param evalString The string to evaluate. 073 */ 074 public static void notNullOrEmpty(String stringDescription, String evalString) { 075 if (StringUtilities.isNullOrEmpty(evalString)) { 076 throwException(stringDescription + " is null or empty."); 077 } 078 } 079 080 /** 081 * Fails the assertion if the integer is less than zero. 082 * 083 * @param evalInt The integer to evaluate. 084 */ 085 public static void notNegative(int evalInt) { 086 notNegative("Integer value " + evalInt + " asserted as not negative", evalInt); 087 } 088 089 /** 090 * Fails the assertion if the integer is less than zero. 091 * 092 * @param intDescription A description of the integer being evaluated. 093 * @param evalInt The integer to evaluate. 094 */ 095 public static void notNegative(String intDescription, int evalInt) { 096 if (evalInt < 0) { 097 throwException(intDescription + " is negative."); 098 } 099 } 100 101 /** 102 * Fails the assertion if the integer is not greater than zero. 103 * 104 * @param evalInt The integer to evaluate. 105 */ 106 public static void greaterThanZero(int evalInt) { 107 greaterThanZero("Integer value " + evalInt + " asserted as greater than zero", evalInt); 108 } 109 110 /** 111 * Fails the assertion if the integer is not greater than zero. 112 * 113 * @param intDescription A description of the integer being evaluated. 114 * @param evalInt The integer to evaluate. 115 */ 116 public static void greaterThanZero(String intDescription, int evalInt) { 117 if (evalInt <= 0) { 118 throwException(intDescription + " is not greater than zero."); 119 } 120 } 121 122 /** 123 * Automatically throws an AssertionException. 124 */ 125 public static void shouldNotReach() { 126 shouldNotReach("Failed assertion in shouldNotReach()."); 127 } 128 129 /** 130 * Automatically throws an AssertionException using the specified message. 131 */ 132 public static void shouldNotReach(String message) { 133 throwException(message); 134 } 135 136 private static void throwException(String message) { 137 throw new AssertionError(message); 138 } 139 140} // class Assert