01: /*--------------------------------------------------------------------------*
02: | Copyright (C) 2006 Christopher Kohlhaas |
03: | |
04: | This program is free software; you can redistribute it and/or modify |
05: | it under the terms of the GNU General Public License as published by the |
06: | Free Software Foundation. A copy of the license has been included with |
07: | these distribution in the COPYING file, if not go to www.fsf.org |
08: | |
09: | As a special exception, you are granted the permissions to link this |
10: | program with every library, which license fulfills the Open Source |
11: | Definition as published by the Open Source Initiative (OSI). |
12: *--------------------------------------------------------------------------*/
13: package org.rapla.components.util;
14:
15: /** Some of the assert functionality of 1.4 for 1.3 versions of Rapla*/
16: public class Assert {
17: static String NOT_NULL_ASSERTION = "notNull-Assertion";
18: static String IS_TRUE_ASSERTION = "isTrue-Assertion";
19: static String ASSERTION_FAIL = "Assertion fail";
20: static boolean _bActivate = true;
21:
22: public static void notNull(Object obj, String text) {
23: if (obj == null && isActivated()) {
24: doAssert(getText(NOT_NULL_ASSERTION, text));
25: }
26: }
27:
28: public static void notNull(Object obj) {
29: if (obj == null && isActivated()) {
30: doAssert(getText(NOT_NULL_ASSERTION, ""));
31: }
32: }
33:
34: public static void isTrue(boolean condition, String text) {
35: if (!condition && isActivated()) {
36: doAssert(getText(IS_TRUE_ASSERTION, text));
37: } // end of if ()
38: }
39:
40: public static void isTrue(boolean condition) {
41: if (!condition && isActivated()) {
42: doAssert(getText(IS_TRUE_ASSERTION, ""));
43: } // end of if ()
44: }
45:
46: public static void fail() throws AssertionError {
47: doAssert(getText(ASSERTION_FAIL, ""));
48: }
49:
50: public static void fail(String text) throws AssertionError {
51: doAssert(getText(ASSERTION_FAIL, text));
52: }
53:
54: private static void doAssert(String text) throws AssertionError {
55: System.err.println(text);
56: throw new AssertionError(text);
57: }
58:
59: static boolean isActivated() {
60: return _bActivate;
61: }
62:
63: static void setActivated(boolean bActivate) {
64: _bActivate = bActivate;
65: }
66:
67: static String getText(String type, String text) {
68: return (type + " failed '" + text + "'");
69: }
70: }
|