Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 882033a0670cf7b07b33e5e7c4d33d5f3d0c56fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/*****************************************************************************
 * Copyright (c) 2015 Christian W. Damus and others.
 * 
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *   Christian W. Damus - Initial API and implementation
 *   
 *****************************************************************************/

package org.eclipse.papyrus.junit.utils.rules;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executor;

import org.eclipse.papyrus.junit.utils.Activator;
import org.junit.rules.TestRule;
import org.junit.rules.TestWatcher;
import org.junit.runner.Description;

import com.google.common.collect.Queues;

/**
 * A JUnit {@linkplain TestRule rule} that is an {@link Executor} running tasks at clean-up of the
 * test execution.
 */
public class ExecutorRule extends TestWatcher implements Executor {
	private final BlockingQueue<Runnable> queue = Queues.newLinkedBlockingQueue();

	public ExecutorRule() {
		super();
	}

	@Override
	public void execute(Runnable command) {
		queue.add(command);
	}

	protected void runPending() {
		for (Runnable next = queue.poll(); next != null; next = queue.poll()) {
			try {
				next.run();
			} catch (Exception e) {
				Activator.log.error("Uncaught exception in test shutdown runnable.", e); //$NON-NLS-1$
			}
		}
	}

	@Override
	protected void finished(Description description) {
		runPending();
	}
}

Back to the top