Author: danielc.roth
Date: 2008-09-22 17:25:58 -0400 (Mon, 22 Sep 2008)
New Revision: 9105
Added:
trunk/src/test/excel/unit/org/jboss/seam/test/excel/unit/TestCsvWorkbook.java
Modified:
trunk/src/excel/org/jboss/seam/excel/csv/CsvExcelWorkbook.java
trunk/src/test/excel/unit/org/jboss/seam/test/excel/unit/testng.xml
Log:
Added som CSV Export unit tests
Modified: trunk/src/excel/org/jboss/seam/excel/csv/CsvExcelWorkbook.java
===================================================================
--- trunk/src/excel/org/jboss/seam/excel/csv/CsvExcelWorkbook.java 2008-09-22 20:21:44 UTC
(rev 9104)
+++ trunk/src/excel/org/jboss/seam/excel/csv/CsvExcelWorkbook.java 2008-09-22 21:25:58 UTC
(rev 9105)
@@ -28,6 +28,7 @@
private int currentRow = 0;
private int maxRow = 0;
private int maxColumn = 0;
+ private int sheetStartRow = 0;
private final String COLUMN_DELIMITER = "\"";
private final String LINEBREAK = "\n";
@@ -52,7 +53,7 @@
private void createOrSelectWorksheet(String worksheetName, Integer startRow, Integer
startColumn)
{
- if (sheetName != null && sheetName.equals(worksheetName))
+ if (sheetName != null && !sheetName.equals(worksheetName))
{
throw new RuntimeException("You cannot export multiple sheet workbooks to
excel.");
}
@@ -60,6 +61,7 @@
sheetName = worksheetName;
currentColumn = (startColumn == null) ? 0 : startColumn;
currentRow = (startRow == null) ? 0 : startRow;
+ sheetStartRow = currentRow;
}
public byte[] getBytes()
@@ -83,7 +85,7 @@
public void nextColumn()
{
currentColumn++;
- currentRow = 0;
+ currentRow = sheetStartRow;
}
public DocumentType getDocumentType()
Added: trunk/src/test/excel/unit/org/jboss/seam/test/excel/unit/TestCsvWorkbook.java
===================================================================
--- trunk/src/test/excel/unit/org/jboss/seam/test/excel/unit/TestCsvWorkbook.java
(rev 0)
+++
trunk/src/test/excel/unit/org/jboss/seam/test/excel/unit/TestCsvWorkbook.java 2008-09-22
21:25:58 UTC (rev 9105)
@@ -0,0 +1,141 @@
+package org.jboss.seam.test.excel.unit;
+
+import org.jboss.seam.excel.csv.CsvExcelWorkbook;
+import org.jboss.seam.excel.ui.UICell;
+import org.jboss.seam.excel.ui.UIWorkbook;
+import org.jboss.seam.excel.ui.UIWorksheet;
+import org.testng.annotations.Test;
+
+@Test
+public class TestCsvWorkbook
+{
+
+ @Test(expectedExceptions = { RuntimeException.class })
+ public void testOneSheetOnly()
+ {
+ CsvExcelWorkbook wb = new CsvExcelWorkbook();
+ UIWorkbook uiWorkbook = new UIWorkbook();
+ wb.createWorkbook(uiWorkbook);
+
+ UIWorksheet sheet = new UIWorksheet();
+ sheet.setName("sheet");
+ wb.createOrSelectWorksheet(sheet);
+
+ UIWorksheet sheet2 = new UIWorksheet();
+ sheet2.setName("sheet2");
+ wb.createOrSelectWorksheet(sheet2);
+
+ }
+
+ @Test
+ public void testSimpleAdd()
+ {
+ CsvExcelWorkbook wb = new CsvExcelWorkbook();
+ UIWorkbook uiWorkbook = new UIWorkbook();
+ wb.createWorkbook(uiWorkbook);
+
+ UIWorksheet sheet = new UIWorksheet();
+ sheet.setName("sheet");
+ wb.createOrSelectWorksheet(sheet);
+
+ for (int i = 0; i < 2; i++)
+ {
+ for (int j = 0; j < 2; j++)
+ {
+ UICell cell = new UICell();
+ cell.setValue(i + "_" + j);
+ wb.addItem(cell);
+ }
+ wb.nextColumn();
+ }
+
+ byte[] correct = new
String("\"0_0\",\"1_0\"\n\"0_1\",\"1_1\"\n").getBytes();
+ byte[] created = wb.getBytes();
+
+ for (int i = 0; i < created.length; i++)
+ {
+ assert correct[i] == created[i];
+ }
+
+ }
+
+ public void testAddExplicit()
+ {
+ CsvExcelWorkbook wb = new CsvExcelWorkbook();
+ UIWorkbook uiWorkbook = new UIWorkbook();
+ wb.createWorkbook(uiWorkbook);
+
+ UIWorksheet sheet = new UIWorksheet();
+ sheet.setName("sheet");
+ wb.createOrSelectWorksheet(sheet);
+
+ UICell cell = new UICell();
+ cell.setValue("A1");
+ cell.setColumn(0);
+ cell.setRow(0);
+ wb.addItem(cell);
+ cell.setValue("C2");
+ cell.setColumn(2);
+ cell.setRow(1);
+ wb.addItem(cell);
+
+ byte[] correct = new
String("\"A1\",\"\",\"\"\n\"\",\"\",\"C2\"\n").getBytes();
+ byte[] created = wb.getBytes();
+
+ System.out.println(new String(created));
+
+ for (int i = 0; i < created.length; i++)
+ {
+ assert correct[i] == created[i];
+ }
+
+ }
+
+ @Test
+ public void testOverlapAdd()
+ {
+ CsvExcelWorkbook wb = new CsvExcelWorkbook();
+ UIWorkbook uiWorkbook = new UIWorkbook();
+ wb.createWorkbook(uiWorkbook);
+
+ UIWorksheet sheet = new UIWorksheet();
+ sheet.setName("sheet");
+ wb.createOrSelectWorksheet(sheet);
+
+ for (int i = 0; i < 2; i++)
+ {
+ for (int j = 0; j < 2; j++)
+ {
+ UICell cell = new UICell();
+ cell.setValue(i + "_" + j);
+ wb.addItem(cell);
+ }
+ wb.nextColumn();
+ }
+
+ sheet.setStartColumn(1);
+ sheet.setStartRow(1);
+ wb.createOrSelectWorksheet(sheet);
+
+ for (int i = 0; i < 2; i++)
+ {
+ for (int j = 0; j < 2; j++)
+ {
+ UICell cell = new UICell();
+ cell.setValue(i + "_" + j);
+ wb.addItem(cell);
+ }
+ wb.nextColumn();
+ }
+
+ byte[] correct = new
String("\"0_0\",\"1_0\",\"\"\n\"0_1\",\"0_0\",\"1_0\"\n\"\",\"0_1\",\"1_1\"\n").getBytes();
+ byte[] created = wb.getBytes();
+
+ for (int i = 0; i < created.length; i++)
+ {
+ assert correct[i] == created[i];
+ }
+
+ }
+
+}
Modified: trunk/src/test/excel/unit/org/jboss/seam/test/excel/unit/testng.xml
===================================================================
--- trunk/src/test/excel/unit/org/jboss/seam/test/excel/unit/testng.xml 2008-09-22
20:21:44 UTC (rev 9104)
+++ trunk/src/test/excel/unit/org/jboss/seam/test/excel/unit/testng.xml 2008-09-22
21:25:58 UTC (rev 9105)
@@ -2,7 +2,8 @@
<suite name="Seam Mail Unit Testsuite" verbose="2"
parallel="false">
<test name="Seam Unit Tests: Excel">
- <classes>
+ <classes>
+ <class name="org.jboss.seam.test.excel.unit.TestCsvWorkbook"/>
</classes>
</test>
</suite>
Show replies by date