All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.retest.recheck.testng.RecheckHook Maven / Gradle / Ivy

There is a newer version: 1.13.0
Show newest version
package de.retest.recheck.testng;

import static de.retest.recheck.testng.Execute.execute;

import java.util.Arrays;
import java.util.function.Consumer;
import java.util.stream.Collectors;

import org.testng.IHookCallBack;
import org.testng.IHookable;
import org.testng.ITestResult;

import de.retest.recheck.RecheckLifecycle;

/**
 * Administer the lifecycle of {@link RecheckLifecycle} objects of a test.
 */
public class RecheckHook implements IHookable {

	@Override
	public void run( final IHookCallBack callBack, final ITestResult testResult ) {
		startTest( testResult );
		callBack.runTestMethod( testResult );
		try {
			capTest( testResult );
		} finally {
			cap( testResult );
		}
	}

	private void startTest( final ITestResult result ) {
		final String testName = resolveName( result );
		final Object testInstance = result.getInstance();
		final Consumer startTest = r -> r.startTest( testName );
		execute( startTest ).on( testInstance );
	}

	private String resolveName( final ITestResult result ) {
		final String parameters = concatParameters( result );
		return result.getName() + parameters;
	}

	private String concatParameters( final ITestResult result ) {
		final Object[] parameters = result.getParameters();
		if ( null == parameters ) {
			return "";
		}
		return Arrays.stream( parameters ).map( String::valueOf ).collect( Collectors.joining( "_", "_", "" ) );
	}

	private void capTest( final ITestResult result ) {
		final Object testInstance = result.getInstance();
		final Consumer cap = RecheckLifecycle::capTest;
		execute( cap ).on( testInstance );
	}

	private void cap( final ITestResult result ) {
		final Object testInstance = result.getInstance();
		final Consumer cap = RecheckLifecycle::cap;
		execute( cap ).on( testInstance );
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy