You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2014/07/31 16:55:05 UTC

svn commit: r1614914 - in /poi/trunk/src/ooxml: java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java

Author: nick
Date: Thu Jul 31 14:55:04 2014
New Revision: 1614914

URL: http://svn.apache.org/r1614914
Log:
Fix bug #56527 - Avoid NPE from XSSFHyperLink when setting the cell
it references on a new link

Modified:
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java?rev=1614914&r1=1614913&r2=1614914&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFHyperlink.java Thu Jul 31 14:55:04 2014
@@ -25,7 +25,6 @@ import org.apache.poi.ss.usermodel.Hyper
 import org.apache.poi.ss.util.CellReference;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTHyperlink;
 
-
 /**
  * XSSF Implementation of a Hyperlink.
  * Note - unlike with HSSF, many kinds of hyperlink
@@ -223,9 +222,16 @@ public class XSSFHyperlink implements Hy
     protected void setCellReference(String ref) {
         _ctHyperlink.setRef(ref);
     }
+    protected void setCellReference(CellReference ref) {
+        setCellReference(ref.formatAsString());
+    }
 
     private CellReference buildCellReference() {
-        return new CellReference(_ctHyperlink.getRef());
+        String ref = _ctHyperlink.getRef();
+        if (ref == null) {
+            ref = "A1";
+        }
+        return new CellReference(ref);
     }
 
 
@@ -273,15 +279,12 @@ public class XSSFHyperlink implements Hy
      * @param col the 0-based column of the first cell that contains the hyperlink
      */
     public void setFirstColumn(int col) {
-        _ctHyperlink.setRef(
-                new CellReference(
-                        getFirstRow(), col
-                ).formatAsString()
-        );
+        setCellReference(new CellReference( getFirstRow(), col ));
     }
 
     /**
-     * Set the column of the last cell that contains the hyperlink
+     * Set the column of the last cell that contains the hyperlink.
+     * For XSSF, a Hyperlink may only reference one cell
      *
      * @param col the 0-based column of the last cell that contains the hyperlink
      */
@@ -295,15 +298,12 @@ public class XSSFHyperlink implements Hy
      * @param row the 0-based row of the first cell that contains the hyperlink
      */
     public void setFirstRow(int row) {
-        _ctHyperlink.setRef(
-                new CellReference(
-                        row, getFirstColumn()
-                ).formatAsString()
-        );
+        setCellReference(new CellReference( row, getFirstColumn() ));
     }
 
     /**
-     * Set the row of the last cell that contains the hyperlink
+     * Set the row of the last cell that contains the hyperlink.
+     * For XSSF, a Hyperlink may only reference one cell
      *
      * @param row the 0-based row of the last cell that contains the hyperlink
      */

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java?rev=1614914&r1=1614913&r2=1614914&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java Thu Jul 31 14:55:04 2014
@@ -18,7 +18,13 @@
 package org.apache.poi.xssf.usermodel;
 
 import static org.hamcrest.core.IsEqual.equalTo;
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertThat;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
@@ -44,7 +50,26 @@ import org.apache.poi.ss.formula.eval.Er
 import org.apache.poi.ss.formula.eval.NumberEval;
 import org.apache.poi.ss.formula.eval.ValueEval;
 import org.apache.poi.ss.formula.functions.Function;
-import org.apache.poi.ss.usermodel.*;
+import org.apache.poi.ss.usermodel.BaseTestBugzillaIssues;
+import org.apache.poi.ss.usermodel.Cell;
+import org.apache.poi.ss.usermodel.CellStyle;
+import org.apache.poi.ss.usermodel.CellValue;
+import org.apache.poi.ss.usermodel.ClientAnchor;
+import org.apache.poi.ss.usermodel.Comment;
+import org.apache.poi.ss.usermodel.CreationHelper;
+import org.apache.poi.ss.usermodel.DataFormatter;
+import org.apache.poi.ss.usermodel.DateUtil;
+import org.apache.poi.ss.usermodel.Drawing;
+import org.apache.poi.ss.usermodel.Font;
+import org.apache.poi.ss.usermodel.FormulaError;
+import org.apache.poi.ss.usermodel.FormulaEvaluator;
+import org.apache.poi.ss.usermodel.Hyperlink;
+import org.apache.poi.ss.usermodel.IndexedColors;
+import org.apache.poi.ss.usermodel.Name;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Sheet;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.usermodel.WorkbookFactory;
 import org.apache.poi.ss.util.AreaReference;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.CellReference;
@@ -1761,6 +1786,36 @@ public final class TestXSSFBugs extends 
         NumberEval eval = new NumberEval(Math.floor(excelDate));
         checkValue(excel, eval.getStringValue() + ".0");
     }
+    
+    @Test
+    public void testBug56527() {
+        XSSFWorkbook wb = new XSSFWorkbook();
+        XSSFSheet sheet = wb.createSheet();
+        XSSFCreationHelper creationHelper = wb.getCreationHelper();
+        XSSFHyperlink hyperlink;
+        
+        // Try with a cell reference
+        hyperlink = creationHelper.createHyperlink(Hyperlink.LINK_URL);
+        sheet.addHyperlink(hyperlink);
+        hyperlink.setAddress("http://myurl");
+        hyperlink.setCellReference("B4");
+        assertEquals(3, hyperlink.getFirstRow());
+        assertEquals(1, hyperlink.getFirstColumn());
+        assertEquals(3, hyperlink.getLastRow());
+        assertEquals(1, hyperlink.getLastColumn());
+        
+        // Try with explicit rows / columns
+        hyperlink = creationHelper.createHyperlink(Hyperlink.LINK_URL);
+        sheet.addHyperlink(hyperlink);
+        hyperlink.setAddress("http://myurl");
+        hyperlink.setFirstRow(5);
+        hyperlink.setFirstColumn(3);
+        
+        assertEquals(5, hyperlink.getFirstRow());
+        assertEquals(3, hyperlink.getFirstColumn());
+        assertEquals(5, hyperlink.getLastRow());
+        assertEquals(3, hyperlink.getLastColumn());
+    }
 
     private void checkValue(XSSFWorkbook excel, String expect) {
         XSSFFormulaEvaluator evaluator = new XSSFFormulaEvaluator(excel);



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org