55import java .nio .file .Path ;
66import java .util .ArrayList ;
77import java .util .List ;
8- import java .util .Map ;
9- import java .util .Optional ;
108import java .util .concurrent .ConcurrentHashMap ;
119
1210import org .junit .runner .Description ;
11+
12+ import com .google .common .base .Function ;
13+ import com .google .common .base .Optional ;
1314import com .nordstrom .common .file .PathUtils ;
1415
1516/**
1920 */
2021public class ArtifactCollector <T extends ArtifactType > extends AtomIdentity {
2122
22- private static final Map <Description , List <ArtifactCollector <? extends ArtifactType >>> watcherMap =
23- new ConcurrentHashMap <>();
23+ private static final ConcurrentHashMap <Description , List <ArtifactCollector <? extends ArtifactType >>> watcherMap ;
24+ private static final Function <Description , List <ArtifactCollector <? extends ArtifactType >>> newInstance ;
25+
26+ static {
27+ watcherMap = new ConcurrentHashMap <>();
28+ newInstance = new Function <Description , List <ArtifactCollector <? extends ArtifactType >>>() {
29+ @ Override
30+ public List <ArtifactCollector <? extends ArtifactType >> apply (Description input ) {
31+ return new ArrayList <>();
32+ }
33+ };
34+ }
2435
2536 private final T provider ;
2637 private final List <Path > artifactPaths = new ArrayList <>();
@@ -37,7 +48,7 @@ public ArtifactCollector(Object instance, T provider) {
3748 public void starting (Description description ) {
3849 super .starting (description );
3950 List <ArtifactCollector <? extends ArtifactType >> watcherList =
40- watcherMap .computeIfAbsent (description , k -> new ArrayList <>() );
51+ LifecycleHooks .computeIfAbsent (watcherMap , description , newInstance );
4152 watcherList .add (this );
4253 }
4354
@@ -57,22 +68,24 @@ public void failed(Throwable e, Description description) {
5768 */
5869 public Optional <Path > captureArtifact (Throwable reason ) {
5970 if (! provider .canGetArtifact (getInstance ())) {
60- return Optional .empty ();
71+ return Optional .absent ();
6172 }
6273
6374 byte [] artifact = provider .getArtifact (getInstance (), reason );
6475 if ((artifact == null ) || (artifact .length == 0 )) {
65- return Optional .empty ();
76+ return Optional .absent ();
6677 }
6778
6879 Path collectionPath = getCollectionPath ();
6980 if (!collectionPath .toFile ().exists ()) {
7081 try {
7182 Files .createDirectories (collectionPath );
7283 } catch (IOException e ) {
73- String messageTemplate = "Unable to create collection directory ({}); no artifact was captured" ;
74- Optional .ofNullable (provider .getLogger ()).ifPresent (l -> l .warn (messageTemplate , collectionPath , e ));
75- return Optional .empty ();
84+ if (provider .getLogger () != null ) {
85+ String messageTemplate = "Unable to create collection directory ({}); no artifact was captured" ;
86+ provider .getLogger ().warn (messageTemplate , collectionPath , e );
87+ }
88+ return Optional .absent ();
7689 }
7790 }
7891
@@ -83,19 +96,22 @@ public Optional<Path> captureArtifact(Throwable reason) {
8396 getArtifactBaseName (),
8497 provider .getArtifactExtension ());
8598 } catch (IOException e ) {
86- Optional .ofNullable (provider .getLogger ()).ifPresent (
87- l -> l .warn ("Unable to get output path; no artifact was captured" , e ));
88- return Optional .empty ();
99+ if (provider .getLogger () != null ) {
100+ provider .getLogger ().warn ("Unable to get output path; no artifact was captured" , e );
101+ }
102+ return Optional .absent ();
89103 }
90104
91105 try {
92- Optional .ofNullable (provider .getLogger ()).ifPresent (
93- l -> l .info ("Saving captured artifact to ({})." , artifactPath ));
106+ if (provider .getLogger () != null ) {
107+ provider .getLogger ().info ("Saving captured artifact to ({})." , artifactPath );
108+ }
94109 Files .write (artifactPath , artifact );
95110 } catch (IOException e ) {
96- Optional .ofNullable (provider .getLogger ()).ifPresent (
97- l -> l .warn ("I/O error saving to ({}); no artifact was captured" , artifactPath , e ));
98- return Optional .empty ();
111+ if (provider .getLogger () != null ) {
112+ provider .getLogger ().warn ("I/O error saving to ({}); no artifact was captured" , artifactPath , e );
113+ }
114+ return Optional .absent ();
99115 }
100116
101117 recordArtifactPath (artifactPath );
@@ -109,7 +125,23 @@ public Optional<Path> captureArtifact(Throwable reason) {
109125 */
110126 private Path getCollectionPath () {
111127 Path collectionPath = PathUtils .ReportsDirectory .getPathForObject (getInstance ());
112- return collectionPath .resolve (provider .getArtifactPath (getInstance ()));
128+ return collectionPath .resolve (getArtifactPath (getInstance ()));
129+ }
130+
131+ /**
132+ * Get the path at which to store artifacts.
133+ * <p>
134+ * <b>NOTE</b>: The returned path can be either relative or absolute.
135+ *
136+ * @param instance JUnit test class instance
137+ * @return artifact storage path
138+ */
139+ private Path getArtifactPath (Object instance ) {
140+ Path artifactPath = provider .getArtifactPath (instance );
141+ if (artifactPath == null ) {
142+ artifactPath = PathUtils .ReportsDirectory .getPathForObject (instance );
143+ }
144+ return artifactPath ;
113145 }
114146
115147 /**
@@ -147,7 +179,7 @@ private void recordArtifactPath(Path artifactPath) {
147179 */
148180 public Optional <List <Path >> retrieveArtifactPaths () {
149181 if (artifactPaths .isEmpty ()) {
150- return Optional .empty ();
182+ return Optional .absent ();
151183 } else {
152184 return Optional .of (artifactPaths );
153185 }
@@ -181,7 +213,7 @@ public T getArtifactProvider() {
181213 }
182214 }
183215 }
184- return Optional .empty ();
216+ return Optional .absent ();
185217 }
186218
187219}
0 commit comments