Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.selgp.moladek.core.loggers;
  
  
  
  import java.io.*;
 import java.util.List;

Provides formatted logging support.

Author(s):
guillem.hernandez
 
 public class TestLogger {
 
     private final List<LogItemlogEvents = new ArrayList<LogItem>();
     private final TestMetrics testMetrics = new TestMetrics();
 
     private final String callingClassName;
     private final String logName;
 
     public TestLogger(Method testMethodObject[] testArguments) {
          = testMethod.getDeclaringClass().getName();
         // Tests using dataProviders inject different arguments and must log into
         // different files.
          =
                 FileUtil.replaceInvalidChars(testMethod.getName() + "_" + testArguments.hashCode() + "_%s");
         presetTestMetrics(testMethod.getName());
     }
 
     public TestLogger(String testNameString callingClassName) {
         this. = callingClassName;
          = FileUtil.replaceInvalidChars(testName + "_%s");
         presetTestMetrics(testName);
     }
 
     private TestMetrics getTestMetrics() {
         return ;
     }
 
     public List<LogItemgetLogEvents() {
         return ;
     }
 
     public void resetLogs() {
         getLogEvents().clear();
         getTestMetrics().resetMetrics();
     }
 
     private void presetFormatter(ResultsFormatter formatter) {
         String baseUri = "../screenshots";
         formatter.setResourceBaseUri(baseUri);
         formatter.setResourcesPath(.);
     }
 
     private void presetTestMetrics(String testName) {
         getTestMetrics().resetMetrics();
         getTestMetrics().setTestName(testName);
     }
 
     public void finalize(File resultFileNameAndPath) {
         FileWriter file = null;
         try {
             file = new FileWriter(resultFileNameAndPath);
             BufferedWriter buffer = new BufferedWriter(file);
             PrintWriter writer = new PrintWriter(buffer);
 
             ResultsFormatter formattter = new HtmlResultsFormatter(writer.);
             presetFormatter(formattter);
             new LogEventsPostProcessor(formatttergetLogEvents(), getTestMetrics());
 
             writer.close();
             buffer.close();
             file.close();
         } catch (IOException e) {
             throw new RuntimeException(e);
         }
     }
 
     public void log(String commandNameString[] argsString resultlong cmdStartMillis) {
         LogItem currentCommand =
                 presetLogItem(commandNameargsresultcmdStartMillis, System.currentTimeMillis());
 
         currentCommand
         String sourceMethodName = "unknown";
         if (classOrNull != null) {
             sourceMethodName = classOrNull.getMethodName();
         }
         currentCommand.setSourceMethod(sourceMethodName);
 
         getLogEvents().add(currentCommand);
     }
 
     public void logException(String commandNameString[] argsThrowable exception,
                             long cmdStartMillis) {
        String resultContent;
        String errorMessage;
        if (exception != null) {
            resultContent = exception.getClass().getName() + " - " + exception.getMessage();
            errorMessage = exception.getMessage();
        } else {
            resultContent = "FAIL";
            if (args.length > 0) {
                errorMessage = args[0];
            } else {
                errorMessage = "ERROR: real error-msg could not be determined";
            }
        }
        log(commandNameargsresultContentcmdStartMillis);
        getTestMetrics().setLastFailedCommandMessage(errorMessage);
    }
    public void logComment(final String comment) {
        long cmdStartMillis = System.currentTimeMillis();
        log("comment"new String[]{comment""}, ""cmdStartMillis);
    }
    public void logSeparator() {
        long cmdStartMillis = System.currentTimeMillis();
        log("separator"new String[]{""""}, ""cmdStartMillis);
    }
    private LogItem presetLogItem(String commandNameString[] argsString result,
                                  long cmdStartMillislong cmdEndMillis) {
        LogItem logitem = new LogItem();
        logitem.setCommandName(commandName);
        logitem.setArgs(args);
        logitem.setResult(result);
        logitem.setCmdStartMillis(cmdStartMillis);
        logitem.setCmdEndMillis(cmdEndMillis);
        return logitem;
    }
    private String getCallingClassName() {
        return ;
    }
        StackTraceElement[] testElements = Thread.currentThread().getStackTrace();
        StackTraceElement currentCallingClassAsStackTraceElement = null;
        for (StackTraceElement stackTraceElement : testElements) {
            if (containsClassName(stackTraceElementgetCallingClassName())) {
                currentCallingClassAsStackTraceElement = stackTraceElement;
                break;
            }
        }
        return currentCallingClassAsStackTraceElement;
    }
    private boolean containsClassName(StackTraceElement stackTraceElementString wantedClassName) {
        String className = stackTraceElement.getClassName();
        return className.contains(wantedClassName);
    }
        if (stackTraceElement != null) {
            return stackTraceElement.getClassName() + "#" + stackTraceElement.getLineNumber();
        } else {
            return "";
        }
    }
    public String getLogName() {
        return ;
    }
New to GrepCode? Check out our FAQ X