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.

TestXSSFVMLDrawing.java 7.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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 static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertNotNull;
  18. import static org.junit.Assert.assertNull;
  19. import static org.junit.Assert.assertSame;
  20. import static org.junit.Assert.assertTrue;
  21. import java.io.ByteArrayInputStream;
  22. import java.io.ByteArrayOutputStream;
  23. import java.io.IOException;
  24. import java.io.InputStream;
  25. import java.math.BigInteger;
  26. import java.util.List;
  27. import java.util.regex.Pattern;
  28. import org.apache.poi.POIDataSamples;
  29. import org.apache.xmlbeans.XmlException;
  30. import org.apache.xmlbeans.XmlObject;
  31. import org.junit.Test;
  32. import com.microsoft.schemas.office.excel.CTClientData;
  33. import com.microsoft.schemas.office.excel.STObjectType;
  34. import com.microsoft.schemas.office.excel.STTrueFalseBlank;
  35. import com.microsoft.schemas.office.office.CTShapeLayout;
  36. import com.microsoft.schemas.office.office.STConnectType;
  37. import com.microsoft.schemas.office.office.STInsetMode;
  38. import com.microsoft.schemas.vml.CTShadow;
  39. import com.microsoft.schemas.vml.CTShape;
  40. import com.microsoft.schemas.vml.CTShapetype;
  41. import com.microsoft.schemas.vml.STExt;
  42. import com.microsoft.schemas.vml.STTrueFalse;
  43. public class TestXSSFVMLDrawing {
  44. @Test
  45. public void testNew() throws IOException, XmlException {
  46. XSSFVMLDrawing vml = new XSSFVMLDrawing();
  47. List<XmlObject> items = vml.getItems();
  48. assertEquals(2, items.size());
  49. assertTrue(items.get(0) instanceof CTShapeLayout);
  50. CTShapeLayout layout = (CTShapeLayout)items.get(0);
  51. assertEquals(STExt.EDIT, layout.getExt());
  52. assertEquals(STExt.EDIT, layout.getIdmap().getExt());
  53. assertEquals("1", layout.getIdmap().getData());
  54. assertTrue(items.get(1) instanceof CTShapetype);
  55. CTShapetype type = (CTShapetype)items.get(1);
  56. assertEquals("21600,21600", type.getCoordsize());
  57. assertEquals(202.0f, type.getSpt(), 0);
  58. assertEquals("m,l,21600r21600,l21600,xe", type.getPath2());
  59. assertEquals("_x0000_t202", type.getId());
  60. assertEquals(STTrueFalse.T, type.getPathArray(0).getGradientshapeok());
  61. assertEquals(STConnectType.RECT, type.getPathArray(0).getConnecttype());
  62. CTShape shape = vml.newCommentShape();
  63. assertEquals(3, items.size());
  64. assertSame(items.get(2), shape);
  65. assertEquals("#_x0000_t202", shape.getType());
  66. assertEquals("position:absolute; visibility:hidden", shape.getStyle());
  67. assertEquals("#ffffe1", shape.getFillcolor());
  68. assertEquals(STInsetMode.AUTO, shape.getInsetmode());
  69. assertEquals("#ffffe1", shape.getFillArray(0).getColor());
  70. CTShadow shadow = shape.getShadowArray(0);
  71. assertEquals(STTrueFalse.T, shadow.getOn());
  72. assertEquals("black", shadow.getColor());
  73. assertEquals(STTrueFalse.T, shadow.getObscured());
  74. assertEquals(STConnectType.NONE, shape.getPathArray(0).getConnecttype());
  75. assertEquals("mso-direction-alt:auto", shape.getTextboxArray(0).getStyle());
  76. CTClientData cldata = shape.getClientDataArray(0);
  77. assertEquals(STObjectType.NOTE, cldata.getObjectType());
  78. assertEquals(1, cldata.sizeOfMoveWithCellsArray());
  79. assertEquals(1, cldata.sizeOfSizeWithCellsArray());
  80. assertEquals("1, 15, 0, 2, 3, 15, 3, 16", cldata.getAnchorArray(0));
  81. assertEquals("False", cldata.getAutoFillArray(0).toString());
  82. assertEquals(0, cldata.getRowArray(0).intValue());
  83. assertEquals(0, cldata.getColumnArray(0).intValue());
  84. assertEquals("[]", cldata.getVisibleList().toString());
  85. cldata.setVisibleArray(new STTrueFalseBlank.Enum[] { STTrueFalseBlank.Enum.forString("True") });
  86. assertEquals("[True]", cldata.getVisibleList().toString());
  87. //serialize and read again
  88. ByteArrayOutputStream out = new ByteArrayOutputStream();
  89. vml.write(out);
  90. XSSFVMLDrawing vml2 = new XSSFVMLDrawing();
  91. vml2.read(new ByteArrayInputStream(out.toByteArray()));
  92. List<XmlObject> items2 = vml2.getItems();
  93. assertEquals(3, items2.size());
  94. assertTrue(items2.get(0) instanceof CTShapeLayout);
  95. assertTrue(items2.get(1) instanceof CTShapetype);
  96. assertTrue(items2.get(2) instanceof CTShape);
  97. }
  98. @Test
  99. public void testFindCommentShape() throws IOException, XmlException {
  100. XSSFVMLDrawing vml = new XSSFVMLDrawing();
  101. InputStream stream = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("vmlDrawing1.vml");
  102. try {
  103. vml.read(stream);
  104. } finally {
  105. stream.close();
  106. }
  107. CTShape sh_a1 = vml.findCommentShape(0, 0);
  108. assertNotNull(sh_a1);
  109. assertEquals("_x0000_s1025", sh_a1.getId());
  110. CTShape sh_b1 = vml.findCommentShape(0, 1);
  111. assertNotNull(sh_b1);
  112. assertEquals("_x0000_s1026", sh_b1.getId());
  113. CTShape sh_c1 = vml.findCommentShape(0, 2);
  114. assertNull(sh_c1);
  115. CTShape sh_d1 = vml.newCommentShape();
  116. assertEquals("_x0000_s1027", sh_d1.getId());
  117. sh_d1.getClientDataArray(0).setRowArray(0, new BigInteger("0"));
  118. sh_d1.getClientDataArray(0).setColumnArray(0, new BigInteger("3"));
  119. assertSame(sh_d1, vml.findCommentShape(0, 3));
  120. //newly created drawing
  121. XSSFVMLDrawing newVml = new XSSFVMLDrawing();
  122. assertNull(newVml.findCommentShape(0, 0));
  123. sh_a1 = newVml.newCommentShape();
  124. assertEquals("_x0000_s1025", sh_a1.getId());
  125. sh_a1.getClientDataArray(0).setRowArray(0, new BigInteger("0"));
  126. sh_a1.getClientDataArray(0).setColumnArray(0, new BigInteger("1"));
  127. assertSame(sh_a1, newVml.findCommentShape(0, 1));
  128. }
  129. @Test
  130. public void testRemoveCommentShape() throws IOException, XmlException {
  131. XSSFVMLDrawing vml = new XSSFVMLDrawing();
  132. InputStream stream = POIDataSamples.getSpreadSheetInstance().openResourceAsStream("vmlDrawing1.vml");
  133. try {
  134. vml.read(stream);
  135. } finally {
  136. stream.close();
  137. }
  138. CTShape sh_a1 = vml.findCommentShape(0, 0);
  139. assertNotNull(sh_a1);
  140. assertTrue(vml.removeCommentShape(0, 0));
  141. assertNull(vml.findCommentShape(0, 0));
  142. }
  143. @Test
  144. public void testEvilUnclosedBRFixing() throws IOException, XmlException {
  145. XSSFVMLDrawing vml = new XSSFVMLDrawing();
  146. InputStream stream = POIDataSamples.getOpenXML4JInstance().openResourceAsStream("bug-60626.vml");
  147. try {
  148. vml.read(stream);
  149. } finally {
  150. stream.close();
  151. }
  152. Pattern p = Pattern.compile("<br/>");
  153. int count = 0;
  154. for (XmlObject xo : vml.getItems()) {
  155. String split[] = p.split(xo.toString());
  156. count += split.length-1;
  157. }
  158. assertEquals(16, count);
  159. }
  160. }