View Javadoc

1   // $Id: ExecutionResultTest.java 326 2007-09-21 18:43:13Z 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 junit.framework.TestCase;
41  import junit.framework.TestFailure;
42  import junitx.ddtunit.resources.RunnerMonitor;
43  
44  /**
45   * Test different execution runs with different DDTTestResults.
46   * 
47   * @author jg
48   */
49  public class ExecutionResultTest extends TestCase {
50  	private static final String LF = System.getProperty("line.separator");
51  
52  	private DDTTestResult result;
53  
54  	private String testResource;
55  
56  	private String classId;
57  
58  	/**
59  	 * Constructor of ExecutionResultTest
60  	 * 
61  	 * @param name
62  	 */
63  	public ExecutionResultTest(String name) {
64  		super(name);
65  	}
66  
67  	/**
68  	 * Setup test environment
69  	 * 
70  	 * @see junit.framework.TestCase#setUp()
71  	 */
72  	public void setUp() {
73  		result = new DDTTestResult();
74  		result.addListener(RunnerMonitor.getInstance());
75  	}
76  
77  	/**
78  	 * Execute internal DDTTestCase with one method and three xml based tests,
79  	 * no execution exception.
80  	 * 
81  	 */
82  	public void testRunThreeMethodTestsNoProblem() {
83  		this.testResource = "ExecutionResultTest";
84  		this.classId = "ExecutionResultNoProblem";
85  
86  		String testMethod = "testThreeTestsNoProblem";
87  		DDTExecutionTest test = new DDTExecutionTest(testMethod);
88  
89  		test.run(result);
90  		assertEquals("Wrong number of methods executed", 1, result.runCount());
91  		assertEquals("No failures expected", 0, result.failureCount());
92  		assertEquals("No errors expected", 0, result.errorCount());
93  
94  		//
95  		assertEquals("Wrong number of method tests.", 3, result
96  				.runMethodTestCount());
97  		assertEquals("No failures in method test execution expected.", 0,
98  				result.methodTestFailureCount());
99  		assertEquals("No errors in method test execution expected.", 0, result
100 				.methodTestErrorCount());
101 	}
102 
103 	/**
104 	 * Execute internal DDTTestCase with one method and three xml based tests,
105 	 * one AssertionFailed exception.
106 	 * 
107 	 */
108 	public void testRunThreeMethodTestsOneAssertFailedException() {
109 		this.testResource = "ExecutionResultTest";
110 		this.classId = "ExecutionResultNoProblem";
111 
112 		String testMethod = "testThreeTestsOneAssertFailed";
113 		DDTExecutionTest test = new DDTExecutionTest(testMethod);
114 
115 		test.run(result);
116 		assertEquals("Wrong number of methods executed", 1, result.runCount());
117 		assertEquals("No errors expected", 0, result.errorCount());
118 		assertEquals("One failures expected", 1, result.failureCount());
119 
120 		// check returned failure message
121 		String message = ((TestFailure) result.failures().nextElement())
122 				.exceptionMessage();
123 		String expected = "method testThreeTestsOneAssertFailed - Total: 3, Errors: 0, Failures: 1"
124 				+ LF
125 				+ "F-(my-second-testcase) Wrong isEqual assert on (multiline) string [position 0] expected:<my-unexpected-testcase> but was:<my-second-testcase>";
126 		assertEquals("Wrong exepction message in JUnit failure", expected,
127 				message);
128 		assertEquals("Wrong number of method tests.", 3, result
129 				.runMethodTestCount());
130 		assertEquals("No errors in method test execution expected.", 0, result
131 				.methodTestErrorCount());
132 		assertEquals("One failures in method test execution expected.", 1,
133 				result.methodTestFailureCount());
134 		message = ((TestFailure) result.methodTestFailures().next())
135 				.exceptionMessage();
136 		expected = "Wrong isEqual assert on (multiline) string [position 0] "
137 				+ "expected:<my-unexpected-testcase> but was:<my-second-testcase>";
138 		assertEquals("Wrong exception message of method test failure",
139 				expected, message);
140 	}
141 
142 	/**
143 	 * Execute internal DDTTestCase with one method and three xml based tests,
144 	 * one AssertionFailed and one other exception.
145 	 * 
146 	 */
147 	public void testRunThreeMethodTestsOneAssertFailedOneException() {
148 		this.testResource = "ExecutionResultTest";
149 		this.classId = "ExecutionResultNoProblem";
150 
151 		String testMethod = "testThreeTestsOneAssertFailedOneException";
152 		DDTExecutionTest test = new DDTExecutionTest(testMethod);
153 
154 		test.run(result);
155 		assertEquals("Wrong number of methods executed", 1, result.runCount());
156 		assertEquals("One errors expected", 1, result.errorCount());
157 		assertEquals("One failures expected", 0, result.failureCount());
158 		assertEquals("Wrong number of method tests executed", 3, result
159 				.runMethodTestCount());
160 		assertEquals("One errors expected", 1, result.methodTestErrorCount());
161 		assertEquals("One failures expected", 1, result
162 				.methodTestFailureCount());
163 
164 		// check returned error message
165 		String message = ((TestFailure) result.errors().nextElement())
166 				.exceptionMessage();
167 		String expected = "method testThreeTestsOneAssertFailedOneException - "
168 				+ "Total: 3, Errors: 1, Failures: 1"
169 				+ LF
170 				+ "E-(my-first-testcase) Throw expected exception"
171 				+ LF
172 				+ "F-(my-second-testcase) Wrong isEqual assert on (multiline) "
173 				+ "string [position 0] expected:<my-unexpected-testcase> but was:"
174 				+ "<my-second-testcase>";
175 		assertEquals("Wrong exception message in JUnit failure", expected,
176 				message);
177 		assertEquals("One failures in method test execution expected.", 1,
178 				result.methodTestFailureCount());
179 		message = (result.methodTestFailures().next()).exceptionMessage();
180 		expected = "Wrong isEqual assert on (multiline) string [position 0]"
181 				+ " expected:<my-unexpected-testcase> but was:<my-second-testcase>";
182 		assertEquals("Wrong exception message of method test failure",
183 				expected, message);
184 	}
185 
186 	/**
187 	 * Internal DDTTestCase class for starting test execution. <br/>XML test
188 	 * resource and classId are parametrised using outer class fields.
189 	 * 
190 	 * @author jg
191 	 */
192 	private class DDTExecutionTest extends DDTTestCase {
193 		/**
194 		 * Instanciate DDTTestCase with method name to execute
195 		 * 
196 		 * @param name
197 		 */
198 		public DDTExecutionTest(String name) {
199 			super(name);
200 		}
201 
202 		/**
203 		 * @see junitx.ddtunit.DDTTestCase#initTestData()
204 		 */
205 		protected void initContext() {
206 			initTestData(testResource, classId);
207 		}
208 
209 		/**
210 		 * Run test method with three xml based tests and no exceptions expected
211 		 * during execution.
212 		 */
213 		public void testThreeTestsNoProblem() {
214 		}
215 
216 		/**
217 		 * Run test method with three xml based tests and one assertino
218 		 * exception during execution
219 		 */
220 		public void testThreeTestsOneAssertFailed() {
221 			addObjectToAssert("result", getObject("objId"));
222 		}
223 
224 		/**
225 		 * Run test method with three xml based tests and one assertion and one
226 		 * other exception during execution
227 		 */
228 		public void testThreeTestsOneAssertFailedOneException() {
229 			String value = (String) getObject("objId");
230 
231 			addObjectToAssert("result", value);
232 
233 			if ("my-first-testcase".equals(value)) {
234 				throw new DDTException("Throw expected exception");
235 			}
236 		}
237 	}
238 }