Skip to content

Commit

Permalink
Merge pull request #10 from Nordstrom/yaroslav-orel-factory-fix
Browse files Browse the repository at this point in the history
Yaroslav orel factory fix
  • Loading branch information
sbabcoc authored Jan 8, 2018
2 parents edae974 + 02be88c commit fa4647c
Show file tree
Hide file tree
Showing 4 changed files with 255 additions and 139 deletions.
41 changes: 34 additions & 7 deletions src/main/java/com/nordstrom/automation/testng/ListenerChain.java
Original file line number Diff line number Diff line change
Expand Up @@ -596,14 +596,26 @@ public void onAfterClass(ITestClass testClass) {
* @param listenerType listener type
* @return optional listener instance
*/
@SuppressWarnings("unchecked")
public static <T extends ITestNGListener> Optional<T>
getAttachedListener(ISuite suite, Class<T> listenerType) {

Objects.requireNonNull(suite, "[suite] must be non-null");
Objects.requireNonNull(listenerType, "[listenerType] must be non-null");
ListenerChain chain = (ListenerChain) suite.getAttribute(LISTENER_CHAIN);
for (ITestNGListener listener : chain.listeners) {
Objects.requireNonNull(chain, "Specified suite has no ListenerChain");
return chain.getAttachedListener(listenerType);
}

/**
* Get reference to an instance of the specified listener type.
*
* @param <T> listener type
* @param listenerType listener type
* @return optional listener instance
*/
@SuppressWarnings("unchecked")
public <T extends ITestNGListener> Optional<T> getAttachedListener(Class<T> listenerType) {
for (ITestNGListener listener : listeners) {
if (listener.getClass() == listenerType) {
return Optional.of((T) listener);
}
Expand All @@ -617,7 +629,9 @@ public void onAfterClass(ITestClass testClass) {
* @param testMethod test method
*/
private void attachListeners(Method testMethod) {
attachListeners(testMethod.getDeclaringClass());
if (testMethod != null) {
processLinkedListeners(testMethod.getDeclaringClass());
}
}

/**
Expand All @@ -632,11 +646,11 @@ private void attachListeners(Method testMethod) {
*/
private void attachListeners(Class<?> testClass, Constructor<?> testCtor, Method testMethod) {
if (testClass != null) {
attachListeners(testClass);
processLinkedListeners(testClass);
} else if (testCtor != null) {
attachListeners(testCtor.getDeclaringClass());
} else {
attachListeners(testMethod.getDeclaringClass());
processLinkedListeners(testCtor.getDeclaringClass());
} else if (testMethod != null) {
processLinkedListeners(testMethod.getDeclaringClass());
}
}

Expand All @@ -646,6 +660,19 @@ private void attachListeners(Class<?> testClass, Constructor<?> testCtor, Method
* @param testClass test class
*/
private void attachListeners(Class<?> testClass) {
if (testClass != null) {
processLinkedListeners(testClass);
}
}

/**
* Process the {@link LinkedListeners} annotation of the specified test class.
*
* @param testClass test class
*/
private void processLinkedListeners(Class<?> testClass) {
Objects.requireNonNull(testClass, "[testClass] must be non-null");

LinkedListeners annotation = testClass.getAnnotation(LinkedListeners.class);
if (null != annotation) {
Class<?> markedClass = testClass;
Expand Down
64 changes: 34 additions & 30 deletions src/test/java/com/nordstrom/automation/testng/ChainedListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,39 @@ public class ChainedListener
implements IAnnotationTransformer3, ISuiteListener, IConfigurationListener2,
IInvokedMethodListener, ITestListener, IMethodInterceptor, IClassListener {

static Set<String> configSuccess = Collections.synchronizedSet(new HashSet<>());
static Set<String> configFailure = Collections.synchronizedSet(new HashSet<>());
static Set<String> configSkipped = Collections.synchronizedSet(new HashSet<>());
static Set<String> beforeConfig = Collections.synchronizedSet(new HashSet<>());
Set<String> configSuccess = Collections.synchronizedSet(new HashSet<>());
Set<String> configFailure = Collections.synchronizedSet(new HashSet<>());
Set<String> configSkipped = Collections.synchronizedSet(new HashSet<>());
Set<String> beforeConfig = Collections.synchronizedSet(new HashSet<>());

static Set<String> beforeMethodBefore = Collections.synchronizedSet(new HashSet<>());
static Set<String> beforeMethodAfter = Collections.synchronizedSet(new HashSet<>());
static Set<String> testMethodBefore = Collections.synchronizedSet(new HashSet<>());
static Set<String> testMethodAfter = Collections.synchronizedSet(new HashSet<>());
static Set<String> afterMethodBefore = Collections.synchronizedSet(new HashSet<>());
static Set<String> afterMethodAfter = Collections.synchronizedSet(new HashSet<>());
Set<String> beforeMethodBefore = Collections.synchronizedSet(new HashSet<>());
Set<String> beforeMethodAfter = Collections.synchronizedSet(new HashSet<>());
Set<String> testMethodBefore = Collections.synchronizedSet(new HashSet<>());
Set<String> testMethodAfter = Collections.synchronizedSet(new HashSet<>());
Set<String> afterMethodBefore = Collections.synchronizedSet(new HashSet<>());
Set<String> afterMethodAfter = Collections.synchronizedSet(new HashSet<>());

static Set<String> beforeClass = Collections.synchronizedSet(new HashSet<>());
static Set<String> afterClass = Collections.synchronizedSet(new HashSet<>());
Set<String> beforeClass = Collections.synchronizedSet(new HashSet<>());
Set<String> afterClass = Collections.synchronizedSet(new HashSet<>());

static Set<String> testStarted = Collections.synchronizedSet(new HashSet<>());
static Set<String> testSuccess = Collections.synchronizedSet(new HashSet<>());
static Set<String> testFailure = Collections.synchronizedSet(new HashSet<>());
static Set<String> testSkipped = Collections.synchronizedSet(new HashSet<>());
static Set<String> testCurved = Collections.synchronizedSet(new HashSet<>());
static Set<String> testsBegun = Collections.synchronizedSet(new HashSet<>());
static Set<String> testsEnded = Collections.synchronizedSet(new HashSet<>());
Set<String> testStarted = Collections.synchronizedSet(new HashSet<>());
Set<String> testSuccess = Collections.synchronizedSet(new HashSet<>());
Set<String> testFailure = Collections.synchronizedSet(new HashSet<>());
Set<String> testSkipped = Collections.synchronizedSet(new HashSet<>());
Set<String> testCurved = Collections.synchronizedSet(new HashSet<>());
Set<String> testsBegun = Collections.synchronizedSet(new HashSet<>());
Set<String> testsEnded = Collections.synchronizedSet(new HashSet<>());

static Set<String> suiteBegun = Collections.synchronizedSet(new HashSet<>());
static Set<String> suiteEnded = Collections.synchronizedSet(new HashSet<>());
Set<String> suiteBegun = Collections.synchronizedSet(new HashSet<>());
Set<String> suiteEnded = Collections.synchronizedSet(new HashSet<>());

static Set<String> xformTest = Collections.synchronizedSet(new HashSet<>());
static Set<String> xformConfig = Collections.synchronizedSet(new HashSet<>());
static Set<String> xformProvider = Collections.synchronizedSet(new HashSet<>());
static Set<String> xformFactory = Collections.synchronizedSet(new HashSet<>());
static Set<String> xformListeners = Collections.synchronizedSet(new HashSet<>());
Set<String> xformTest = Collections.synchronizedSet(new HashSet<>());
Set<String> xformConfig = Collections.synchronizedSet(new HashSet<>());
Set<String> xformProvider = Collections.synchronizedSet(new HashSet<>());
Set<String> xformFactory = Collections.synchronizedSet(new HashSet<>());
Set<String> xformListeners = Collections.synchronizedSet(new HashSet<>());

static Set<String> interceptor = Collections.synchronizedSet(new HashSet<>());
Set<String> interceptor = Collections.synchronizedSet(new HashSet<>());

@Override
public void onConfigurationSuccess(ITestResult itr) {
Expand Down Expand Up @@ -191,18 +191,22 @@ public void transform(IConfigurationAnnotation annotation, Class testClass,

@Override
public void transform(IDataProviderAnnotation annotation, Method method) {
xformProvider.add(method.getName());
xformProvider.add("method: " + method.getName());
}

@Override
public void transform(IFactoryAnnotation annotation, Method method) {
xformFactory.add(method.getName());
if (method != null) {
xformFactory.add("method: " + method.getName());
} else {
xformFactory.add("ctor: (unknown)");
}
}

@SuppressWarnings("rawtypes")
@Override
public void transform(IListenersAnnotation annotation, Class testClass) {
xformListeners.add(testClass.getSimpleName());
xformListeners.add("class: " + testClass.getSimpleName());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.nordstrom.automation.testng;

import org.testng.annotations.DataProvider;
import org.testng.annotations.Factory;
import org.testng.annotations.Test;

@LinkedListeners({ChainedListener.class, ExecutionFlowController.class})
public class ConstructorFactory {

public ConstructorFactory() {
}

@Factory(dataProvider = "ints")
public ConstructorFactory(final int i) {
// not important
}

@DataProvider
public Object[] ints() {
return new Object[]{1, 2, 3};
}

@Test
public void fakeTest() {
// not important
}
}
Loading

0 comments on commit fa4647c

Please sign in to comment.