View Javadoc

1   // $Id: DDTTestCase.java 351 2008-08-14 20:20:56Z 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  
39  package junitx.ddtunit.optional.junit4;
40  
41  import java.lang.reflect.Constructor;
42  import java.lang.reflect.InvocationTargetException;
43  import java.lang.reflect.Method;
44  import java.util.Enumeration;
45  
46  import junit.framework.TestCase;
47  import junit.framework.TestFailure;
48  import junit.framework.TestResult;
49  
50  import org.junit.After;
51  import org.junit.Before;
52  import org.junit.Ignore;
53  import org.junit.internal.runners.InitializationError;
54  import org.junit.internal.runners.JUnit4ClassRunner;
55  import org.junit.runner.Description;
56  import org.junit.runner.notification.Failure;
57  import org.junit.runner.notification.RunNotifier;
58  
59  public class DDTUnit4ClassRunner extends JUnit4ClassRunner {
60  
61  	private final Class<? extends TestCase> clazz;
62  
63  	public DDTUnit4ClassRunner(Class<? extends TestCase> clazz)
64  			throws InitializationError {
65  		super(clazz);
66  		this.clazz = clazz;
67  	}
68  
69  	@Override
70  	protected void invokeTestMethod(Method method, RunNotifier runNotifier) {
71  
72  		Description testDescription = Description.createTestDescription(clazz,
73  				method.getName());
74  
75  		try {
76  			if (method.isAnnotationPresent(Ignore.class)) {
77  				runNotifier.fireTestIgnored(testDescription);
78  			}
79  			Constructor<? extends TestCase> constructor = clazz
80  					.getConstructor(String.class);
81  
82  			TestCase newInstance = constructor.newInstance(method.getName());
83  
84  			runNotifier.fireTestStarted(testDescription);
85  
86  			TestResult result;
87  			try {
88  				setUp(newInstance);
89  
90  				result = newInstance.run();
91  			} finally {
92  
93  				tearDown(newInstance);
94  			}
95  
96  			if (!result.wasSuccessful()) {
97  
98  				for (Enumeration<TestFailure> en = result.errors(); en
99  						.hasMoreElements();) {
100 					TestFailure failure = en.nextElement();
101 					runNotifier.fireTestFailure(new Failure(testDescription,
102 							failure.thrownException()));
103 				}
104 
105 				for (Enumeration<TestFailure> en = result.failures(); en
106 						.hasMoreElements();) {
107 					TestFailure failure = en.nextElement();
108 					runNotifier.fireTestFailure(new Failure(testDescription,
109 							failure.thrownException()));
110 				}
111 
112 			}
113 
114 			runNotifier.fireTestFinished(testDescription);
115 
116 		} catch (Throwable e) {
117 			runNotifier.fireTestFailure(new Failure(testDescription, e));
118 		}
119 	}
120 
121 	private void tearDown(TestCase newInstance)
122 			throws IllegalArgumentException, IllegalAccessException,
123 			InvocationTargetException {
124 		Method[] methods = clazz.getMethods();
125 		for (Method method : methods) {
126 			if ((method.isAnnotationPresent(After.class) && !method
127 					.isAnnotationPresent(Ignore.class))) {
128 				method.invoke(newInstance, new Object[] {});
129 			}
130 		}
131 
132 	}
133 
134 	private void setUp(TestCase newInstance) throws IllegalArgumentException,
135 			IllegalAccessException, InvocationTargetException {
136 		Method[] methods = clazz.getMethods();
137 		for (Method method : methods) {
138 			// System.out.println("su " + method.getName() );
139 			if ((method.isAnnotationPresent(Before.class) && !method
140 					.isAnnotationPresent(Ignore.class))) {
141 				method.invoke(newInstance, new Object[] {});
142 			}
143 		}
144 
145 	}
146 
147 }