Author: onealj
Date: Wed Sep 21 05:20:36 2016
New Revision: 1761673

URL: http://svn.apache.org/viewvc?rev=1761673&view=rev
Log:
bug 59983: add failing unit test demonstrating formula shifting is apply twice 
to shared formulas; patch from Luca Martini

Added:
    poi/trunk/test-data/spreadsheet/TestShiftRowSharedFormula.xlsx   (with 
props)
Modified:
    
poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowShifter.java
    
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRows.java

Modified: 
poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowShifter.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowShifter.java?rev=1761673&r1=1761672&r2=1761673&view=diff
==============================================================================
--- 
poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowShifter.java
 (original)
+++ 
poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/helpers/XSSFRowShifter.java
 Wed Sep 21 05:20:36 2016
@@ -147,7 +147,8 @@ public final class XSSFRowShifter extend
 
                 }
 
-                if (f.isSetRef()) { //Range of cells which the formula applies 
to.
+                //Range of cells which the formula applies to.
+                if (f.isSetRef()) {
                     String ref = f.getRef();
                     String shiftedRef = shiftFormula(row, ref, shifter);
                     if (shiftedRef != null) f.setRef(shiftedRef);

Modified: 
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRows.java
URL: 
http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRows.java?rev=1761673&r1=1761672&r2=1761673&view=diff
==============================================================================
--- 
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRows.java
 (original)
+++ 
poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheetShiftRows.java
 Wed Sep 21 05:20:36 2016
@@ -17,6 +17,8 @@
 
 package org.apache.poi.xssf.usermodel;
 
+import static org.apache.poi.POITestCase.skipTest;
+import static org.apache.poi.POITestCase.testPassesNow;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
@@ -24,6 +26,8 @@ import static org.junit.Assert.fail;
 
 import java.io.IOException;
 
+import junit.framework.AssertionFailedError;
+
 import org.apache.poi.ss.usermodel.BaseTestSheetShiftRows;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.CellType;
@@ -35,7 +39,7 @@ import org.apache.poi.ss.util.CellAddres
 import org.apache.poi.ss.util.CellUtil;
 import org.apache.poi.xssf.XSSFITestDataProvider;
 import org.apache.poi.xssf.XSSFTestDataSamples;
-import org.junit.Ignore;
+import org.apache.xmlbeans.impl.values.XmlValueDisconnectedException;
 import org.junit.Test;
 
 public final class TestXSSFSheetShiftRows extends BaseTestSheetShiftRows {
@@ -377,7 +381,9 @@ public final class TestXSSFSheetShiftRow
         wb.close();
     }
     
-    @Ignore("Bug 59733 - shiftRows() causes 
org.apache.xmlbeans.impl.values.XmlValueDisconnectedException")
+    // This test is written as expected-to-fail and should be rewritten
+    // as expected-to-pass when the bug is fixed.
+    //@Ignore("Bug 59733 - shiftRows() causes 
org.apache.xmlbeans.impl.values.XmlValueDisconnectedException")
     @Test
     public void bug59733() throws IOException {
         Workbook workbook = new XSSFWorkbook();
@@ -399,9 +405,48 @@ public final class TestXSSFSheetShiftRow
             at 
org.apache.poi.xssf.usermodel.XSSFRow.getRowNum(XSSFRow.java:363)
             at 
org.apache.poi.xssf.usermodel.TestXSSFSheetShiftRows.bug59733(TestXSSFSheetShiftRows.java:393)
          */
-        sheet.removeRow(sheet.getRow(0));
-        assertEquals(1, sheet.getRow(1).getRowNum());
+        try {
+            sheet.removeRow(sheet.getRow(0));
+            assertEquals(1, sheet.getRow(1).getRowNum());
+            testPassesNow(59733);
+        } catch (XmlValueDisconnectedException e) {
+            skipTest(e);
+        }
+        
 
         workbook.close();
+    }
+
+    private static String getCellFormula(Sheet sheet, String address) {
+        CellAddress cellAddress = new CellAddress(address);
+        Row row = sheet.getRow(cellAddress.getRow());
+        assertNotNull(row);
+        Cell cell = row.getCell(cellAddress.getColumn());
+        assertNotNull(cell);
+        assertEquals(CellType.FORMULA, cell.getCellTypeEnum());
+        return cell.getCellFormula();
+    }
+
+    // This test is written as expected-to-fail and should be rewritten
+    // as expected-to-pass when the bug is fixed.
+    @Test
+    public void testSharedFormulas() throws Exception {
+        XSSFWorkbook wb = 
XSSFTestDataSamples.openSampleWorkbook("TestShiftRowSharedFormula.xlsx");
+        XSSFSheet sheet = wb.getSheetAt(0);
+        assertEquals("SUM(C2:C4)", getCellFormula(sheet, "C5"));
+        assertEquals("SUM(D2:D4)", getCellFormula(sheet, "D5"));
+        assertEquals("SUM(E2:E4)", getCellFormula(sheet, "E5"));
+
+        sheet.shiftRows(3, sheet.getLastRowNum(), 1);
+        try {
+            assertEquals("SUM(C2:C5)", getCellFormula(sheet, "C6"));
+            assertEquals("SUM(D2:D5)", getCellFormula(sheet, "D6"));
+            assertEquals("SUM(E2:E5)", getCellFormula(sheet, "E6"));
+            testPassesNow(59983);
+        } catch (AssertionError e) {
+            skipTest(e);
+        }
+        
+        wb.close();
     }
 }

Added: poi/trunk/test-data/spreadsheet/TestShiftRowSharedFormula.xlsx
URL: 
http://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/TestShiftRowSharedFormula.xlsx?rev=1761673&view=auto
==============================================================================
Binary file - no diff available.

Propchange: poi/trunk/test-data/spreadsheet/TestShiftRowSharedFormula.xlsx
------------------------------------------------------------------------------
--- svn:mime-type (added)
+++ svn:mime-type Wed Sep 21 05:20:36 2016
@@ -0,0 +1 @@
+application/vnd.openxmlformats-officedocument.spreadsheetml.sheet



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

Reply via email to