package com.lazerycode.jmeter;

import org.apache.maven.plugin.MojoFailureException;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
/**
 * Handles checking the jmeter xml logs for errors and failures.
 *
 * @author Jon Roberts
 */
public class ErrorScanner {

    private static final String ERROR_ELEMENT = "<error>true</error>";
    private static final String REQUEST_FAILURE_ELEMENT = "s=\"false\"";
    private static final String FAILURE_ELEMENT = "<failure>true</failure>";

    private boolean ignoreErrors;
    private boolean ignoreFailures;
    private int failureCount = 0;
    private int errorCount = 0;

    public ErrorScanner(boolean ignoreErrors, boolean ignoreFailures) {
        this.ignoreErrors = ignoreErrors;
        this.ignoreFailures = ignoreFailures;
    }

    /**
     * Check given file for errors
     * @param file
     * @return true of file doesn't contain errors
     * @throws IOException
     */
    public boolean hasTestPassed(File file) throws IOException {
        resetErrorAndFailureCount();
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(file));
            String line;
            while ((line = in.readLine()) != null) {
                this.checkLineForErrors(line);
            }
        } finally {
            if (in != null) {
                in.close();
            }
        }
        if (this.errorCount == 0 && this.failureCount == 0) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @return failureCount
     */
    public int getFailureCount() {
        return this.failureCount;
    }

    /**
     * @return errorCount
     */
    public int getErrorCount() {
        return this.errorCount;
    }

    // ---------------------------------------------------------

    /**
     * protected for testing
     *
     * @param line
     * @return boolean
     * @throws MojoFailureException
     */
    protected boolean checkLineForErrors(String line) {
        boolean lineContainsError = false;
        if (line.contains(ERROR_ELEMENT)) {
            if (!this.ignoreErrors) {
                this.errorCount++;
                lineContainsError = true;
            }
        }
        if (line.contains(FAILURE_ELEMENT) || line.contains(REQUEST_FAILURE_ELEMENT)) {
            if (!this.ignoreFailures) {
                this.failureCount++;
                lineContainsError = true;
            }
        }
        return lineContainsError;
    }

    // =========================================================

    private void resetErrorAndFailureCount() {
        this.failureCount = 0;
        this.errorCount = 0;
    }
}

@@ -314,16 +314,67 @@
 nner %7B%0A%0A
+    //TODO: where would such an element come from?%0A
     priv
@@ -436,16 +436,67 @@
 rror%3E%22;%0A
+    //TODO: where would such an element come from?%0A
     priv
@@ -519,24 +519,16 @@
  String 
-REQUEST_
 FAILURE_
@@ -542,22 +542,35 @@
  = %22
-s=%5C%22false%5C%22
+%3Cfailure%3Etrue%3C/failure%3E
 %22;%0A
+%0A
     
@@ -589,32 +589,40 @@
 ic final String 
+REQUEST_
 FAILURE_ELEMENT 
@@ -624,39 +624,27 @@
 MENT = %22
-%3Cfailure%3Etrue%3C/failure%3E
+s=%5C%22false%5C%22
 %22;%0A%0A    
@@ -1808,32 +1808,62 @@
 %0A    %7D%0A%0A    /**%0A
+     * TODO: what are errors?%0A
      * @return e

add TODOs for weird code