001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.junit.tests;
011:
012: import java.util.ArrayList;
013: import java.util.Arrays;
014: import java.util.Enumeration;
015: import java.util.Iterator;
016: import java.util.List;
017: import junit.extensions.TestDecorator;
018: import junit.extensions.TestSetup;
019: import junit.framework.Test;
020: import junit.framework.TestCase;
021: import junit.framework.TestSuite;
022: import org.eclipse.jdt.internal.junit.runner.FailuresFirstPrioritizer;
023:
024: public class TestPriorization extends TestCase {
025:
026: public void testReorderSimple() {
027: TestSuite suite = createSuiteDEF();
028: String[] priority = { "testF(org.eclipse.jdt.junit.tests.TestPriorizationSuite2)" };
029: Test prioritized = prioritize(suite, priority);
030: List order = new ArrayList();
031: collectOrder(prioritized, order);
032: String[] expected = { "testF", "testD", "testE" };
033: checkOrder(expected, order);
034: }
035:
036: public void testReorderCustomSuite() {
037: // custom suite
038: // D
039: // E
040: // F
041: TestSuite suite = new TestSuite() {
042: };
043: suite.addTest(new TestPriorizationSuite2("testD"));
044: suite.addTest(new TestPriorizationSuite2("testE"));
045: suite.addTest(new TestPriorizationSuite2("testF"));
046:
047: String[] priority = { "testF(org.eclipse.jdt.junit.tests.TestPriorizationSuite2)" };
048: Test prioritized = prioritize(suite, priority);
049:
050: List order = new ArrayList();
051: collectOrder(prioritized, order);
052: String[] expected = { "testF", "testD", "testE" };
053: //printOrder(order);
054: checkOrder(expected, order);
055: }
056:
057: public void testReorderSimpleWithDecorator() {
058: // suite
059: // decorator
060: // D
061: // E
062: // F
063:
064: Test suite = new TestSetup(createSuiteDEF()) {
065: protected void setUp() throws Exception {
066: }
067: };
068:
069: String[] priority = { "testF(org.eclipse.jdt.junit.tests.TestPriorizationSuite2)" };
070: Test reordered = prioritize(suite, priority);
071: List order = new ArrayList();
072: collectOrder(reordered, order);
073: String[] expected = { "testF", "testD", "testE" };
074: checkOrder(expected, order);
075: }
076:
077: public void testReorderWithPropagation() {
078: // suite
079: // suite1
080: // A
081: // B
082: // C
083: // suite2
084: // D
085: // E
086: // F
087: TestSuite suite = new TestSuite();
088: suite.addTest(createSuiteABC());
089: suite.addTest(createSuiteDEF());
090:
091: String[] priority = { "testF(org.eclipse.jdt.junit.tests.TestPriorizationSuite2)" };
092: Test reordered = prioritize(suite, priority);
093: List order = new ArrayList();
094: collectOrder(reordered, order);
095:
096: String[] expected = { "testF", "testD", "testE", "testA",
097: "testB", "testC", };
098: checkOrder(expected, order);
099: }
100:
101: public void testReorderWithPropagationWithDecorator() {
102: // suite
103: // suite1
104: // A
105: // B
106: // C
107: // decorator
108: // suite2
109: // D
110: // E
111: // F
112: TestSuite suite = new TestSuite();
113: suite.addTest(createSuiteABC());
114: TestSuite suite2 = createSuiteDEF();
115: suite.addTest(new TestSetup(suite2) {
116: protected void setUp() throws Exception {
117: }
118: });
119:
120: String[] priority = { "testF(org.eclipse.jdt.junit.tests.TestPriorizationSuite2)" };
121: Test reordered = prioritize(suite, priority);
122: List order = new ArrayList();
123: collectOrder(reordered, order);
124:
125: String[] expected = { "testF", "testD", "testE", "testA",
126: "testB", "testC", };
127: checkOrder(expected, order);
128: }
129:
130: public void testReorderWithPropagation2() {
131: // suite4
132: // suite3
133: // X
134: // Y
135: // Z
136: // suite
137: // suite1
138: // A
139: // B
140: // C
141: // suite2
142: // D
143: // E
144: // F*
145: TestSuite suite = new TestSuite();
146: suite.addTest(createSuiteABC());
147: suite.addTest(createSuiteDEF());
148: TestSuite suite4 = new TestSuite();
149: suite4.addTest(createSuiteXYZ());
150: suite4.addTest(suite);
151:
152: String[] priority = { "testF(org.eclipse.jdt.junit.tests.TestPriorizationSuite2)" };
153: Test reordered = prioritize(suite4, priority);
154: List order = new ArrayList();
155: collectOrder(reordered, order);
156:
157: String[] expected = { "testF", "testD", "testE", "testA",
158: "testB", "testC", "testX", "testY", "testZ", };
159: checkOrder(expected, order);
160: }
161:
162: public void testReorderWithPropagationBug() {
163: // suite4
164: // suite3
165: // X
166: // Y
167: // Z
168: // suite
169: // suite1
170: // A
171: // B
172: // C
173: // suite2
174: // D
175: // E*
176: // F*
177: TestSuite suite = new TestSuite();
178: suite.addTest(createSuiteABC());
179: suite.addTest(createSuiteDEF());
180: TestSuite suite4 = new TestSuite();
181: suite4.addTest(createSuiteXYZ());
182: suite4.addTest(suite);
183:
184: String[] priority = {
185: "testE(org.eclipse.jdt.junit.tests.TestPriorizationSuite2)",
186: "testF(org.eclipse.jdt.junit.tests.TestPriorizationSuite2)" };
187: Test reordered = prioritize(suite4, priority);
188: List order = new ArrayList();
189: collectOrder(reordered, order);
190:
191: String[] expected = { "testF", "testE", "testD", "testA",
192: "testB", "testC", "testX", "testY", "testZ", };
193: //printOrder(order);
194: checkOrder(expected, order);
195: }
196:
197: public void testReorderWithPropagation3() {
198: // suite4
199: // suite3
200: // X
201: // Y
202: // Z*
203: // suite
204: // suite1
205: // A
206: // B
207: // C
208: // suite2
209: // D
210: // E
211: // F*
212: TestSuite suite = new TestSuite();
213: suite.addTest(createSuiteABC());
214: suite.addTest(createSuiteDEF());
215: TestSuite suite4 = new TestSuite();
216: suite4.addTest(createSuiteXYZ());
217: suite4.addTest(suite);
218:
219: String[] priority = {
220: "testF(org.eclipse.jdt.junit.tests.TestPriorizationSuite2)",
221: "testZ(org.eclipse.jdt.junit.tests.TestPriorizationSuite)" };
222: Test reordered = prioritize(suite4, priority);
223: List order = new ArrayList();
224: collectOrder(reordered, order);
225:
226: String[] expected = { "testF", "testD", "testE", "testA",
227: "testB", "testC", "testZ", "testX", "testY", };
228: checkOrder(expected, order);
229: }
230:
231: public void testReorder() {
232: // suite
233: // suite1
234: // X
235: // Y
236: // Z*
237: // suite2
238: // suite3
239: // A
240: // B
241: // C
242: // suite4
243: // D
244: // E
245: // F*
246: TestSuite suite = new TestSuite();
247: suite.addTestSuite(TestPriorizationSuite.class);
248: TestSuite suite2 = new TestSuite();
249: suite2.addTestSuite(TestPriorizationSuite1.class);
250: suite2.addTestSuite(TestPriorizationSuite2.class);
251: suite.addTest(suite2);
252: String[] priority = {
253: "testF(org.eclipse.jdt.junit.tests.TestPriorizationSuite2)",
254: "testZ(org.eclipse.jdt.junit.tests.TestPriorizationSuite)" };
255: Test reordered = prioritize(suite, priority);
256: List order = new ArrayList();
257: collectOrder(reordered, order);
258:
259: // can't check for exact order, since order of Class.getDeclaredMethods() is unspecified (bug 144503)
260: List suiteTests = new ArrayList(Arrays.asList(new String[] {
261: "testX", "testY", "testZ" }));
262: List suite1Tests = new ArrayList(Arrays.asList(new String[] {
263: "testA", "testB", "testC" }));
264: List suite2Tests = new ArrayList(Arrays.asList(new String[] {
265: "testD", "testE", "testF" }));
266:
267: assertEquals("testF", (String) order.get(0));
268: assertEquals("testZ", (String) order.get(6));
269: for (int i = 0; i < 3; i++) {
270: String test = (String) order.get(i);
271: assertTrue(test, suite2Tests.remove(test));
272: }
273: for (int i = 3; i < 6; i++) {
274: String test = (String) order.get(i);
275: assertTrue(test, suite1Tests.remove(test));
276: }
277: for (int i = 6; i < 9; i++) {
278: String test = (String) order.get(i);
279: assertTrue(test, suiteTests.remove(test));
280: }
281: }
282:
283: private TestSuite createSuiteDEF() {
284: // suite
285: // D
286: // E
287: // F
288: TestSuite suite = new TestSuite();
289: suite.addTest(new TestPriorizationSuite2("testD"));
290: suite.addTest(new TestPriorizationSuite2("testE"));
291: suite.addTest(new TestPriorizationSuite2("testF"));
292: return suite;
293: }
294:
295: private TestSuite createSuiteABC() {
296: // suite
297: // A
298: // B
299: // C
300: TestSuite suite = new TestSuite();
301: suite.addTest(new TestPriorizationSuite1("testA"));
302: suite.addTest(new TestPriorizationSuite1("testB"));
303: suite.addTest(new TestPriorizationSuite1("testC"));
304: return suite;
305: }
306:
307: private TestSuite createSuiteXYZ() {
308: // suite
309: // X
310: // Y
311: // Z
312: TestSuite suite = new TestSuite();
313: suite.addTest(new TestPriorizationSuite("testX"));
314: suite.addTest(new TestPriorizationSuite("testY"));
315: suite.addTest(new TestPriorizationSuite("testZ"));
316: return suite;
317: }
318:
319: private void checkOrder(String[] expected, List order) {
320: // assertEquals(Arrays.asList(expected), order);
321:
322: assertEquals(enumerate(Arrays.asList(expected)),
323: enumerate(order));
324: }
325:
326: private static String enumerate(List list) {
327: StringBuffer buf = new StringBuffer();
328: for (Iterator iter = list.iterator(); iter.hasNext();) {
329: String s = (String) iter.next();
330: buf.append(s).append('\n');
331: }
332: return buf.toString();
333: }
334:
335: /*
336: private void printOrder(List order) {
337: for (int i= 0; i < order.size(); i++) {
338: String s= (String)order.get(i);
339: System.out.println(s);
340: }
341: }
342: */
343:
344: private void collectOrder(Test suite, List order) {
345: if (suite instanceof TestCase) {
346: String s = suite.toString();
347: s = s.substring(0, s.indexOf('('));
348: order.add(s);
349: } else if (suite instanceof TestSuite) {
350: TestSuite aSuite = (TestSuite) suite;
351: for (Enumeration e = aSuite.tests(); e.hasMoreElements();) {
352: Test test = (Test) e.nextElement();
353: collectOrder(test, order);
354: }
355: } else if (suite instanceof TestDecorator) {
356: TestDecorator aDecorator = (TestDecorator) suite;
357: collectOrder(aDecorator.getTest(), order);
358: }
359: }
360:
361: private Test prioritize(Test suite, String[] priority) {
362: FailuresFirstPrioritizer prioritizer = new FailuresFirstPrioritizer(
363: priority);
364: return prioritizer.prioritize(suite);
365: }
366: }
|