Skip to content

Commit

Permalink
AbstractUnitTest/AbstractIntegrationTest refactored to new context
Browse files Browse the repository at this point in the history
  • Loading branch information
virgo47 committed Mar 2, 2020
1 parent 5dc70e3 commit a928cc1
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 106 deletions.
Expand Up @@ -9,26 +9,26 @@
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.ITestResult;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

import com.evolveum.midpoint.tools.testng.UnitTestMixin;
import com.evolveum.midpoint.tools.testng.MidpointTestContext;
import com.evolveum.midpoint.tools.testng.MidpointTestMixin;
import com.evolveum.midpoint.tools.testng.SimpleMidpointTestContext;
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;

/**
* Base test class for tests integrated with Spring providing {@link UnitTestMixin} implementation.
* Base test class for tests integrated with Spring providing {@link MidpointTestMixin} implementation.
* Can be extended by any unit test class that would otherwise extend
* {@link AbstractTestNGSpringContextTests}.
*/
public abstract class AbstractSpringTest extends AbstractTestNGSpringContextTests
implements UnitTestMixin {

private static final ThreadLocal<ITestResult> TEST_CONTEXT_THREAD_LOCAL = new ThreadLocal<>();
implements MidpointTestMixin {

/**
* Hides parent's logger, but that one is from commons-logging and we don't want that.
Expand All @@ -37,36 +37,26 @@ public abstract class AbstractSpringTest extends AbstractTestNGSpringContextTest

@BeforeMethod
public void startTestContext(ITestResult testResult) {
Class<?> testClass = testResult.getMethod().getTestClass().getRealClass();
String testMethodName = testResult.getMethod().getMethodName();
displayTestTitle(testClass.getSimpleName() + "." + testMethodName);

TEST_CONTEXT_THREAD_LOCAL.set(testResult);
SimpleMidpointTestContext context = SimpleMidpointTestContext.create(testResult);
displayTestTitle(context.getTestName());
}

@AfterMethod
public void finishTestContext(ITestResult testResult) {
TEST_CONTEXT_THREAD_LOCAL.remove();

displayDefaultTestFooter(testResult);
SimpleMidpointTestContext context = SimpleMidpointTestContext.get();
SimpleMidpointTestContext.destroy();
displayDefaultTestFooter(context.getTestName(), testResult);
}

@Override
public Trace logger() {
return logger;
}

@NotNull
public String contextName() {
ITestResult context = TEST_CONTEXT_THREAD_LOCAL.get();
return context != null
? getClass().getSimpleName() + "." + context.getMethod().getMethodName()
: getClass().getSimpleName();
}

@Override
public String getTestNameShort() {
return TEST_CONTEXT_THREAD_LOCAL.get().getMethod().getMethodName();
@Nullable
public MidpointTestContext getTestContext() {
return SimpleMidpointTestContext.get();
}

/**
Expand Down
Expand Up @@ -37,6 +37,7 @@
import ch.qos.logback.classic.LoggerContext;
import org.apache.commons.lang.SystemUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.opends.server.types.Entry;
import org.opends.server.types.SearchResultEntry;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -107,6 +108,7 @@
import com.evolveum.midpoint.test.ldap.OpenDJController;
import com.evolveum.midpoint.test.util.*;
import com.evolveum.midpoint.tools.testng.CurrentTestResultHolder;
import com.evolveum.midpoint.tools.testng.MidpointTestContext;
import com.evolveum.midpoint.util.*;
import com.evolveum.midpoint.util.exception.*;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
Expand Down Expand Up @@ -224,6 +226,12 @@ public void initSystem() throws Exception {
TestUtil.assertSuccessOrWarning("initSystem failed (result)", result, 1);
}

@Override
@Nullable
public MidpointTestContext getTestContext() {
return MidpointTestContextWithTask.get();
}

/**
* Test class initialization.
*/
Expand All @@ -242,7 +250,7 @@ protected void postInitSystem(Task initTask, OperationResult initResult) throws
/**
* Creates test method context which includes customized {@link Task}
* (see {@link #createTask(String)}) and other test related info wrapped as
* {@link MidpointTestMethodContext} and stores it in thread-local variable for future access.
* {@link MidpointTestContextWithTask} and stores it in thread-local variable for future access.
* This implementation fully overrides version from {@link AbstractSpringTest}.
*/
@BeforeMethod
Expand All @@ -268,7 +276,7 @@ public void startTestContext(ITestResult testResult) {
// (I.e. not via the test context.)
// task.setResult(result);

MidpointTestMethodContext.create(testClass, testMethodName, task, task.getResult());
MidpointTestContextWithTask.create(testClass, testMethodName, task, task.getResult());
// TODO inttest: remove after fix in TracerImpl
TestNameHolder.setCurrentTestName(contextName());
}
Expand All @@ -279,10 +287,10 @@ public void startTestContext(ITestResult testResult) {
*/
@AfterMethod
public void finishTestContext(ITestResult testResult) {
MidpointTestMethodContext context = MidpointTestMethodContext.get();
MidpointTestMethodContext.destroy(); // let's destroy it before anything else in this method
MidpointTestContextWithTask context = MidpointTestContextWithTask.get();
MidpointTestContextWithTask.destroy(); // let's destroy it before anything else in this method

displayDefaultTestFooter(testResult);
displayDefaultTestFooter(context.getTestName(), testResult);

Task task = context.getTask();
if (task != null) {
Expand Down Expand Up @@ -333,7 +341,7 @@ public void setPredefinedTestMethodTracing(PredefinedTestMethodTracing predefine
* Returns default pre-created test-method-scoped {@link Task}.
*/
protected Task getTestTask() {
return MidpointTestMethodContext.get().getTask();
return MidpointTestContextWithTask.get().getTask();
}

/**
Expand Down Expand Up @@ -373,7 +381,6 @@ protected Task createTask() {
return createPlainTask(null);
}


/**
* Subclasses may override this if test task needs additional customization.
* Each task used or created by the test is customized - this applies to default
Expand All @@ -383,16 +390,6 @@ protected void customizeTask(Task task) {
// nothing by default
}

@NotNull
public String contextName() {
MidpointTestMethodContext context = MidpointTestMethodContext.get();
return context != null ? context.getTestName() : getClass().getSimpleName();
}

public String getTestNameShort() {
return MidpointTestMethodContext.get().getTestNameShort();
}

/**
* Creates new subresult for default pre-created test-method-scoped {@link Task}.
*/
Expand Down Expand Up @@ -420,7 +417,7 @@ protected OperationResult createOperationalResult(String nameSuffix) {
* after method, only displayed.
*/
protected OperationResult getTestOperationResult() {
return MidpointTestMethodContext.get().getResult();
return MidpointTestContextWithTask.get().getResult();
}

public <C extends Containerable> S_ItemEntry deltaFor(Class<C> objectClass) throws SchemaException {
Expand Down
Expand Up @@ -9,16 +9,18 @@

import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.tools.testng.MidpointTestContext;

/**
* Value object carrying test context information like task, result and method name.
* <p>
* Static methods are used for creating the context and working with it via {@link ThreadLocal}.
* <b>It is important to to call {@link #destroy()} at the end (in some after-method).</b>
*/
public final class MidpointTestMethodContext {
public final class MidpointTestContextWithTask implements MidpointTestContext {

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

/**
* Actual test class - not abstract (where method may be implemented) but executed test class.
Expand All @@ -40,7 +42,7 @@ public final class MidpointTestMethodContext {
*/
private final OperationResult result;

private MidpointTestMethodContext(
private MidpointTestContextWithTask(
Class<?> testClass, String methodName, Task task, OperationResult result) {

this.testClass = testClass;
Expand All @@ -49,17 +51,13 @@ private MidpointTestMethodContext(
this.result = result;
}

/**
* Returns name of the test - which is "class-simple-name.method".
*/
public String getTestName() {
return testClass.getSimpleName() + "." + methodName;
@Override
public Class<?> getTestClass() {
return testClass;
}

/**
* Returns short name of the test - which is method name.
*/
public String getTestNameShort() {
@Override
public String getTestMethodName() {
return methodName;
}

Expand All @@ -71,16 +69,16 @@ public OperationResult getResult() {
return result;
}

public static MidpointTestMethodContext create(
public static MidpointTestContextWithTask create(
Class<?> testClass, String methodName, Task task, OperationResult result) {

MidpointTestMethodContext ctx =
new MidpointTestMethodContext(testClass, methodName, task, result);
MidpointTestContextWithTask ctx =
new MidpointTestContextWithTask(testClass, methodName, task, result);
TEST_CONTEXT_THREAD_LOCAL.set(ctx);
return ctx;
}

public static MidpointTestMethodContext get() {
public static MidpointTestContextWithTask get() {
return TEST_CONTEXT_THREAD_LOCAL.get();
}

Expand Down
Expand Up @@ -6,55 +6,42 @@
*/
package com.evolveum.midpoint.tools.testng;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

/**
* Base test class providing basic {@link UnitTestMixin} implementation.
* Base test class providing basic {@link MidpointTestMixin} implementation.
* Can be extended by any unit test class that otherwise doesn't extend anything.
*/
public abstract class AbstractUnitTest implements UnitTestMixin {

private static final ThreadLocal<ITestResult> TEST_CONTEXT_THREAD_LOCAL = new ThreadLocal<>();
public abstract class AbstractUnitTest implements MidpointTestMixin {

protected final Logger logger = LoggerFactory.getLogger(getClass());

@BeforeMethod
public void startTestContext(ITestResult testResult) {
Class<?> testClass = testResult.getMethod().getTestClass().getRealClass();
String testMethodName = testResult.getMethod().getMethodName();
displayTestTitle(testClass.getSimpleName() + "." + testMethodName);

TEST_CONTEXT_THREAD_LOCAL.set(testResult);
SimpleMidpointTestContext context = SimpleMidpointTestContext.create(testResult);
displayTestTitle(context.getTestName());
}

@AfterMethod
public void finishTestContext(ITestResult testResult) {
TEST_CONTEXT_THREAD_LOCAL.remove();

displayDefaultTestFooter(testResult);
}

@Override
public Logger logger() {
return logger;
SimpleMidpointTestContext context = SimpleMidpointTestContext.get();
SimpleMidpointTestContext.destroy();
displayDefaultTestFooter(context.getTestName(), testResult);
}

@Override
@NotNull
public String contextName() {
ITestResult context = TEST_CONTEXT_THREAD_LOCAL.get();
return context != null
? getTestName(context)
: getClass().getSimpleName();
@Nullable
public MidpointTestContext getTestContext() {
return SimpleMidpointTestContext.get();
}

@Override
public String getTestNameShort() {
return TEST_CONTEXT_THREAD_LOCAL.get().getMethod().getMethodName();
public Logger logger() {
return logger;
}
}

0 comments on commit a928cc1

Please sign in to comment.