Skip to content

Commit 4ea2d90

Browse files
authored
Upgrade to latest Java-Utils; eliminate Guava usage (#67)
1 parent b168b4b commit 4ea2d90

File tree

7 files changed

+65
-38
lines changed

7 files changed

+65
-38
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ repositories {
194194

195195
dependencies {
196196
constraints {
197-
api 'com.nordstrom.tools:java-utils:3.1.1'
197+
api 'com.nordstrom.tools:java-utils:3.2.0'
198198
api 'com.nordstrom.tools:settings:3.0.5'
199199
}
200200
api 'com.nordstrom.tools:java-utils'

src/main/java/com/nordstrom/automation/testng/AbstractListenerChain.java

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
import java.util.Collections;
99
import java.util.HashSet;
1010
import java.util.List;
11+
import java.util.ListIterator;
1112
import java.util.Objects;
13+
import java.util.Optional;
1214
import java.util.ServiceLoader;
1315
import java.util.Set;
1416

@@ -29,9 +31,6 @@
2931
import org.testng.ITestResult;
3032
import org.testng.annotations.ITestAnnotation;
3133

32-
import com.google.common.base.Optional;
33-
import com.google.common.collect.Lists;
34-
3534
/**
3635
* This TestNG listener enables the addition of other listeners at runtime and guarantees the order in which they're
3736
* invoked. This is similar in behavior to a JUnit rule chain.
@@ -124,9 +123,10 @@ public void onStart(ISuite suite) {
124123
suite.setAttribute(LISTENER_CHAIN, this);
125124

126125
synchronized(suiteListeners) {
127-
for (ISuiteListener suiteListener : Lists.reverse(suiteListeners)) {
128-
suiteListener.onStart(suite);
129-
}
126+
ListIterator<ISuiteListener> iterator = suiteListeners.listIterator(suiteListeners.size());
127+
while (iterator.hasPrevious()) {
128+
iterator.previous().onStart(suite);
129+
}
130130
}
131131
}
132132

@@ -226,9 +226,10 @@ public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
226226
// @Override omitted to avoid interface conflict
227227
public void beforeInvocation(IInvokedMethod method, ITestResult testResult, ITestContext context) {
228228
synchronized(methodListeners) {
229-
for (IInvokedMethodListener methodListener : Lists.reverse(methodListeners)) {
230-
methodListener.beforeInvocation(method, testResult);
231-
}
229+
ListIterator<IInvokedMethodListener> iterator = methodListeners.listIterator(methodListeners.size());
230+
while (iterator.hasPrevious()) {
231+
iterator.previous().beforeInvocation(method, testResult);
232+
}
232233
}
233234
}
234235

@@ -261,9 +262,10 @@ public void afterInvocation(IInvokedMethod method, ITestResult testResult, ITest
261262
@Override
262263
public void onTestStart(ITestResult result) {
263264
synchronized(testListeners) {
264-
for (ITestListener testListener : Lists.reverse(testListeners)) {
265-
testListener.onTestStart(result);
266-
}
265+
ListIterator<ITestListener> iterator = testListeners.listIterator(testListeners.size());
266+
while (iterator.hasPrevious()) {
267+
iterator.previous().onTestStart(result);
268+
}
267269
}
268270
}
269271

@@ -343,9 +345,10 @@ public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
343345
@Override
344346
public void onStart(ITestContext context) {
345347
synchronized (testListeners) {
346-
for (ITestListener testListener : Lists.reverse(testListeners)) {
347-
testListener.onStart(context);
348-
}
348+
ListIterator<ITestListener> iterator = testListeners.listIterator(testListeners.size());
349+
while (iterator.hasPrevious()) {
350+
iterator.previous().onStart(context);
351+
}
349352
}
350353
}
351354

@@ -394,9 +397,10 @@ public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestConte
394397
@Override
395398
public void onBeforeClass(ITestClass testClass) {
396399
synchronized (classListeners) {
397-
for (IClassListener classListener : Lists.reverse(classListeners)) {
398-
classListener.onBeforeClass(testClass);
399-
}
400+
ListIterator<IClassListener> iterator = classListeners.listIterator(classListeners.size());
401+
while (iterator.hasPrevious()) {
402+
iterator.previous().onBeforeClass(testClass);
403+
}
400404
}
401405
}
402406

@@ -479,7 +483,7 @@ public <T extends ITestNGListener> Optional<T> getAttachedListener(Class<T> list
479483
return Optional.of((T) listener);
480484
}
481485
}
482-
return Optional.absent();
486+
return Optional.empty();
483487
}
484488

485489
/**

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
import java.util.ArrayList;
88
import java.util.Arrays;
99
import java.util.List;
10+
import java.util.Optional;
1011

1112
import org.testng.IConfigurationListener;
1213
import org.testng.ITestContext;
1314
import org.testng.ITestListener;
1415
import org.testng.ITestResult;
1516

16-
import com.google.common.base.Optional;
1717
import com.nordstrom.common.file.PathUtils;
1818

1919
/**
@@ -97,12 +97,12 @@ public void onTestSuccess(ITestResult result) {
9797
*/
9898
public synchronized Optional<Path> captureArtifact(ITestResult result) {
9999
if (! provider.canGetArtifact(result)) {
100-
return Optional.absent();
100+
return Optional.empty();
101101
}
102102

103103
byte[] artifact = provider.getArtifact(result);
104104
if ((artifact == null) || (artifact.length == 0)) {
105-
return Optional.absent();
105+
return Optional.empty();
106106
}
107107

108108
Path collectionPath = getCollectionPath(result);
@@ -114,7 +114,7 @@ public synchronized Optional<Path> captureArtifact(ITestResult result) {
114114
String messageTemplate = "Unable to create collection directory ({}); no artifact was captured";
115115
provider.getLogger().warn(messageTemplate, collectionPath, e);
116116
}
117-
return Optional.absent();
117+
return Optional.empty();
118118
}
119119
}
120120

@@ -128,7 +128,7 @@ public synchronized Optional<Path> captureArtifact(ITestResult result) {
128128
if (provider.getLogger() != null) {
129129
provider.getLogger().warn("Unable to get output path; no artifact was captured", e);
130130
}
131-
return Optional.absent();
131+
return Optional.empty();
132132
}
133133

134134
try {
@@ -140,7 +140,7 @@ public synchronized Optional<Path> captureArtifact(ITestResult result) {
140140
if (provider.getLogger() != null) {
141141
provider.getLogger().warn("I/O error saving to ({}); no artifact was captured", artifactPath, e);
142142
}
143-
return Optional.absent();
143+
return Optional.empty();
144144
}
145145

146146
recordArtifactPath(artifactPath, result);
@@ -208,7 +208,7 @@ public static synchronized Optional<List<Path>> retrieveArtifactPaths(ITestResul
208208
if (artifactPaths != null) {
209209
return Optional.of(artifactPaths);
210210
} else {
211-
return Optional.absent();
211+
return Optional.empty();
212212
}
213213
}
214214

src/main/java/com/nordstrom/automation/testng/DataProviders.java

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
import java.util.ArrayList;
44
import java.util.Arrays;
5+
import java.util.Collections;
56
import java.util.HashSet;
67
import java.util.Iterator;
78
import java.util.List;
89
import java.util.Set;
9-
10-
import com.google.common.collect.Sets;
10+
import java.util.stream.Collectors;
11+
import java.util.stream.Stream;
1112

1213
/**
1314
* This class is meant for utility methods to assist in creating DataProviders
@@ -50,11 +51,11 @@ public static Iterator<Object[]> createIterator(Object... providers) {
5051
// otherwise, if this is an array of array of object
5152
} else if (thisObj instanceof Object[][]) {
5253
// add to list as set of array of object
53-
setList.add(Sets.newHashSet((Object[][]) thisObj));
54+
setList.add(newHashSet((Object[][]) thisObj));
5455
// otherwise, if this is an array of object
5556
} else if (thisObj instanceof Object[]) {
5657
// add to list as set of array of object
57-
setList.add(Sets.newHashSet(unflatten((Object[]) thisObj)));
58+
setList.add(newHashSet(unflatten((Object[]) thisObj)));
5859
} else {
5960
throw new IllegalArgumentException(
6061
"Types of all arguments must be Object[][], Iterator<Object[]>, or Object[]");
@@ -86,7 +87,7 @@ private static class CartesianDataProvider implements Iterator<Object[]> {
8687

8788
CartesianDataProvider(List<Set<Object[]>> setList) {
8889
// store iterator over Cartesian product
89-
provider = Sets.cartesianProduct(setList).iterator();
90+
provider = cartesianProduct(setList).iterator();
9091
}
9192

9293
@Override
@@ -123,7 +124,29 @@ public Object[] next() {
123124
public void remove() {
124125
provider.remove();
125126
}
127+
128+
public <T> List<List<T>> cartesianProduct(List<Set<T>> setList) {
129+
return cartesianProductWorker(setList,0).collect(Collectors.toList());
130+
}
126131

132+
public <T> Stream<List<T>> cartesianProductWorker(List<Set<T>> setList, int index) {
133+
if (index == setList.size()) {
134+
List<T> emptyList = new ArrayList<>();
135+
return Stream.of(emptyList);
136+
}
137+
Set<T> currentSet = setList.get(index);
138+
return currentSet.stream().flatMap(element -> cartesianProductWorker(setList, index+1)
139+
.map(list -> {
140+
List<T> newList = new ArrayList<>(list);
141+
newList.add(0, element);
142+
return newList;
143+
}));
144+
}
127145
}
128146

147+
public static <T> Set<T> newHashSet(T[] items) {
148+
Set<T> newSet = new HashSet<>();
149+
Collections.addAll(newSet, items);
150+
return newSet;
151+
}
129152
}

src/test/java/com/nordstrom/automation/testng/ArtifactCollectorTestCases.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
import static org.testng.Assert.assertTrue;
44
import static org.testng.Assert.fail;
55

6+
import java.util.Optional;
7+
68
import org.testng.ITestResult;
79
import org.testng.Reporter;
810
import org.testng.annotations.Test;
911

10-
import com.google.common.base.Optional;
11-
1212
@LinkedListeners({UnitTestCapture.class, ExecutionFlowController.class})
1313
public class ArtifactCollectorTestCases {
1414

src/test/java/com/nordstrom/automation/testng/ListenerChainTestCases.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import static org.testng.Assert.fail;
55

66
import java.lang.reflect.Method;
7+
import java.util.Optional;
8+
79
import org.testng.ITestResult;
810
import org.testng.Reporter;
911
import org.testng.SkipException;
@@ -13,8 +15,6 @@
1315
import org.testng.annotations.Listeners;
1416
import org.testng.annotations.Test;
1517

16-
import com.google.common.base.Optional;
17-
1818
/**
1919
* This test class is driven by {@link ListenerChainTest}. Attempts to run the tests in this class without specifying
2020
* a group will typically fail, because this results in execution of tests that are intended to fail "unexpectedly".

src/test/java/com/nordstrom/automation/testng/UnitTestCapture.java

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

33
import java.nio.file.Path;
4-
import org.testng.ITestResult;
4+
import java.util.Optional;
55

6-
import com.google.common.base.Optional;
6+
import org.testng.ITestResult;
77

88
public class UnitTestCapture extends ArtifactCollector<UnitTestArtifact> {
99

0 commit comments

Comments
 (0)