8
8
import java .util .Collection ;
9
9
10
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'.
11
+ * A general purpose assertion utility
13
12
*/
14
13
public class Assert {
15
14
16
- private static final String 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
- // parameters to these assert method. Removing the call altogether would most certainly change the logic of the system
25
- // in potentially silent and catastrophic 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
15
/**
34
16
* This returns an exception, instead of throwing one, so that you can do (e.g.): <code>
35
17
* public Object foo() { throw Assert.failure("doesn't work"); }
@@ -65,7 +47,7 @@ public static TCAssertionError failure(Object message) {
65
47
* @param expr Expression
66
48
*/
67
49
public static void eval (boolean expr ) {
68
- if (( !expr ) && isEnabled () ) { throw failure ("Assertion failed" ); }
50
+ if (!expr ) { throw failure ("Assertion failed" ); }
69
51
return ;
70
52
}
71
53
@@ -76,7 +58,7 @@ public static void eval(boolean expr) {
76
58
* @param message Message for assertion error if false
77
59
*/
78
60
public static void eval (Object message , boolean expr ) {
79
- if (( !expr ) && isEnabled () ) { throw failure ("Assertion failed: " + StringUtil .safeToString (message )); }
61
+ if (!expr ) { throw failure ("Assertion failed: " + StringUtil .safeToString (message )); }
80
62
return ;
81
63
}
82
64
@@ -135,7 +117,7 @@ public static void assertNull(Object o) {
135
117
* @param what Message for error
136
118
*/
137
119
public static void assertNull (Object what , Object o ) {
138
- if (( o != null ) && isEnabled () ) { throw failure (StringUtil .safeToString (what ) + " was not null" ); }
120
+ if (o != null ) { throw failure (StringUtil .safeToString (what ) + " was not null" ); }
139
121
}
140
122
141
123
/**
@@ -145,7 +127,7 @@ public static void assertNull(Object what, Object o) {
145
127
* @param what Message for error
146
128
*/
147
129
public static void assertNotNull (Object what , Object o ) {
148
- if (( o == null ) && isEnabled () ) { throw new NullPointerException (StringUtil .safeToString (what ) + " is null" ); }
130
+ if (o == null ) { throw new NullPointerException (StringUtil .safeToString (what ) + " is null" ); }
149
131
}
150
132
151
133
/**
@@ -163,7 +145,6 @@ public static void assertNotNull(Object o) {
163
145
* @param array Array
164
146
*/
165
147
public static void assertNoNullElements (Object [] array ) {
166
- if (!isEnabled ()) return ;
167
148
assertNotNull (array );
168
149
169
150
for (int i = 0 ; i < array .length ; i ++) {
@@ -177,7 +158,6 @@ public static void assertNoNullElements(Object[] array) {
177
158
* @param array Array of strings
178
159
*/
179
160
public static void assertNoBlankElements (String [] array ) {
180
- if (!isEnabled ()) return ;
181
161
assertNotNull (array );
182
162
183
163
for (String s : array )
@@ -192,7 +172,7 @@ public static void assertNoBlankElements(String[] array) {
192
172
*/
193
173
public static void assertNotEmpty (Object what , String s ) {
194
174
assertNotNull (what , s );
195
- if (( s .length () == 0 ) && isEnabled () ) throw new IllegalArgumentException (StringUtil .safeToString (what )
175
+ if (s .length () == 0 ) throw new IllegalArgumentException (StringUtil .safeToString (what )
196
176
+ " is empty" );
197
177
}
198
178
@@ -213,7 +193,7 @@ public static void assertNotEmpty(String s) {
213
193
*/
214
194
public static void assertNotBlank (Object what , String s ) {
215
195
assertNotEmpty (what , s );
216
- if (( s .trim ().length () == 0 ) && isEnabled () ) throw new IllegalArgumentException (StringUtil .safeToString (what )
196
+ if (s .trim ().length () == 0 ) throw new IllegalArgumentException (StringUtil .safeToString (what )
217
197
+ " is blank" );
218
198
}
219
199
@@ -248,7 +228,7 @@ public static void assertSame(Object lhs, Object rhs) {
248
228
* @param actual Actual value
249
229
*/
250
230
public static void assertEquals (int expected , int actual ) {
251
- if (expected != actual && isEnabled () ) { throw new TCAssertionError ("Expected <" + expected + "> but got <" + actual + ">" ); }
231
+ if (expected != actual ) { throw new TCAssertionError ("Expected <" + expected + "> but got <" + actual + ">" ); }
252
232
}
253
233
254
234
/**
@@ -269,7 +249,7 @@ public static void assertEquals(long expected, long actual) {
269
249
* @param msg Message, should be non-null
270
250
*/
271
251
public static void assertEquals (Object msg , int expected , int actual ) {
272
- if (expected != actual && isEnabled () ) { throw new TCAssertionError (msg + ": Expected <" + expected + "> but got <"
252
+ if (expected != actual ) { throw new TCAssertionError (msg + ": Expected <" + expected + "> but got <"
273
253
+ actual + ">" ); }
274
254
}
275
255
@@ -280,7 +260,7 @@ public static void assertEquals(Object msg, int expected, int actual) {
280
260
* @param actual Actual value
281
261
*/
282
262
public static void assertEquals (double expected , double actual ) {
283
- if (expected != actual && isEnabled () ) { throw new TCAssertionError ("Expected <" + expected + "> but got <" + actual + ">" ); }
263
+ if (expected != actual ) { throw new TCAssertionError ("Expected <" + expected + "> but got <" + actual + ">" ); }
284
264
}
285
265
286
266
/**
@@ -291,7 +271,7 @@ public static void assertEquals(double expected, double actual) {
291
271
* @param epsilon Maximum allowed difference between expected and actual
292
272
*/
293
273
public static void assertEquals (double expected , double actual , double epsilon ) {
294
- if (Math .abs (actual - expected ) > Math .abs (epsilon ) && isEnabled () ) { throw new TCAssertionError ("Expected <" + expected
274
+ if (Math .abs (actual - expected ) > Math .abs (epsilon )) { throw new TCAssertionError ("Expected <" + expected
295
275
+ "> but got <" + actual + ">" ); }
296
276
}
297
277
@@ -302,7 +282,7 @@ public static void assertEquals(double expected, double actual, double epsilon)
302
282
* @param actual Actual value
303
283
*/
304
284
public static void assertEquals (boolean expected , boolean actual ) {
305
- if (expected != actual && isEnabled () ) { throw new TCAssertionError ("Expected <" + expected + "> but got <" + actual + ">" ); }
285
+ if (expected != actual ) { throw new TCAssertionError ("Expected <" + expected + "> but got <" + actual + ">" ); }
306
286
}
307
287
308
288
/**
@@ -313,7 +293,7 @@ public static void assertEquals(boolean expected, boolean actual) {
313
293
*/
314
294
public static void assertEquals (byte [] expected , byte [] actual ) {
315
295
boolean expr = (expected == null ) ? actual == null : Arrays .equals (expected , actual );
316
- if (!expr && isEnabled () ) { throw new TCAssertionError ("Got differing byte[]s" ); }
296
+ if (!expr ) { throw new TCAssertionError ("Got differing byte[]s" ); }
317
297
}
318
298
319
299
/**
@@ -328,7 +308,7 @@ public static void assertEquals(Object expected, Object actual) {
328
308
329
309
public static void assertEquals (Object msg , Object expected , Object actual ) {
330
310
boolean expr = (expected == null ) ? actual == null : expected .equals (actual );
331
- if (!expr && isEnabled () ) { throw new TCAssertionError ((msg != null ? (msg + ": " ) : "" ) + "Expected <"
311
+ if (!expr ) { throw new TCAssertionError ((msg != null ? (msg + ": " ) : "" ) + "Expected <"
332
312
+ expected + "> but got <" + actual + ">" ); }
333
313
}
334
314
@@ -366,10 +346,9 @@ public static void assertContainsElement(Object[] objectArray, Object requiredEl
366
346
for (Object element : objectArray ) {
367
347
if (element == requiredElement ) return ;
368
348
}
369
- if (isEnabled ()) {
370
- throw failure ("Element<" + requiredElement + "> not found in array "
371
- + StringUtil .toString (objectArray , "," , "<" , ">" ));
372
- }
349
+
350
+ throw failure ("Element<" + requiredElement + "> not found in array "
351
+ + StringUtil .toString (objectArray , "," , "<" , ">" ));
373
352
}
374
353
375
354
public static void assertDoesNotContainsElement (Object [] objectArray , Object element ) {
@@ -385,9 +364,7 @@ public static void assertDoesNotContainsElement(Object[] objectArray, Object ele
385
364
* Throw assertion error with generic message
386
365
*/
387
366
public static void fail () {
388
- if (isEnabled ()) {
389
- throw failure ("generic failure" );
390
- }
367
+ throw failure ("generic failure" );
391
368
}
392
369
393
370
/**
@@ -396,35 +373,6 @@ public static void fail() {
396
373
* @param message Message
397
374
*/
398
375
public static void fail (String message ) {
399
- if (isEnabled ()) {
400
- throw failure (message );
401
- }
402
- }
403
-
404
- /**
405
- * Assert precondition
406
- *
407
- * @param v Precondition
408
- */
409
- public static void pre (boolean v ) {
410
- if (!v && isEnabled ()) throw new TCAssertionError ("Precondition failed" );
411
- }
412
-
413
- /**
414
- * Assert postcondition
415
- *
416
- * @param v Postcondition
417
- */
418
- public static void post (boolean v ) {
419
- if (!v && isEnabled ()) throw new TCAssertionError ("Postcondition failed" );
420
- }
421
-
422
- /**
423
- * Assert invariant
424
- *
425
- * @param v Invariant
426
- */
427
- public static void inv (boolean v ) {
428
- if (!v && isEnabled ()) throw new TCAssertionError ("Invariant failed" );
376
+ throw failure (message );
429
377
}
430
378
}
0 commit comments