-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathAllureAspectJ.java
More file actions
159 lines (134 loc) · 5.43 KB
/
AllureAspectJ.java
File metadata and controls
159 lines (134 loc) · 5.43 KB
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
/*
* Copyright 2016-2024 Qameta Software Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.qameta.allure.assertj;
import io.qameta.allure.Allure;
import io.qameta.allure.AllureLifecycle;
import io.qameta.allure.model.Status;
import io.qameta.allure.model.StepResult;
import io.qameta.allure.util.ObjectUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static io.qameta.allure.util.ResultsUtils.getStatus;
import static io.qameta.allure.util.ResultsUtils.getStatusDetails;
/**
* @author charlie (Dmitry Baev).
* @author sskorol (Sergey Korol).
*/
@SuppressWarnings("all")
@Aspect
public class AllureAspectJ {
private static final Logger LOGGER = LoggerFactory.getLogger(AllureAspectJ.class);
private static InheritableThreadLocal<AllureLifecycle> lifecycle = new InheritableThreadLocal<AllureLifecycle>() {
@Override
protected AllureLifecycle initialValue() {
return Allure.getLifecycle();
}
};
@Pointcut("execution(!private org.assertj.core.api.AbstractAssert.new(..))")
public void anyAssertCreation() {
//pointcut body, should be empty
}
@Pointcut("execution(* org.assertj.core.api.AssertJProxySetup.*(..))")
public void proxyMethod() {
//pointcut body, should be empty
}
@Pointcut("execution(public * org.assertj.core.api.AbstractAssert+.*(..)) && !proxyMethod()")
public void anyAssert() {
//pointcut body, should be empty
}
@After("anyAssertCreation()")
public void logAssertCreation(final JoinPoint joinPoint) {
final String actual = joinPoint.getArgs().length > 0
? ObjectUtils.toString(joinPoint.getArgs()[0])
: "<?>";
final String uuid = UUID.randomUUID().toString();
final String name = String.format("assertThat \'%s\'", actual);
final StepResult result = new StepResult()
.setName(name)
.setStatus(Status.PASSED);
getLifecycle().startStep(uuid, result);
getLifecycle().stopStep(uuid);
}
@Before("anyAssert()")
public void stepStart(final JoinPoint joinPoint) {
final String uuid = UUID.randomUUID().toString();
final String name = getStepName(joinPoint);
if (isStartBubbling(name)) return;
final StepResult result = new StepResult()
.setName(name);
getLifecycle().startStep(uuid, result);
}
@AfterThrowing(pointcut = "anyAssert()", throwing = "e")
public void stepFailed(final JoinPoint joinPoin, final Throwable e) {
final String name = getStepName(joinPoin);
if (isEndBubbling(name)) return;
getLifecycle().updateStep(s -> s
.setStatus(getStatus(e).orElse(Status.BROKEN))
.setStatusDetails(getStatusDetails(e).orElse(null)));
getLifecycle().stopStep();
}
@AfterReturning(pointcut = "anyAssert()")
public void stepStop(final JoinPoint joinPoin) {
final String name = getStepName(joinPoin);
if (isEndBubbling(name)) return;
getLifecycle().updateStep(s -> s.setStatus(Status.PASSED));
getLifecycle().stopStep();
}
/**
* For tests only.
*
* @param allure allure lifecycle to set.
*/
public static void setLifecycle(final AllureLifecycle allure) {
lifecycle.set(allure);
}
public static AllureLifecycle getLifecycle() {
return lifecycle.get();
}
private static String arrayToString(final Object... array) {
return Stream.of(array)
.map(ObjectUtils::toString)
.collect(Collectors.joining(" "));
}
private String getStepName(JoinPoint joinPoint) {
return joinPoint.getArgs().length > 0
? String.format("%s \'%s\'", joinPoint.getSignature().getName(), arrayToString(joinPoint.getArgs()))
: joinPoint.getSignature().getName();
}
private boolean isStartBubbling(String stepName) {
final Optional<StepResult> lastResult = getLifecycle().getCurrentStepResult();
if (!lastResult.isPresent()) return false;
if (lastResult.get().getStop() != null) return false;
return stepName.equals(lastResult.get().getName());
}
private boolean isEndBubbling(String stepName) {
final Optional<StepResult> lastResult = getLifecycle().getCurrentStepResult();
if (!lastResult.isPresent()) return true;
if (lastResult.get().getStop() != null) return true;
return !stepName.equals(lastResult.get().getName());
}
}