Skip to content

Commit ed22615

Browse files
committed
init commit
0 parents  commit ed22615

49 files changed

Lines changed: 2451 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.gradle
2+
schema
3+
build
4+
5+
#IDEA Files
6+
.idea
7+
*.iml
8+
*.ipr
9+
10+
#Eclipse files
11+
*.settings
12+
*.classpath
13+
*.project
14+
*.pydevproject
15+
16+
#Mac OS stuff
17+
.DS_Store

AUTHORS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The following authors have created the source code of "Allure Integrations"
2+
published and distributed by Dmitry Baev as the owner:
3+
4+
* Dmitry Baev <[email protected]>

LICENSE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(C) Dmitry Baev, 2016

allure-java-commons/build.gradle

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
description = 'Allure Java Commons'
2+
3+
apply plugin: 'java'
4+
5+
dependencies {
6+
compile "io.qameta.allure:model-v2:$allureModelV2Version"
7+
compile 'com.google.guava:guava'
8+
compile 'org.aspectj:aspectjrt'
9+
}
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
package io.qameta.allure;
2+
3+
import io.qameta.allure.model.Status;
4+
import io.qameta.allure.model.Attachment;
5+
import io.qameta.allure.model.ExecutableItem;
6+
import io.qameta.allure.model.StatusDetails;
7+
import io.qameta.allure.model.TestAfterResult;
8+
import io.qameta.allure.model.TestBeforeResult;
9+
import io.qameta.allure.model.TestCaseResult;
10+
import io.qameta.allure.model.TestGroupResult;
11+
import io.qameta.allure.model.TestStepResult;
12+
import io.qameta.allure.model.WithAfter;
13+
import io.qameta.allure.model.WithAttachments;
14+
import io.qameta.allure.model.WithBefore;
15+
import io.qameta.allure.model.WithSteps;
16+
import org.slf4j.Logger;
17+
import org.slf4j.LoggerFactory;
18+
19+
import java.nio.file.Path;
20+
import java.nio.file.Paths;
21+
import java.util.HashMap;
22+
import java.util.LinkedList;
23+
import java.util.Map;
24+
import java.util.Objects;
25+
import java.util.function.Consumer;
26+
27+
import static io.qameta.allure.AllureUtils.write;
28+
import static io.qameta.allure.AllureUtils.writeAttachment;
29+
30+
/**
31+
* @author charlie (Dmitry Baev).
32+
*/
33+
public class Allure {
34+
35+
private static final Logger LOGGER = LoggerFactory.getLogger(Allure.class);
36+
37+
private static final Path OUTPUT = Paths.get("target/allure-results");
38+
39+
private final Map<String, Object> storage = new HashMap<>();
40+
41+
private final ThreadLocal<LinkedList<String>> currentStepContext =
42+
InheritableThreadLocal.withInitial(LinkedList::new);
43+
44+
public static final Allure LIFECYCLE = new Allure();
45+
46+
public void startTestGroup(String uuid, TestGroupResult result) {
47+
LOGGER.info("Start test group {}", uuid);
48+
put(uuid, result).withStart(System.currentTimeMillis());
49+
}
50+
51+
public void updateTestGroup(String uuid, Consumer<TestGroupResult> update) {
52+
LOGGER.info("Update test group {}", uuid);
53+
update.accept(get(uuid, TestGroupResult.class));
54+
}
55+
56+
public void stopTestGroup(String uuid) {
57+
LOGGER.info("Stop test group {}", uuid);
58+
TestGroupResult result = remove(uuid, TestGroupResult.class)
59+
.withStop(System.currentTimeMillis());
60+
write(result, OUTPUT);
61+
}
62+
63+
public void startTestBefore(String parentUuid, String uuid, TestBeforeResult result) {
64+
LOGGER.info("Start test before {} with parent {}", uuid, parentUuid);
65+
put(uuid, result).withStart(System.currentTimeMillis());
66+
get(parentUuid, WithBefore.class).getBefores().add(result);
67+
currentStepContext.remove();
68+
currentStepContext.get().push(uuid);
69+
}
70+
71+
public void stopTestBefore() {
72+
stopTestBefore(currentStepContext.get().getLast());
73+
}
74+
75+
public void stopTestBefore(String uuid) {
76+
LOGGER.info("Stop test before {}", uuid);
77+
currentStepContext.remove();
78+
remove(uuid, TestBeforeResult.class).withStop(System.currentTimeMillis());
79+
}
80+
81+
public void startTestAfter(String parentUuid, String uuid, TestAfterResult result) {
82+
LOGGER.info("Start test after {} with parent {}", uuid, parentUuid);
83+
put(uuid, result).withStart(System.currentTimeMillis());
84+
get(parentUuid, WithAfter.class).getAfters().add(result);
85+
currentStepContext.remove();
86+
currentStepContext.get().push(uuid);
87+
}
88+
89+
public void stopTestAfter(String uuid) {
90+
LOGGER.info("Stop test after {}", uuid);
91+
currentStepContext.remove();
92+
remove(uuid, TestAfterResult.class).withStop(System.currentTimeMillis());
93+
}
94+
95+
public void scheduleTestCase(String uuid, TestCaseResult result) {
96+
LOGGER.info("Schedule test case {}", uuid);
97+
put(uuid, result);
98+
}
99+
100+
public void cancelTestCase(String uuid, StatusDetails details) {
101+
LOGGER.info("Cancel test case {}", uuid);
102+
long currentTimeMillis = System.currentTimeMillis();
103+
get(uuid, TestCaseResult.class)
104+
.withStatus(Status.CANCELED)
105+
.withStatusDetails(details)
106+
.withStart(currentTimeMillis)
107+
.withStop(currentTimeMillis);
108+
}
109+
110+
public void startTestCase(String uuid) {
111+
LOGGER.info("Start test case {}", uuid);
112+
get(uuid, TestCaseResult.class).setStart(System.currentTimeMillis());
113+
currentStepContext.remove();
114+
currentStepContext.get().push(uuid);
115+
}
116+
117+
public void updateTestCase(Consumer<TestCaseResult> update) {
118+
updateTestCase(currentStepContext.get().getLast(), update);
119+
}
120+
121+
public void updateTestCase(String uuid, Consumer<TestCaseResult> update) {
122+
LOGGER.info("Update test case {}", uuid);
123+
update.accept(get(uuid, TestCaseResult.class));
124+
}
125+
126+
public void stopTestCase() {
127+
stopTestCase(currentStepContext.get().getLast());
128+
}
129+
130+
public void stopTestCase(String uuid) {
131+
LOGGER.info("Stop test case {}", uuid);
132+
currentStepContext.remove();
133+
get(uuid, TestCaseResult.class).setStop(System.currentTimeMillis());
134+
}
135+
136+
public void closeTestCase(String uuid) {
137+
LOGGER.info("Close test case {}", uuid);
138+
write(remove(uuid, TestCaseResult.class), OUTPUT);
139+
}
140+
141+
public void addAttachment(byte[] content, String name, String type) {
142+
String uuid = currentStepContext.get().getLast();
143+
LOGGER.info("Adding attachment for {}", uuid);
144+
Attachment attachment = writeAttachment(content, name, type, OUTPUT);
145+
get(uuid, WithAttachments.class).getAttachments().add(attachment);
146+
}
147+
148+
public void startStep(String uuid, TestStepResult result) {
149+
LinkedList<String> uuids = currentStepContext.get();
150+
startStep(uuids.isEmpty() ? null : uuids.getFirst(), uuid, result);
151+
}
152+
153+
public void startStep(String parentUuid, String uuid, TestStepResult result) {
154+
LOGGER.info("Start step {} with parent {}", uuid, parentUuid);
155+
put(uuid, result).withStart(System.currentTimeMillis());
156+
currentStepContext.get().push(uuid);
157+
158+
if (Objects.nonNull(parentUuid)) {
159+
get(parentUuid, WithSteps.class).getSteps().add(result);
160+
}
161+
}
162+
163+
public void updateStep(Consumer<TestStepResult> update) {
164+
updateStep(currentStepContext.get().getFirst(), update);
165+
}
166+
167+
public void updateStep(String uuid, Consumer<TestStepResult> update) {
168+
LOGGER.info("Update step {}", uuid);
169+
update.accept(get(uuid, TestStepResult.class));
170+
}
171+
172+
public void stopStep() {
173+
stopStep(currentStepContext.get().getFirst());
174+
}
175+
176+
public void stopStep(String uuid) {
177+
LOGGER.info("Stop step {}", uuid);
178+
remove(uuid, TestStepResult.class).withStop(System.currentTimeMillis());
179+
currentStepContext.get().pop();
180+
}
181+
182+
private <T> T put(String uuid, T item) {
183+
Objects.requireNonNull(uuid, "Uuid can't be null");
184+
storage.put(uuid, item);
185+
return item;
186+
}
187+
188+
private <T> T get(String uuid, Class<T> clazz) {
189+
Objects.requireNonNull(uuid, "Uuid can't be null");
190+
T result = cast(storage.computeIfAbsent(uuid, u -> new TestCaseResult().withName("Unknown")), clazz);
191+
return Objects.requireNonNull(result, "Could not find " + clazz + " by uuid " + uuid);
192+
}
193+
194+
private <T> T remove(String uuid, Class<T> clazz) {
195+
Objects.requireNonNull(uuid, "Uuid can't be null");
196+
T result = cast(storage.remove(uuid), clazz);
197+
return Objects.requireNonNull(result, "Could not find " + clazz + " by uuid " + uuid);
198+
}
199+
200+
private <T> T cast(Object obj, Class<T> clazz) {
201+
if (clazz.isInstance(obj)) {
202+
return clazz.cast(obj);
203+
}
204+
throw new IllegalStateException("Can not cast " + obj + " to " + clazz);
205+
}
206+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package io.qameta.allure;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
/**
10+
* A file with additional information captured during a test such
11+
* as log, screenshot, log file, dump, server response and so on.
12+
* When some test fails attachments help to understand the reason
13+
* of failure faster.
14+
* <p/>
15+
* An attachment is simply a method annotated with
16+
* {@link Attachment} returns either String or byte array
17+
* which should be added to report:
18+
* <p/>
19+
* <pre>
20+
* &#064;Attachment(value = "Page screenshot", type = "image/png")
21+
* public byte[] saveScreenshot(byte[] screenShot) {
22+
* return screenShot;
23+
* }
24+
* </pre>
25+
*
26+
* @author Dmitry Baev [email protected]
27+
* Date: 15.05.14
28+
* @since 1.4.0
29+
*/
30+
@Documented
31+
@Retention(RetentionPolicy.RUNTIME)
32+
@Target(ElementType.METHOD)
33+
public @interface Attachment {
34+
35+
/**
36+
* Attachment name
37+
*/
38+
String value() default "{method}";
39+
40+
/**
41+
* Valid attachment MimeType, for example "text/plain" or "application/json"
42+
*/
43+
String type() default "";
44+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.qameta.allure;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
/**
10+
* Using this annotation you can add some text description
11+
* to test suite or test case.
12+
* <pre>
13+
* &#064;Test
14+
* &#064;Description("This is an example of my test")
15+
* public void myTest() throws Exception {
16+
* ...
17+
* }
18+
* </pre>
19+
*
20+
* @author Dmitry Baev [email protected]
21+
* Date: 10.24.13
22+
*/
23+
@Documented
24+
@Retention(RetentionPolicy.RUNTIME)
25+
@Target({ElementType.METHOD, ElementType.TYPE})
26+
public @interface Description {
27+
28+
String value();
29+
30+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package io.qameta.allure;
2+
3+
import java.lang.annotation.*;
4+
5+
/**
6+
* In order to group your tests by features simply annotate test suite
7+
* or test case with {@link Features}
8+
* annotation. This annotation can take either one string or a string
9+
* array because one test case can relate to several features:
10+
* <p/>
11+
* <pre>
12+
* &#064;Features({"Feature1", "Feature2"})
13+
* &#064;Test
14+
* public void myTest() {
15+
* ...
16+
* }
17+
*
18+
* &#064;Features("Feature")
19+
* &#064;Test
20+
* public void myTest() {
21+
* ...
22+
* }
23+
* </pre>
24+
* @author Dmitry Baev [email protected]
25+
* Date: 25.12.13
26+
*/
27+
@Documented
28+
@Retention(RetentionPolicy.RUNTIME)
29+
@Target({ElementType.METHOD, ElementType.TYPE})
30+
public @interface Features {
31+
32+
String[] value();
33+
34+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package io.qameta.allure;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
/**
10+
* Use this annotation to link a single issue from issue tracker to test cases and test suites. Usage:
11+
* <pre>
12+
* &#064;Issue("MYPROJECT-1")
13+
* public void myTest() {
14+
* ...
15+
* }
16+
* </pre>
17+
*/
18+
@Documented
19+
@Retention(RetentionPolicy.RUNTIME)
20+
@Target({ElementType.METHOD, ElementType.TYPE})
21+
public @interface Issue {
22+
23+
String value();
24+
25+
}

0 commit comments

Comments
 (0)