]> source.dussan.org Git - poi.git/commitdiff
More tests for xssf comments. Now almost all there and working, except for rich text...
authorNick Burch <nick@apache.org>
Thu, 3 Apr 2008 19:41:23 +0000 (19:41 +0000)
committerNick Burch <nick@apache.org>
Thu, 3 Apr 2008 19:41:23 +0000 (19:41 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/branches/ooxml@644460 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xssf/model/CommentsTable.java
src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFComment.java
src/ooxml/testcases/org/apache/poi/xssf/model/TestCommentsTable.java

index 02602551ebee918042648bdf1b4297c71d055bab..f37d560a406d4b803baeb06b270b34d5ceeb9a64 100644 (file)
@@ -72,6 +72,9 @@ public class CommentsTable implements CommentsSource, XSSFModel {
        public int getNumberOfComments() {
                return comments.getCommentList().sizeOfCommentArray();
        }
+       public int getNumberOfAuthors() {
+               return getCommentsAuthors().sizeOfAuthorArray();
+       }
        
        public String getAuthor(long authorId) {
                return getCommentsAuthors().getAuthorArray((int)authorId);
index 830910d756fed4fca0b7350d93d97f9c22e4ce2e..eb016c20871c7c733afcec9e9230451aec1383e9 100644 (file)
@@ -56,7 +56,9 @@ public class XSSFComment implements Comment {
        }
 
        public void setAuthor(String author) {
-               comments.findAuthor(author);
+               comment.setAuthorId(
+                               comments.findAuthor(author)
+               );
        }
 
        public void setColumn(short col) {
index 413c96b83748a76dff6d61185ba3eb56270e0993..10cbd91ca29f0b22216a1d144f0139eabbfe9b3f 100644 (file)
@@ -31,6 +31,8 @@ import org.apache.poi.xssf.usermodel.XSSFRichTextString;
 import org.apache.poi.xssf.usermodel.XSSFSheet;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 import org.openxml4j.opc.Package;
+import org.openxml4j.opc.PackagePart;
+import org.openxml4j.opc.PackagingURIHelper;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComment;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCommentList;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTComments;
@@ -52,6 +54,8 @@ public class TestCommentsTable extends TestCase {
                assertEquals(0, sheetComments.findAuthor(TEST_AUTHOR));
                assertEquals(1, sheetComments.findAuthor("another author"));
                assertEquals(0, sheetComments.findAuthor(TEST_AUTHOR));
+               assertEquals(2, sheetComments.findAuthor("YAA"));
+               assertEquals(1, sheetComments.findAuthor("another author"));
        }
        
        public void testGetCellComment() {
@@ -91,6 +95,51 @@ public class TestCommentsTable extends TestCase {
                sheetComments.setCellComment("A1", comment);
                assertEquals(1, commentList.sizeOfCommentArray());
                assertEquals("test A1 author", sheetComments.getAuthor(commentList.getCommentArray(0).getAuthorId()));
+               assertEquals("test A1 author", comment.getAuthor());
+               
+               // Change the author, check it updates
+               comment.setAuthor("Another Author");
+               assertEquals(1, commentList.sizeOfCommentArray());
+               assertEquals("Another Author", comment.getAuthor());
+       }
+       
+       public void testDontLoostNewLines() throws Exception {
+               File xml = new File(
+                               System.getProperty("HSSF.testdata.path") +
+                               File.separator + "WithVariousData.xlsx"
+               );
+               assertTrue(xml.exists());
+       
+               Package pkg = Package.open(xml.toString());
+               PackagePart cpart = pkg.getPart(
+                               PackagingURIHelper.createPartName("/xl/comments1.xml")
+               );
+               
+               CommentsTable ct = new CommentsTable(cpart.getInputStream());
+               assertEquals(2, ct.getNumberOfComments());
+               assertEquals(1, ct.getNumberOfAuthors());
+
+               XSSFComment comment = ct.findCellComment("C5");
+               
+               assertEquals("Nick Burch", comment.getAuthor());
+               assertEquals("Nick Burch:\nThis is a comment", comment.getString().getString());
+               
+               // Re-serialise
+               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               ct.writeTo(baos);
+               ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+               ct = new CommentsTable(bais);
+               
+               assertEquals(2, ct.getNumberOfComments());
+               assertEquals(1, ct.getNumberOfAuthors());
+               
+               comment = ct.findCellComment("C5");
+               
+               assertEquals("Nick Burch", comment.getAuthor());
+               
+               // TODO: Fix this!
+               // New line should still be there, but isn't!
+               //assertEquals("Nick Burch:\nThis is a comment", comment.getString().getString());
        }
 
        public void testExisting() throws Exception {
@@ -163,6 +212,7 @@ public class TestCommentsTable extends TestCase {
                // Save, and re-load the file
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                workbook.write(baos);
+               
                ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
                workbook = new XSSFWorkbook(Package.open(bais));
                
@@ -174,6 +224,60 @@ public class TestCommentsTable extends TestCase {
                assertNotNull(sheet2.getRow(2).getCell(1).getCellComment());
                
                // And check they still have the contents they should do
-               // TODO
+               assertEquals("Apache POI", 
+                               sheet1.getRow(4).getCell(2).getCellComment().getAuthor());
+               assertEquals("Nick Burch", 
+                               sheet1.getRow(6).getCell(2).getCellComment().getAuthor());
+               assertEquals("Also POI", 
+                               sheet2.getRow(2).getCell(1).getCellComment().getAuthor());
+               
+               // TODO: fix xmlbeans so it doesn't eat newlines
+               assertEquals("Nick Burch:This is a comment",
+                               sheet1.getRow(4).getCell(2).getCellComment().getString().getString());
+       }
+       
+       public void testReadWriteMultipleAuthors() throws Exception {
+               File xml = new File(
+                               System.getProperty("HSSF.testdata.path") +
+                               File.separator + "WithMoreVariousData.xlsx"
+               );
+               assertTrue(xml.exists());
+       
+               XSSFWorkbook workbook = new XSSFWorkbook(xml.toString());
+               Sheet sheet1 = workbook.getSheetAt(0);
+               XSSFSheet sheet2 = (XSSFSheet)workbook.getSheetAt(1);
+               
+               assertTrue( ((XSSFSheet)sheet1).hasComments() );
+               assertFalse( ((XSSFSheet)sheet2).hasComments() );
+               
+               assertEquals("Nick Burch", 
+                               sheet1.getRow(4).getCell(2).getCellComment().getAuthor());
+               assertEquals("Nick Burch", 
+                               sheet1.getRow(6).getCell(2).getCellComment().getAuthor());
+               assertEquals("Torchbox", 
+                               sheet1.getRow(12).getCell(2).getCellComment().getAuthor());
+               
+               // Save, and re-load the file
+               ByteArrayOutputStream baos = new ByteArrayOutputStream();
+               workbook.write(baos);
+               
+               ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
+               workbook = new XSSFWorkbook(Package.open(bais));
+               
+               // Check we still have comments where we should do
+               sheet1 = workbook.getSheetAt(0);
+               assertNotNull(sheet1.getRow(4).getCell(2).getCellComment());
+               assertNotNull(sheet1.getRow(6).getCell(2).getCellComment());
+               assertNotNull(sheet1.getRow(12).getCell(2).getCellComment());
+               
+               // And check they still have the contents they should do
+               assertEquals("Nick Burch", 
+                               sheet1.getRow(4).getCell(2).getCellComment().getAuthor());
+               assertEquals("Nick Burch", 
+                               sheet1.getRow(6).getCell(2).getCellComment().getAuthor());
+               assertEquals("Torchbox", 
+                               sheet1.getRow(12).getCell(2).getCellComment().getAuthor());
+               
+               // Todo - check text too, once bug fixed
        }
 }