001: // $Id: ExceptionHandlerTest.java 216 2006-03-02 23:17:18Z 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 java.lang.reflect.InvocationTargetException;
040:
041: import junit.framework.AssertionFailedError;
042: import junit.framework.TestCase;
043: import junitx.ddtunit.data.ExceptionAsserter;
044: import junitx.ddtunit.data.ObjectAsserter;
045: import junitx.ddtunit.data.TestClusterDataSet;
046: import junitx.ddtunit.data.processing.ClusterDataSetTestCreator;
047:
048: /**
049: * TODO Comment for DDTExceptionHandlerTest
050: *
051: * @author jg
052: */
053: public class ExceptionHandlerTest extends TestCase {
054: private ExceptionHandler handler;
055:
056: private String testClass;
057:
058: private String testMethod;
059:
060: private String testId;
061:
062: private TestClusterDataSet classDataSet;
063:
064: private static final String LF = System
065: .getProperty("line.separator");
066:
067: /**
068: * Constructor for DDTExceptionHandlerTest.
069: *
070: * @param name
071: */
072: public ExceptionHandlerTest(String name) {
073: super (name);
074: }
075:
076: /*
077: * @see TestCase#setUp()
078: */
079: protected void setUp() throws Exception {
080: testClass = "ExceptionHandlerTest";
081: testMethod = "methodName";
082: testId = "testId";
083: classDataSet = ClusterDataSetTestCreator.getClusterDataSet(
084: testClass, testMethod, testId);
085: handler = new ExceptionHandler(testMethod);
086: }
087:
088: /**
089: * Test behavior of method DDTExceptionHandler.process() by providing an
090: * unknown exception.
091: *
092: */
093: public void testProcessUnknownException() throws Throwable {
094: String message = "Uncaught exception";
095: try {
096: handler.process(testId, new DDTException(message),
097: this .classDataSet.getAssertMap(this .testMethod,
098: this .testId));
099: handler.summarizeProblems(this .classDataSet
100: .size(this .testMethod));
101: fail("Should throw unexpected exception.");
102: } catch (DDTException ex) {
103: assertEquals("Wrong exception message", message, ex
104: .getMessage());
105: }
106: }
107:
108: /**
109: * Test behavior of method DDTExceptionHandler.process() by providing an
110: * unknown exception and no valid assertion data.
111: *
112: */
113: public void testProcessInvocationTargetExNoAsserts()
114: throws Throwable {
115: String message = "Uncaught exception";
116: try {
117: Exception testEx = new InvocationTargetException(
118: new DDTException(message));
119:
120: handler.process(testId, testEx, null);
121: handler.summarizeProblems(this .classDataSet
122: .size(this .testMethod));
123: fail("Should throw unexpected exception.");
124: } catch (DDTException ex) {
125: assertEquals("Wrong exception message", message, ex
126: .getMessage());
127: }
128: }
129:
130: /**
131: * Test behavior of method DDTExceptionHandler.process() by providing a
132: * known exception.
133: *
134: */
135: public void testProcessExpectedException() {
136: try {
137: String exceptionId = "exception0";
138: String exceptionMessage = "Expected exception";
139: ExceptionAsserter assertObject = new ExceptionAsserter(
140: exceptionId, "java.lang.Exception", "ISEQUAL");
141:
142: assertObject.setValue(new Exception(exceptionMessage));
143: this .classDataSet.getTestDataSet(this .testMethod, testId)
144: .getAssertMap().put(exceptionId, assertObject);
145: // Exceptin to catch
146: Exception myEx = new Exception(exceptionMessage);
147: Throwable actualEx = new InvocationTargetException(myEx);
148: assertObject.setActualObject(myEx);
149: handler.process(this .testId, actualEx, this .classDataSet
150: .getAssertMap(this .testMethod, this .testId));
151: handler.summarizeProblems(this .classDataSet
152: .size(this .testMethod));
153: } catch (Throwable e) {
154: fail("Should not throw exception. "
155: + e.getClass().getName());
156: }
157: }
158:
159: /**
160: * Test if defined but not caught exception is detected ass failure.
161: */
162: public void testCheckOnExpectedException() {
163: String exceptionId = "exception0";
164: String exceptionMessage = "Expected exception";
165: String expectedMessage = "There is/are 1 expected exception(s) defined in test 'testId'"
166: + LF
167: + " (last as hint): java.lang.Exception - Expected exception";
168:
169: ExceptionAsserter assertObject = new ExceptionAsserter(
170: exceptionId, "java.lang.Exception",
171: ObjectAsserter.ASSERT_ACTION_ISEQUAL);
172:
173: assertObject.setValue(new Exception(exceptionMessage));
174: this .classDataSet.getTestDataSet(this .testMethod, testId)
175: .getAssertMap().put(exceptionId, assertObject);
176: // Exceptin to catch
177: Exception myEx = new Exception(exceptionMessage);
178: assertObject.setActualObject(myEx);
179: try {
180: handler.checkOnExpectedException(this .testId,
181: this .classDataSet.getAssertMap(this .testMethod,
182: this .testId));
183: fail("Expected AssertionFailedError");
184: } catch (AssertionFailedError ex) {
185: assertEquals("Wrong exception message", expectedMessage, ex
186: .getMessage());
187: }
188: }
189: }
|