-
Notifications
You must be signed in to change notification settings - Fork 99
/
TestAnnotations.java
267 lines (221 loc) · 8.75 KB
/
TestAnnotations.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package net.thucydides.core.annotations;
import ch.lambdaj.function.convert.Converter;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import net.thucydides.core.model.TestTag;
import net.thucydides.core.reports.html.Formatter;
import org.apache.commons.lang3.StringUtils;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static ch.lambdaj.Lambda.convert;
import static net.thucydides.core.util.NameConverter.withNoArguments;
/**
* Utility class used to help process annotations on tests and test steps.
*/
public class TestAnnotations {
private final Class<?> testClass;
private TestAnnotations(final Class<?> testClass) {
this.testClass = testClass;
}
public static TestAnnotations forClass(final Class<?> testClass) {
return new TestAnnotations(testClass);
}
public Optional<String> getAnnotatedTitleForMethod(final String methodName) {
if ((testClass != null) && (testClassHasMethodCalled(methodName))) {
return getAnnotatedTitle(methodName);
}
return Optional.absent();
}
public boolean isPending(final String methodName) {
Optional<Method> method = getMethodCalled(methodName);
return method.isPresent() && isPending(method.get());
}
public static boolean isPending(final Method method) {
return method != null && (method.getAnnotation(Pending.class) != null);
}
public static boolean isIgnored(final Method method) {
if (method != null) {
return hasAnnotationCalled(method, "Ignore");
}
return false;
}
private static boolean hasAnnotationCalled(Method method, String annotationName) {
Annotation[] annotations = method.getAnnotations();
for (Annotation annotation : annotations) {
if (annotation.annotationType().getSimpleName().equals(annotationName)) {
return true;
}
}
return false;
}
public boolean isIgnored(final String methodName) {
Optional<Method> method = getMethodCalled(methodName);
return method.isPresent() && isIgnored(method.get());
}
private Optional<String> getAnnotatedTitle(String methodName) {
Optional<Method> testMethod = getMethodCalled(methodName);
if (testMethod.isPresent()) {
Title titleAnnotation = testMethod.get().getAnnotation(Title.class);
if (titleAnnotation != null) {
return Optional.of(titleAnnotation.value());
}
}
return Optional.absent();
}
private boolean testClassHasMethodCalled(final String methodName) {
return (getMethodCalled(methodName).isPresent());
}
private Optional<Method> getMethodCalled(final String methodName) {
if (testClass == null) {
return Optional.absent();
}
String baseMethodName = withNoArguments(methodName);
try {
if (baseMethodName == null) {
return Optional.absent();
} else {
return Optional.fromNullable(testClass.getMethod(baseMethodName));
}
} catch (NoSuchMethodException e) {
return Optional.absent();
}
}
/**
* Return a list of the issues mentioned in the title annotation of this method.
*/
public List<String> getAnnotatedIssuesForMethodTitle(String methodName) {
Optional<String> title = getAnnotatedTitleForMethod(methodName);
if (title.isPresent()) {
return Formatter.issuesIn(title.get());
} else {
return Formatter.issuesIn(methodName);
}
}
private Optional<String> getAnnotatedIssue(String methodName) {
Optional<Method> testMethod = getMethodCalled(methodName);
if ((testMethod.isPresent()) && (testMethod.get().getAnnotation(Issue.class) != null)) {
return Optional.of(testMethod.get().getAnnotation(Issue.class).value());
}
return Optional.absent();
}
private String[] getAnnotatedIssues(String methodName) {
Optional<Method> testMethod = getMethodCalled(methodName);
if ((testMethod.isPresent()) && (testMethod.get().getAnnotation(Issues.class) != null)) {
return testMethod.get().getAnnotation(Issues.class).value();
}
return new String[]{};
}
/**
* Return a list of the issues mentioned in the Issue annotation of this method.
* @param methodName the name of the test method in the Java test class, if applicable.
* returns
*/
public Optional<String> getAnnotatedIssueForMethod(String methodName) {
return getAnnotatedIssue(methodName);
}
public String[] getAnnotatedIssuesForMethod(String methodName) {
return getAnnotatedIssues(methodName);
}
public String getAnnotatedIssueForTestCase(Class<?> testCase) {
Issue issueAnnotation = testCase.getAnnotation(Issue.class);
if (issueAnnotation != null) {
return issueAnnotation.value();
} else {
return null;
}
}
public String[] getAnnotatedIssuesForTestCase(Class<?> testCase) {
Issues issueAnnotation = testCase.getAnnotation(Issues.class);
if (issueAnnotation != null) {
return issueAnnotation.value();
} else {
return null;
}
}
public List<String> getIssuesForMethod(String methodName) {
List<String> issues = new ArrayList<String>();
if (testClass != null) {
addIssuesFromMethod(methodName, issues);
} else {
addIssuesFromTestScenarioName(methodName, issues);
}
return issues;
}
private void addIssuesFromTestScenarioName(String methodName, List<String> issues) {
issues.addAll(getAnnotatedIssuesForMethodTitle(methodName));
}
private void addIssuesFromMethod(String methodName, List<String> issues) {
if (getAnnotatedIssues(methodName) != null) {
issues.addAll(Arrays.asList(getAnnotatedIssues(methodName)));
}
if (getAnnotatedIssue(methodName).isPresent()) {
issues.add(getAnnotatedIssue(methodName).get());
}
if (getAnnotatedTitle(methodName) != null) {
addIssuesFromTestScenarioName(methodName, issues);
}
}
public List<TestTag> getTagsForMethod(String methodName) {
List<TestTag> allTags = new ArrayList<TestTag>(getTags());
allTags.addAll(getTagsFor(methodName));
return ImmutableList.copyOf(allTags);
}
public List<TestTag> getTags() {
List<TestTag> tags = new ArrayList<TestTag>();
if (testClass != null) {
addTagValues(tags, testClass.getAnnotation(WithTagValuesOf.class));
addTags(tags, testClass.getAnnotation(WithTags.class));
addTag(tags, testClass.getAnnotation(WithTag.class));
}
return tags;
}
private void addTag(List<TestTag> tags, WithTag tagAnnotation) {
if (tagAnnotation != null) {
tags.add(convertToTestTag(tagAnnotation));
}
}
private void addTags(List<TestTag> tags, WithTags tagSet) {
if (tagSet != null) {
tags.addAll(convert(tagSet.value(), toTestTags()));
}
}
private void addTagValues(List<TestTag> tags, WithTagValuesOf tagSet) {
if (tagSet != null) {
tags.addAll(convert(tagSet.value(), fromStringValuesToTestTags()));
}
}
private List<TestTag> getTagsFor(String methodName) {
List<TestTag> tags = new ArrayList<TestTag>();
Optional<Method> testMethod = getMethodCalled(methodName);
if (testMethod.isPresent()) {
addTagValues(tags, testMethod.get().getAnnotation(WithTagValuesOf.class));
addTags(tags, testMethod.get().getAnnotation(WithTags.class));
addTag(tags, testMethod.get().getAnnotation(WithTag.class));
}
return tags;
}
public TestTag convertToTestTag(WithTag withTag) {
if (StringUtils.isEmpty(withTag.value())) {
return TestTag.withName(withTag.name()).andType(withTag.type());
} else {
return TestTag.withValue(withTag.value());
}
}
private Converter<Object, TestTag> toTestTags() {
return new Converter<Object, TestTag>() {
public TestTag convert(Object tag) {
return convertToTestTag((WithTag) tag);
}
};
}
private Converter<Object, TestTag> fromStringValuesToTestTags() {
return new Converter<Object, TestTag>() {
public TestTag convert(Object tagValue) {
return TestTag.withValue((String) tagValue);
}
};
}
}