/
Requirements.java
138 lines (120 loc) · 3.7 KB
/
Requirements.java
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*******************************************************************************
* Copyright (c) 2017 Red Hat, Inc and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* Red Hat, Inc - initial API and implementation
*******************************************************************************/
package org.eclipse.reddeer.junit.internal.requirement;
import java.lang.annotation.Annotation;
import java.util.Iterator;
import java.util.List;
import java.util.function.Consumer;
import org.eclipse.reddeer.common.logging.Logger;
import org.eclipse.reddeer.junit.execution.PriorityComparator;
import org.eclipse.reddeer.junit.requirement.Requirement;
import org.eclipse.reddeer.junit.screenshot.CaptureScreenshotException;
import org.eclipse.reddeer.junit.screenshot.ScreenshotCapturer;
/**
* Aggregates {@link Requirement} objects and allows to perform tasks on them easily.
*
* @author Lucia Jelinkova
* @author Andrej Podhradsky (apodhrad@redhat.com)
*
*/
public class Requirements implements Requirement<Annotation>, Iterable<Requirement<?>>{
private List<Requirement<?>> requirements;
private Class<?> clazz;
private String configID;
private Logger log = Logger.getLogger(Requirements.class);
/**
* Instantiates a new requirements.
*
* @param requirements the requirements
* @param clazz the clazz
* @param configID the config id
*/
public Requirements(List<Requirement<?>> requirements, Class<?> clazz, String configID) {
super();
if (requirements == null){
throw new IllegalArgumentException("The requirements list was null");
}
if (clazz == null) {
throw new IllegalArgumentException("The class containing requirements is null");
}
requirements.sort(new PriorityComparator());
this.requirements = requirements;
this.clazz = clazz;
this.configID = configID;
}
@Override
public Iterator<Requirement<?>> iterator() {
return requirements.iterator();
}
/**
* Size.
*
* @return the int
*/
public int size(){
return requirements.size();
}
@Override
public void fulfill() {
runSafely(Requirement::fulfill);
}
@Override
public void cleanUp() {
runSafely(Requirement::cleanUp);
}
@Override
public void runBefore() {
runSafely(Requirement::runBefore);
}
@Override
public void runBeforeClass() {
runSafely(Requirement::runBeforeClass);
}
@Override
public void runAfter() {
runSafely(Requirement::runAfter);
}
@Override
public void runAfterClass() {
runSafely(Requirement::runAfterClass);
}
private void runSafely(Consumer<Requirement<?>> run) {
requirements.forEach(requirement -> runSafely(run, requirement));
}
private void runSafely(Consumer<Requirement<?>> run, Requirement<?> requirement) {
try {
run.accept(requirement);
} catch (Throwable t) {
handleException(t, requirement);
throw t;
}
}
@Override
public void setDeclaration(Annotation declaration) {
throw new IllegalStateException("This method should never be called on wrapper object");
}
private void handleException(Throwable ex, Requirement<?> r) {
if (ScreenshotCapturer.shouldCaptureScreenshotOnException(ex)) {
ScreenshotCapturer screenshotCapturer = ScreenshotCapturer.getInstance();
try {
screenshotCapturer.captureScreenshotOnFailure(configID,
ScreenshotCapturer.getScreenshotFileName(clazz, null, r.getClass().getSimpleName()));
} catch (CaptureScreenshotException e) {
e.printInfo(log);
}
}
}
@Override
public Annotation getDeclaration() {
throw new IllegalStateException("This method should never be called on wrapper object");
}
}