Skip to content

Commit 69da339

Browse files
committed
rft lifecycle singleton
1 parent 8225ead commit 69da339

File tree

8 files changed

+49
-21
lines changed

8 files changed

+49
-21
lines changed

allure-java-commons/src/main/java/io/qameta/allure/Allure.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,26 @@
77

88
import java.io.InputStream;
99
import java.nio.charset.StandardCharsets;
10+
import java.util.Objects;
1011

1112
/**
1213
* @author charlie (Dmitry Baev).
1314
*/
1415
public final class Allure {
1516

16-
private static AllureLifecycle lifecycle = AllureLifecycle.INSTANCE;
17+
private static AllureLifecycle lifecycle = null;
1718

1819
Allure() {
1920
throw new IllegalStateException("Do not instance");
2021
}
2122

23+
public static AllureLifecycle getLifecycle() {
24+
if (Objects.isNull(lifecycle)) {
25+
lifecycle = new AllureLifecycle();
26+
}
27+
return lifecycle;
28+
}
29+
2230
public static void addStep(String name) {
2331
lifecycle.addStep(new StepResult()
2432
.withName(name)

allure-java-commons/src/main/java/io/qameta/allure/AllureLifecycle.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ public class AllureLifecycle {
3434

3535
private static final Logger LOGGER = LoggerFactory.getLogger(AllureLifecycle.class);
3636

37-
public static final AllureLifecycle INSTANCE = new AllureLifecycle();
38-
3937
private final Map<String, Object> storage = new ConcurrentHashMap<>();
4038

4139
private final ThreadLocal<LinkedList<String>> currentStepContext =

allure-java-commons/src/main/java/io/qameta/allure/aspects/AttachmentsAspects.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.qameta.allure.aspects;
22

3+
import io.qameta.allure.Allure;
34
import io.qameta.allure.AllureLifecycle;
45
import io.qameta.allure.Attachment;
56
import org.aspectj.lang.JoinPoint;
@@ -9,6 +10,7 @@
910
import org.aspectj.lang.reflect.MethodSignature;
1011

1112
import java.nio.charset.StandardCharsets;
13+
import java.util.Objects;
1214

1315
/**
1416
* Aspects (AspectJ) for handling {@link Attachment}.
@@ -19,7 +21,18 @@
1921
@Aspect
2022
public class AttachmentsAspects {
2123

22-
private static AllureLifecycle ALLURE = AllureLifecycle.INSTANCE;
24+
private static AllureLifecycle lifecycle = null;
25+
26+
public static AllureLifecycle getLifecycle() {
27+
if (Objects.isNull(lifecycle)) {
28+
lifecycle = Allure.getLifecycle();
29+
}
30+
return lifecycle;
31+
}
32+
33+
public static void setLifecycle(AllureLifecycle lifecycle) {
34+
AttachmentsAspects.lifecycle = lifecycle;
35+
}
2336

2437
/**
2538
* Pointcut for things annotated with {@link Attachment}
@@ -48,6 +61,6 @@ public void attachment(JoinPoint joinPoint, Object result) {
4861
.getAnnotation(Attachment.class);
4962
byte[] bytes = (result instanceof byte[]) ? (byte[]) result : result.toString()
5063
.getBytes(StandardCharsets.UTF_8);
51-
ALLURE.addAttachment(attachment.value(), attachment.type(), attachment.fileExtension(), bytes);
64+
lifecycle.addAttachment(attachment.value(), attachment.type(), attachment.fileExtension(), bytes);
5265
}
5366
}

allure-java-commons/src/main/java/io/qameta/allure/aspects/StepsAspects.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.qameta.allure.aspects;
22

3+
import io.qameta.allure.Allure;
34
import io.qameta.allure.AllureLifecycle;
45
import io.qameta.allure.Step;
56
import io.qameta.allure.model.Parameter;
@@ -28,7 +29,7 @@
2829
@Aspect
2930
public class StepsAspects {
3031

31-
private static AllureLifecycle ALLURE = AllureLifecycle.INSTANCE;
32+
private static AllureLifecycle lifecycle = null;
3233

3334
@Pointcut("@annotation(io.qameta.allure.Step)")
3435
public void withStepAnnotation() {
@@ -47,28 +48,35 @@ public void stepStart(JoinPoint joinPoint) {
4748
StepResult result = new StepResult()
4849
.withName(getName(methodSignature))
4950
.withParameters(getParameters(methodSignature, joinPoint.getArgs()));
50-
ALLURE.startStep(uuid, result);
51+
getLifecycle().startStep(uuid, result);
5152
}
5253

5354
@AfterThrowing(pointcut = "anyMethod() && withStepAnnotation()", throwing = "e")
5455
public void stepFailed(Throwable e) {
55-
ALLURE.updateStep(result -> result
56+
getLifecycle().updateStep(result -> result
5657
.withStatus(getStatus(e).orElse(Status.BROKEN))
5758
.withStatusDetails(getStatusDetails(e).orElse(null)));
58-
ALLURE.stopStep();
59+
getLifecycle().stopStep();
5960
}
6061

6162
@AfterReturning("anyMethod() && withStepAnnotation()")
6263
public void stepStop() {
63-
ALLURE.updateStep(step -> step.withStatus(Status.PASSED));
64-
ALLURE.stopStep();
64+
getLifecycle().updateStep(step -> step.withStatus(Status.PASSED));
65+
getLifecycle().stopStep();
6566
}
6667

6768
/**
6869
* For tests only
6970
*/
70-
public static void setAllure(AllureLifecycle allure) {
71-
StepsAspects.ALLURE = allure;
71+
public static void setLifecycle(AllureLifecycle allure) {
72+
lifecycle = allure;
73+
}
74+
75+
public static AllureLifecycle getLifecycle() {
76+
if (Objects.isNull(lifecycle)) {
77+
lifecycle = Allure.getLifecycle();
78+
}
79+
return lifecycle;
7280
}
7381

7482
private static Parameter[] getParameters(MethodSignature signature, Object... args) {

allure-junit4/src/main/java/io/qameta/allure/junit4/AllureJunit4.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.qameta.allure.junit4;
22

3-
import io.qameta.allure.AllureLifecycle;
3+
import io.qameta.allure.Allure;
44
import io.qameta.allure.model.Label;
55
import io.qameta.allure.model.TestResult;
66
import io.qameta.allure.model.TestResultContainer;
@@ -46,7 +46,7 @@ public void testStarted(Description description) throws Exception {
4646
String id = getHistoryId(description);
4747
String testGroupId = getSuiteUuid(description.getClassName());
4848

49-
AllureLifecycle.INSTANCE.scheduleTestCase(testGroupId, new TestResult()
49+
Allure.getLifecycle().scheduleTestCase(testGroupId, new TestResult()
5050
.withUuid(uuid)
5151
.withHistoryId(id)
5252
.withName(description.getMethodName())
@@ -80,11 +80,11 @@ public void testIgnored(Description description) throws Exception {
8080
private void createTestGroups(String parentUuid, List<Description> descriptions) {
8181
descriptions.forEach(description -> {
8282
String uuid = getSuiteUuid(description.getClassName());
83-
AllureLifecycle.INSTANCE.startTestContainer(parentUuid, new TestResultContainer()
83+
Allure.getLifecycle().startTestContainer(parentUuid, new TestResultContainer()
8484
.withUuid(uuid)
8585
.withName(description.getDisplayName())
8686
);
87-
AllureLifecycle.INSTANCE.writeTestContainer(uuid);
87+
Allure.getLifecycle().writeTestContainer(uuid);
8888

8989
description.getChildren().forEach(child ->
9090
createTestGroups(uuid, description.getChildren())

allure-testng/src/main/java/io/qameta/allure/testng/AllureTestNg.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.qameta.allure.testng;
22

3+
import io.qameta.allure.Allure;
34
import io.qameta.allure.AllureLifecycle;
45
import io.qameta.allure.Epic;
56
import io.qameta.allure.Feature;
@@ -93,7 +94,7 @@ public AllureTestNg(AllureLifecycle lifecycle) {
9394
}
9495

9596
public AllureTestNg() {
96-
this.lifecycle = AllureLifecycle.INSTANCE;
97+
this.lifecycle = Allure.getLifecycle();
9798
}
9899

99100
public AllureLifecycle getLifecycle() {

allure-testng/src/test/java/io/qameta/allure/test/FeatureCombinationsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
/**
3434
* @author Egor Borisov [email protected]
3535
*/
36-
@Test(testName = "Supported TestNG features")
36+
@Test
3737
public class FeatureCombinationsTest {
3838

3939
private static final Condition<List<? extends ExecutableItem>> ALL_FINISHED = new Condition<>(items ->
@@ -51,7 +51,7 @@ public class FeatureCombinationsTest {
5151
public void prepare() {
5252
results = new AllureResultsWriterStub();
5353
final AllureLifecycle lifecycle = new AllureLifecycle(results);
54-
StepsAspects.setAllure(lifecycle);
54+
StepsAspects.setLifecycle(lifecycle);
5555
AllureTestNg adapter = new AllureTestNg(lifecycle);
5656
testNg = new TestNG(false);
5757
testNg.addListener((ITestNGListener) adapter);

allure-testng/src/test/java/io/qameta/allure/test/ListenerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
/**
3434
* @author Egor Borisov [email protected]
3535
*/
36-
@Test(testName = "Listener tests")
36+
@Test
3737
public class ListenerTest {
3838

3939
private static final String STRING_PARAMETER = "String parameter";

0 commit comments

Comments
 (0)