Skip to content

Commit

Permalink
tests: introduced MidpointTestContext and its base implementation
Browse files Browse the repository at this point in the history
This context also provides basic getTestName* methods based on
abstract methods getTestClass() and getTestMethodName().
  • Loading branch information
virgo47 committed Mar 2, 2020
1 parent aafe61e commit 5dc70e3
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 0 deletions.
@@ -0,0 +1,38 @@
package com.evolveum.midpoint.tools.testng;

/**
* Basic contract for test-method context (typically available through thread-local variable).
*/
public interface MidpointTestContext {

/**
* Returns the actual instantiated test class.
*/
Class<?> getTestClass();

/**
* Returns the name of the test method.
*/
String getTestMethodName();

/**
* Returns test name in form of "class-simple-name.method".
*/
default String getTestName() {
return getTestClass().getSimpleName() + "." + getTestMethodName();
}

/**
* Returns short test name - currently the same like {@link #getTestMethodName()}.
*/
default String getTestNameShort() {
return getTestMethodName();
}

/**
* Returns long test name in form of "fully.qualified.class-name.method".
*/
default String getTestNameLong() {
return getTestClass().getName() + "." + getTestMethodName();
}
}
@@ -0,0 +1,39 @@
package com.evolveum.midpoint.tools.testng;

import org.testng.ITestResult;

public class SimpleMidpointTestContext implements MidpointTestContext {

private static final ThreadLocal<SimpleMidpointTestContext> TEST_CONTEXT_THREAD_LOCAL =
new ThreadLocal<>();

private final ITestResult testResult;

public SimpleMidpointTestContext(ITestResult testResult) {
this.testResult = testResult;
}

@Override
public Class<?> getTestClass() {
return testResult.getMethod().getTestClass().getRealClass();
}

@Override
public String getTestMethodName() {
return testResult.getMethod().getMethodName();
}

public static SimpleMidpointTestContext create(ITestResult testResult) {
SimpleMidpointTestContext ctx = new SimpleMidpointTestContext(testResult);
TEST_CONTEXT_THREAD_LOCAL.set(ctx);
return ctx;
}

public static SimpleMidpointTestContext get() {
return TEST_CONTEXT_THREAD_LOCAL.get();
}

public static void destroy() {
TEST_CONTEXT_THREAD_LOCAL.remove();
}
}

0 comments on commit 5dc70e3

Please sign in to comment.