-
Notifications
You must be signed in to change notification settings - Fork 6
/
ArtifactCollector.java
212 lines (188 loc) · 7.32 KB
/
ArtifactCollector.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
package com.nordstrom.automation.junit;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import org.junit.runner.Description;
import com.nordstrom.common.file.PathUtils;
/**
* This is the base class for implementations of scenario-specific artifact collectors.
*
* @param <T> scenario-specific artifact type
*/
public class ArtifactCollector<T extends ArtifactType> extends AtomIdentity {
private static final ConcurrentHashMap<Integer, List<ArtifactCollector<? extends ArtifactType>>> WATCHER_MAP;
private static final Function<Integer, List<ArtifactCollector<? extends ArtifactType>>> NEW_INSTANCE;
static {
WATCHER_MAP = new ConcurrentHashMap<>();
NEW_INSTANCE = new Function<Integer, List<ArtifactCollector<? extends ArtifactType>>>() {
@Override
public List<ArtifactCollector<? extends ArtifactType>> apply(Integer input) {
return new ArrayList<>();
}
};
}
private final T provider;
private final List<Path> artifactPaths = new ArrayList<>();
public ArtifactCollector(Object instance, T provider) {
super(instance);
this.provider = provider;
}
/**
* {@inheritDoc}
*/
@Override
public void starting(Description description) {
super.starting(description);
List<ArtifactCollector<? extends ArtifactType>> watcherList =
LifecycleHooks.computeIfAbsent(WATCHER_MAP, description.hashCode(), NEW_INSTANCE);
watcherList.add(this);
}
/**
* {@inheritDoc}
*/
@Override
public void failed(Throwable e, Description description) {
captureArtifact(e);
}
/**
* Capture artifact from the current test result context.
*
* @param reason impetus for capture request; may be 'null'
* @return (optional) path at which the captured artifact was stored
*/
public Optional<Path> captureArtifact(Throwable reason) {
if (! provider.canGetArtifact(getInstance())) {
return Optional.empty();
}
byte[] artifact = provider.getArtifact(getInstance(), reason);
if ((artifact == null) || (artifact.length == 0)) {
return Optional.empty();
}
Path collectionPath = getCollectionPath();
if (!collectionPath.toFile().exists()) {
try {
Files.createDirectories(collectionPath);
} catch (IOException e) {
if (provider.getLogger() != null) {
String messageTemplate = "Unable to create collection directory ({}); no artifact was captured";
provider.getLogger().warn(messageTemplate, collectionPath, e);
}
return Optional.empty();
}
}
Path artifactPath;
try {
artifactPath = PathUtils.getNextPath(
collectionPath,
getArtifactBaseName(),
provider.getArtifactExtension());
} catch (IOException e) {
if (provider.getLogger() != null) {
provider.getLogger().warn("Unable to get output path; no artifact was captured", e);
}
return Optional.empty();
}
try {
if (provider.getLogger() != null) {
provider.getLogger().info("Saving captured artifact to ({}).", artifactPath);
}
Files.write(artifactPath, artifact);
} catch (IOException e) {
if (provider.getLogger() != null) {
provider.getLogger().warn("I/O error saving to ({}); no artifact was captured", artifactPath, e);
}
return Optional.empty();
}
recordArtifactPath(artifactPath);
return Optional.of(artifactPath);
}
/**
* Get path of directory at which to store artifacts.
*
* @return path of artifact storage directory
*/
private Path getCollectionPath() {
Path collectionPath = PathUtils.ReportsDirectory.getPathForObject(getInstance());
return collectionPath.resolve(provider.getArtifactPath(getInstance()));
}
/**
* Get base name for artifact files for the specified test result.
* <br><br>
* <b>NOTE</b>: The base name is derived from the name of the current test.
* If the method is parameterized, a hash code is computed from the parameter
* values and appended to the base name as an 8-digit hexadecimal integer.
*
* @return artifact file base name
*/
private String getArtifactBaseName() {
int hashcode = getParameters().hashCode();
if (hashcode != 0) {
String hashStr = String.format("%08X", hashcode);
return getDescription().getMethodName() + "-" + hashStr;
} else {
return getDescription().getMethodName();
}
}
/**
* Record the path at which the specified artifact was store in the indicated test result.
*
* @param artifactPath path at which the captured artifact was stored
*/
private void recordArtifactPath(Path artifactPath) {
artifactPaths.add(artifactPath);
}
/**
* Retrieve the paths of artifacts that were stored in the indicated test result.
*
* @return (optional) list of artifact paths
*/
public Optional<List<Path>> retrieveArtifactPaths() {
if (artifactPaths.isEmpty()) {
return Optional.empty();
} else {
return Optional.of(artifactPaths);
}
}
/**
* Get the artifact provider object.
*
* @return artifact provider object
*/
public T getArtifactProvider() {
return provider;
}
/**
* Get reference to an instance of the specified watcher type associated with the described method.
*
* @param <S> type-specific artifact collector class
* @param description JUnit method description object
* @param watcherType watcher type
* @return optional watcher instance
*/
@SuppressWarnings("unchecked")
public static <S extends ArtifactCollector<? extends ArtifactType>> Optional<S>
getWatcher(Description description, Class<S> watcherType) {
List<ArtifactCollector<? extends ArtifactType>> watcherList = WATCHER_MAP.get(description.hashCode());
if (watcherList != null) {
for (ArtifactCollector<? extends ArtifactType> watcher : watcherList) {
if (watcher.getClass() == watcherType) {
return Optional.of((S) watcher);
}
}
}
return Optional.empty();
}
/**
* Release the watchers for the specified description.
*
* @param description JUnit method description
*/
static void releaseWatchersOf(Description description) {
WATCHER_MAP.remove(description.hashCode());
}
}