Skip to content

Commit

Permalink
Upgrade to latest Java-Utils; eliminate Guava usage (#67)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbabcoc authored Jun 1, 2024
1 parent b168b4b commit 4ea2d90
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 38 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ repositories {

dependencies {
constraints {
api 'com.nordstrom.tools:java-utils:3.1.1'
api 'com.nordstrom.tools:java-utils:3.2.0'
api 'com.nordstrom.tools:settings:3.0.5'
}
api 'com.nordstrom.tools:java-utils'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Objects;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.Set;

Expand All @@ -29,9 +31,6 @@
import org.testng.ITestResult;
import org.testng.annotations.ITestAnnotation;

import com.google.common.base.Optional;
import com.google.common.collect.Lists;

/**
* This TestNG listener enables the addition of other listeners at runtime and guarantees the order in which they're
* invoked. This is similar in behavior to a JUnit rule chain.
Expand Down Expand Up @@ -124,9 +123,10 @@ public void onStart(ISuite suite) {
suite.setAttribute(LISTENER_CHAIN, this);

synchronized(suiteListeners) {
for (ISuiteListener suiteListener : Lists.reverse(suiteListeners)) {
suiteListener.onStart(suite);
}
ListIterator<ISuiteListener> iterator = suiteListeners.listIterator(suiteListeners.size());
while (iterator.hasPrevious()) {
iterator.previous().onStart(suite);
}
}
}

Expand Down Expand Up @@ -226,9 +226,10 @@ public void afterInvocation(IInvokedMethod method, ITestResult testResult) {
// @Override omitted to avoid interface conflict
public void beforeInvocation(IInvokedMethod method, ITestResult testResult, ITestContext context) {
synchronized(methodListeners) {
for (IInvokedMethodListener methodListener : Lists.reverse(methodListeners)) {
methodListener.beforeInvocation(method, testResult);
}
ListIterator<IInvokedMethodListener> iterator = methodListeners.listIterator(methodListeners.size());
while (iterator.hasPrevious()) {
iterator.previous().beforeInvocation(method, testResult);
}
}
}

Expand Down Expand Up @@ -261,9 +262,10 @@ public void afterInvocation(IInvokedMethod method, ITestResult testResult, ITest
@Override
public void onTestStart(ITestResult result) {
synchronized(testListeners) {
for (ITestListener testListener : Lists.reverse(testListeners)) {
testListener.onTestStart(result);
}
ListIterator<ITestListener> iterator = testListeners.listIterator(testListeners.size());
while (iterator.hasPrevious()) {
iterator.previous().onTestStart(result);
}
}
}

Expand Down Expand Up @@ -343,9 +345,10 @@ public void onTestFailedButWithinSuccessPercentage(ITestResult result) {
@Override
public void onStart(ITestContext context) {
synchronized (testListeners) {
for (ITestListener testListener : Lists.reverse(testListeners)) {
testListener.onStart(context);
}
ListIterator<ITestListener> iterator = testListeners.listIterator(testListeners.size());
while (iterator.hasPrevious()) {
iterator.previous().onStart(context);
}
}
}

Expand Down Expand Up @@ -394,9 +397,10 @@ public List<IMethodInstance> intercept(List<IMethodInstance> methods, ITestConte
@Override
public void onBeforeClass(ITestClass testClass) {
synchronized (classListeners) {
for (IClassListener classListener : Lists.reverse(classListeners)) {
classListener.onBeforeClass(testClass);
}
ListIterator<IClassListener> iterator = classListeners.listIterator(classListeners.size());
while (iterator.hasPrevious()) {
iterator.previous().onBeforeClass(testClass);
}
}
}

Expand Down Expand Up @@ -479,7 +483,7 @@ public <T extends ITestNGListener> Optional<T> getAttachedListener(Class<T> list
return Optional.of((T) listener);
}
}
return Optional.absent();
return Optional.empty();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.testng.IConfigurationListener;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;

import com.google.common.base.Optional;
import com.nordstrom.common.file.PathUtils;

/**
Expand Down Expand Up @@ -97,12 +97,12 @@ public void onTestSuccess(ITestResult result) {
*/
public synchronized Optional<Path> captureArtifact(ITestResult result) {
if (! provider.canGetArtifact(result)) {
return Optional.absent();
return Optional.empty();
}

byte[] artifact = provider.getArtifact(result);
if ((artifact == null) || (artifact.length == 0)) {
return Optional.absent();
return Optional.empty();
}

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

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

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

recordArtifactPath(artifactPath, result);
Expand Down Expand Up @@ -208,7 +208,7 @@ public static synchronized Optional<List<Path>> retrieveArtifactPaths(ITestResul
if (artifactPaths != null) {
return Optional.of(artifactPaths);
} else {
return Optional.absent();
return Optional.empty();
}
}

Expand Down
33 changes: 28 additions & 5 deletions src/main/java/com/nordstrom/automation/testng/DataProviders.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.google.common.collect.Sets;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* This class is meant for utility methods to assist in creating DataProviders
Expand Down Expand Up @@ -50,11 +51,11 @@ public static Iterator<Object[]> createIterator(Object... providers) {
// otherwise, if this is an array of array of object
} else if (thisObj instanceof Object[][]) {
// add to list as set of array of object
setList.add(Sets.newHashSet((Object[][]) thisObj));
setList.add(newHashSet((Object[][]) thisObj));
// otherwise, if this is an array of object
} else if (thisObj instanceof Object[]) {
// add to list as set of array of object
setList.add(Sets.newHashSet(unflatten((Object[]) thisObj)));
setList.add(newHashSet(unflatten((Object[]) thisObj)));
} else {
throw new IllegalArgumentException(
"Types of all arguments must be Object[][], Iterator<Object[]>, or Object[]");
Expand Down Expand Up @@ -86,7 +87,7 @@ private static class CartesianDataProvider implements Iterator<Object[]> {

CartesianDataProvider(List<Set<Object[]>> setList) {
// store iterator over Cartesian product
provider = Sets.cartesianProduct(setList).iterator();
provider = cartesianProduct(setList).iterator();
}

@Override
Expand Down Expand Up @@ -123,7 +124,29 @@ public Object[] next() {
public void remove() {
provider.remove();
}

public <T> List<List<T>> cartesianProduct(List<Set<T>> setList) {
return cartesianProductWorker(setList,0).collect(Collectors.toList());
}

public <T> Stream<List<T>> cartesianProductWorker(List<Set<T>> setList, int index) {
if (index == setList.size()) {
List<T> emptyList = new ArrayList<>();
return Stream.of(emptyList);
}
Set<T> currentSet = setList.get(index);
return currentSet.stream().flatMap(element -> cartesianProductWorker(setList, index+1)
.map(list -> {
List<T> newList = new ArrayList<>(list);
newList.add(0, element);
return newList;
}));
}
}

public static <T> Set<T> newHashSet(T[] items) {
Set<T> newSet = new HashSet<>();
Collections.addAll(newSet, items);
return newSet;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;

import java.util.Optional;

import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.Test;

import com.google.common.base.Optional;

@LinkedListeners({UnitTestCapture.class, ExecutionFlowController.class})
public class ArtifactCollectorTestCases {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import static org.testng.Assert.fail;

import java.lang.reflect.Method;
import java.util.Optional;

import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.SkipException;
Expand All @@ -13,8 +15,6 @@
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

import com.google.common.base.Optional;

/**
* This test class is driven by {@link ListenerChainTest}. Attempts to run the tests in this class without specifying
* a group will typically fail, because this results in execution of tests that are intended to fail "unexpectedly".
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.nordstrom.automation.testng;

import java.nio.file.Path;
import org.testng.ITestResult;
import java.util.Optional;

import com.google.common.base.Optional;
import org.testng.ITestResult;

public class UnitTestCapture extends ArtifactCollector<UnitTestArtifact> {

Expand Down

0 comments on commit 4ea2d90

Please sign in to comment.