<commit_before>/*
 * Created by IntelliJ IDEA.
 * User: amrk
 * Date: Jul 9, 2005
 * Time: 4:19:20 PM
 */
package com.theoryinpractice.testng;

import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

import com.intellij.execution.filters.Filter;
import com.intellij.execution.filters.HyperlinkInfo;
import com.intellij.execution.filters.TextConsoleBuidlerFactory;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.ui.ConsoleView;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.theoryinpractice.testng.model.TestNGConsoleProperties;
import com.theoryinpractice.testng.ui.TestNGResults;
import com.theoryinpractice.testng.ui.DiffHyperLink;
import org.testng.remote.strprotocol.MessageHelper;
import org.testng.remote.strprotocol.TestResultMessage;

public class TestNGConsoleView implements ConsoleView
{
    private ConsoleView console;
    private TestNGResults testNGResults;
    private final List<Printable> allOutput = new ArrayList<Printable>();
    private int mark;
    private TestNGConsoleProperties consoleProperties;

    public TestNGConsoleView(Project project, TestNGConsoleProperties consoleProperties) {
        this.consoleProperties = consoleProperties;
        buildView(project);
    }

    public TestNGResults getResultsView() {
        return testNGResults;
    }

    public JComponent getComponent() {
        return testNGResults.getMain();
    }

    public TestNGConsoleProperties getConsoleProperties() {
        return consoleProperties;
    }

    public void addTestResult(TestResultMessage result) {
        List<Printable> list = null;
        if (result.getResult() == MessageHelper.TEST_STARTED) {
            mark();
        } else {
            String stackTrace = result.getStackTrace();
            if (stackTrace != null && stackTrace.length() > 10) {
                //trim useless crud from stacktrace
                String trimmed = trimStackTrace(stackTrace);
                List<Printable> printables = getPrintables(result, trimmed, ConsoleViewContentType.ERROR_OUTPUT);
                synchronized(allOutput) {
                    allOutput.addAll(printables);
                }
            }
            list = getPrintablesSinceMark();
        }

        testNGResults.addTestResult(result, list);
    }

    private String trimStackTrace(String stackTrace) {
        String[] lines = stackTrace.split("\n");
        StringBuilder builder = new StringBuilder();

        if (lines.length > 0) {
            int i = lines.length - 1;
            while (i >= 0) {
                //first 4 chars are '\t at '
                int startIndex = lines[i].indexOf('a') + 3;
                if (lines[i].length() > 4 && (lines[i].startsWith("org.testng.", startIndex)
                        || lines[i].startsWith("sun.reflect.DelegatingMethodAccessorImpl", startIndex)
                        || lines[i].startsWith("sun.reflect.NativeMethodAccessorImpl", startIndex)
                        || lines[i].startsWith("java.lang.reflect.Method", startIndex)
                        || lines[i].startsWith("com.intellij.rt.execution.application.AppMain", startIndex)
                )) {

                } else {
                    // we're done with internals, so we know the rest are ok
                    break;
                }
                i--;
            }
            for (int j = 0; j <= i; j++) {
                builder.append(lines[j]);
                builder.append('\n');
            }
        }
        return builder.toString();
    }

    private void buildView(Project project) {
        console = TextConsoleBuidlerFactory.getInstance().createBuilder(project).getConsole();
        testNGResults = new TestNGResults(project, this);
        testNGResults.getTabbedPane().add("Output", console.getComponent());
        testNGResults.getTabbedPane().add("Statistics", new JScrollPane(testNGResults.getResultsTable()));
    }

    public void mark() {
        mark = allOutput.size();
    }

    public List<Printable> getPrintablesSinceMark() {
        synchronized (allOutput) {
            return new ArrayList<Printable>(allOutput.subList(mark, allOutput.size()));
        }
    }

    public void dispose() {
        console.dispose();
        console = null;
        testNGResults.dispose();
        testNGResults = null;
        consoleProperties.dispose();
        consoleProperties = null;
    }

    private List<Printable> getPrintables(TestResultMessage result, String s, ConsoleViewContentType type) {
        List<Printable> printables = new ArrayList<Printable>();
        //figure out if we have a diff we need to hyperlink
        //TODO replace this with a saner regexp
        String assertText = "java.lang.AssertionError: expected:<";
        if(s.startsWith(assertText)) {
            printables.add(new Chunk("java.lang.AssertionError:", type));
            String end = "> but was:<";
            int actualStart = s.indexOf(end, assertText.length());
            String expected = s.substring(assertText.length(), actualStart);
            int actualEnd = s.indexOf("org.testng.Assert", actualStart + end.length());
            actualEnd = s.lastIndexOf('>', actualEnd);
            int stackTraceEnd = s.lastIndexOf("org.testng.Assert.");
            stackTraceEnd = s.indexOf('\n', stackTraceEnd) + 1;
            //we have an assert with expected/actual, so we parse it out and create a diff hyperlink
            DiffHyperLink link = new DiffHyperLink(expected, s.substring(actualStart + end.length(), actualEnd), null);
            //TODO should do some more farting about to find the equality assertion that failed and show that as title
            //same as junit diff view
            link.setTitle(result.getTestClass() + '#' + result.getMethod() + "() failed");
            printables.add(link);
            printables.add(new Chunk(trimStackTrace(s.substring(stackTraceEnd)), type));
        } else {
            printables.add(new Chunk(s, type));
        }
        return printables;
    }
    
    public void print(String s, ConsoleViewContentType contentType) {
        Chunk chunk = new Chunk(s, contentType);
        synchronized (allOutput) {
            allOutput.add(chunk);
        }
    }

    public void reset() {
        setView(allOutput);
    }

    public void clear() {
        console.clear();
    }

    public void scrollTo(int offset) {
        console.scrollTo(offset);
    }

    public void attachToProcess(ProcessHandler processHandler) {
        console.attachToProcess(processHandler);
    }

    public void setOutputPaused(boolean value) {
        console.setOutputPaused(value);
    }

    public boolean isOutputPaused() {
        return console.isOutputPaused();
    }

    public boolean hasDeferredOutput() {
        return console.hasDeferredOutput();
    }

    public void performWhenNoDeferredOutput(Runnable runnable) {
        console.performWhenNoDeferredOutput(runnable);
    }

    public void setHelpId(String helpId) {
        console.setHelpId(helpId);
    }

    public void addMessageFilter(Filter filter) {
        console.addMessageFilter(filter);
    }

    public void printHyperlink(String hyperlinkText, HyperlinkInfo info) {
        console.printHyperlink(hyperlinkText, info);
    }

    public int getContentSize() {
        return console.getContentSize();
    }

    public boolean canPause() {
        return console.canPause();
    }

    public void setView(final List<Printable> output) {
        if (!ApplicationManager.getApplication().isDispatchThread()) {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run() {
                    setView(output);
                }
            });<commit_msg>Added needed method for new selena builds.<commit_after>/*
 * Created by IntelliJ IDEA.
 * User: amrk
 * Date: Jul 9, 2005
 * Time: 4:19:20 PM
 */
package com.theoryinpractice.testng;

import java.util.ArrayList;
import java.util.List;
import javax.swing.*;

import com.intellij.execution.filters.Filter;
import com.intellij.execution.filters.HyperlinkInfo;
import com.intellij.execution.filters.TextConsoleBuidlerFactory;
import com.intellij.execution.process.ProcessHandler;
import com.intellij.execution.ui.ConsoleView;
import com.intellij.execution.ui.ConsoleViewContentType;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.project.Project;
import com.theoryinpractice.testng.model.TestNGConsoleProperties;
import com.theoryinpractice.testng.ui.DiffHyperLink;
import com.theoryinpractice.testng.ui.TestNGResults;
import org.testng.remote.strprotocol.MessageHelper;
import org.testng.remote.strprotocol.TestResultMessage;

public class TestNGConsoleView implements ConsoleView
{
    private ConsoleView console;
    private TestNGResults testNGResults;
    private final List<Printable> allOutput = new ArrayList<Printable>();
    private int mark;
    private TestNGConsoleProperties consoleProperties;

    public TestNGConsoleView(Project project, TestNGConsoleProperties consoleProperties) {
        this.consoleProperties = consoleProperties;
        buildView(project);
    }

    public TestNGResults getResultsView() {
        return testNGResults;
    }

    public JComponent getComponent() {
        return testNGResults.getMain();
    }

    public TestNGConsoleProperties getConsoleProperties() {
        return consoleProperties;
    }

    public void addTestResult(TestResultMessage result) {
        List<Printable> list = null;
        if (result.getResult() == MessageHelper.TEST_STARTED) {
            mark();
        } else {
            String stackTrace = result.getStackTrace();
            if (stackTrace != null && stackTrace.length() > 10) {
                //trim useless crud from stacktrace
                String trimmed = trimStackTrace(stackTrace);
                List<Printable> printables = getPrintables(result, trimmed, ConsoleViewContentType.ERROR_OUTPUT);
                synchronized (allOutput) {
                    allOutput.addAll(printables);
                }
            }
            list = getPrintablesSinceMark();
        }

        testNGResults.addTestResult(result, list);
    }

    private String trimStackTrace(String stackTrace) {
        String[] lines = stackTrace.split("\n");
        StringBuilder builder = new StringBuilder();

        if (lines.length > 0) {
            int i = lines.length - 1;
            while (i >= 0) {
                //first 4 chars are '\t at '
                int startIndex = lines[i].indexOf('a') + 3;
                if (lines[i].length() > 4 && (lines[i].startsWith("org.testng.", startIndex)
                        || lines[i].startsWith("sun.reflect.DelegatingMethodAccessorImpl", startIndex)
                        || lines[i].startsWith("sun.reflect.NativeMethodAccessorImpl", startIndex)
                        || lines[i].startsWith("java.lang.reflect.Method", startIndex)
                        || lines[i].startsWith("com.intellij.rt.execution.application.AppMain", startIndex)
                )) {

                } else {
                    // we're done with internals, so we know the rest are ok
                    break;
                }
                i--;
            }
            for (int j = 0; j <= i; j++) {
                builder.append(lines[j]);
                builder.append('\n');
            }
        }
        return builder.toString();
    }

    private void buildView(Project project) {
        console = TextConsoleBuidlerFactory.getInstance().createBuilder(project).getConsole();
        testNGResults = new TestNGResults(project, this);
        testNGResults.getTabbedPane().add("Output", console.getComponent());
        testNGResults.getTabbedPane().add("Statistics", new JScrollPane(testNGResults.getResultsTable()));
    }

    public void mark() {
        mark = allOutput.size();
    }

    public List<Printable> getPrintablesSinceMark() {
        synchronized (allOutput) {
            return new ArrayList<Printable>(allOutput.subList(mark, allOutput.size()));
        }
    }

    public void dispose() {
        console.dispose();
        console = null;
        testNGResults.dispose();
        testNGResults = null;
        consoleProperties.dispose();
        consoleProperties = null;
    }

    private List<Printable> getPrintables(TestResultMessage result, String s, ConsoleViewContentType type) {
        List<Printable> printables = new ArrayList<Printable>();
        //figure out if we have a diff we need to hyperlink
        //TODO replace this with a saner regexp
        String assertText = "java.lang.AssertionError: expected:<";
        if (s.startsWith(assertText)) {
            printables.add(new Chunk("java.lang.AssertionError:", type));
            String end = "> but was:<";
            int actualStart = s.indexOf(end, assertText.length());
            String expected = s.substring(assertText.length(), actualStart);
            int actualEnd = s.indexOf("org.testng.Assert", actualStart + end.length());
            actualEnd = s.lastIndexOf('>', actualEnd);
            int stackTraceEnd = s.lastIndexOf("org.testng.Assert.");
            stackTraceEnd = s.indexOf('\n', stackTraceEnd) + 1;
            //we have an assert with expected/actual, so we parse it out and create a diff hyperlink
            DiffHyperLink link = new DiffHyperLink(expected, s.substring(actualStart + end.length(), actualEnd), null);
            //TODO should do some more farting about to find the equality assertion that failed and show that as title
            //same as junit diff view
            link.setTitle(result.getTestClass() + '#' + result.getMethod() + "() failed");
            printables.add(link);
            printables.add(new Chunk(trimStackTrace(s.substring(stackTraceEnd)), type));
        } else {
            printables.add(new Chunk(s, type));
        }
        return printables;
    }

    public void print(String s, ConsoleViewContentType contentType) {
        Chunk chunk = new Chunk(s, contentType);
        synchronized (allOutput) {
            allOutput.add(chunk);
        }
    }

    public void reset() {
        setView(allOutput);
    }

    public void clear() {
        console.clear();
    }

    public void scrollTo(int offset) {
        console.scrollTo(offset);
    }

    public void attachToProcess(ProcessHandler processHandler) {
        console.attachToProcess(processHandler);
    }

    public void setOutputPaused(boolean value) {
        console.setOutputPaused(value);
    }

    public boolean isOutputPaused() {
        return console.isOutputPaused();
    }

    public boolean hasDeferredOutput() {
        return console.hasDeferredOutput();
    }

    public void performWhenNoDeferredOutput(Runnable runnable) {
        console.performWhenNoDeferredOutput(runnable);
    }

    public void setHelpId(String helpId) {
        console.setHelpId(helpId);
    }

    public void addMessageFilter(Filter filter) {
        console.addMessageFilter(filter);
    }

    public JComponent getPreferredFocusableComponent() {
        return console.getComponent();
    }

    public void printHyperlink(String hyperlinkText, HyperlinkInfo info) {
        console.printHyperlink(hyperlinkText, info);
    }

    public int getContentSize() {
        return console.getContentSize();
    }

    public boolean canPause() {
        return console.canPause();
    }

    public void setView(final List<Printable> output) {
        if (!ApplicationManager.getApplication().isDispatchThread()) {
            SwingUtilities.invokeLater(new Runnable()
            {
                public void run() {
                    setView(output);
                }
            });
{'fraction_non_alphanumeric': 0.10248837501570944, 'fraction_numerical': 0.0025135101168782203, 'mean_word_length': 2.465055519268452, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4824768', 'n_tokens_mistral': 4346, 'n_tokens_neox': 4147, 'n_words': 937}