-
Notifications
You must be signed in to change notification settings - Fork 240
Expand file tree
/
Copy pathAllureAspectJ.java
More file actions
196 lines (172 loc) · 6.38 KB
/
Copy pathAllureAspectJ.java
File metadata and controls
196 lines (172 loc) · 6.38 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
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
/*
* Copyright 2016-2026 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 org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.assertj.core.api.AbstractAssert;
import java.util.function.Supplier;
/**
* Captures user-side AssertJ factories and fluent calls, then delegates assertion-chain state
* to {@link AssertJRecorder}.
*
*/
@SuppressWarnings("all")
@Aspect
public class AllureAspectJ {
private static final ThreadLocal<AssertJRecorder> RECORDER = ThreadLocal.withInitial(AssertJRecorder::new);
private static final ThreadLocal<Boolean> RECORDING_MUTED = ThreadLocal.withInitial(() -> false);
@Pointcut(
"("
+ "call(public static * org.assertj.core.api.Assertions*.assertThat*(..))"
+ " || call(public static * org.assertj.core.api.BDDAssertions*.then*(..))"
+ " || call(public * org.assertj.core.api.*SoftAssertionsProvider+.assertThat*(..))"
+ " || call(public * org.assertj.core.api.*SoftAssertionsProvider+.then*(..))"
+ ")"
)
/**
* Handles the assert factory call callback.
*/
public void assertFactoryCall() {
//pointcut body, should be empty
}
@Pointcut(
"("
+ "call(public * org.assertj.core.api.AbstractAssert+.*(..))"
+ " || call(public * org.assertj.core.api.Assert+.*(..))"
+ " || call(public * org.assertj.core.api.Descriptable+.*(..))"
+ ")"
+ " && target(assertion)"
)
/**
* Handles the assert operation call callback.
*
* @param assertion the assertion
*/
public void assertOperationCall(final AbstractAssert<?, ?> assertion) {
//pointcut body, should be empty
}
/**
* Handles the user code call callback.
*/
@Pointcut("!within(org.assertj..*) && !within(io.qameta.allure.assertj.AllureAspectJ)")
public void userCodeCall() {
//pointcut body, should be empty
}
@AfterReturning(
pointcut = "assertFactoryCall() && userCodeCall()",
returning = "result"
)
/**
* Handles the log assert creation callback.
*
* @param joinPoint the join point
* @param result the model object or framework result to process
*/
public void logAssertCreation(final JoinPoint joinPoint, final Object result) {
if (isRecordingMuted() || !(result instanceof AbstractAssert)) {
return;
}
final AbstractAssert<?, ?> assertion = (AbstractAssert<?, ?>) result;
getRecorder().assertionCreated(getLifecycle(), assertion, firstArgumentOf(joinPoint));
}
/**
* Returns the log assert operation.
*
* @param joinPoint the join point
* @param assertion the assertion
* @return the log assert operation
* @throws Throwable if the underlying framework operation fails
*/
@Around("assertOperationCall(assertion) && userCodeCall()")
public Object logAssertOperation(final ProceedingJoinPoint joinPoint,
final AbstractAssert<?, ?> assertion)
throws Throwable {
final String methodName = getMethodName(joinPoint);
if (isRecordingMuted() || getRecorder().isIgnored(methodName)) {
return joinPoint.proceed();
}
final AssertJOperation operation = getRecorder().startOperation(
getLifecycle(),
assertion,
methodName,
joinPoint.getArgs()
);
try {
final Object result = joinPoint.proceed();
getRecorder().operationPassed(operation, result);
return result;
} catch (Throwable throwable) {
getRecorder().operationFailed(operation, throwable);
throw throwable;
}
}
@After(
"execution(public void org.assertj.core.api.DefaultAssertionErrorCollector.collectAssertionError("
+ "java.lang.AssertionError)) && args(error)"
)
/**
* Handles the soft assertion failed callback.
*
* @param error the error reported by the framework
*/
public void softAssertionFailed(final AssertionError error) {
getRecorder().softAssertionFailed(error);
}
/**
* Returns the lifecycle.
*
* @return the Allure lifecycle used by this integration
*/
public static AllureLifecycle getLifecycle() {
return Allure.getLifecycle();
}
/**
* Drops the calling thread's recorder state. Invoked by {@link AssertJLifecycleListener} at test boundaries so
* chain bookkeeping never accumulates across tests.
*/
public static void clearContext() {
RECORDER.remove();
}
static <T> T withoutRecording(final Supplier<T> supplier) {
final boolean previous = RECORDING_MUTED.get();
RECORDING_MUTED.set(true);
try {
return supplier.get();
} finally {
RECORDING_MUTED.set(previous);
}
}
private static AssertJRecorder getRecorder() {
return RECORDER.get();
}
private static boolean isRecordingMuted() {
return RECORDING_MUTED.get();
}
private static Object firstArgumentOf(final JoinPoint joinPoint) {
return joinPoint.getArgs().length == 0 ? null : joinPoint.getArgs()[0];
}
private static String getMethodName(final ProceedingJoinPoint joinPoint) {
return ((MethodSignature) joinPoint.getSignature()).getMethod().getName();
}
}