de.retest.recheck.testng.RecheckHook Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of recheck-testng-extension Show documentation
Show all versions of recheck-testng-extension Show documentation
TestNG extension reducing boilerplate code in tests.
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 );
}
}