Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension/report info 2 #197

Merged
merged 10 commits into from
Jan 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Testerra
*
* (C) 2020, Mike Reiche, T-Systems Multimedia Solutions GmbH, Deutsche Telekom AG
* (C) 2021, Mike Reiche, T-Systems MMS GmbH, Deutsche Telekom AG
*
* Deutsche Telekom AG and all other contributors /
* copyright owners license this file to you under the Apache
Expand All @@ -17,14 +17,14 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package eu.tsystems.mms.tic.testframework.logging;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public interface Loggable {
Prompt prompt = new Prompt();

default Logger log() {
return LoggerFactory.getLogger(this.getClass());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Testerra
*
* (C) 2020, Mike Reiche, T-Systems Multimedia Solutions GmbH, Deutsche Telekom AG
* (C) 2021, Mike Reiche, T-Systems MMS GmbH, Deutsche Telekom AG
*
* Deutsche Telekom AG and all other contributors /
* copyright owners license this file to you under the Apache
Expand All @@ -19,26 +19,42 @@
* under the License.
*/

package eu.tsystems.mms.tic.testframework.report;
package eu.tsystems.mms.tic.testframework.logging;

import eu.tsystems.mms.tic.testframework.report.model.context.LogMessage;
import eu.tsystems.mms.tic.testframework.report.model.context.MethodContext;
import eu.tsystems.mms.tic.testframework.report.model.steps.TestStep;
import eu.tsystems.mms.tic.testframework.report.utils.ExecutionContextController;
import java.util.Arrays;
import java.util.Optional;
import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.appender.AbstractAppender;
import org.apache.logging.log4j.message.Message;

public class TestStepLogAppender extends AbstractAppender {
public class MethodContextLogAppender extends AbstractAppender {

public TestStepLogAppender() {
super("TestStepLogAppender", null, null, true, null);
public MethodContextLogAppender() {
super("MethodContextLogAppender", null, null, true, null);
}

@Override
public void append(LogEvent event) {
LogMessage logMessage = new LogMessage(event);
Optional<MethodContext> optionalMethodContext = ExecutionContextController.getMethodContextForThread();
public void append(final LogEvent event) {
final LogMessage logMessage = new LogMessage(event);
Optional<MethodContext> optionalMethodContext;
final Message message = event.getMessage();
if (message.getParameters() != null) {
optionalMethodContext = Arrays.stream(event.getMessage().getParameters())
.filter(MethodContext.class::isInstance)
.map(o -> (MethodContext) o)
.findFirst();
} else {
optionalMethodContext = Optional.empty();
}

// Otherwise, we take the current method context from execution context
if (!optionalMethodContext.isPresent()) {
optionalMethodContext = ExecutionContextController.getMethodContextForThread();
}

if (optionalMethodContext.isPresent()) {
optionalMethodContext.get().addLogMessage(logMessage);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Testerra
*
* (C) 2020, Mike Reiche, T-Systems Multimedia Solutions GmbH, Deutsche Telekom AG
* (C) 2021, Mike Reiche, T-Systems MMS GmbH, Deutsche Telekom AG
*
* Deutsche Telekom AG and all other contributors /
* copyright owners license this file to you under the Apache
Expand All @@ -17,16 +17,9 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package eu.tsystems.mms.tic.testframework.logging;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public interface Loggable {
package eu.tsystems.mms.tic.testframework.logging;

default Logger log() {
return LoggerFactory.getLogger(this.getClass());
}
public class Prompt {
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import eu.tsystems.mms.tic.testframework.execution.testng.worker.start.SortMethodsByPriorityMethodInterceptor;
import eu.tsystems.mms.tic.testframework.internal.BuildInformation;
import eu.tsystems.mms.tic.testframework.logging.Loggable;
import eu.tsystems.mms.tic.testframework.logging.MethodContextLogAppender;
import eu.tsystems.mms.tic.testframework.monitor.JVMMonitor;
import eu.tsystems.mms.tic.testframework.report.hooks.ConfigMethodHook;
import eu.tsystems.mms.tic.testframework.report.hooks.TestMethodHook;
Expand Down Expand Up @@ -121,6 +122,7 @@ public class TesterraListener implements
private static DefaultTestNGContextGenerator contextGenerator;
private static final TestStatusController testStatusController = new TestStatusController();
private static final ConcurrentHashMap<ITestNGMethod, Boolean> dataProviderSemaphore = new ConcurrentHashMap<>();
private static final MethodContextLogAppender logAppender;

static {
String logLevel = PropertyManager.getProperty("log4j.level");
Expand All @@ -130,6 +132,12 @@ public class TesterraListener implements
}
DefaultConfiguration defaultConfiguration = new DefaultConfiguration();
loggerContext = Configurator.initialize(defaultConfiguration);

// Enable report formatter here
logAppender = new MethodContextLogAppender();
logAppender.start();
loggerContext.getRootLogger().addAppender(logAppender);

buildInformation = new BuildInformation();
eventBus = new EventBus();
report = new DefaultReport();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@
package eu.tsystems.mms.tic.testframework.report.model.context;

import eu.tsystems.mms.tic.testframework.logging.Loggable;
import eu.tsystems.mms.tic.testframework.logging.Prompt;
import java.util.Arrays;
import java.util.Optional;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LogEvent;

import java.io.Serializable;
import java.util.Date;
import org.apache.logging.log4j.message.Message;

/**
* Copy of {@link LogEvent} because Log4j2 reuses the event instance
Expand All @@ -39,28 +41,30 @@ public class LogMessage implements Serializable, Loggable {
private final Throwable thrown;
private final String message;
private final Level level;
private final boolean prompt;

/**
* Creates a log message based on a Log4J log event
*/
public LogMessage(LogEvent event) {
this.timestamp = event.getTimeMillis();
this.threadName = event.getThreadName();
this.loggerName = event.getLoggerName();
this.thrown = event.getThrown();
this.message = event.getMessage().getFormattedMessage();
final Message message = event.getMessage();
if (message.getParameters() != null) {
this.prompt = Arrays.stream(message.getParameters()).anyMatch(Prompt.class::isInstance);
} else {
this.prompt = false;
}
this.message = message.getFormattedMessage();
this.level = event.getLevel();
}

public Level getLogLevel() {
return this.level;
}

/**
* Required by velocity templates
*/
@Deprecated
public Date getDate() {
return new Date(this.getTimestamp());
}

public long getTimestamp() {
return this.timestamp;
}
Expand All @@ -77,7 +81,11 @@ public String getMessage() {
return this.message;
}

public Throwable getThrown() {
return this.thrown;
public Optional<Throwable> getThrown() {
return Optional.ofNullable(this.thrown);
}

public boolean isPrompt() {
return prompt;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import eu.tsystems.mms.tic.testframework.annotations.Fails;
import eu.tsystems.mms.tic.testframework.events.ContextUpdateEvent;
import eu.tsystems.mms.tic.testframework.internal.Counters;
import eu.tsystems.mms.tic.testframework.logging.Loggable;
import eu.tsystems.mms.tic.testframework.report.FailureCorridor;
import eu.tsystems.mms.tic.testframework.report.Status;
import eu.tsystems.mms.tic.testframework.report.TesterraListener;
Expand Down Expand Up @@ -64,13 +65,7 @@ public enum Type {
private final String threadName;
private TestStep lastFailedStep;
private Class failureCorridorClass = FailureCorridor.High.class;

/**
* @deprecated
*/
public final List<String> infos = new LinkedList<>();
private final List<SessionContext> sessionContexts = new LinkedList<>();
private String priorityMessage = null;
private final TestStepController testStepController = new TestStepController();
private final List<MethodContext> relatedMethodContexts = new LinkedList<>();
private final List<MethodContext> dependsOnMethodContexts = new LinkedList<>();
Expand Down Expand Up @@ -235,14 +230,7 @@ public void addError(ErrorContext errorContext) {

@Deprecated
public void addPriorityMessage(String msg) {

if (priorityMessage == null) {
priorityMessage = "";
}

if (!priorityMessage.contains(msg)) {
priorityMessage += msg;
}
log().info(msg, this, Loggable.prompt);
}

public boolean isConfigMethod() {
Expand Down Expand Up @@ -347,15 +335,24 @@ public String getThreadName() {
return threadName;
}

/**
* @deprecated Use {@link TestStepAction#readEntries()} instead
*/
public Stream<String> readInfos() {
return infos.stream();
return Stream.empty();
}

/**
* @deprecated Use {@link TestStepAction#addLogMessage(LogMessage)} instead
*/
public void addInfo(String info) {
this.infos.add(info);
log().info(info, this, Loggable.prompt);
}

/**
* @deprecated Use {@link TestStepAction#readEntries()} instead
*/
public Optional<String> getPriorityMessage() {
return Optional.ofNullable(priorityMessage);
return Optional.empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,8 @@ public static MethodContext getMethodContextFromTestContextAndMethod(ITestContex
public static MethodContext setCurrentTestResult(ITestResult iTestResult) {
CURRENT_TEST_RESULT.set(iTestResult);
MethodContext methodContext = getMethodContextFromTestResult(iTestResult);
setCurrentMethodContext(methodContext);
return methodContext;
}

/**
* Set currently active test.
*
* @param methodContext Method Context.
*/
private static void setCurrentMethodContext(final MethodContext methodContext) {
CURRENT_METHOD_CONTEXT.set(methodContext);
return methodContext;
}

public static void setCurrentSessionContext(final SessionContext sessionContext) {
Expand Down
Loading