Project

General

Profile

Download (2.29 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) throws InitializationError {
42
		super(klass);
43
		dataPortalContext = DataPortalManager.currentDataPortalContext();
44
	}
45

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

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

    
58
		if (!isApplicableToContext) {
59
			testNotifier.fireTestIgnored();
60
			return;
61
		}
62

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

    
75
}
(3-3/5)