
package org.eclipse.birt.test.performance;

import java.io.File;
import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.Map;

import org.eclipse.birt.test.performance.parser.Configuration;
import org.eclipse.birt.test.performance.parser.ConfigurationReader;
import org.eclipse.birt.test.performance.parser.TestCaseNode;
import org.eclipse.birt.test.performance.parser.Utility;
import org.eclipse.birt.test.performance.reporting.Reporter;
import org.eclipse.core.runtime.IPlatformRunnable;

/**
 * PerformanceExecutor - represent executable entry point into plug-in
 */

public class PerformanceExecutor implements IPlatformRunnable
{

	public Object run( Object args ) throws Exception
	{
		PerformanceExecutor.main( null );
		return null;
	}

	/**
	 * Entry point method for Java App.
	 * 
	 * @param args -
	 *            args
	 * @throws Exception
	 */

	public static void main( String[] args ) throws Exception
	{
		
		File outputdir = new File( Constants.BASE_PATH + Constants.OUTPUT_PATH );
		Utility.removeFolder( outputdir );
		outputdir.mkdir( );

		ConfigurationReader.parse( Constants.BASE_PATH + "config.xml" ); //$NON-NLS-1$

		Map cases = Configuration.getInstance( ).getTestCases( );
		Iterator testcases = cases.values( ).iterator( );
		while ( testcases.hasNext( ) )
		{
			TestCaseNode testcase = (TestCaseNode) testcases.next( );
			new CaseRunner( testcase ).run( );
		}

		String timestamp = Constants.DATE_FORMAT.format( Calendar.getInstance( )
				.getTime( ) );
		File report = new File( Constants.BASE_PATH,
				"reporting_" + timestamp + ".xml" ); //$NON-NLS-1$//$NON-NLS-2$
		Writer writer = new FileWriter( report );

		new Reporter( writer ).report( new ArrayList( cases.values( ) ) );
		System.out.println( "Finish successfully." ); //$NON-NLS-1$
	}
}