Permalink
Browse files

test and fix for ODFTOOLKIT-325 NPE in cell.getTimeValue()

git-svn-id: https://svn.apache.org/repos/asf/incubator/odf/trunk@1417744 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information...
1 parent e84fa6f commit 5f4f9d64008176732cd675783c46f5244a5fc6ac Florian Hopf committed Dec 6, 2012
View
3 odfdom/src/main/java/org/odftoolkit/odfdom/doc/table/OdfTableCell.java
@@ -883,6 +883,9 @@ public String getStringValue() {
public Calendar getTimeValue() {
if (getTypeAttr() == OfficeValueTypeAttribute.Value.TIME) {
String timeStr = mCellElement.getOfficeTimeValueAttribute();
+ if (timeStr == null) {
+ return null;
+ }
Date date = parseString(timeStr, DEFAULT_TIME_FORMAT);
Calendar calender = Calendar.getInstance();
calender.setTime(date);
View
22 odfdom/src/test/java/org/odftoolkit/odfdom/doc/table/TableCellTest.java
@@ -32,12 +32,12 @@
import junit.framework.Assert;
import org.junit.Before;
-import org.junit.Ignore;
import org.junit.Test;
import org.odftoolkit.odfdom.doc.OdfSpreadsheetDocument;
import org.odftoolkit.odfdom.doc.OdfTextDocument;
import org.odftoolkit.odfdom.dom.OdfContentDom;
import org.odftoolkit.odfdom.dom.OdfStylesDom;
+import org.odftoolkit.odfdom.dom.attribute.office.OfficeValueTypeAttribute;
import org.odftoolkit.odfdom.dom.element.draw.DrawFrameElement;
import org.odftoolkit.odfdom.dom.element.draw.DrawImageElement;
import org.odftoolkit.odfdom.dom.element.style.StyleParagraphPropertiesElement;
@@ -50,7 +50,6 @@
import org.odftoolkit.odfdom.type.Color;
import org.odftoolkit.odfdom.utils.ResourceUtilities;
-@Ignore
public class TableCellTest {
final static String SAMPLE_SPREADSHEET = "TestSpreadsheetTable";
@@ -1051,4 +1050,23 @@ public void testSetDefaultCellStyle() {
Assert.fail();
}
}
+
+ @Test
+ public void testGetFromEmptyDateValue() throws Exception {
+ OdfSpreadsheetDocument doc = OdfSpreadsheetDocument.newSpreadsheetDocument();
+ OdfTable table = OdfTable.newTable(doc);
+ OdfTableCell dateCell = table.appendRow().getCellByIndex(0);
+ dateCell.setValueType(OfficeValueTypeAttribute.Value.DATE.toString());
+ Assert.assertNull(dateCell.getDateValue());
+ }
+
+ @Test
+ public void testGetFromEmptyTimeValue() throws Exception {
+ OdfSpreadsheetDocument doc = OdfSpreadsheetDocument.newSpreadsheetDocument();
+ OdfTable table = OdfTable.newTable(doc);
+ OdfTableCell timeCell = table.appendRow().getCellByIndex(0);
+ timeCell.setValueType(OfficeValueTypeAttribute.Value.TIME.toString());
+ Assert.assertNull(timeCell.getTimeValue());
+ }
+
}
View
3 simple/src/main/java/org/odftoolkit/simple/table/Cell.java
@@ -1201,6 +1201,9 @@ public String getStringValue() {
public Calendar getTimeValue() {
if (getTypeAttr() == OfficeValueTypeAttribute.Value.TIME) {
String timeStr = mCellElement.getOfficeTimeValueAttribute();
+ if (timeStr == null) {
+ return null;
+ }
Date date = parseString(timeStr, DEFAULT_TIME_FORMAT);
Calendar calender = Calendar.getInstance();
calender.setTime(date);
View
17 simple/src/test/java/org/odftoolkit/simple/table/TableCellTest.java
@@ -35,6 +35,7 @@ Licensed to the Apache Software Foundation (ASF) under one
import org.junit.Before;
import org.junit.Test;
import org.odftoolkit.odfdom.dom.OdfContentDom;
+import org.odftoolkit.odfdom.dom.attribute.office.OfficeValueTypeAttribute;
import org.odftoolkit.odfdom.dom.element.draw.DrawFrameElement;
import org.odftoolkit.odfdom.dom.element.style.StyleParagraphPropertiesElement;
import org.odftoolkit.odfdom.dom.style.OdfStyleFamily;
@@ -581,6 +582,22 @@ public void testGetSetDateValue() {
Assert.assertEquals(0, fcell.getDateValue().compareTo(expectedCalendar));
}
+ @Test
+ public void testGetFromEmptyDateValue() throws Exception {
+ Table table = odsdoc.getTableByName("Sheet1");
+ Cell cell = table.appendRow().getCellByIndex(1);
+ cell.setValueType(OfficeValueTypeAttribute.Value.DATE.toString());
+ Assert.assertNull(cell.getDateValue());
+ }
+
+ @Test
+ public void testGetFromEmptyTimeValue() throws Exception {
+ Table table = odsdoc.getTableByName("Sheet1");
+ Cell cell = table.appendRow().getCellByIndex(1);
+ cell.setValueType(OfficeValueTypeAttribute.Value.TIME.toString());
+ Assert.assertNull(cell.getTimeValue());
+ }
+
@Test
public void testGetSetStringValue() {
int rowindex = 6, columnindex = 0;

0 comments on commit 5f4f9d6

Please sign in to comment.