001: //$Id: DDTExceptionClassTest.java 148 2005-05-24 23:04:20Z jg_hamburg $
002: /********************************************************************************
003: * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
004: * Copyright (c) 2004, Joerg and Kai Gellien
005: * All rights reserved.
006: *
007: * The Software is provided under the terms of the Common Public License 1.0
008: * as provided with the distribution of DDTUnit in the file cpl-v10.html.
009: * Redistribution and use in source and binary forms, with or without
010: * modification, are permitted provided that the following conditions
011: * are met:
012: *
013: * + Redistributions of source code must retain the above copyright
014: * notice, this list of conditions and the following disclaimer.
015: *
016: * + Redistributions in binary form must reproduce the above
017: * copyright notice, this list of conditions and the following
018: * disclaimer in the documentation and/or other materials provided
019: * with the distribution.
020: *
021: * + Neither the name of the authors or DDTUnit, nor the
022: * names of its contributors may be used to endorse or promote
023: * products derived from this software without specific prior
024: * written permission.
025: *
026: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
027: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
028: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
029: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
030: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
031: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
032: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
033: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
034: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
035: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
036: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
037: ********************************************************************************/package junitx.ddtunit;
038:
039: import junit.framework.Test;
040: import junit.framework.TestCase;
041: import junit.framework.TestSuite;
042:
043: /**
044: * Check functionality provided by {@link junitx.ddtunit.DDTException}.
045: *
046: * @author jg
047: */
048: public class DDTExceptionClassTest extends TestCase {
049: private final String LF = System.getProperty("line.separator");
050:
051: /**
052: * Instanciate unit test with method name to execute
053: *
054: * @param name of testmethod to execute
055: */
056: public DDTExceptionClassTest(String name) {
057: super (name);
058: }
059:
060: /**
061: * Create test suite defined by this class
062: *
063: * @return Test containing all tests provided by this class
064: */
065: public static Test suite() {
066: return new TestSuite(DDTExceptionClassTest.class);
067: }
068:
069: /**
070: * Test functionallity of
071: * {@link DDTException#create(StringBuffer, Throwable)}using simple
072: * exception without a cause exception
073: */
074: public void testCreateSingleException() {
075: String msg = "My single exception";
076: Throwable single = new RuntimeException(msg);
077: StringBuffer sb = new StringBuffer(msg);
078: DDTException ddtException = DDTException.create(sb, single);
079: StringBuffer expectedMessage = new StringBuffer(sb.toString())
080: .append(LF)
081: .append("Caused by:java.lang.RuntimeException - " + msg);
082:
083: assertEquals("Wrong exception message", expectedMessage
084: .toString(), ddtException.getMessage());
085: }
086:
087: /**
088: * Test functionallity of
089: * {@link DDTException#create(StringBuffer, Throwable)}using exception with
090: * one cause exception
091: */
092: public void testCreateDoubleException() {
093: String msg = "My exception";
094: Throwable single = new RuntimeException(msg);
095: NullPointerException nullEx = new NullPointerException(
096: "Root cause");
097:
098: single.initCause(nullEx);
099:
100: StringBuffer sb = new StringBuffer("First line of message");
101: DDTException ddtException = DDTException.create(sb, single);
102: StringBuffer expectedMessage = new StringBuffer(sb.toString())
103: .append(LF)
104: .append("Caused by:java.lang.RuntimeException - ")
105: .append(msg)
106: .append(LF)
107: .append(
108: "Caused by (java.lang.NullPointerException) Root cause");
109:
110: assertEquals("Wrong exception message", expectedMessage
111: .toString(), ddtException.getMessage());
112: }
113: }
|