You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestXSSFComment.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.xssf.usermodel;
  16. import org.apache.poi.hssf.usermodel.HSSFRichTextString;
  17. import org.apache.poi.ss.usermodel.*;
  18. import org.apache.poi.ss.util.CellReference;
  19. import org.apache.poi.xssf.model.CommentsTable;
  20. import org.apache.poi.xssf.XSSFTestDataSamples;
  21. import org.apache.poi.xssf.XSSFITestDataProvider;
  22. import org.apache.poi.POIXMLDocumentPart;
  23. import org.apache.xmlbeans.XmlObject;
  24. import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
  25. import junit.framework.TestCase;
  26. import junit.framework.AssertionFailedError;
  27. import java.io.ByteArrayOutputStream;
  28. import java.io.ByteArrayInputStream;
  29. import java.io.IOException;
  30. import java.util.List;
  31. import schemasMicrosoftComVml.CTShape;
  32. public class TestXSSFComment extends BaseTestCellComment {
  33. private static final String TEST_RICHTEXTSTRING = "test richtextstring";
  34. @Override
  35. protected XSSFITestDataProvider getTestDataProvider(){
  36. return XSSFITestDataProvider.getInstance();
  37. }
  38. /**
  39. * test that we can read cell comments from an existing workbook.
  40. */
  41. public void testReadComments() {
  42. readComments("SimpleWithComments.xlsx");
  43. }
  44. /**
  45. * test that we can modify existing cell comments
  46. */
  47. public void testModifyComments() throws IOException {
  48. modifyComments("SimpleWithComments.xlsx");
  49. }
  50. public void testDeleteComments() throws Exception {
  51. deleteComments("SimpleWithComments.xlsx");
  52. }
  53. /**
  54. * test properties of a newly constructed comment
  55. */
  56. public void testConstructor() {
  57. CommentsTable sheetComments = new CommentsTable();
  58. assertNotNull(sheetComments.getCTComments().getCommentList());
  59. assertNotNull(sheetComments.getCTComments().getAuthors());
  60. assertEquals(1, sheetComments.getCTComments().getAuthors().sizeOfAuthorArray());
  61. assertEquals(1, sheetComments.getNumberOfAuthors());
  62. CTComment ctComment = sheetComments.newComment();
  63. CTShape vmlShape = CTShape.Factory.newInstance();
  64. XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
  65. assertEquals(null, comment.getString());
  66. assertEquals(0, comment.getRow());
  67. assertEquals(0, comment.getColumn());
  68. assertEquals("", comment.getAuthor());
  69. assertEquals(false, comment.isVisible());
  70. }
  71. public void testGetSetCol() {
  72. CommentsTable sheetComments = new CommentsTable();
  73. XSSFVMLDrawing vml = new XSSFVMLDrawing();
  74. CTComment ctComment = sheetComments.newComment();
  75. CTShape vmlShape = vml.newCommentShape();
  76. XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
  77. comment.setColumn(1);
  78. assertEquals(1, comment.getColumn());
  79. assertEquals(1, new CellReference(ctComment.getRef()).getCol());
  80. assertEquals(1, vmlShape.getClientDataArray(0).getColumnArray(0).intValue());
  81. comment.setColumn(5);
  82. assertEquals(5, comment.getColumn());
  83. assertEquals(5, new CellReference(ctComment.getRef()).getCol());
  84. assertEquals(5, vmlShape.getClientDataArray(0).getColumnArray(0).intValue());
  85. }
  86. public void testGetSetRow() {
  87. CommentsTable sheetComments = new CommentsTable();
  88. XSSFVMLDrawing vml = new XSSFVMLDrawing();
  89. CTComment ctComment = sheetComments.newComment();
  90. CTShape vmlShape = vml.newCommentShape();
  91. XSSFComment comment = new XSSFComment(sheetComments, ctComment, vmlShape);
  92. comment.setRow(1);
  93. assertEquals(1, comment.getRow());
  94. assertEquals(1, new CellReference(ctComment.getRef()).getRow());
  95. assertEquals(1, vmlShape.getClientDataArray(0).getRowArray(0).intValue());
  96. comment.setRow(5);
  97. assertEquals(5, comment.getRow());
  98. assertEquals(5, new CellReference(ctComment.getRef()).getRow());
  99. assertEquals(5, vmlShape.getClientDataArray(0).getRowArray(0).intValue());
  100. }
  101. public void testSetString() {
  102. XSSFWorkbook wb = new XSSFWorkbook();
  103. XSSFSheet sh = wb.createSheet();
  104. XSSFComment comment = sh.createDrawingPatriarch().createCellComment(new XSSFClientAnchor());
  105. //passing HSSFRichTextString is incorrect
  106. try {
  107. comment.setString(new HSSFRichTextString(TEST_RICHTEXTSTRING));
  108. fail("expected exception");
  109. } catch (IllegalArgumentException e){
  110. ;
  111. }
  112. //simple string argument
  113. comment.setString(TEST_RICHTEXTSTRING);
  114. assertEquals(TEST_RICHTEXTSTRING, comment.getString().getString());
  115. //if the text is already set, it should be overridden, not added twice!
  116. comment.setString(TEST_RICHTEXTSTRING);
  117. CTComment ctComment = comment.getCTComment();
  118. XmlObject[] obj = ctComment.selectPath(
  119. "declare namespace w='http://schemas.openxmlformats.org/spreadsheetml/2006/main' .//w:text");
  120. assertEquals(1, obj.length);
  121. assertEquals(TEST_RICHTEXTSTRING, comment.getString().getString());
  122. //sequential call of comment.getString() should return the same XSSFRichTextString object
  123. assertSame(comment.getString(), comment.getString());
  124. XSSFRichTextString richText = new XSSFRichTextString(TEST_RICHTEXTSTRING);
  125. XSSFFont font1 = wb.createFont();
  126. font1.setFontName("Tahoma");
  127. font1.setFontHeight(8.5);
  128. font1.setItalic(true);
  129. font1.setColor(IndexedColors.BLUE_GREY.getIndex());
  130. richText.applyFont(0, 5, font1);
  131. //check the low-level stuff
  132. comment.setString(richText);
  133. obj = ctComment.selectPath(
  134. "declare namespace w='http://schemas.openxmlformats.org/spreadsheetml/2006/main' .//w:text");
  135. assertEquals(1, obj.length);
  136. assertSame(comment.getString(), richText);
  137. //check that the rich text is set in the comment
  138. CTRPrElt rPr = richText.getCTRst().getRArray(0).getRPr();
  139. assertEquals(true, rPr.getIArray()[0].getVal());
  140. assertEquals(8.5, rPr.getSzArray()[0].getVal());
  141. assertEquals(IndexedColors.BLUE_GREY.getIndex(), rPr.getColorArray()[0].getIndexed());
  142. assertEquals("Tahoma", rPr.getRFontArray()[0].getVal());
  143. }
  144. public void testAuthor() {
  145. CommentsTable sheetComments = new CommentsTable();
  146. CTComment ctComment = sheetComments.newComment();
  147. assertEquals(1, sheetComments.getNumberOfAuthors());
  148. XSSFComment comment = new XSSFComment(sheetComments, ctComment, null);
  149. assertEquals("", comment.getAuthor());
  150. comment.setAuthor("Apache POI");
  151. assertEquals("Apache POI", comment.getAuthor());
  152. assertEquals(2, sheetComments.getNumberOfAuthors());
  153. comment.setAuthor("Apache POI");
  154. assertEquals(2, sheetComments.getNumberOfAuthors());
  155. comment.setAuthor("");
  156. assertEquals("", comment.getAuthor());
  157. assertEquals(2, sheetComments.getNumberOfAuthors());
  158. }
  159. }