001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.modules.uml.core.support;
043:
044: import javax.swing.JOptionPane;
045:
046: /**
047: * A set of debug utilities.
048: *
049: * @author Trey Spiva
050: */
051: public class Debug extends DebugSupport {
052: /**
053: * Asserts that a condition is true. If the assertion fails the error
054: * will be reported in a dialog box.
055: *
056: * @param condition The test condition.
057: * @return <code>true</code> if the condition was successiful.
058: */
059: public static boolean assertTrue(boolean condition) {
060: return assertTrue(condition, "Not Equal To True Error");
061: }
062:
063: /**
064: * Asserts that a condition is true. If the assertion fails the error
065: * will be reported in a dialog box.
066: *
067: * @param condition The test condition.
068: * @param msg The error message to display to the user.
069: * @return <code>true</code> if the condition was successiful.
070: */
071: public static boolean assertTrue(boolean condition, String msg) {
072: boolean retVal = true;
073:
074: try {
075: assert (condition == true);
076: } catch (AssertionError e) {
077: fail(e, msg);
078: retVal = false;
079: }
080:
081: return retVal;
082: }
083:
084: /**
085: * Asserts that a condition is false. If the assertion fails the error
086: * will be reported in a dialog box.
087: *
088: * @param condition The test condition.
089: * @return <code>true</code> if the condition was successiful.
090: */
091: public static boolean assertFalse(boolean condition) {
092: return assertFalse(condition, "Not Equal To False Error");
093: }
094:
095: /**
096: * Asserts that a condition is false. If the assertion fails the error
097: * will be reported in a dialog box.
098: *
099: * @param condition The test condition.
100: * @param msg The error message to display to the user.
101: * @return <code>true</code> if the condition was successiful.
102: */
103: public static boolean assertFalse(boolean condition, String msg) {
104: boolean retVal = true;
105:
106: try {
107: assert (condition == false);
108: } catch (AssertionError e) {
109: fail(e, msg);
110: retVal = false;
111: }
112:
113: return retVal;
114: }
115:
116: /**
117: * Asserts that an object isn't null. If the assertion fails the error
118: * will be reported in a dialog box.
119: *
120: * @param condition The test condition.
121: * @return <code>true</code> if the condition was successiful.
122: */
123: public static boolean assertNotNull(Object object) {
124: return assertNotNull(object, "Not Equal To Null Error");
125: }
126:
127: /**
128: * Asserts that an object isn't null. If the assertion fails the error
129: * will be reported in a dialog box.
130: *
131: * @param object The object to test.
132: * @param msg The error message to display to the user.
133: * @return <code>true</code> if the condition was successiful.
134: */
135: public static boolean assertNotNull(Object object, String msg) {
136: boolean retVal = true;
137:
138: try {
139: assert (object != null);
140: } catch (AssertionError e) {
141: fail(e, msg);
142: retVal = false;
143: }
144:
145: return retVal;
146: }
147:
148: /**
149: * Asserts that an object is null. If the assertion fails the error
150: * will be reported in a dialog box.
151: *
152: * @param condition The test condition.
153: * @return <code>true</code> if the condition was successiful.
154: */
155: public static boolean assertNull(Object object) {
156: return assertNull(object, "Equal To Null Error");
157: }
158:
159: /**
160: * Asserts that an object is null. If the assertion fails the error
161: * will be reported in a dialog box.
162: *
163: * @param condition The test condition.
164: * @param msg The error message to display to the user.
165: * @return <code>true</code> if the condition was successiful.
166: */
167: public static boolean assertNull(Object object, String msg) {
168: boolean retVal = true;
169:
170: try {
171: assert (object == null);
172: } catch (AssertionError e) {
173: fail(e, msg);
174: retVal = false;
175: }
176:
177: return retVal;
178: }
179:
180: /**
181: * Reports an exception to the user. The error is sent to stderr as well
182: * as reported to a dialog box.
183: *
184: * @param e
185: * @param msg
186: */
187: protected static void fail(AssertionError e, String msg) {
188: //return if debug not enabled
189: if (!isEnabled())
190: return;
191:
192: StringBuffer errMsg = new StringBuffer(msg);
193:
194: StackTraceElement[] elements = e.getStackTrace();
195: // Since the top Element will be the assert I will not add it to the
196: // error message.
197: for (int i = 2; i < elements.length; i++) {
198: errMsg.append(System.getProperty("line.separator"));
199:
200: StackTraceElement element = elements[i];
201:
202: // the "\t at " at the beginning of the line helps us to add a hyperlink to the code
203: errMsg.append("\t at " + element.toString());
204: }
205:
206: out.println(errMsg);
207: //JOptionPane.showMessageDialog(null, errMsg, "Assertion Failure", JOptionPane.ERROR_MESSAGE);
208: }
209: }
|