1 4 package com.tc.util; 5 6 import java.util.Arrays ; 7 import java.util.Collection ; 8 import java.util.Iterator ; 9 10 14 public class Assert { 15 16 private static final String ASSERT_PROPERTY_NAME = "tcassert"; 17 18 private static final boolean enabled = Boolean.valueOf(System.getProperty(ASSERT_PROPERTY_NAME, "true")) 27 .booleanValue(); 28 29 private static boolean isEnabled() { 30 return enabled; 31 } 32 33 public static TCAssertionError failure(Object message, Throwable t) { 38 return new TCAssertionError(StringUtil.safeToString(message), t); 39 } 40 41 public static TCAssertionError failure(Object message) { 42 return new TCAssertionError(StringUtil.safeToString(message)); 43 } 44 45 public static void eval(boolean expr) { 46 if ((!expr) && isEnabled()) { throw failure("Assertion failed"); } 47 return; 48 } 49 50 public static void eval(Object message, boolean expr) { 51 if ((!expr) && isEnabled()) { throw failure("Assertion failed: " + StringUtil.safeToString(message)); } 52 return; 53 } 54 55 public static void assertTrue(boolean expr) { 56 eval(expr); 57 } 58 59 public static void assertTrue(Object message, boolean expr) { 60 eval(message, expr); 61 } 62 63 public static void assertFalse(boolean expr) { 64 eval(!expr); 65 } 66 67 public static void assertFalse(Object message, boolean expr) { 68 eval(message, !expr); 69 } 70 71 public static void assertNull(Object o) { 72 assertNull("object", o); 73 } 74 75 public static void assertNull(Object what, Object o) { 76 if ((o != null) && isEnabled()) { throw failure(StringUtil.safeToString(what) + " was not null"); } 77 } 78 79 public static void assertNotNull(Object what, Object o) { 80 if ((o == null) && isEnabled()) { throw new NullPointerException (StringUtil.safeToString(what) + " is null"); } 81 } 82 83 public static void assertNotNull(Object o) { 84 assertNotNull("object", o); 85 } 86 87 public static void assertNoNullElements(Object [] array) { 89 if (!isEnabled()) return; 90 assertNotNull(array); 91 92 for (int i = 0; i < array.length; i++) { 93 assertNotNull("item " + i, array[i]); 94 } 95 } 96 97 public static void assertNoBlankElements(String [] array) { 98 if (!isEnabled()) return; 99 assertNotNull(array); 100 101 for (int i = 0; i < array.length; ++i) 102 assertNotBlank(array[i]); 103 } 104 105 public static void assertNotEmpty(Object what, String s) { 106 assertNotNull(what, s); 107 if ((s.length() == 0) && isEnabled()) throw new IllegalArgumentException (StringUtil.safeToString(what) 108 + " is empty"); 109 } 110 111 public static void assertNotEmpty(String s) { 112 assertNotEmpty("string", s); 113 } 114 115 public static void assertNotBlank(Object what, String s) { 116 assertNotEmpty(what, s); 117 if ((s.trim().length() == 0) && isEnabled()) throw new IllegalArgumentException (StringUtil.safeToString(what) 118 + " is blank"); 119 } 120 121 public static void assertNotBlank(String s) { 122 assertNotBlank("string", s); 123 } 124 125 public static void assertSame(Object lhs, Object rhs) { 126 if (lhs == null) { 127 eval("leftHandSide == null, but rightHandSide != null", rhs == null); 128 } else { 129 eval("leftHandSide != null, but rightHandSide == null", rhs != null); 130 eval("leftHandSide != rightHandSide", lhs == rhs); 131 } 132 } 133 134 public static void assertEquals(int expected, int actual) { 135 if (expected != actual) { throw new TCAssertionError("Expected <" + expected + "> but got <" + actual + ">"); } 136 } 137 138 public static void assertEquals(double expected, double actual) { 139 if (expected != actual) { throw new TCAssertionError("Expected <" + expected + "> but got <" + actual + ">"); } 140 } 141 142 public static void assertEquals(double expected, double actual, double epsilon) { 143 if (Math.abs(actual - expected) > Math.abs(epsilon)) { throw new TCAssertionError("Expected <" + expected + "> but got <" + actual + ">"); } 144 } 145 146 public static void assertEquals(boolean expected, boolean actual) { 147 if (expected != actual) { throw new TCAssertionError("Expected <" + expected + "> but got <" + actual + ">"); } 148 } 149 150 public static void assertEquals(byte[] expected, byte[] actual) { 151 boolean expr = (expected == null) ? actual == null : Arrays.equals(expected, actual); 152 if (!expr) { throw new TCAssertionError("Expected <" + expected + "> but got <" + actual + ">"); } 153 } 154 155 public static void assertEquals(Object expected, Object actual) { 156 boolean expr = (expected == null) ? actual == null : expected.equals(actual); 157 if (!expr) { throw new TCAssertionError("Expected <" + expected + "> but got <" + actual + ">"); } 158 } 159 160 public static void assertConsistentCollection(Collection collection, Class elementClass, boolean allowNullElements) { 161 assertNotNull("Collection", collection); 162 assertNotNull("Element class", elementClass); 163 for (Iterator pos = collection.iterator(); pos.hasNext();) { 164 Object element = pos.next(); 165 if (!allowNullElements) { 166 assertNotNull(element); 167 } 168 if (element != null) { 169 eval("Element '" + element + "' is not an instance of '" + elementClass.getName() + "'", elementClass 170 .isInstance(element)); 171 } 172 } 173 } 174 175 179 public static void assertContainsElement(Object [] objectArray, Object requiredElement) { 180 assertNotNull(objectArray); 181 for (int pos = 0; pos < objectArray.length; pos++) { 182 if (objectArray[pos] == requiredElement) return; 183 } 184 throw failure("Element<" + requiredElement + "> not found in array " 185 + StringUtil.toString(objectArray, ",", "<", ">")); 186 } 187 188 public static void fail() { 189 throw failure("generic failure"); 190 } 191 192 public static void pre(boolean v){ 193 if (!v) throw new TCAssertionError("Precondition failed"); 194 } 195 196 public static void post(boolean v){ 197 if (!v) throw new TCAssertionError("Postcondition failed"); 198 } 199 public static void inv(boolean v){ 200 if (!v) throw new TCAssertionError("Invariant failed"); 201 } 202 } | Popular Tags |