KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > Assert


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.util;
5
6 import java.util.Arrays JavaDoc;
7 import java.util.Collection JavaDoc;
8 import java.util.Iterator JavaDoc;
9
10 /**
11  * A general purpose assertion utility. By default it is on, but you can disable the throwing of exceptions by giving
12  * the system property "tcassert" a value of 'false'.
13  */

14 public class Assert {
15
16   private static final String JavaDoc ASSERT_PROPERTY_NAME = "tcassert";
17
18   // When (if) we want to run *without* assertions enabled by default, use the line below to initialize instead
19
// private static final boolean enabled = Boolean.getBoolean(ASSERT_PROPERTY_NAME);
20
//
21
// NOTE: We need to be VERY careful about casually turning off assertions. It's one thing to make the assertions not
22
// throw errors (which the current disable/enable mechanism does). It's entirely something different to remove the
23
// calls to assertions. At the time of this writing, there are state modifying method calls in the code base that are
24
// paremeters to these assert method. Removing the call altogher would most certainly change the logic of the system
25
// in potentially silent and catastropic ways
26
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   // This returns an exception, instead of throwing one, so that you can do (e.g.):
34
// public Object foo() { throw Assert.failure("doesn't work"); }
35
// or whatever. If this just threw the exception itself, the compiler would complain
36
// (above) that there's no value being returned.
37
public static TCAssertionError failure(Object JavaDoc message, Throwable JavaDoc t) {
38     return new TCAssertionError(StringUtil.safeToString(message), t);
39   }
40
41   public static TCAssertionError failure(Object JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc message, boolean expr) {
68     eval(message, !expr);
69   }
70
71   public static void assertNull(Object JavaDoc o) {
72     assertNull("object", o);
73   }
74
75   public static void assertNull(Object JavaDoc what, Object JavaDoc o) {
76     if ((o != null) && isEnabled()) { throw failure(StringUtil.safeToString(what) + " was not null"); }
77   }
78
79   public static void assertNotNull(Object JavaDoc what, Object JavaDoc o) {
80     if ((o == null) && isEnabled()) { throw new NullPointerException JavaDoc(StringUtil.safeToString(what) + " is null"); }
81   }
82
83   public static void assertNotNull(Object JavaDoc o) {
84     assertNotNull("object", o);
85   }
86
87   // validate that the given (1 dimensional) array of references contains no nulls
88
public static void assertNoNullElements(Object JavaDoc[] 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 JavaDoc[] 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 JavaDoc what, String JavaDoc s) {
106     assertNotNull(what, s);
107     if ((s.length() == 0) && isEnabled()) throw new IllegalArgumentException JavaDoc(StringUtil.safeToString(what)
108                                                                              + " is empty");
109   }
110
111   public static void assertNotEmpty(String JavaDoc s) {
112     assertNotEmpty("string", s);
113   }
114
115   public static void assertNotBlank(Object JavaDoc what, String JavaDoc s) {
116     assertNotEmpty(what, s);
117     if ((s.trim().length() == 0) && isEnabled()) throw new IllegalArgumentException JavaDoc(StringUtil.safeToString(what)
118                                                                                     + " is blank");
119   }
120
121   public static void assertNotBlank(String JavaDoc s) {
122     assertNotBlank("string", s);
123   }
124
125   public static void assertSame(Object JavaDoc lhs, Object JavaDoc 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 JavaDoc expected, Object JavaDoc 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 JavaDoc collection, Class JavaDoc elementClass, boolean allowNullElements) {
161     assertNotNull("Collection", collection);
162     assertNotNull("Element class", elementClass);
163     for (Iterator JavaDoc pos = collection.iterator(); pos.hasNext();) {
164       Object JavaDoc 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   /**
176    * Tests for equality using the <code>==</code> operator, <em>not</em> <code>Object.equals(Object)</code>.
177    * <code>null</code> is a valid element.
178    */

179   public static void assertContainsElement(Object JavaDoc[] objectArray, Object JavaDoc 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