Project

General

Profile

Download (2.3 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
 * Copyright (C) 2009 EDIT
4
 * European Distributed Institute of Taxonomy 
5
 * http://www.e-taxonomy.eu
6
 * 
7
 * The contents of this file are subject to the Mozilla Public License Version 1.1
8
 * See LICENSE.TXT at the top of this package for the full license terms.
9
 */
10
package eu.etaxonomy.dataportal;
11

    
12
import java.lang.annotation.ElementType;
13
import java.lang.annotation.Inherited;
14
import java.lang.annotation.Retention;
15
import java.lang.annotation.RetentionPolicy;
16
import java.lang.annotation.Target;
17

    
18
import org.junit.internal.AssumptionViolatedException;
19
import org.junit.internal.runners.model.EachTestNotifier;
20
import org.junit.runner.notification.RunNotifier;
21
import org.junit.runner.notification.StoppedByUserException;
22
import org.junit.runners.BlockJUnit4ClassRunner;
23
import org.junit.runners.model.InitializationError;
24
import org.junit.runners.model.Statement;
25

    
26
public class DataPortalContextAwareRunner extends BlockJUnit4ClassRunner {
27

    
28
	@Retention(RetentionPolicy.RUNTIME)
29
	@Target(ElementType.TYPE)
30
	@Inherited
31
	public @interface DataPortalContexts {
32
		/**
33
		 * @return an array of DataPortalContext to which the annotated test
34
		 *         class is applicable
35
		 */
36
		DataPortalContext[] value();
37
	}
38

    
39
	private DataPortalContext dataPortalContext;
40

    
41
	public DataPortalContextAwareRunner(Class<?> klass)
42
			throws InitializationError {
43
		super(klass);
44
		dataPortalContext = DataPortalManager.currentDataPortalContext();
45
	}
46

    
47
	@Override
48
	public void run(final RunNotifier notifier) {
49
		EachTestNotifier testNotifier = new EachTestNotifier(notifier,
50
				getDescription());
51

    
52
		boolean isApplicableToContext = false;
53
		DataPortalContexts dataPortalContextsAnotation = getTestClass()
54
				.getJavaClass().getAnnotation(DataPortalContexts.class);
55
		for (DataPortalContext cntxt : dataPortalContextsAnotation.value()) {
56
			if (dataPortalContext.equals(cntxt)) {
57
				isApplicableToContext = true;
58
			}
59
		}
60

    
61
		if (!isApplicableToContext) {
62
			testNotifier.fireTestIgnored();
63
			return;
64
		}
65

    
66
		try {
67
			Statement statement = classBlock(notifier);
68
			statement.evaluate();
69
		} catch (AssumptionViolatedException e) {
70
			testNotifier.fireTestIgnored();
71
		} catch (StoppedByUserException e) {
72
			throw e;
73
		} catch (Throwable e) {
74
			testNotifier.addFailure(e);
75
		}
76
	}
77

    
78
}
(3-3/4)