Skip to content

Commit c0f4ce8

Browse files
committed
add source quality control
1 parent 648a6ca commit c0f4ce8

File tree

33 files changed

+381
-294
lines changed

33 files changed

+381
-294
lines changed

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,16 @@
1010
import java.util.Objects;
1111

1212
/**
13-
* @author charlie (Dmitry Baev).
13+
* The class contains some useful methods to work with {@link AllureLifecycle}.
1414
*/
1515
public final class Allure {
1616

17-
private static AllureLifecycle lifecycle = null;
17+
private static final String TXT_EXTENSION = ".txt";
18+
private static final String TEXT_PLAIN = "text/plain";
1819

19-
Allure() {
20+
private static AllureLifecycle lifecycle;
21+
22+
private Allure() {
2023
throw new IllegalStateException("Do not instance");
2124
}
2225

@@ -27,7 +30,7 @@ public static AllureLifecycle getLifecycle() {
2730
return lifecycle;
2831
}
2932

30-
public static void addStep(String name) {
33+
public static void addStep(final String name) {
3134
lifecycle.addStep(new StepResult()
3235
.withName(name)
3336
.withStart(System.currentTimeMillis())
@@ -37,7 +40,7 @@ public static void addStep(String name) {
3740
);
3841
}
3942

40-
public static void addStep(String name, Status status, StatusDetails statusDetails) {
43+
public static void addStep(final String name, final Status status, final StatusDetails statusDetails) {
4144
lifecycle.addStep(new StepResult()
4245
.withName(name)
4346
.withStart(System.currentTimeMillis())
@@ -48,27 +51,31 @@ public static void addStep(String name, Status status, StatusDetails statusDetai
4851
);
4952
}
5053

51-
public static void addAttachment(String name, String content) {
52-
lifecycle.addAttachment(name, "text/plain", ".txt", content.getBytes(StandardCharsets.UTF_8));
54+
public static void addAttachment(final String name, final String content) {
55+
lifecycle.addAttachment(name, TEXT_PLAIN, TXT_EXTENSION, content.getBytes(StandardCharsets.UTF_8));
5356
}
5457

55-
public static void addAttachment(String name, String type, String content) {
56-
lifecycle.addAttachment(name, type, ".txt", content.getBytes(StandardCharsets.UTF_8));
58+
public static void addAttachment(final String name, final String type, final String content) {
59+
lifecycle.addAttachment(name, type, TXT_EXTENSION, content.getBytes(StandardCharsets.UTF_8));
5760
}
5861

59-
public static void addAttachment(String name, String type, String content, String fileExtension) {
62+
@SuppressWarnings("PMD.UseObjectForClearerAPI")
63+
public static void addAttachment(final String name, final String type,
64+
final String content, final String fileExtension) {
6065
lifecycle.addAttachment(name, type, fileExtension, content.getBytes(StandardCharsets.UTF_8));
6166
}
6267

63-
public static void addAttachment(String name, InputStream content) {
68+
public static void addAttachment(final String name, final InputStream content) {
6469
lifecycle.addAttachment(name, null, null, content);
6570
}
6671

67-
public static void addAttachment(String name, String type, InputStream content, String fileExtension) {
72+
@SuppressWarnings("PMD.UseObjectForClearerAPI")
73+
public static void addAttachment(final String name, final String type,
74+
final InputStream content, final String fileExtension) {
6875
lifecycle.addAttachment(name, type, fileExtension, content);
6976
}
7077

71-
public static void setLifecycle(AllureLifecycle lifecycle) {
78+
public static void setLifecycle(final AllureLifecycle lifecycle) {
7279
Allure.lifecycle = lifecycle;
7380
}
7481
}

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

Lines changed: 57 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,10 @@
2626

2727
import static io.qameta.allure.AllureConstants.ATTACHMENT_FILE_SUFFIX;
2828

29-
3029
/**
31-
* @author charlie (Dmitry Baev).
30+
* The class contains Allure context and methods to change it.
3231
*/
32+
@SuppressWarnings("PMD.TooManyMethods")
3333
public class AllureLifecycle {
3434

3535
private static final Logger LOGGER = LoggerFactory.getLogger(AllureLifecycle.class);
@@ -39,9 +39,9 @@ public class AllureLifecycle {
3939
private final ThreadLocal<LinkedList<String>> currentStepContext =
4040
InheritableThreadLocal.withInitial(LinkedList::new);
4141

42-
private AllureResultsWriter writer;
42+
private final AllureResultsWriter writer;
4343

44-
public AllureLifecycle(AllureResultsWriter writer) {
44+
public AllureLifecycle(final AllureResultsWriter writer) {
4545
this.writer = writer;
4646
}
4747

@@ -54,82 +54,82 @@ private static FileSystemResultsWriter getDefaultWriter() {
5454
return new FileSystemResultsWriter(Paths.get(path));
5555
}
5656

57-
public void startTestContainer(String parentUuid, TestResultContainer container) {
57+
public void startTestContainer(final String parentUuid, final TestResultContainer container) {
5858
get(parentUuid, TestResultContainer.class)
5959
.getChildren().add(container.getUuid());
6060
startTestContainer(container);
6161
}
6262

63-
public void startTestContainer(TestResultContainer container) {
63+
public void startTestContainer(final TestResultContainer container) {
6464
LOGGER.debug("Start test result container {}", container.getUuid());
6565
put(container.getUuid(), container)
6666
.withStart(System.currentTimeMillis());
6767
}
6868

69-
public void updateTestContainer(String uuid, Consumer<TestResultContainer> update) {
69+
public void updateTestContainer(final String uuid, final Consumer<TestResultContainer> update) {
7070
LOGGER.debug("Update test result container {}", uuid);
7171
update.accept(get(uuid, TestResultContainer.class));
7272
}
7373

74-
public void stopTestContainer(String uuid) {
75-
LOGGER.debug("Update test result container {}", uuid);
74+
public void stopTestContainer(final String uuid) {
75+
LOGGER.debug("Stop test result container {}", uuid);
7676
get(uuid, TestResultContainer.class)
7777
.withStop(System.currentTimeMillis());
7878
}
7979

80-
public void writeTestContainer(String uuid) {
80+
public void writeTestContainer(final String uuid) {
8181
LOGGER.debug("Stop test group {}", uuid);
8282
writer.write(remove(uuid, TestResultContainer.class));
8383
}
8484

85-
public void startBeforeFixture(String parentUuid, String uuid, FixtureResult result) {
85+
public void startBeforeFixture(final String parentUuid, final String uuid, final FixtureResult result) {
8686
LOGGER.debug("Start test before {} with parent {}", uuid, parentUuid);
8787
startFixture(parentUuid, uuid, result, TestResultContainer::getBefores);
8888
}
8989

90-
public void startAfterFixture(String parentUuid, String uuid, FixtureResult result) {
90+
public void startAfterFixture(final String parentUuid, final String uuid, final FixtureResult result) {
9191
LOGGER.debug("Start test after {} with parent {}", uuid, parentUuid);
9292
startFixture(parentUuid, uuid, result, TestResultContainer::getAfters);
9393
}
9494

95-
private void startFixture(String parentUuid, String uuid, FixtureResult result,
96-
Function<TestResultContainer, List<FixtureResult>> fixturesGetter) {
95+
private void startFixture(final String parentUuid, final String uuid, final FixtureResult result,
96+
final Function<TestResultContainer, List<FixtureResult>> fixturesGetter) {
9797
put(uuid, result)
9898
.withStage(Stage.RUNNING)
9999
.withStart(System.currentTimeMillis());
100-
TestResultContainer container = get(parentUuid, TestResultContainer.class);
100+
final TestResultContainer container = get(parentUuid, TestResultContainer.class);
101101
fixturesGetter.apply(container).add(result);
102102
currentStepContext.remove();
103103
currentStepContext.get().push(uuid);
104104
}
105105

106-
public void updateFixture(String uuid, Consumer<FixtureResult> update) {
106+
public void updateFixture(final String uuid, final Consumer<FixtureResult> update) {
107107
LOGGER.debug("Update test group {}", uuid);
108108
update.accept(get(uuid, FixtureResult.class));
109109
}
110110

111-
public void stopFixture(String uuid) {
111+
public void stopFixture(final String uuid) {
112112
LOGGER.debug("Stop test before {}", uuid);
113113
currentStepContext.remove();
114114
remove(uuid, FixtureResult.class)
115115
.withStage(Stage.FINISHED)
116116
.withStop(System.currentTimeMillis());
117117
}
118118

119-
public void scheduleTestCase(String parentUuid, TestResult result) {
119+
public void scheduleTestCase(final String parentUuid, final TestResult result) {
120120
LOGGER.debug("Add test case {} to {}", result.getUuid(), parentUuid);
121121
get(parentUuid, TestResultContainer.class)
122122
.getChildren().add(result.getUuid());
123123
scheduleTestCase(result);
124124
}
125125

126-
public void scheduleTestCase(TestResult result) {
126+
public void scheduleTestCase(final TestResult result) {
127127
LOGGER.debug("Schedule test case {}", result.getUuid());
128128
put(result.getUuid(), result)
129129
.withStage(Stage.SCHEDULED);
130130
}
131131

132-
public void startTestCase(String uuid) {
132+
public void startTestCase(final String uuid) {
133133
LOGGER.debug("Start test case {}", uuid);
134134
get(uuid, TestResult.class)
135135
.withStage(Stage.RUNNING)
@@ -138,37 +138,40 @@ public void startTestCase(String uuid) {
138138
currentStepContext.get().push(uuid);
139139
}
140140

141-
public void updateTestCase(String uuid, Consumer<TestResult> update) {
141+
public void updateTestCase(final String uuid, final Consumer<TestResult> update) {
142142
LOGGER.debug("Update test case {}", uuid);
143143
update.accept(get(uuid, TestResult.class));
144144
}
145145

146-
public void stopTestCase(String uuid) {
146+
public void stopTestCase(final String uuid) {
147147
LOGGER.debug("Stop test case {}", uuid);
148148
currentStepContext.remove();
149149
get(uuid, TestResult.class)
150150
.withStage(Stage.FINISHED)
151151
.withStop(System.currentTimeMillis());
152152
}
153153

154-
public void writeTestCase(String uuid) {
154+
public void writeTestCase(final String uuid) {
155155
LOGGER.debug("Close test case {}", uuid);
156156
writer.write(remove(uuid, TestResult.class));
157157
}
158158

159-
public void addAttachment(String name, String type, String fileExtension, byte[] body) {
159+
public void addAttachment(final String name, final String type,
160+
final String fileExtension, final byte[] body) {
160161
addAttachment(name, type, fileExtension, new ByteArrayInputStream(body));
161162
}
162163

163-
public void addAttachment(String name, String type, String fileExtension, InputStream stream) {
164-
String uuid = currentStepContext.get().getFirst();
164+
@SuppressWarnings({"PMD.NullAssignment", "PMD.UseObjectForClearerAPI"})
165+
public void addAttachment(final String name, final String type,
166+
final String fileExtension, final InputStream stream) {
167+
final String uuid = currentStepContext.get().getFirst();
165168
LOGGER.debug("Adding attachment to item with uuid {}", uuid);
166-
String extension = Optional.ofNullable(fileExtension)
169+
final String extension = Optional.ofNullable(fileExtension)
167170
.filter(ext -> !ext.isEmpty())
168-
.map(ext -> ext.startsWith(".") ? ext : "." + ext)
171+
.map(ext -> ext.charAt(0) == '.' ? ext : "." + ext)
169172
.orElse("");
170-
String source = UUID.randomUUID().toString() + ATTACHMENT_FILE_SUFFIX + extension;
171-
Attachment attachment = new Attachment()
173+
final String source = UUID.randomUUID().toString() + ATTACHMENT_FILE_SUFFIX + extension;
174+
final Attachment attachment = new Attachment()
172175
.withName(isEmpty(name) ? null : name)
173176
.withType(isEmpty(type) ? null : type)
174177
.withSource(source);
@@ -177,16 +180,17 @@ public void addAttachment(String name, String type, String fileExtension, InputS
177180
get(uuid, WithAttachments.class).getAttachments().add(attachment);
178181
}
179182

180-
public void addStep(StepResult result) {
183+
public void addStep(final StepResult result) {
181184
get(currentStepContext.get().getFirst(), WithSteps.class).getSteps().add(result);
182185
}
183186

184-
public void startStep(String uuid, StepResult result) {
185-
LinkedList<String> uuids = currentStepContext.get();
187+
@SuppressWarnings("PMD.NullAssignment")
188+
public void startStep(final String uuid, final StepResult result) {
189+
final LinkedList<String> uuids = currentStepContext.get();
186190
startStep(uuids.isEmpty() ? null : uuids.getFirst(), uuid, result);
187191
}
188192

189-
public void startStep(String parentUuid, String uuid, StepResult result) {
193+
public void startStep(final String parentUuid, final String uuid, final StepResult result) {
190194
LOGGER.debug("Start step {} with parent {}", uuid, parentUuid);
191195
put(uuid, result)
192196
.withStage(Stage.RUNNING)
@@ -198,11 +202,11 @@ public void startStep(String parentUuid, String uuid, StepResult result) {
198202
}
199203
}
200204

201-
public void updateStep(Consumer<StepResult> update) {
205+
public void updateStep(final Consumer<StepResult> update) {
202206
updateStep(currentStepContext.get().getFirst(), update);
203207
}
204208

205-
public void updateStep(String uuid, Consumer<StepResult> update) {
209+
public void updateStep(final String uuid, final Consumer<StepResult> update) {
206210
LOGGER.debug("Update step {}", uuid);
207211
update.accept(get(uuid, StepResult.class));
208212
}
@@ -211,40 +215,46 @@ public void stopStep() {
211215
stopStep(currentStepContext.get().getFirst());
212216
}
213217

214-
public void stopStep(String uuid) {
218+
public void stopStep(final String uuid) {
215219
LOGGER.debug("Stop step {}", uuid);
216220
remove(uuid, StepResult.class)
217221
.withStage(Stage.FINISHED)
218222
.withStop(System.currentTimeMillis());
219223
currentStepContext.get().pop();
220224
}
221225

222-
private <T> T put(String uuid, T item) {
223-
Objects.requireNonNull(uuid, "Uuid can't be null");
226+
private <T> T put(final String uuid, final T item) {
227+
Objects.requireNonNull(uuid, "Can't put item to storage: uuid can't be null");
224228
storage.put(uuid, item);
225229
return item;
226230
}
227231

228-
private <T> T get(String uuid, Class<T> clazz) {
229-
Objects.requireNonNull(uuid, "Uuid can't be null");
230-
Object obj = Objects.requireNonNull(storage.get(uuid), "Could not find " + clazz + " by uuid " + uuid);
232+
private <T> T get(final String uuid, final Class<T> clazz) {
233+
Objects.requireNonNull(uuid, "Can't get item from storage: uuid can't be null");
234+
final Object obj = Objects.requireNonNull(
235+
storage.get(uuid),
236+
String.format("Could not get %s by uuid %s", clazz, uuid)
237+
);
231238
return cast(obj, clazz);
232239
}
233240

234-
private <T> T remove(String uuid, Class<T> clazz) {
235-
Objects.requireNonNull(uuid, "Uuid can't be null");
236-
Object obj = Objects.requireNonNull(storage.remove(uuid), "Could not find " + clazz + " by uuid " + uuid);
241+
private <T> T remove(final String uuid, final Class<T> clazz) {
242+
Objects.requireNonNull(uuid, "Can't remove item from storage: uuid can't be null");
243+
final Object obj = Objects.requireNonNull(
244+
storage.remove(uuid),
245+
String.format("Could not remove %s by uuid %s", clazz, uuid)
246+
);
237247
return cast(obj, clazz);
238248
}
239249

240-
private <T> T cast(Object obj, Class<T> clazz) {
250+
private <T> T cast(final Object obj, final Class<T> clazz) {
241251
if (clazz.isInstance(obj)) {
242252
return clazz.cast(obj);
243253
}
244254
throw new IllegalStateException("Can not cast " + obj + " to " + clazz);
245255
}
246256

247-
private boolean isEmpty(String s) {
257+
private boolean isEmpty(final String s) {
248258
return Objects.isNull(s) || s.isEmpty();
249259
}
250260
}

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@
66
import java.lang.annotation.RetentionPolicy;
77
import java.lang.annotation.Target;
88

9+
/**
10+
* Used to mark methods that produce attachments. Returned value of such methods
11+
* will be copied and shown in the report as attachment.
12+
*/
913
@Documented
1014
@Retention(RetentionPolicy.RUNTIME)
1115
@Target(ElementType.METHOD)
1216
public @interface Attachment {
1317

1418
/**
15-
* Attachment name
19+
* Attachment name.
1620
*/
1721
String value() default "{method}";
1822

1923
/**
20-
* Valid attachment MimeType, for example "text/plain" or "application/json"
24+
* Valid attachment MimeType, for example "text/plain" or "application/json".
2125
*/
2226
String type() default "";
2327

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
import java.lang.annotation.RetentionPolicy;
99
import java.lang.annotation.Target;
1010

11+
/**
12+
* Used to mark tests with epic label.
13+
*/
1114
@Documented
1215
@Inherited
1316
@Retention(RetentionPolicy.RUNTIME)

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import java.lang.annotation.RetentionPolicy;
88
import java.lang.annotation.Target;
99

10+
/**
11+
* Wrapper annotation for {@link Epic}.
12+
*/
1013
@Documented
1114
@Inherited
1215
@Retention(RetentionPolicy.RUNTIME)

0 commit comments

Comments
 (0)