Skip to content

Commit 9dd042e

Browse files
authored
add links support (fixes #1, via #9)
1 parent 74bbf96 commit 9dd042e

File tree

20 files changed

+559
-134
lines changed

20 files changed

+559
-134
lines changed

allure-java-commons/build.gradle

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@ dependencies {
66
compile 'io.qameta.allure:allure2-model-api'
77
compile 'org.slf4j:slf4j-api'
88
compile 'org.aspectj:aspectjrt'
9+
10+
testCompile 'junit:junit'
11+
testCompile 'org.slf4j:slf4j-simple'
12+
testCompile 'org.assertj:assertj-core'
913
}

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

Lines changed: 0 additions & 30 deletions
This file was deleted.

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,17 @@
22

33
import java.lang.annotation.Documented;
44
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Inherited;
6+
import java.lang.annotation.Repeatable;
57
import java.lang.annotation.Retention;
68
import java.lang.annotation.RetentionPolicy;
79
import java.lang.annotation.Target;
810

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-
*/
1811
@Documented
12+
@Inherited
1913
@Retention(RetentionPolicy.RUNTIME)
2014
@Target({ElementType.METHOD, ElementType.TYPE})
15+
@Repeatable(Issues.class)
2116
public @interface Issue {
2217

2318
String value();

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,13 @@
22

33
import java.lang.annotation.Documented;
44
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Inherited;
56
import java.lang.annotation.Retention;
67
import java.lang.annotation.RetentionPolicy;
78
import java.lang.annotation.Target;
89

9-
/**
10-
* Use this annotation to link multiple issues from issue tracker to test cases and test suites. Usage:
11-
* <p/>
12-
* <pre>
13-
* &#064;Issues({
14-
* &#064;Issue("MYPROJECT-1"),
15-
* &#064;Issue("MYPROJECT-2")
16-
* })
17-
* public void myTest(){
18-
* ...
19-
* }
20-
* </pre>
21-
*
22-
* @author Dmitry Baev charlie@yandex-team.ru
23-
* Date: 01.08.14
24-
*/
2510
@Documented
11+
@Inherited
2612
@Retention(RetentionPolicy.RUNTIME)
2713
@Target({ElementType.METHOD, ElementType.TYPE})
2814
public @interface Issues {
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package io.qameta.allure;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Inherited;
6+
import java.lang.annotation.Repeatable;
7+
import java.lang.annotation.Retention;
8+
import java.lang.annotation.RetentionPolicy;
9+
import java.lang.annotation.Target;
10+
11+
/**
12+
* Use this annotation to add some links to results. Usage:
13+
* <pre>
14+
* &#064;Link("https://qameta.io")
15+
* public void myTest() {
16+
* ...
17+
* }
18+
* </pre>
19+
*/
20+
@Documented
21+
@Inherited
22+
@Retention(RetentionPolicy.RUNTIME)
23+
@Target({ElementType.METHOD, ElementType.TYPE})
24+
@Repeatable(Links.class)
25+
public @interface Link {
26+
27+
/**
28+
* Alias for {@link #name()}
29+
*/
30+
String value() default "";
31+
32+
/**
33+
* Name for link, by default url.
34+
*/
35+
String name() default "";
36+
37+
/**
38+
* Url for link. By default will search for system property `allure.link.{type}.pattern`, and use it
39+
* to generate url.
40+
*/
41+
String url() default "";
42+
43+
/**
44+
* This type is used for create an icon for link. Also there is few reserved types such as issue and tms.
45+
*/
46+
String type() default "custom";
47+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.qameta.allure;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Inherited;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
/**
11+
* @author charlie (Dmitry Baev).
12+
*/
13+
@Documented
14+
@Inherited
15+
@Retention(RetentionPolicy.RUNTIME)
16+
@Target({ElementType.METHOD, ElementType.TYPE})
17+
public @interface Links {
18+
19+
Link[] value();
20+
21+
}

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

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

3+
import io.qameta.allure.model.Link;
34
import io.qameta.allure.model.Status;
45
import io.qameta.allure.model.StatusDetails;
56
import org.slf4j.Logger;
@@ -28,12 +29,45 @@ public final class ResultsUtils {
2829
public static final String ALLURE_THREAD_NAME_SYSPROP = "allure.threadName";
2930
public static final String ALLURE_THREAD_NAME_ENV = "ALLURE_THREAD_NAME";
3031

32+
public static final String ISSUE_LINK_TYPE = "issue";
33+
public static final String TMS_LINK_TYPE = "tms";
34+
3135
private static String CACHED_HOST = null;
3236

3337
ResultsUtils() {
3438
throw new IllegalStateException("Do not instance");
3539
}
3640

41+
public static io.qameta.allure.model.Link createIssueLink(String value) {
42+
return createLink(value, null, null, ISSUE_LINK_TYPE);
43+
}
44+
45+
public static io.qameta.allure.model.Link createTmsLink(String value) {
46+
return createLink(value, null, null, TMS_LINK_TYPE);
47+
}
48+
49+
public static Link createLink(io.qameta.allure.Link link) {
50+
return createLink(link.value(), link.name(), link.url(), link.type());
51+
}
52+
53+
public static Link createLink(io.qameta.allure.Issue link) {
54+
return createIssueLink(link.value());
55+
}
56+
57+
public static Link createLink(io.qameta.allure.TmsLink link) {
58+
return createTmsLink(link.value());
59+
}
60+
61+
public static io.qameta.allure.model.Link createLink(String value, String name, String url, String type) {
62+
String resolvedName = firstNonEmpty(value).orElse(name);
63+
String resolvedUrl = firstNonEmpty(url)
64+
.orElseGet(() -> getLinkUrl(resolvedName, type));
65+
return new io.qameta.allure.model.Link()
66+
.withName(resolvedName)
67+
.withUrl(resolvedUrl)
68+
.withType(type);
69+
}
70+
3771
public static String getHostName() {
3872
String fromProperty = System.getProperty(ALLURE_HOST_NAME_SYSPROP);
3973
String fromEnv = System.getenv(ALLURE_HOST_NAME_ENV);
@@ -43,32 +77,13 @@ public static String getHostName() {
4377
.orElseGet(ResultsUtils::getRealHostName);
4478
}
4579

46-
private static String getRealHostName() {
47-
if (Objects.isNull(CACHED_HOST)) {
48-
try {
49-
CACHED_HOST = InetAddress.getLocalHost().getHostName();
50-
} catch (UnknownHostException e) { //NOSONAR
51-
LOGGER.debug("Could not get host name {}", e);
52-
CACHED_HOST = "default";
53-
}
54-
}
55-
return CACHED_HOST;
56-
}
57-
58-
public static String getTheadName() {
80+
public static String getThreadName() {
5981
String fromProperty = System.getProperty(ALLURE_THREAD_NAME_SYSPROP);
6082
String fromEnv = System.getenv(ALLURE_THREAD_NAME_ENV);
6183
return Stream.of(fromProperty, fromEnv)
6284
.filter(Objects::nonNull)
6385
.findFirst()
64-
.orElseGet(ResultsUtils::getRealTheadName);
65-
}
66-
67-
private static String getRealTheadName() {
68-
return String.format("%s.%s(%s)",
69-
ManagementFactory.getRuntimeMXBean().getName(),
70-
Thread.currentThread().getName(),
71-
Thread.currentThread().getId());
86+
.orElseGet(ResultsUtils::getRealThreadName);
7287
}
7388

7489
public static Optional<Status> getStatus(Throwable throwable) {
@@ -83,6 +98,44 @@ public static Optional<StatusDetails> getStatusDetails(Throwable e) {
8398
.withTrace(getStackTraceAsString(throwable)));
8499
}
85100

101+
public static Optional<String> firstNonEmpty(String... items) {
102+
return Stream.of(items)
103+
.filter(Objects::nonNull)
104+
.filter(item -> !item.isEmpty())
105+
.findFirst();
106+
}
107+
108+
public static String getLinkTypePatternPropertyName(String type) {
109+
return String.format("allure.link.%s.pattern", type);
110+
}
111+
112+
private static String getLinkUrl(String name, String type) {
113+
String pattern = System.getProperty(getLinkTypePatternPropertyName(type));
114+
if (Objects.isNull(pattern)) {
115+
return null;
116+
}
117+
return pattern.replaceAll("\\{}", Objects.isNull(name) ? "" : name);
118+
}
119+
120+
private static String getRealHostName() {
121+
if (Objects.isNull(CACHED_HOST)) {
122+
try {
123+
CACHED_HOST = InetAddress.getLocalHost().getHostName();
124+
} catch (UnknownHostException e) { //NOSONAR
125+
LOGGER.debug("Could not get host name {}", e);
126+
CACHED_HOST = "default";
127+
}
128+
}
129+
return CACHED_HOST;
130+
}
131+
132+
private static String getRealThreadName() {
133+
return String.format("%s.%s(%s)",
134+
ManagementFactory.getRuntimeMXBean().getName(),
135+
Thread.currentThread().getName(),
136+
Thread.currentThread().getId());
137+
}
138+
86139
private static String getStackTraceAsString(Throwable throwable) {
87140
StringWriter stringWriter = new StringWriter();
88141
throwable.printStackTrace(new PrintWriter(stringWriter));

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

Lines changed: 0 additions & 21 deletions
This file was deleted.
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package io.qameta.allure;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Inherited;
6+
import java.lang.annotation.Repeatable;
7+
import java.lang.annotation.Retention;
8+
import java.lang.annotation.RetentionPolicy;
9+
import java.lang.annotation.Target;
10+
11+
@Documented
12+
@Inherited
13+
@Retention(RetentionPolicy.RUNTIME)
14+
@Target({ElementType.METHOD, ElementType.TYPE})
15+
@Repeatable(TmsLinks.class)
16+
public @interface TmsLink {
17+
18+
String value();
19+
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.qameta.allure;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Inherited;
6+
import java.lang.annotation.Retention;
7+
import java.lang.annotation.RetentionPolicy;
8+
import java.lang.annotation.Target;
9+
10+
@Documented
11+
@Inherited
12+
@Retention(RetentionPolicy.RUNTIME)
13+
@Target({ElementType.METHOD, ElementType.TYPE})
14+
public @interface TmsLinks {
15+
16+
TmsLink[] value();
17+
18+
}

0 commit comments

Comments
 (0)