Skip to content

Commit 2caa8e2

Browse files
authored
Merge pull request #44 from Nordstrom/pr/support-java-7
Pr/support java 7
2 parents ed1e6a0 + d6f0c40 commit 2caa8e2

19 files changed

+228
-130
lines changed

pom.xml

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<modelVersion>4.0.0</modelVersion>
33
<groupId>com.nordstrom.tools</groupId>
44
<artifactId>junit-foundation</artifactId>
5-
<version>9.2.1-SNAPSHOT</version>
5+
<version>9.3.0-SNAPSHOT</version>
66
<packaging>jar</packaging>
77

88
<name>JUnit Foundation</name>
@@ -27,17 +27,18 @@
2727

2828
<properties>
2929
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
30-
<maven.compiler.source>1.8</maven.compiler.source>
31-
<maven.compiler.target>1.8</maven.compiler.target>
32-
<java-utils.version>1.8.0</java-utils.version>
30+
<maven.compiler.source>1.7</maven.compiler.source>
31+
<maven.compiler.target>1.7</maven.compiler.target>
32+
<java-utils.version>1.9.0</java-utils.version>
3333
<surefire-plugin.version>2.22.0</surefire-plugin.version>
3434
<source-plugin.version>3.0.1</source-plugin.version>
3535
<javadoc-plugin.version>2.10.4</javadoc-plugin.version>
36-
<settings.version>2.2.2</settings.version>
36+
<settings.version>2.3.0</settings.version>
3737
<junit.version>4.12</junit.version>
3838
<testng.version>6.10</testng.version>
3939
<bytebuddy.version>1.9.5</bytebuddy.version>
4040
<logback.version>1.2.2</logback.version>
41+
<guava.version>23.5-jre</guava.version>
4142
<gpg-plugin.version>1.6</gpg-plugin.version>
4243
<staging-plugin.version>1.6.7</staging-plugin.version>
4344
<release-plugin.version>2.5.3</release-plugin.version>
@@ -100,6 +101,11 @@
100101
<artifactId>logback-classic</artifactId>
101102
<version>${logback.version}</version>
102103
</dependency>
104+
<dependency>
105+
<groupId>com.google.guava</groupId>
106+
<artifactId>guava</artifactId>
107+
<version>${guava.version}</version>
108+
</dependency>
103109
</dependencies>
104110
</dependencyManagement>
105111

@@ -134,6 +140,10 @@
134140
<groupId>ch.qos.logback</groupId>
135141
<artifactId>logback-classic</artifactId>
136142
</dependency>
143+
<dependency>
144+
<groupId>com.google.guava</groupId>
145+
<artifactId>guava</artifactId>
146+
</dependency>
137147
</dependencies>
138148

139149
<build>

src/main/java/com/nordstrom/automation/junit/ArtifactCollector.java

Lines changed: 53 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
import java.nio.file.Path;
66
import java.util.ArrayList;
77
import java.util.List;
8-
import java.util.Map;
9-
import java.util.Optional;
108
import java.util.concurrent.ConcurrentHashMap;
119

1210
import org.junit.runner.Description;
11+
12+
import com.google.common.base.Function;
13+
import com.google.common.base.Optional;
1314
import com.nordstrom.common.file.PathUtils;
1415

1516
/**
@@ -19,8 +20,18 @@
1920
*/
2021
public 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
}
Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
package com.nordstrom.automation.junit;
22

3-
import java.util.Map;
4-
import java.util.Optional;
5-
63
import org.junit.runner.Description;
74

85
import com.nordstrom.common.params.Params;
@@ -18,25 +15,4 @@ public interface ArtifactParams extends Params {
1815
* @return JUnit method description object
1916
*/
2017
Description getDescription();
21-
22-
/**
23-
* Assemble a map of test class instance parameters.
24-
*
25-
* @param params array of {@link Param} objects; may be {@code null} or empty
26-
* @return optional map of parameters (may be empty)
27-
*/
28-
public static Optional<Map<String, Object>> mapOf(Param... params) {
29-
return Params.mapOf(params);
30-
}
31-
32-
/**
33-
* Create a test parameter object for the specified key/value pair.
34-
*
35-
* @param key test parameter key (name)
36-
* @param val test parameter value
37-
* @return test parameter object
38-
*/
39-
public static Param param(String key, Object val) {
40-
return Params.param(key, val);
41-
}
4218
}

src/main/java/com/nordstrom/automation/junit/ArtifactType.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import java.nio.file.Path;
44
import org.slf4j.Logger;
55

6-
import com.nordstrom.common.file.PathUtils;
7-
86
/**
97
* This interface defines the contract fulfilled by artifact capture providers. Instances of this interface supply the
108
* scenario-specific implementation for artifact capture through the {@link ArtifactCollector} listener.
@@ -75,9 +73,7 @@ public interface ArtifactType {
7573
*
7674
* @return logger for this artifact (may be {@code null})
7775
*/
78-
default Logger getLogger() {
79-
return null;
80-
}
76+
Logger getLogger();
8177

8278
/**
8379
* Determine if artifact capture is available in the specified context.
@@ -102,12 +98,9 @@ default Logger getLogger() {
10298
* <b>NOTE</b>: The returned path can be either relative or absolute.
10399
*
104100
* @param instance JUnit test class instance
105-
* @return artifact storage path
101+
* @return artifact storage path; {@code null} to accept default path
106102
*/
107-
default Path getArtifactPath(Object instance) {
108-
return PathUtils.ReportsDirectory.getPathForObject(instance);
109-
}
110-
103+
Path getArtifactPath(Object instance);
111104

112105
/**
113106
* Get the extension for artifact files of this type.

src/main/java/com/nordstrom/automation/junit/AtomIdentity.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.nordstrom.automation.junit;
22

33
import java.util.Map;
4-
import java.util.Optional;
54

65
import org.junit.rules.TestWatcher;
76
import org.junit.runner.Description;
87

8+
import com.google.common.base.Optional;
9+
910
/**
1011
* This is the base class for test watchers that need access to the test class instance for the current atomic test.
1112
* For test classes that implement the {@link ArtifactParams} interface, instance parameters can be retrieved via the
@@ -54,7 +55,7 @@ public Optional<Map<String, Object>> getParameters() {
5455
if (instance instanceof ArtifactParams) {
5556
return ((ArtifactParams) instance).getParameters();
5657
}
57-
return Optional.empty();
58+
return Optional.absent();
5859
}
5960

6061
}

src/main/java/com/nordstrom/automation/junit/CreateTest.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.nordstrom.automation.junit;
22

33
import java.util.Map;
4-
import java.util.Optional;
54
import java.util.ServiceLoader;
65
import java.util.concurrent.Callable;
76
import java.util.concurrent.ConcurrentHashMap;
87

8+
import com.google.common.base.Optional;
9+
910
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
1011
import net.bytebuddy.implementation.bind.annotation.SuperCall;
1112
import net.bytebuddy.implementation.bind.annotation.This;
@@ -20,10 +21,16 @@ public class CreateTest {
2021
private static final ServiceLoader<TestObjectWatcher> objectWatcherLoader;
2122
private static final Map<Object, Object> TARGET_TO_RUNNER = new ConcurrentHashMap<>();
2223
private static final Map<Object, Object> RUNNER_TO_TARGET = new ConcurrentHashMap<>();
23-
private static final ThreadLocal<DepthGauge> DEPTH = ThreadLocal.withInitial(DepthGauge::new);
24+
private static final ThreadLocal<DepthGauge> DEPTH;
2425

2526
static {
2627
objectWatcherLoader = ServiceLoader.load(TestObjectWatcher.class);
28+
DEPTH = new ThreadLocal<DepthGauge>() {
29+
@Override
30+
protected DepthGauge initialValue() {
31+
return new DepthGauge();
32+
}
33+
};
2734
}
2835

2936
/**
@@ -99,6 +106,6 @@ static <T extends JUnitWatcher> Optional<T> getAttachedWatcher(Class<T> watcherT
99106
}
100107
}
101108
}
102-
return Optional.empty();
109+
return Optional.absent();
103110
}
104111
}

0 commit comments

Comments
 (0)