package net.randomsync.testng.excel;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.testng.ITestNGListener;
import org.testng.TestNG;
import org.testng.xml.XmlSuite;

public class ExcelTestNGRunner {
	private String source;
	private TestNG testng;
	private IExcelFileParser parser;

	// TODO implement local variables to hold TestNG configuration parameters,
	// otherwise the need to set to any custom parameter (like thread count)
	// will require the user of this class to create TestNG object

	public ExcelTestNGRunner(String source) {
		this.source = source;
	}

	public ExcelTestNGRunner(String source, IExcelFileParser parser) {
		this.source = source;
		this.parser = parser;
	}

	public void setSource(String source) {
		this.source = source;
	}

	public void setTestng(TestNG testng) {
		this.testng = testng;
	}

	public void setParser(IExcelFileParser parser) {
		this.parser = parser;
	}

	public void run() {
		File srcFile = new File(source);
		File[] filesList = null; // if source is a directory, this will hold the
									// list of excel files
		// make sure the file source exists
		if (!srcFile.exists()) {
			throw new IllegalArgumentException("The source for the Excel "
					+ "file(s) cannot be found.");
		}
		// if source is a folder, get all excel files within it
		if (srcFile.isDirectory()) {
			filesList = srcFile.listFiles(new ExcelFileNameFilter());
		} else {
			filesList = new File[] { srcFile };
		}
		if (this.testng == null) {
			this.testng = new TestNG();
		}
		if (this.parser == null) {
			this.parser = new ExcelSuiteParser();
		}
		// this will keep a list of all XmlSuites to be run
		List<XmlSuite> allSuites = new ArrayList<XmlSuite>();
		// parse each file and each worksheet into an XmlSuite
		for (File file : filesList) {
			List<XmlSuite> suites = new ArrayList<XmlSuite>();
			try {
				suites = parser.getXmlSuites(file, false);
			} catch (InvalidFormatException e) {
				// e.printStackTrace();
				// any issues with parsing, skip this suite and continue
				continue;
			} catch (IOException e) {
				// e.printStackTrace();
				// any issues with parsing, skip this suite and continue
				continue;
			}
			for (XmlSuite suite : suites) {
				allSuites.add(suite);
			}
		}
		testng.setXmlSuites(allSuites);
		testng.run();
	}

	// helper methods to specify testng configuration
	public void setSuiteThreadPoolSize(int suiteThreadPoolSize) {
		if (testng != null) {
			testng.setSuiteThreadPoolSize(suiteThreadPoolSize);
		}
	}

	public void addListener(ITestNGListener listener) {
		if (testng != null) {
			testng.addListener(listener);
		}
	}

	public void setVerbose(int verbose) {
		if (testng != null) {
			testng.setVerbose(verbose);
		}
	}

	public void setPreserveOrder(boolean order) {
		if (testng != null) {
			testng.setPreserveOrder(order);
		}
	}

	public void setThreadCount(int threadCount) {
		if (testng != null) {
			testng.setThreadCount(threadCount);
		}
	}

	class ExcelFileNameFilter implements FilenameFilter {

		@Override
		public boolean accept(File file, String name) {
			if (name.endsWith(".xls") || name.endsWith(".xlsx")) {
				return true;
			}
			return false;
		}

	}

}

@@ -59,47 +59,8 @@
 e;%0D%0A
-import java.io.FileNotFoundException;%0D%0A
 impo
@@ -414,24 +414,39 @@
 estNG testng
+ = new TestNG()
 ;%0D%0A%09private 
@@ -477,227 +477,8 @@
 %0D%0A%0D%0A
-%09// TODO implement local variables to hold TestNG configuration parameters,%0D%0A%09// otherwise the need to set to any custom parameter (like thread count)%0D%0A%09// will require the user of this class to create TestNG object%0D%0A%0D%0A
 %09pub

initialize TestNG at object creation