1 /*******************************************************************************
2  * Copyright (c) 2006, 2017 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *   David Saff (saff@mit.edu) - initial API and implementation
13  *             (bug 102632: [JUnit] Support for JUnit 4.)
14  *******************************************************************************/
15 
16 package org.eclipse.jdt.internal.junit4.runner;
17 
18 import org.junit.runner.Description;
19 import org.junit.runner.Result;
20 import org.junit.runner.Runner;
21 import org.junit.runner.notification.RunListener;
22 import org.junit.runner.notification.RunNotifier;
23 import org.junit.runner.notification.StoppedByUserException;
24 
25 import org.eclipse.jdt.internal.junit.runner.IStopListener;
26 import org.eclipse.jdt.internal.junit.runner.ITestIdentifier;
27 import org.eclipse.jdt.internal.junit.runner.ITestReference;
28 import org.eclipse.jdt.internal.junit.runner.IVisitsTestTrees;
29 import org.eclipse.jdt.internal.junit.runner.TestExecution;
30 
31 public class JUnit4TestReference implements ITestReference {
32 	protected final Runner fRunner;
33 
34 	protected final Description fRoot;
35 
JUnit4TestReference(Runner runner, Description root)36 	public JUnit4TestReference(Runner runner, Description root) {
37 		fRunner= runner;
38 		fRoot= root;
39 	}
40 
countTestCases()41 	public int countTestCases() {
42 		return countTestCases(fRoot);
43 	}
44 
countTestCases(Description description)45 	private int countTestCases(Description description) {
46 		if (description.isTest()) {
47 			return 1;
48 		} else {
49 			int result= 0;
50 			for (Description child : description.getChildren()) {
51 				result+= countTestCases(child);
52 			}
53 			return result;
54 		}
55 	}
56 
57 	@Override
equals(Object obj)58 	public boolean equals(Object obj) {
59 		if (!(obj instanceof JUnit4TestReference))
60 			return false;
61 
62 		JUnit4TestReference ref= (JUnit4TestReference)obj;
63 		return (ref.fRoot.equals(fRoot));
64 	}
65 
getIdentifier()66 	public ITestIdentifier getIdentifier() {
67 		return new JUnit4Identifier(fRoot);
68 	}
69 
70 	@Override
hashCode()71 	public int hashCode() {
72 		return fRoot.hashCode();
73 	}
74 
run(TestExecution execution)75 	public void run(TestExecution execution) {
76 		final RunNotifier notifier= new RunNotifier();
77 		notifier.addListener(new JUnit4TestListener(execution.getListener()));
78 		execution.addStopListener(new IStopListener() {
79 			public void stop() {
80 				notifier.pleaseStop();
81 			}
82 		});
83 
84 		Result result= new Result();
85 		RunListener listener= result.createListener();
86 		notifier.addListener(listener);
87 		try {
88 			notifier.fireTestRunStarted(fRunner.getDescription());
89 			fRunner.run(notifier);
90 			notifier.fireTestRunFinished(result);
91 		} catch (StoppedByUserException e) {
92 			// not interesting, see https://bugs.eclipse.org/329498
93 		} finally {
94 			notifier.removeListener(listener);
95 		}
96 	}
97 
sendTree(IVisitsTestTrees notified)98 	public void sendTree(IVisitsTestTrees notified) {
99 		sendTree(notified, fRoot);
100 	}
101 
sendTree(final IVisitsTestTrees notified, Description description)102 	private void sendTree(final IVisitsTestTrees notified, Description description) {
103 		if (description.isTest()) {
104 			notified.visitTreeEntry(new JUnit4Identifier(description), false, 1, false, "-1"); //$NON-NLS-1$
105 		} else {
106 			notified.visitTreeEntry(new JUnit4Identifier(description), true, description.getChildren().size(), false, "-1"); //$NON-NLS-1$
107 			for (Description child : description.getChildren()) {
108 				sendTree(notified, child);
109 			}
110 		}
111 	}
112 
113 	@Override
toString()114 	public String toString() {
115 		return fRoot.toString();
116 	}
117 }
118