]> source.dussan.org Git - poi.git/commitdiff
Apply patch to fix bug 56835: Unreadable content when adding multiple comments to...
authorDominik Stadler <centic@apache.org>
Tue, 21 Oct 2014 15:16:14 +0000 (15:16 +0000)
committerDominik Stadler <centic@apache.org>
Tue, 21 Oct 2014 15:16:14 +0000 (15:16 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1633397 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFDrawing.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFBugs.java
src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFDrawing.java

index 100b539dea8d481ddcfdc653a89d65a004dd9955..012c3a256c21a513a915b0b976ef86e77e7dc6b3 100644 (file)
@@ -127,7 +127,8 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing {
         out.close();
     }
 
-       public XSSFClientAnchor createAnchor(int dx1, int dy1, int dx2, int dy2,
+       @Override
+    public XSSFClientAnchor createAnchor(int dx1, int dy1, int dx2, int dy2,
                        int col1, int row1, int col2, int row2) {
                return new XSSFClientAnchor(dx1, dy1, dx2, dy2, col1, row1, col2, row2);
        }
@@ -177,6 +178,7 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing {
         return shape;
     }
 
+    @Override
     public XSSFPicture createPicture(ClientAnchor anchor, int pictureIndex){
         return createPicture((XSSFClientAnchor)anchor, pictureIndex);
     }
@@ -202,7 +204,8 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing {
         return chart;
     }
 
-       public XSSFChart createChart(ClientAnchor anchor) {
+       @Override
+    public XSSFChart createChart(ClientAnchor anchor) {
                return createChart((XSSFClientAnchor)anchor);
        }
 
@@ -212,6 +215,7 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing {
      * @param pictureIndex the index of the picture in the workbook collection of pictures,
      *   {@link org.apache.poi.xssf.usermodel.XSSFWorkbook#getAllPictures()} .
      */
+    @SuppressWarnings("resource")
     protected PackageRelationship addPictureReference(int pictureIndex){
         XSSFWorkbook wb = (XSSFWorkbook)getParent().getParent();
         XSSFPictureData data = wb.getAllPictures().get(pictureIndex);
@@ -285,6 +289,7 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing {
         *               to the sheet.
         * @return the newly created comment.
         */
+    @Override
     public XSSFComment createCellComment(ClientAnchor anchor) {
         XSSFClientAnchor ca = (XSSFClientAnchor)anchor;
         XSSFSheet sheet = (XSSFSheet)getParent();
@@ -300,8 +305,12 @@ public final class XSSFDrawing extends POIXMLDocumentPart implements Drawing {
             vmlShape.getClientDataArray(0).setAnchorArray(0, position);
         }
         String ref = new CellReference(ca.getRow1(), ca.getCol1()).formatAsString();
-               XSSFComment shape = new XSSFComment(comments, comments.newComment(ref), vmlShape);
-        return shape;
+
+        if(comments.findCellComment(ref) != null) {
+            throw new IllegalArgumentException("Multiple cell comments in one cell are not allowed, cell: " + ref);
+        }
+        
+        return new XSSFComment(comments, comments.newComment(ref), vmlShape);
     }
 
     /**
index 79bbc4ea6d9c5b30a3eeab8f8be0342443ea70b7..658d03e501d41766ffea994fa6527fc5513c2e19 100644 (file)
 package org.apache.poi.xssf.usermodel;
 
 import static org.hamcrest.core.IsEqual.equalTo;
-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 static org.junit.Assert.*;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
@@ -52,26 +46,7 @@ import org.apache.poi.ss.formula.eval.ErrorEval;
 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.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.usermodel.*;
 import org.apache.poi.ss.util.AreaReference;
 import org.apache.poi.ss.util.CellRangeAddress;
 import org.apache.poi.ss.util.CellReference;
@@ -1331,6 +1306,11 @@ public final class TestXSSFBugs extends BaseTestBugzillaIssues {
              factory.createRichTextString("I like this cell. It's my favourite."));
        comment1.setAuthor("Bob T. Fish");
        
+       anchor = factory.createClientAnchor();
+       anchor.setCol1(0);
+       anchor.setCol2(4);
+       anchor.setRow1(1);
+       anchor.setRow2(1);
        Comment comment2 = drawing.createCellComment(anchor);
        comment2.setString(
              factory.createRichTextString("This is much less fun..."));
index d69a62f48e482ffac91a8c060e5b6ba10c693dbd..e0c6d40552522537bd09a49445017876c79d03f2 100644 (file)
@@ -26,6 +26,7 @@ import junit.framework.TestCase;
 
 import org.apache.poi.POIXMLDocumentPart;
 import org.apache.poi.openxml4j.opc.OPCPackage;
+import org.apache.poi.ss.usermodel.ClientAnchor;
 import org.apache.poi.ss.usermodel.FontUnderline;
 import org.apache.poi.xssf.XSSFTestDataSamples;
 import org.openxmlformats.schemas.drawingml.x2006.main.CTTextCharacterProperties;
@@ -37,7 +38,7 @@ import org.openxmlformats.schemas.drawingml.x2006.spreadsheetDrawing.CTDrawing;
  * @author Yegor Kozlov
  */
 public class TestXSSFDrawing extends TestCase {
-    public void testRead(){
+    public void testRead() throws IOException{
         XSSFWorkbook wb = XSSFTestDataSamples.openSampleWorkbook("WithDrawing.xlsx");
         XSSFSheet sheet = wb.getSheetAt(0);
         //the sheet has one relationship and it is XSSFDrawing
@@ -719,4 +720,26 @@ public class TestXSSFDrawing extends TestCase {
             stream.close();
         }*/
     }
+
+    public void testBug56835CellComment() throws Exception {
+        XSSFWorkbook wb = new XSSFWorkbook();
+        try {
+            XSSFSheet sheet = wb.createSheet();
+            XSSFDrawing drawing = sheet.createDrawingPatriarch();
+            
+            // first comment works
+            ClientAnchor anchor = new XSSFClientAnchor(1, 1, 2, 2, 3, 3, 4, 4);
+            XSSFComment comment = drawing.createCellComment(anchor);
+            assertNotNull(comment);
+            
+            try {
+                drawing.createCellComment(anchor);
+                fail("Should fail if we try to add the same comment for the same cell");
+            } catch (IllegalArgumentException e) {
+                // expected
+            }
+        } finally {
+            wb.close();
+        }
+    }
 }