package technology.tabula;

import static org.junit.Assert.*;

import java.io.IOException;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.ParseException;
import org.junit.Test;

public class TestCommandLineApp {

	private String csvFromCommandLineArgs(String[] args) throws ParseException {
		CommandLineParser parser = new GnuParser();
		CommandLine cmd = parser.parse(CommandLineApp.buildOptions(), args);

		StringBuilder stringBuilder = new StringBuilder();
		new CommandLineApp(stringBuilder, cmd).extractTables(cmd);

		return stringBuilder.toString();
	}

	@Test
	public void testExtractSpreadsheetWithArea() throws ParseException, IOException {

		String expectedCsv = UtilsForTesting.loadCsv("src/test/resources/technology/tabula/csv/spreadsheet_no_bounding_frame.csv");

		assertEquals(expectedCsv, this.csvFromCommandLineArgs(new String[] {
				"src/test/resources/technology/tabula/spreadsheet_no_bounding_frame.pdf",
				"-p", "1", "-a",
				"150.56,58.9,654.7,536.12", "-f",
				"CSV"
		}));
	}

	@Test
	public void testExtractSpreadsheetWithAreaAndNewFile() throws ParseException, IOException {

		String expectedCsv = UtilsForTesting.loadCsv("src/test/resources/technology/tabula/csv/spreadsheet_no_bounding_frame.csv");

		 this.csvFromCommandLineArgs(new String[] {
					"src/test/resources/technology/tabula/spreadsheet_no_bounding_frame.pdf",
					"-p", "1", "-a",
					"150.56,58.9,654.7,536.12", "-f",
					"CSV", "-o", "outputFile"
			});
		//assertEquals(expectedCsv,);
	}


	@Test
	public void testExtractJSONWithArea() throws ParseException, IOException {

		String expectedJson = UtilsForTesting.loadJson("src/test/resources/technology/tabula/json/spanning_cells_basic.json");

		assertEquals(expectedJson, this.csvFromCommandLineArgs(new String[] {
				"src/test/resources/technology/tabula/spanning_cells.pdf",
				"-p", "1", "-a",
				"150.56,58.9,654.7,536.12", "-f",
				"JSON"
		}));
	}

	@Test
	public void testGuessOption() throws ParseException, IOException {
		String expectedCsvNoGuessing = UtilsForTesting.loadCsv("src/test/resources/technology/tabula/csv/TestCommandLineApp_testGuessOption_no_guessing.csv");
		assertEquals(expectedCsvNoGuessing, this.csvFromCommandLineArgs(new String[] {
				"src/test/resources/technology/tabula/icdar2013-dataset/competition-dataset-eu/eu-001.pdf",
				"-p", "1",
				"-f", "CSV"
		}));

		String expectedCsvWithGuessing = UtilsForTesting.loadCsv("src/test/resources/technology/tabula/csv/TestCommandLineApp_testGuessOption_with_guessing.csv");
		assertEquals(expectedCsvWithGuessing, this.csvFromCommandLineArgs(new String[] {
				"src/test/resources/technology/tabula/icdar2013-dataset/competition-dataset-eu/eu-001.pdf",
				"-p", "1",
				"-f", "CSV",
				"-g"
		}));
	}

}

@@ -83,16 +83,138 @@
 ception;
+%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path;%0Aimport java.nio.file.FileSystem;%0Aimport java.nio.file.FileSystems;
 %0A%0Aimport
@@ -1234,32 +1234,994 @@
 CSV%22%0A%09%09%7D));%0A%09%7D%0A%0A
+%09@Test%0A%09public void testExtractBatchSpreadsheetWithArea() throws ParseException, IOException %7B%0A%09%09FileSystem fs = FileSystems.getDefault();%0A%09%09String expectedCsv = UtilsForTesting.loadCsv(%22src/test/resources/technology/tabula/csv/spreadsheet_no_bounding_frame.csv%22);%0A%09%09Path tmpFolder = Files.createTempDirectory(%22tabula-java-batch-test%22);%0A%09%09tmpFolder.toFile().deleteOnExit();%0A%0A%09%09Path copiedPDF = tmpFolder.resolve(fs.getPath(%22spreadsheet.pdf%22));%0A%09%09Path sourcePDF = fs.getPath(%22src/test/resources/technology/tabula/spreadsheet_no_bounding_frame.pdf%22);%0A%09%09Files.copy(sourcePDF, copiedPDF);%0A%09%09copiedPDF.toFile().deleteOnExit();%0A%0A%09%09this.csvFromCommandLineArgs(new String%5B%5D %7B%0A%09%09%09%09%22-b%22, tmpFolder.toString(),%0A%09%09%09%09%22-p%22, %221%22, %22-a%22,%0A%09%09%09%09%22150.56,58.9,654.7,536.12%22, %22-f%22,%0A%09%09%09%09%22CSV%22%0A%09%09%7D);%0A%0A%09%09Path csvPath = tmpFolder.resolve(fs.getPath(%22spreadsheet.csv%22));%0A%09%09assertTrue(csvPath.toFile().exists());%0A%09%09assertArrayEquals(expectedCsv.getBytes(), Files.readAllBytes(csvPath));%0A%09%7D%0A%0A
 %09@Test%0A%09public v

Add test for batch mode of commandline app