1 /******************************************************************************** 2 * DDTUnit, a Datadriven Approach to Unit- and Moduletesting 3 * Copyright (c) 2004, Joerg and Kai Gellien 4 * All rights reserved. 5 * 6 * The Software is provided under the terms of the Common Public License 1.0 7 * as provided with the distribution of DDTUnit in the file cpl-v10.html. 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * + Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * + Redistributions in binary form must reproduce the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer in the documentation and/or other materials provided 18 * with the distribution. 19 * 20 * + Neither the name of the authors or DDTUnit, nor the 21 * names of its contributors may be used to endorse or promote 22 * products derived from this software without specific prior 23 * written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR 29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 30 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 32 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 36 ********************************************************************************/ 37 package junitx.ddtunit; 38 39 /** 40 * Base exception of ddtunit package. 41 * 42 * @author jg 43 */ 44 public class DDTException extends RuntimeException { 45 private Throwable cause; 46 47 private final static String LF = System.getProperty("line.separator"); 48 49 /** 50 * Standard exception class for any ddtunit specific problems 51 * 52 */ 53 public DDTException() { 54 super(); 55 } 56 57 /** 58 * Standard exception class for any ddtunit specific problems 59 * 60 * @param message to display in exception 61 */ 62 public DDTException(String message) { 63 super(message); 64 } 65 66 /** 67 * Standard exception class for any ddtunit specific problems 68 * 69 * @param aCause of actuall exception 70 */ 71 public DDTException(Throwable aCause) { 72 super(aCause); 73 this.cause = aCause; 74 } 75 76 /** 77 * Standard exception class for any ddtunit specific problems 78 * 79 * @param message to display in exception 80 * @param aCause of actuall exception 81 */ 82 public DDTException(String message, Throwable aCause) { 83 super(message, aCause); 84 this.cause = aCause; 85 } 86 87 /** 88 * get cause exception 89 * 90 * @see java.lang.Throwable#getCause() 91 * @return cause of exception or null if not exists 92 */ 93 public Throwable getCause() { 94 return this.cause; 95 } 96 97 /** 98 * Create exception with compact trace hirachie information summariesed in 99 * exception message. 100 * 101 * @param message provided as prefix of exception 102 * @param exception to start information exception 103 * @return DDTException with created message 104 */ 105 public static DDTException create(StringBuffer message, Throwable exception) { 106 StringBuffer msg = new StringBuffer(); 107 DDTException ddtException = null; 108 msg.append(message); 109 if (exception != null) { 110 msg.append(LF).append("Caused by:").append( 111 exception.getClass().getName()).append(" - ").append( 112 exception.getMessage()); 113 } 114 msg.append(getCauseMessage(exception)); 115 ddtException = new DDTException(msg.toString(), exception); 116 return ddtException; 117 } 118 119 private static String getCauseMessage(Throwable exception) { 120 StringBuffer msg = new StringBuffer(); 121 if (exception != null) { 122 Throwable cause = exception.getCause(); 123 if (cause != null && exception != cause) { 124 msg.append(LF).append("Caused by (").append( 125 cause.getClass().getName()).append(") "); 126 String causeMessage = cause.getMessage(); 127 if (causeMessage == null || causeMessage.compareTo("") == 0) { 128 msg.append("<No cause message>"); 129 } else { 130 msg.append(cause.getMessage()); 131 } 132 msg.append(getCauseMessage(cause)); 133 } 134 } 135 return msg.toString(); 136 } 137 }