Package org.jboss.ant.taskdefs

Source Code of org.jboss.ant.taskdefs.JUnitLiteProfilerListener

/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.ant.taskdefs;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;

import junit.framework.AssertionFailedError;
import junit.framework.Test;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter;
import org.apache.tools.ant.taskdefs.optional.junit.JUnitTest;
import org.apache.tools.ant.taskdefs.optional.junit.JUnitVersionHelper;
import org.apache.tools.ant.taskdefs.optional.junit.XMLConstants;
import org.jboss.profiler.liteversion.consolidator.ConsolidatorData;
import org.jboss.profiler.liteversion.consolidator.DataConsumer;
import org.jboss.profiler.liteversion.output.ConsolidatorDataFactory;


/**
* Generates output information for a specific test
*/

public class JUnitLiteProfilerListener implements JUnitResultFormatter, XMLConstants {

    /**
     * Element for the current test.
     */
    private Hashtable testElements = new Hashtable();

    /**
     * tests that failed.
     */
    private Hashtable failedTests = new Hashtable();

    /**
     * Timing helper.
     */
    private Hashtable testStarts = new Hashtable();

    String jbossProfilerOutputDir;
    String jbossProfilerHost;
    int jbossProfilerPort;

    private ArrayList testCases = new ArrayList();

    /**
     * Where to write the log to.
     */
    private OutputStream out;

    public JUnitLiteProfilerListener() {
    }

    public void setOutput(OutputStream out) {
        this.out = out;
    }

    /**
     * The whole testsuite started.
     */
    public void startTestSuite(JUnitTest suite) {
        this.testCases.clear();
        this.jbossProfilerOutputDir = suite.getProperties().getProperty("junit.batchtest.todir");
        this.jbossProfilerHost = suite.getProperties().getProperty("jbossprofiler.host");
        if (this.jbossProfilerHost==null) {
            jbossProfilerHost = "localhost";
        }
        try {
            jbossProfilerPort = Integer.parseInt(suite.getProperties().getProperty("jbossprofiler.port"));
        } catch (Exception e) {
            jbossProfilerPort=1000;
        }
    }

    /**
     * The whole testsuite ended.
     */
    public void endTestSuite(JUnitTest suite) throws BuildException {
        ConsolidatorData sumData = new ConsolidatorData();
        Iterator iter = this.testCases.iterator();
        while (iter.hasNext()) {
            ConsolidatorData tmpData = (ConsolidatorData)iter.next();
            sumData.add(tmpData);
        }

        outputProfilerSummary();
        stopData();

    }

    /**
     *
     */
    private void outputProfilerSummary() {
        try {
            PrintStream outStream = new PrintStream(new BufferedOutputStream(new FileOutputStream(this.jbossProfilerOutputDir + "/profilerAggr.txt",true)));
            Iterator iterTests = this.testCases.iterator();

            while (iterTests.hasNext()) {
                Object obj = iterTests.next();
                String result = ConsolidatorDataFactory.exportProperties((ConsolidatorData)obj);
                outStream.println(result);
            }
            outStream.close();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
    }

    public void saveData() {
        DataConsumer consumer = new DataConsumer(jbossProfilerHost,jbossProfilerPort);
        try {
            consumer.activateMessage();
            consumer.saveMessage();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void stopData() {
        DataConsumer consumer = new DataConsumer(jbossProfilerHost,jbossProfilerPort);
        try {
            consumer.deActivateMessage();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

        /**
     * @return
     */
    public static ConsolidatorData readData(String host, int port) {
        //System.out.println("ConsolidatorData:Reading data start");
        ConsolidatorData[] data = null;
        ConsolidatorData sumData = new ConsolidatorData();
        try {
            DataConsumer consumer = new DataConsumer(host,port);
            Object [] obj = consumer.getMessages();
            data = new ConsolidatorData[obj.length];
            for (int i=0;i<data.length;i++) {
                data[i] = (ConsolidatorData) obj[i];
                sumData.add(data[i]);
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
        //System.out.println("ConsolidatorData:Reading data end");
        return sumData;
    }

    /**
     * Interface TestListener.
     *
     * <p>A new Test is started.
     */
    public void startTest(Test t) {
        saveData();
        testStarts.put(t, new Long(System.currentTimeMillis()));
    }

    /**
     * Interface TestListener.
     *
     * <p>A Test is finished.
     */
    public void endTest(Test test) {
        if (!testStarts.containsKey(test)) {
            startTest(test);
        }

        ConsolidatorData sumData = readData(jbossProfilerHost,jbossProfilerPort);
        sumData.setTestName(test.getClass().getName() + ":" + JUnitVersionHelper.getTestCaseName(test));
        this.testCases.add(sumData);
    }

    /* (non-Javadoc)
     * @see org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter#setSystemOutput(java.lang.String)
     */
    public void setSystemOutput(String arg0) {
    }

    /* (non-Javadoc)
     * @see org.apache.tools.ant.taskdefs.optional.junit.JUnitResultFormatter#setSystemError(java.lang.String)
     */
    public void setSystemError(String arg0) {
    }

    /* (non-Javadoc)
     * @see junit.framework.TestListener#addError(junit.framework.Test, java.lang.Throwable)
     */
    public void addError(Test arg0, Throwable arg1) {
    }

    /* (non-Javadoc)
     * @see junit.framework.TestListener#addFailure(junit.framework.Test, junit.framework.AssertionFailedError)
     */
    public void addFailure(Test arg0, AssertionFailedError arg1) {
    }


} // XMLJUnitResultFormatter
TOP

Related Classes of org.jboss.ant.taskdefs.JUnitLiteProfilerListener

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.