View Javadoc

1   // $Id: ExceptionHandlerTest.java 216 2006-03-02 23:17:18Z jg_hamburg $
2   /********************************************************************************
3    * DDTUnit, a Datadriven Approach to Unit- and Moduletesting
4    * Copyright (c) 2004, Joerg and Kai Gellien
5    * All rights reserved.
6    *
7    * The Software is provided under the terms of the Common Public License 1.0
8    * as provided with the distribution of DDTUnit in the file cpl-v10.html.
9    * Redistribution and use in source and binary forms, with or without
10   * modification, are permitted provided that the following conditions
11   * are met:
12   *
13   *     + Redistributions of source code must retain the above copyright
14   *       notice, this list of conditions and the following disclaimer.
15   *
16   *     + Redistributions in binary form must reproduce the above
17   *       copyright notice, this list of conditions and the following
18   *       disclaimer in the documentation and/or other materials provided
19   *       with the distribution.
20   *
21   *     + Neither the name of the authors or DDTUnit, nor the
22   *       names of its contributors may be used to endorse or promote
23   *       products derived from this software without specific prior
24   *       written permission.
25   *
26   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
27   * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
28   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
29   * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
30   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31   * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33   * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37   ********************************************************************************/
38  package junitx.ddtunit;
39  
40  import java.lang.reflect.InvocationTargetException;
41  
42  import junit.framework.AssertionFailedError;
43  import junit.framework.TestCase;
44  import junitx.ddtunit.data.ExceptionAsserter;
45  import junitx.ddtunit.data.ObjectAsserter;
46  import junitx.ddtunit.data.TestClusterDataSet;
47  import junitx.ddtunit.data.processing.ClusterDataSetTestCreator;
48  
49  /**
50   * TODO Comment for DDTExceptionHandlerTest
51   * 
52   * @author jg
53   */
54  public class ExceptionHandlerTest extends TestCase {
55      private ExceptionHandler handler;
56  
57      private String testClass;
58  
59      private String testMethod;
60  
61      private String testId;
62  
63      private TestClusterDataSet classDataSet;
64  
65      private static final String LF = System.getProperty("line.separator");
66  
67      /**
68       * Constructor for DDTExceptionHandlerTest.
69       * 
70       * @param name
71       */
72      public ExceptionHandlerTest(String name) {
73          super(name);
74      }
75  
76      /*
77       * @see TestCase#setUp()
78       */
79      protected void setUp() throws Exception {
80          testClass = "ExceptionHandlerTest";
81          testMethod = "methodName";
82          testId = "testId";
83          classDataSet = ClusterDataSetTestCreator.getClusterDataSet(testClass,
84              testMethod, testId);
85          handler = new ExceptionHandler(testMethod);
86      }
87  
88      /**
89       * Test behavior of method DDTExceptionHandler.process() by providing an
90       * unknown exception.
91       * 
92       */
93      public void testProcessUnknownException() throws Throwable {
94          String message = "Uncaught exception";
95          try {
96              handler.process(testId, new DDTException(message),
97                  this.classDataSet.getAssertMap(this.testMethod, this.testId));
98              handler.summarizeProblems(this.classDataSet.size(this.testMethod));
99              fail("Should throw unexpected exception.");
100         } catch (DDTException ex) {
101             assertEquals("Wrong exception message", message, ex.getMessage());
102         }
103     }
104 
105     /**
106      * Test behavior of method DDTExceptionHandler.process() by providing an
107      * unknown exception and no valid assertion data.
108      * 
109      */
110     public void testProcessInvocationTargetExNoAsserts() throws Throwable {
111         String message = "Uncaught exception";
112         try {
113             Exception testEx = new InvocationTargetException(new DDTException(
114                     message));
115 
116             handler.process(testId, testEx, null);
117             handler.summarizeProblems(this.classDataSet.size(this.testMethod));
118             fail("Should throw unexpected exception.");
119         } catch (DDTException ex) {
120             assertEquals("Wrong exception message", message, ex.getMessage());
121         }
122     }
123 
124     /**
125      * Test behavior of method DDTExceptionHandler.process() by providing a
126      * known exception.
127      * 
128      */
129     public void testProcessExpectedException() {
130         try {
131             String exceptionId = "exception0";
132             String exceptionMessage = "Expected exception";
133             ExceptionAsserter assertObject = new ExceptionAsserter(exceptionId,
134                     "java.lang.Exception", "ISEQUAL");
135 
136             assertObject.setValue(new Exception(exceptionMessage));
137             this.classDataSet.getTestDataSet(this.testMethod, testId)
138                 .getAssertMap().put(exceptionId, assertObject);
139             // Exceptin to catch
140             Exception myEx = new Exception(exceptionMessage);
141             Throwable actualEx = new InvocationTargetException(myEx);
142             assertObject.setActualObject(myEx);
143             handler.process(this.testId, actualEx, this.classDataSet
144                 .getAssertMap(this.testMethod, this.testId));
145             handler.summarizeProblems(this.classDataSet.size(this.testMethod));
146         } catch (Throwable e) {
147             fail("Should not throw exception. " + e.getClass().getName());
148         }
149     }
150 
151     /**
152      * Test if defined but not caught exception is detected ass failure.
153      */
154     public void testCheckOnExpectedException() {
155         String exceptionId = "exception0";
156         String exceptionMessage = "Expected exception";
157         String expectedMessage = "There is/are 1 expected exception(s) defined in test 'testId'"
158                 + LF
159                 + " (last as hint): java.lang.Exception - Expected exception";
160 
161         ExceptionAsserter assertObject = new ExceptionAsserter(exceptionId,
162                 "java.lang.Exception", ObjectAsserter.ASSERT_ACTION_ISEQUAL);
163 
164         assertObject.setValue(new Exception(exceptionMessage));
165         this.classDataSet.getTestDataSet(this.testMethod, testId)
166             .getAssertMap().put(exceptionId, assertObject);
167         // Exceptin to catch
168         Exception myEx = new Exception(exceptionMessage);
169         assertObject.setActualObject(myEx);
170         try {
171             handler.checkOnExpectedException(this.testId, this.classDataSet
172                 .getAssertMap(this.testMethod, this.testId));
173             fail("Expected AssertionFailedError");
174         } catch (AssertionFailedError ex) {
175             assertEquals("Wrong exception message", expectedMessage, ex
176                 .getMessage());
177         }
178     }
179 }