Skip to content

Commit 4284d43

Browse files
timeckljacomet
authored andcommitted
remove assert enable stuff
git-svn-id: https://svn.terracotta.org/repo/tc/tc-messaging/branches/private/voltron@26037 7fc7bbf3-cf45-46d4-be06-341739edd864
1 parent 8e3793f commit 4284d43

File tree

1 file changed

+19
-71
lines changed

1 file changed

+19
-71
lines changed

src/main/java/com/tc/util/Assert.java

Lines changed: 19 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,10 @@
88
import java.util.Collection;
99

1010
/**
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
1312
*/
1413
public class Assert {
1514

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-
3315
/**
3416
* This returns an exception, instead of throwing one, so that you can do (e.g.): <code>
3517
* public Object foo() { throw Assert.failure("doesn't work"); }
@@ -65,7 +47,7 @@ public static TCAssertionError failure(Object message) {
6547
* @param expr Expression
6648
*/
6749
public static void eval(boolean expr) {
68-
if ((!expr) && isEnabled()) { throw failure("Assertion failed"); }
50+
if (!expr) { throw failure("Assertion failed"); }
6951
return;
7052
}
7153

@@ -76,7 +58,7 @@ public static void eval(boolean expr) {
7658
* @param message Message for assertion error if false
7759
*/
7860
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)); }
8062
return;
8163
}
8264

@@ -135,7 +117,7 @@ public static void assertNull(Object o) {
135117
* @param what Message for error
136118
*/
137119
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"); }
139121
}
140122

141123
/**
@@ -145,7 +127,7 @@ public static void assertNull(Object what, Object o) {
145127
* @param what Message for error
146128
*/
147129
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"); }
149131
}
150132

151133
/**
@@ -163,7 +145,6 @@ public static void assertNotNull(Object o) {
163145
* @param array Array
164146
*/
165147
public static void assertNoNullElements(Object[] array) {
166-
if (!isEnabled()) return;
167148
assertNotNull(array);
168149

169150
for (int i = 0; i < array.length; i++) {
@@ -177,7 +158,6 @@ public static void assertNoNullElements(Object[] array) {
177158
* @param array Array of strings
178159
*/
179160
public static void assertNoBlankElements(String[] array) {
180-
if (!isEnabled()) return;
181161
assertNotNull(array);
182162

183163
for (String s : array)
@@ -192,7 +172,7 @@ public static void assertNoBlankElements(String[] array) {
192172
*/
193173
public static void assertNotEmpty(Object what, String s) {
194174
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)
196176
+ " is empty");
197177
}
198178

@@ -213,7 +193,7 @@ public static void assertNotEmpty(String s) {
213193
*/
214194
public static void assertNotBlank(Object what, String s) {
215195
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)
217197
+ " is blank");
218198
}
219199

@@ -248,7 +228,7 @@ public static void assertSame(Object lhs, Object rhs) {
248228
* @param actual Actual value
249229
*/
250230
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 + ">"); }
252232
}
253233

254234
/**
@@ -269,7 +249,7 @@ public static void assertEquals(long expected, long actual) {
269249
* @param msg Message, should be non-null
270250
*/
271251
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 <"
273253
+ actual + ">"); }
274254
}
275255

@@ -280,7 +260,7 @@ public static void assertEquals(Object msg, int expected, int actual) {
280260
* @param actual Actual value
281261
*/
282262
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 + ">"); }
284264
}
285265

286266
/**
@@ -291,7 +271,7 @@ public static void assertEquals(double expected, double actual) {
291271
* @param epsilon Maximum allowed difference between expected and actual
292272
*/
293273
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
295275
+ "> but got <" + actual + ">"); }
296276
}
297277

@@ -302,7 +282,7 @@ public static void assertEquals(double expected, double actual, double epsilon)
302282
* @param actual Actual value
303283
*/
304284
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 + ">"); }
306286
}
307287

308288
/**
@@ -313,7 +293,7 @@ public static void assertEquals(boolean expected, boolean actual) {
313293
*/
314294
public static void assertEquals(byte[] expected, byte[] actual) {
315295
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"); }
317297
}
318298

319299
/**
@@ -328,7 +308,7 @@ public static void assertEquals(Object expected, Object actual) {
328308

329309
public static void assertEquals(Object msg, Object expected, Object actual) {
330310
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 <"
332312
+ expected + "> but got <" + actual + ">"); }
333313
}
334314

@@ -366,10 +346,9 @@ public static void assertContainsElement(Object[] objectArray, Object requiredEl
366346
for (Object element : objectArray) {
367347
if (element == requiredElement) return;
368348
}
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, ",", "<", ">"));
373352
}
374353

375354
public static void assertDoesNotContainsElement(Object[] objectArray, Object element) {
@@ -385,9 +364,7 @@ public static void assertDoesNotContainsElement(Object[] objectArray, Object ele
385364
* Throw assertion error with generic message
386365
*/
387366
public static void fail() {
388-
if (isEnabled()) {
389-
throw failure("generic failure");
390-
}
367+
throw failure("generic failure");
391368
}
392369

393370
/**
@@ -396,35 +373,6 @@ public static void fail() {
396373
* @param message Message
397374
*/
398375
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);
429377
}
430378
}

0 commit comments

Comments
 (0)