/
ArtifactType.java
118 lines (109 loc) · 3.59 KB
/
ArtifactType.java
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
package com.nordstrom.automation.junit;
import java.nio.file.Path;
import org.slf4j.Logger;
import com.nordstrom.common.file.PathUtils;
/**
* This interface defines the contract fulfilled by artifact capture providers. Instances of this interface supply the
* scenario-specific implementation for artifact capture through the {@link ArtifactCollector} listener.
* <p>
* <b>IMPLEMENTING ARTIFACTTYPE</b>
* <pre><code>
* package com.nordstrom.example;
*
* import java.nio.file.Path;
*
* import org.slf4j.Logger;
* import org.slf4j.LoggerFactory;
* import org.testng.ITestResult;
*
* import com.nordstrom.automation.junit.ArtifactType;
*
* public class MyArtifactType extends ArtifactType {
*
* private static final String ARTIFACT_PATH = "artifacts";
* private static final String EXTENSION = "txt";
* private static final String ARTIFACT = "This text artifact was captured for '%s'";
* private static final Logger LOGGER = LoggerFactory.getLogger(MyArtifactType.class);
*
* @Override
* public boolean canGetArtifact(Object instance) {
* return true;
* }
*
* @Override
* public byte[] getArtifact(Object instance, Throwable reason) {
* return String.format(ARTIFACT, instance.getSimpleName()).getBytes().clone();
* }
*
* @Override
* public Path getArtifactPath(Object instance) {
* return super.getArtifactPath(instance).resolve(ARTIFACT_PATH);
* }
*
* @Override
* public String getArtifactExtension() {
* return EXTENSION;
* }
*
* @Override
* public Logger getLogger() {
* return LOGGER;
* }
* }
* </code></pre>
* <b>CREATING A TYPE-SPECIFIC ARTIFACT COLLECTOR</b>
* <pre><code>
* package com.nordstrom.example;
*
* import com.nordstrom.automation.testng.ArtifactCollector;
*
* public class MyArtifactCapture extends ArtifactCollector<MyArtifactType> {
*
* public MyArtifactCapture() {
* super(new MyArtifactType());
* }
* }
* </code></pre>
*/
public abstract class ArtifactType {
/**
* Get the SLF4J {@link Logger} for this artifact type.
*
* @return logger for this artifact (may be {@code null})
*/
public Logger getLogger() {
return null;
}
/**
* Determine if artifact capture is available in the specified context.
*
* @param instance JUnit test class instance
* @return 'true' if capture is available; otherwise 'false'
*/
public abstract boolean canGetArtifact(Object instance);
/**
* Capture an artifact from the specified context.
*
* @param instance JUnit test class instance
* @param reason impetus for capture request; may be 'null'
* @return byte array containing the captured artifact; if capture fails, an empty array is returned
*/
public abstract byte[] getArtifact(Object instance, Throwable reason);
/**
* Get the path at which to store artifacts.
* <p>
* <b>NOTE</b>: The returned path can be either relative or absolute.
*
* @param instance JUnit test class instance
* @return artifact storage path
*/
public Path getArtifactPath(Object instance) {
return PathUtils.ReportsDirectory.getPathForObject(instance);
}
/**
* Get the extension for artifact files of this type.
*
* @return artifact file extension
*/
public abstract String getArtifactExtension();
}