aboutsummaryrefslogtreecommitdiffstats
path: root/src/testcases/org/apache/poi
diff options
context:
space:
mode:
authorYegor Kozlov <yegor@apache.org>2011-09-10 19:00:47 +0000
committerYegor Kozlov <yegor@apache.org>2011-09-10 19:00:47 +0000
commite4b2a66d0915c6487a95d87a3e082847d8557107 (patch)
tree22e22cea138d9bdee9de0531a01fa5fb170e1671 /src/testcases/org/apache/poi
parent57c369a5416c1ee967590258e3c37969ad609e36 (diff)
downloadpoi-e4b2a66d0915c6487a95d87a3e082847d8557107.tar.gz
poi-e4b2a66d0915c6487a95d87a3e082847d8557107.zip
more progress with xssf chart api, see Bug 51196
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1167579 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/testcases/org/apache/poi')
-rw-r--r--src/testcases/org/apache/poi/ss/usermodel/charts/TestDataSources.java136
-rw-r--r--src/testcases/org/apache/poi/ss/util/TestSheetBuilder.java75
2 files changed, 177 insertions, 34 deletions
diff --git a/src/testcases/org/apache/poi/ss/usermodel/charts/TestDataSources.java b/src/testcases/org/apache/poi/ss/usermodel/charts/TestDataSources.java
new file mode 100644
index 0000000000..6bf3551286
--- /dev/null
+++ b/src/testcases/org/apache/poi/ss/usermodel/charts/TestDataSources.java
@@ -0,0 +1,136 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+package org.apache.poi.ss.usermodel.charts;
+
+import junit.framework.TestCase;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.util.CellRangeAddress;
+import org.apache.poi.ss.util.SheetBuilder;
+
+/**
+ * Tests for {@link org.apache.poi.ss.usermodel.charts.DataSources}.
+ *
+ * @author Roman Kashitsyn
+ */
+public class TestDataSources extends TestCase {
+
+ private static final Object[][] numericCells = {
+ {0.0, 1.0, 2.0, 3.0, 4.0},
+ {0.0, "=B1*2", "=C1*2", "=D1*2", "=E1*2"}
+ };
+
+ private static final Object[][] stringCells = {
+ { 1, 2, 3, 4, 5},
+ {"A", "B", "C", "D", "E"}
+ };
+
+ private static final Object[][] mixedCells = {
+ {1.0, "2.0", 3.0, "4.0", 5.0, "6.0"}
+ };
+
+ public void testNumericArrayDataSource() {
+ Double[] doubles = new Double[]{1.0, 2.0, 3.0, 4.0, 5.0};
+ ChartDataSource<Double> doubleDataSource = DataSources.fromArray(doubles);
+ assertTrue(doubleDataSource.isNumeric());
+ assertFalse(doubleDataSource.isReference());
+ assertDataSourceIsEqualToArray(doubleDataSource, doubles);
+ }
+
+ public void testStringArrayDataSource() {
+ String[] strings = new String[]{"one", "two", "three", "four", "five"};
+ ChartDataSource<String> stringDataSource = DataSources.fromArray(strings);
+ assertFalse(stringDataSource.isNumeric());
+ assertFalse(stringDataSource.isReference());
+ assertDataSourceIsEqualToArray(stringDataSource, strings);
+ }
+
+ public void testNumericCellDataSource() {
+ Workbook wb = new HSSFWorkbook();
+ Sheet sheet = new SheetBuilder(wb, numericCells).build();
+ CellRangeAddress numCellRange = CellRangeAddress.valueOf("A2:E2");
+ ChartDataSource<Number> numDataSource = DataSources.fromNumericCellRange(sheet, numCellRange);
+ assertTrue(numDataSource.isReference());
+ assertTrue(numDataSource.isNumeric());
+ assertEquals(numericCells[0].length, numDataSource.getPointCount());
+ for (int i = 0; i < numericCells[0].length; ++i) {
+ assertEquals(((Number) numericCells[0][i]).doubleValue() * 2,
+ numDataSource.getPointAt(i).doubleValue(), 0.00001);
+ }
+ }
+
+ public void testStringCellDataSource() {
+ Workbook wb = new HSSFWorkbook();
+ Sheet sheet = new SheetBuilder(wb, stringCells).build();
+ CellRangeAddress numCellRange = CellRangeAddress.valueOf("A2:E2");
+ ChartDataSource<String> numDataSource = DataSources.fromStringCellRange(sheet, numCellRange);
+ assertTrue(numDataSource.isReference());
+ assertFalse(numDataSource.isNumeric());
+ assertEquals(numericCells[0].length, numDataSource.getPointCount());
+ for (int i = 0; i < stringCells[1].length; ++i) {
+ assertEquals(stringCells[1][i], numDataSource.getPointAt(i));
+ }
+ }
+
+ public void testMixedCellDataSource() {
+ Workbook wb = new HSSFWorkbook();
+ Sheet sheet = new SheetBuilder(wb, mixedCells).build();
+ CellRangeAddress mixedCellRange = CellRangeAddress.valueOf("A1:F1");
+ ChartDataSource<String> strDataSource = DataSources.fromStringCellRange(sheet, mixedCellRange);
+ ChartDataSource<Number> numDataSource = DataSources.fromNumericCellRange(sheet, mixedCellRange);
+ for (int i = 0; i < mixedCells[0].length; ++i) {
+ if (i % 2 == 0) {
+ assertNull(strDataSource.getPointAt(i));
+ assertEquals(((Number) mixedCells[0][i]).doubleValue(),
+ numDataSource.getPointAt(i).doubleValue(), 0.00001);
+ } else {
+ assertNull(numDataSource.getPointAt(i));
+ assertEquals(mixedCells[0][i], strDataSource.getPointAt(i));
+ }
+ }
+ }
+
+ public void testIOBExceptionOnInvalidIndex() {
+ Workbook wb = new HSSFWorkbook();
+ Sheet sheet = new SheetBuilder(wb, numericCells).build();
+ CellRangeAddress rangeAddress = CellRangeAddress.valueOf("A2:E2");
+ ChartDataSource<Number> numDataSource = DataSources.fromNumericCellRange(sheet, rangeAddress);
+ IndexOutOfBoundsException exception = null;
+ try {
+ numDataSource.getPointAt(-1);
+ } catch (IndexOutOfBoundsException e) {
+ exception = e;
+ }
+ assertNotNull(exception);
+
+ exception = null;
+ try {
+ numDataSource.getPointAt(numDataSource.getPointCount());
+ } catch (IndexOutOfBoundsException e) {
+ exception = e;
+ }
+ assertNotNull(exception);
+ }
+
+ private <T> void assertDataSourceIsEqualToArray(ChartDataSource<T> ds, T[] array) {
+ assertEquals(ds.getPointCount(), array.length);
+ for (int i = 0; i < array.length; ++i) {
+ assertEquals(ds.getPointAt(i), array[i]);
+ }
+ }
+}
diff --git a/src/testcases/org/apache/poi/ss/util/TestSheetBuilder.java b/src/testcases/org/apache/poi/ss/util/TestSheetBuilder.java
index edb3df9355..ee90ee6c5a 100644
--- a/src/testcases/org/apache/poi/ss/util/TestSheetBuilder.java
+++ b/src/testcases/org/apache/poi/ss/util/TestSheetBuilder.java
@@ -29,49 +29,56 @@ import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* Tests SheetBuilder.
+ *
* @see org.apache.poi.ss.util.SheetBuilder
*/
public final class TestSheetBuilder extends TestCase {
-
- private static Object[][] testData = new Object[][] {
- { 1, 2, 3},
- {new Date(), null, null},
- { "one", "two", "=A1+B2"}
+
+ private static Object[][] testData = new Object[][]{
+ {1, 2, 3},
+ {new Date(), null, null},
+ {"one", "two", "=A1+B2"}
};
public void testNotCreateEmptyCells() {
- Workbook wb = new HSSFWorkbook();
- Sheet sheet = new SheetBuilder(wb, testData).build();
-
- assertEquals(sheet.getPhysicalNumberOfRows(), 3);
-
- Row firstRow = sheet.getRow(0);
- Cell firstCell = firstRow.getCell(0);
-
- assertEquals(firstCell.getCellType(), Cell.CELL_TYPE_NUMERIC);
- assertEquals(1.0, firstCell.getNumericCellValue(), 0.00001);
-
-
- Row secondRow = sheet.getRow(1);
- assertNotNull(secondRow.getCell(0));
- assertNull(secondRow.getCell(2));
-
- Row thirdRow = sheet.getRow(2);
- assertEquals(Cell.CELL_TYPE_STRING, thirdRow.getCell(0).getCellType());
- String cellValue = thirdRow.getCell(0).getStringCellValue();
- assertEquals(testData[2][0].toString(), cellValue);
-
- assertEquals(Cell.CELL_TYPE_FORMULA, thirdRow.getCell(2).getCellType());
- assertEquals("A1+B2", thirdRow.getCell(2).getCellFormula());
+ Workbook wb = new HSSFWorkbook();
+ Sheet sheet = new SheetBuilder(wb, testData).build();
+
+ assertEquals(sheet.getPhysicalNumberOfRows(), 3);
+
+ Row firstRow = sheet.getRow(0);
+ Cell firstCell = firstRow.getCell(0);
+
+ assertEquals(firstCell.getCellType(), Cell.CELL_TYPE_NUMERIC);
+ assertEquals(1.0, firstCell.getNumericCellValue(), 0.00001);
+
+
+ Row secondRow = sheet.getRow(1);
+ assertNotNull(secondRow.getCell(0));
+ assertNull(secondRow.getCell(2));
+
+ Row thirdRow = sheet.getRow(2);
+ assertEquals(Cell.CELL_TYPE_STRING, thirdRow.getCell(0).getCellType());
+ String cellValue = thirdRow.getCell(0).getStringCellValue();
+ assertEquals(testData[2][0].toString(), cellValue);
+
+ assertEquals(Cell.CELL_TYPE_FORMULA, thirdRow.getCell(2).getCellType());
+ assertEquals("A1+B2", thirdRow.getCell(2).getCellFormula());
}
public void testEmptyCells() {
- Workbook wb = new HSSFWorkbook();
- Sheet sheet = new SheetBuilder(wb, testData).setCreateEmptyCells(true).build();
-
- Cell emptyCell = sheet.getRow(1).getCell(1);
- assertNotNull(emptyCell);
- assertEquals(Cell.CELL_TYPE_BLANK, emptyCell.getCellType());
+ Workbook wb = new HSSFWorkbook();
+ Sheet sheet = new SheetBuilder(wb, testData).setCreateEmptyCells(true).build();
+
+ Cell emptyCell = sheet.getRow(1).getCell(1);
+ assertNotNull(emptyCell);
+ assertEquals(Cell.CELL_TYPE_BLANK, emptyCell.getCellType());
}
+ public void testSheetName() {
+ final String sheetName = "TEST SHEET NAME";
+ Workbook wb = new HSSFWorkbook();
+ Sheet sheet = new SheetBuilder(wb, testData).setSheetName(sheetName).build();
+ assertEquals(sheetName, sheet.getSheetName());
+ }
} \ No newline at end of file