001: // $Id: ProcessExpectedExceptionErrorTest.java 202 2005-11-22 19:21:28Z 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.functest;
038:
039: import junit.framework.Test;
040: import junit.framework.TestCase;
041: import junit.framework.TestFailure;
042: import junit.framework.TestResult;
043: import junit.framework.TestSuite;
044: import junit.textui.TestRunner;
045: import junitx.ddtunit.DDTException;
046: import junitx.ddtunit.DDTTestCase;
047:
048: /**
049: * Test class to verify behavior if an expected exception is stated in the test
050: * resource but not raised during test execution.
051: *
052: * @author jg
053: */
054: public class ProcessExpectedExceptionErrorTest extends TestCase {
055: private static final String LF = System
056: .getProperty("line.separator");
057:
058: /**
059: * @param name
060: */
061: public ProcessExpectedExceptionErrorTest(String name) {
062: super (name);
063: }
064:
065: /**
066: * Define tests to run
067: *
068: * @return
069: */
070: public static Test suite() {
071: return new TestSuite(ProcessExpectedExceptionErrorTest.class);
072: }
073:
074: /**
075: * Test behavior on defined but not raised expected exception. An exception
076: * should be raised stating the missing exception.
077: */
078: public void testDefinedbutNotRaisedException() {
079: String expectedMessage = "xxx"
080: + LF
081: + "Caused by:junit.framework.AssertionFailedError - method testDefinedbutNotRaisedException - Total: 1, Errors: 0, Failures: 1"
082: + LF
083: + "F-(myFirstTestCase) There is/are 1 expected exception(s) defined in test 'myFirstTestCase'"
084: + LF
085: + " (last as hint): java.lang.RuntimeException - My Exception";
086:
087: TestResult result = TestRunner.run(new MyDDTTestCase(
088: "testDefinedbutNotRaisedException"));
089:
090: assertEquals("Wrong number of errors", 0, result.errorCount());
091: assertEquals("Wrong number of failures", 1, result
092: .failureCount());
093:
094: Throwable expectedException = ((TestFailure) result.failures()
095: .nextElement()).thrownException();
096: DDTException expected = DDTException.create(new StringBuffer(
097: "xxx"), expectedException);
098: assertEquals("Wrong exception message", expectedMessage,
099: expected.getMessage());
100: }
101:
102: private static class MyDDTTestCase extends DDTTestCase {
103: /**
104: * instanciate testcase for testmethod defined by name
105: *
106: * @param name of testmethod to execute
107: */
108: public MyDDTTestCase(String name) {
109: super (name);
110: }
111:
112: /**
113: * @see junitx.ddtunit.DDTTestCase#initContext()
114: */
115: protected void initContext() {
116: initTestData("ProcessExpectedExceptionTest",
117: "ProcessUnexpectedExceptionTest");
118: }
119:
120: /**
121: * Test behavior on defined but not raised expected exception. An
122: * exception should be raised stating the missing exception.
123: */
124: public void testDefinedbutNotRaisedException() {
125: assertTrue("Should allways be true", true);
126: }
127: }
128: }
|