From 4ea2d90eceecc7f654575cbc965e12646386c831 Mon Sep 17 00:00:00 2001 From: Scott Babcock Date: Fri, 31 May 2024 18:58:25 -0700 Subject: [PATCH] Upgrade to latest Java-Utils; eliminate Guava usage (#67) --- build.gradle | 2 +- .../testng/AbstractListenerChain.java | 42 ++++++++++--------- .../automation/testng/ArtifactCollector.java | 14 +++---- .../automation/testng/DataProviders.java | 33 ++++++++++++--- .../testng/ArtifactCollectorTestCases.java | 4 +- .../testng/ListenerChainTestCases.java | 4 +- .../automation/testng/UnitTestCapture.java | 4 +- 7 files changed, 65 insertions(+), 38 deletions(-) diff --git a/build.gradle b/build.gradle index 8c4c16e..050ca2a 100644 --- a/build.gradle +++ b/build.gradle @@ -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' diff --git a/src/main/java/com/nordstrom/automation/testng/AbstractListenerChain.java b/src/main/java/com/nordstrom/automation/testng/AbstractListenerChain.java index 0a53001..8f398d2 100644 --- a/src/main/java/com/nordstrom/automation/testng/AbstractListenerChain.java +++ b/src/main/java/com/nordstrom/automation/testng/AbstractListenerChain.java @@ -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; @@ -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. @@ -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 iterator = suiteListeners.listIterator(suiteListeners.size()); + while (iterator.hasPrevious()) { + iterator.previous().onStart(suite); + } } } @@ -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 iterator = methodListeners.listIterator(methodListeners.size()); + while (iterator.hasPrevious()) { + iterator.previous().beforeInvocation(method, testResult); + } } } @@ -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 iterator = testListeners.listIterator(testListeners.size()); + while (iterator.hasPrevious()) { + iterator.previous().onTestStart(result); + } } } @@ -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 iterator = testListeners.listIterator(testListeners.size()); + while (iterator.hasPrevious()) { + iterator.previous().onStart(context); + } } } @@ -394,9 +397,10 @@ public List intercept(List methods, ITestConte @Override public void onBeforeClass(ITestClass testClass) { synchronized (classListeners) { - for (IClassListener classListener : Lists.reverse(classListeners)) { - classListener.onBeforeClass(testClass); - } + ListIterator iterator = classListeners.listIterator(classListeners.size()); + while (iterator.hasPrevious()) { + iterator.previous().onBeforeClass(testClass); + } } } @@ -479,7 +483,7 @@ public Optional getAttachedListener(Class list return Optional.of((T) listener); } } - return Optional.absent(); + return Optional.empty(); } /** diff --git a/src/main/java/com/nordstrom/automation/testng/ArtifactCollector.java b/src/main/java/com/nordstrom/automation/testng/ArtifactCollector.java index c5d9b58..445d3ce 100644 --- a/src/main/java/com/nordstrom/automation/testng/ArtifactCollector.java +++ b/src/main/java/com/nordstrom/automation/testng/ArtifactCollector.java @@ -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; /** @@ -97,12 +97,12 @@ public void onTestSuccess(ITestResult result) { */ public synchronized Optional 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); @@ -114,7 +114,7 @@ public synchronized Optional 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(); } } @@ -128,7 +128,7 @@ public synchronized Optional 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 { @@ -140,7 +140,7 @@ public synchronized Optional 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); @@ -208,7 +208,7 @@ public static synchronized Optional> retrieveArtifactPaths(ITestResul if (artifactPaths != null) { return Optional.of(artifactPaths); } else { - return Optional.absent(); + return Optional.empty(); } } diff --git a/src/main/java/com/nordstrom/automation/testng/DataProviders.java b/src/main/java/com/nordstrom/automation/testng/DataProviders.java index e25dce2..fe7d1cc 100644 --- a/src/main/java/com/nordstrom/automation/testng/DataProviders.java +++ b/src/main/java/com/nordstrom/automation/testng/DataProviders.java @@ -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 @@ -50,11 +51,11 @@ public static Iterator 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, or Object[]"); @@ -86,7 +87,7 @@ private static class CartesianDataProvider implements Iterator { CartesianDataProvider(List> setList) { // store iterator over Cartesian product - provider = Sets.cartesianProduct(setList).iterator(); + provider = cartesianProduct(setList).iterator(); } @Override @@ -123,7 +124,29 @@ public Object[] next() { public void remove() { provider.remove(); } + + public List> cartesianProduct(List> setList) { + return cartesianProductWorker(setList,0).collect(Collectors.toList()); + } + public Stream> cartesianProductWorker(List> setList, int index) { + if (index == setList.size()) { + List emptyList = new ArrayList<>(); + return Stream.of(emptyList); + } + Set currentSet = setList.get(index); + return currentSet.stream().flatMap(element -> cartesianProductWorker(setList, index+1) + .map(list -> { + List newList = new ArrayList<>(list); + newList.add(0, element); + return newList; + })); + } } + public static Set newHashSet(T[] items) { + Set newSet = new HashSet<>(); + Collections.addAll(newSet, items); + return newSet; + } } diff --git a/src/test/java/com/nordstrom/automation/testng/ArtifactCollectorTestCases.java b/src/test/java/com/nordstrom/automation/testng/ArtifactCollectorTestCases.java index fd03d3d..4ce62d3 100644 --- a/src/test/java/com/nordstrom/automation/testng/ArtifactCollectorTestCases.java +++ b/src/test/java/com/nordstrom/automation/testng/ArtifactCollectorTestCases.java @@ -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 { diff --git a/src/test/java/com/nordstrom/automation/testng/ListenerChainTestCases.java b/src/test/java/com/nordstrom/automation/testng/ListenerChainTestCases.java index f84f3b6..0a4e0f1 100644 --- a/src/test/java/com/nordstrom/automation/testng/ListenerChainTestCases.java +++ b/src/test/java/com/nordstrom/automation/testng/ListenerChainTestCases.java @@ -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; @@ -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". diff --git a/src/test/java/com/nordstrom/automation/testng/UnitTestCapture.java b/src/test/java/com/nordstrom/automation/testng/UnitTestCapture.java index ba6e7b6..a9b1315 100644 --- a/src/test/java/com/nordstrom/automation/testng/UnitTestCapture.java +++ b/src/test/java/com/nordstrom/automation/testng/UnitTestCapture.java @@ -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 {