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.

TestComment.java 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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.hssf.usermodel;
  16. import junit.framework.TestCase;
  17. import org.apache.poi.ddf.EscherSpRecord;
  18. import org.apache.poi.hssf.HSSFTestDataSamples;
  19. import org.apache.poi.hssf.model.CommentShape;
  20. import org.apache.poi.hssf.model.HSSFTestModelHelper;
  21. import org.apache.poi.hssf.record.*;
  22. import java.io.*;
  23. import java.util.Arrays;
  24. /**
  25. * @author Evgeniy Berlog
  26. * @date 26.06.12
  27. */
  28. public class TestComment extends TestCase {
  29. public void testResultEqualsToAbstractShape() {
  30. HSSFWorkbook wb = new HSSFWorkbook();
  31. HSSFSheet sh = wb.createSheet();
  32. HSSFPatriarch patriarch = sh.createDrawingPatriarch();
  33. HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor());
  34. HSSFRow row = sh.createRow(0);
  35. HSSFCell cell = row.createCell(0);
  36. cell.setCellComment(comment);
  37. CommentShape commentShape = HSSFTestModelHelper.createCommentShape(1025, comment);
  38. assertEquals(comment.getEscherContainer().getChildRecords().size(), 5);
  39. assertEquals(commentShape.getSpContainer().getChildRecords().size(), 5);
  40. //sp record
  41. byte[] expected = commentShape.getSpContainer().getChild(0).serialize();
  42. byte[] actual = comment.getEscherContainer().getChild(0).serialize();
  43. assertEquals(expected.length, actual.length);
  44. assertTrue(Arrays.equals(expected, actual));
  45. expected = commentShape.getSpContainer().getChild(2).serialize();
  46. actual = comment.getEscherContainer().getChild(2).serialize();
  47. assertEquals(expected.length, actual.length);
  48. assertTrue(Arrays.equals(expected, actual));
  49. expected = commentShape.getSpContainer().getChild(3).serialize();
  50. actual = comment.getEscherContainer().getChild(3).serialize();
  51. assertEquals(expected.length, actual.length);
  52. assertTrue(Arrays.equals(expected, actual));
  53. expected = commentShape.getSpContainer().getChild(4).serialize();
  54. actual = comment.getEscherContainer().getChild(4).serialize();
  55. assertEquals(expected.length, actual.length);
  56. assertTrue(Arrays.equals(expected, actual));
  57. ObjRecord obj = comment.getObjRecord();
  58. ObjRecord objShape = commentShape.getObjRecord();
  59. /**shapeId = 1025 % 1024**/
  60. ((CommonObjectDataSubRecord)objShape.getSubRecords().get(0)).setObjectId(1);
  61. expected = obj.serialize();
  62. actual = objShape.serialize();
  63. TextObjectRecord tor = comment.getTextObjectRecord();
  64. TextObjectRecord torShape = commentShape.getTextObjectRecord();
  65. expected = tor.serialize();
  66. actual = torShape.serialize();
  67. assertEquals(expected.length, actual.length);
  68. assertTrue(Arrays.equals(expected, actual));
  69. NoteRecord note = comment.getNoteRecord();
  70. NoteRecord noteShape = commentShape.getNoteRecord();
  71. noteShape.setShapeId(1);
  72. expected = note.serialize();
  73. actual = noteShape.serialize();
  74. assertEquals(expected.length, actual.length);
  75. assertTrue(Arrays.equals(expected, actual));
  76. }
  77. public void testAddToExistingFile() {
  78. HSSFWorkbook wb = new HSSFWorkbook();
  79. HSSFSheet sh = wb.createSheet();
  80. HSSFPatriarch patriarch = sh.createDrawingPatriarch();
  81. int idx = wb.addPicture(new byte[]{1,2,3}, HSSFWorkbook.PICTURE_TYPE_PNG);
  82. HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor());
  83. comment.setColumn(5);
  84. comment.setString(new HSSFRichTextString("comment1"));
  85. comment = patriarch.createCellComment(new HSSFClientAnchor(0,0,100,100,(short)0,0,(short)10,10));
  86. comment.setRow(5);
  87. comment.setString(new HSSFRichTextString("comment2"));
  88. comment.setBackgroundImage(idx);
  89. assertEquals(comment.getBackgroundImageId(), idx);
  90. assertEquals(patriarch.getChildren().size(), 2);
  91. wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
  92. sh = wb.getSheetAt(0);
  93. patriarch = sh.getDrawingPatriarch();
  94. comment = (HSSFComment) patriarch.getChildren().get(1);
  95. assertEquals(comment.getBackgroundImageId(), idx);
  96. comment.resetBackgroundImage();
  97. assertEquals(comment.getBackgroundImageId(), 0);
  98. assertEquals(patriarch.getChildren().size(), 2);
  99. comment = patriarch.createCellComment(new HSSFClientAnchor());
  100. comment.setString(new HSSFRichTextString("comment3"));
  101. assertEquals(patriarch.getChildren().size(), 3);
  102. wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
  103. sh = wb.getSheetAt(0);
  104. patriarch = sh.getDrawingPatriarch();
  105. comment = (HSSFComment) patriarch.getChildren().get(1);
  106. assertEquals(comment.getBackgroundImageId(), 0);
  107. assertEquals(patriarch.getChildren().size(), 3);
  108. assertEquals(((HSSFComment) patriarch.getChildren().get(0)).getString().getString(), "comment1");
  109. assertEquals(((HSSFComment) patriarch.getChildren().get(1)).getString().getString(), "comment2");
  110. assertEquals(((HSSFComment) patriarch.getChildren().get(2)).getString().getString(), "comment3");
  111. }
  112. public void testSetGetProperties() throws IOException {
  113. HSSFWorkbook wb = new HSSFWorkbook();
  114. HSSFSheet sh = wb.createSheet();
  115. HSSFPatriarch patriarch = sh.createDrawingPatriarch();
  116. HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor());
  117. comment.setString(new HSSFRichTextString("comment1"));
  118. assertEquals(comment.getString().getString(), "comment1");
  119. comment.setAuthor("poi");
  120. assertEquals(comment.getAuthor(), "poi");
  121. comment.setColumn(3);
  122. assertEquals(comment.getColumn(), 3);
  123. comment.setRow(4);
  124. assertEquals(comment.getRow(), 4);
  125. comment.setVisible(false);
  126. assertEquals(comment.isVisible(), false);
  127. wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
  128. sh = wb.getSheetAt(0);
  129. patriarch = sh.getDrawingPatriarch();
  130. comment = (HSSFComment) patriarch.getChildren().get(0);
  131. assertEquals(comment.getString().getString(), "comment1");
  132. assertEquals("poi", comment.getAuthor());
  133. assertEquals(comment.getColumn(), 3);
  134. assertEquals(comment.getRow(), 4);
  135. assertEquals(comment.isVisible(), false);
  136. comment.setString(new HSSFRichTextString("comment12"));
  137. comment.setAuthor("poi2");
  138. comment.setColumn(32);
  139. comment.setRow(42);
  140. comment.setVisible(true);
  141. wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
  142. sh = wb.getSheetAt(0);
  143. patriarch = sh.getDrawingPatriarch();
  144. comment = (HSSFComment) patriarch.getChildren().get(0);
  145. assertEquals(comment.getString().getString(), "comment12");
  146. assertEquals("poi2", comment.getAuthor());
  147. assertEquals(comment.getColumn(), 32);
  148. assertEquals(comment.getRow(), 42);
  149. assertEquals(comment.isVisible(), true);
  150. }
  151. public void testExistingFileWithComment(){
  152. HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("drawings.xls");
  153. HSSFSheet sheet = wb.getSheet("comments");
  154. HSSFPatriarch drawing = sheet.getDrawingPatriarch();
  155. assertEquals(1, drawing.getChildren().size());
  156. HSSFComment comment = (HSSFComment) drawing.getChildren().get(0);
  157. assertEquals(comment.getAuthor(), "evgeniy");
  158. assertEquals(comment.getString().getString(), "evgeniy:\npoi test");
  159. assertEquals(comment.getColumn(), 1);
  160. assertEquals(comment.getRow(), 2);
  161. }
  162. public void testFindComments(){
  163. HSSFWorkbook wb = new HSSFWorkbook();
  164. HSSFSheet sh = wb.createSheet();
  165. HSSFPatriarch patriarch = sh.createDrawingPatriarch();
  166. HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor());
  167. HSSFRow row = sh.createRow(5);
  168. HSSFCell cell = row.createCell(4);
  169. cell.setCellComment(comment);
  170. HSSFTestModelHelper.createCommentShape(0, comment);
  171. assertNotNull(sh.findCellComment(5, 4));
  172. assertNull(sh.findCellComment(5, 5));
  173. wb = HSSFTestDataSamples.writeOutAndReadBack(wb);
  174. sh = wb.getSheetAt(0);
  175. assertNotNull(sh.findCellComment(5, 4));
  176. assertNull(sh.findCellComment(5, 5));
  177. }
  178. public void testInitState(){
  179. HSSFWorkbook wb = new HSSFWorkbook();
  180. HSSFSheet sh = wb.createSheet();
  181. HSSFPatriarch patriarch = sh.createDrawingPatriarch();
  182. EscherAggregate agg = HSSFTestHelper.getEscherAggregate(patriarch);
  183. assertEquals(agg.getTailRecords().size(), 0);
  184. HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor());
  185. assertEquals(agg.getTailRecords().size(), 1);
  186. HSSFSimpleShape shape = patriarch.createSimpleShape(new HSSFClientAnchor());
  187. assertEquals(comment.getOptRecord().getEscherProperties().size(), 10);
  188. }
  189. public void testShapeId(){
  190. HSSFWorkbook wb = new HSSFWorkbook();
  191. HSSFSheet sh = wb.createSheet();
  192. HSSFPatriarch patriarch = sh.createDrawingPatriarch();
  193. HSSFComment comment = patriarch.createCellComment(new HSSFClientAnchor());
  194. comment.setShapeId(2024);
  195. /**
  196. * SpRecord.id == shapeId
  197. * ObjRecord.id == shapeId % 1024
  198. * NoteRecord.id == ObjectRecord.id == shapeId % 1024
  199. */
  200. assertEquals(comment.getShapeId(), 2024);
  201. CommonObjectDataSubRecord cod = (CommonObjectDataSubRecord) comment.getObjRecord().getSubRecords().get(0);
  202. assertEquals(cod.getObjectId(), 1000);
  203. EscherSpRecord spRecord = (EscherSpRecord) comment.getEscherContainer().getChild(0);
  204. assertEquals(spRecord.getShapeId(), 2024);
  205. assertEquals(comment.getShapeId(), 2024);
  206. assertEquals(comment.getNoteRecord().getShapeId(), 1000);
  207. }
  208. public void testAttemptToSave2CommentsWithSameCoordinates(){
  209. Object err = null;
  210. HSSFWorkbook wb = new HSSFWorkbook();
  211. HSSFSheet sh = wb.createSheet();
  212. HSSFPatriarch patriarch = sh.createDrawingPatriarch();
  213. patriarch.createCellComment(new HSSFClientAnchor());
  214. patriarch.createCellComment(new HSSFClientAnchor());
  215. try{
  216. HSSFTestDataSamples.writeOutAndReadBack(wb);
  217. } catch (IllegalStateException e){
  218. err = 1;
  219. assertEquals(e.getMessage(), "found multiple cell comments for cell $A$1");
  220. }
  221. assertNotNull(err);
  222. }
  223. }