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.

TestPOIXMLDocument.java 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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;
  16. import static org.junit.Assert.assertEquals;
  17. import static org.junit.Assert.assertFalse;
  18. import static org.junit.Assert.assertNotNull;
  19. import static org.junit.Assert.assertNull;
  20. import static org.junit.Assert.assertSame;
  21. import static org.junit.Assert.fail;
  22. import java.io.File;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.lang.Thread.UncaughtExceptionHandler;
  27. import java.lang.reflect.InvocationTargetException;
  28. import java.util.ArrayList;
  29. import java.util.HashMap;
  30. import java.util.HashSet;
  31. import java.util.List;
  32. import org.apache.poi.POIXMLDocumentPart.RelationPart;
  33. import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
  34. import org.apache.poi.openxml4j.exceptions.OpenXML4JRuntimeException;
  35. import org.apache.poi.openxml4j.opc.OPCPackage;
  36. import org.apache.poi.openxml4j.opc.PackagePart;
  37. import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
  38. import org.apache.poi.util.IOUtils;
  39. import org.apache.poi.util.NullOutputStream;
  40. import org.apache.poi.util.PackageHelper;
  41. import org.apache.poi.util.TempFile;
  42. import org.apache.poi.xslf.usermodel.XMLSlideShow;
  43. import org.apache.poi.xssf.usermodel.XSSFRelation;
  44. import org.apache.poi.xwpf.usermodel.XWPFRelation;
  45. import org.junit.Test;
  46. /**
  47. * Test recursive read and write of OPC packages
  48. */
  49. public final class TestPOIXMLDocument {
  50. private static class OPCParser extends POIXMLDocument {
  51. public OPCParser(OPCPackage pkg) {
  52. super(pkg);
  53. }
  54. public OPCParser(OPCPackage pkg, String coreDocumentRel) {
  55. super(pkg, coreDocumentRel);
  56. }
  57. @Override
  58. public List<PackagePart> getAllEmbedds() {
  59. throw new RuntimeException("not supported");
  60. }
  61. public void parse(POIXMLFactory factory) throws IOException{
  62. load(factory);
  63. }
  64. }
  65. private static final class TestFactory extends POIXMLFactory {
  66. public TestFactory() {
  67. //
  68. }
  69. @Override
  70. protected POIXMLRelation getDescriptor(String relationshipType) {
  71. return null;
  72. }
  73. /**
  74. * @since POI 3.14-Beta1
  75. */
  76. @Override
  77. protected POIXMLDocumentPart createDocumentPart
  78. (Class<? extends POIXMLDocumentPart> cls, Class<?>[] classes, Object[] values)
  79. throws SecurityException, NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException {
  80. return null;
  81. }
  82. }
  83. private static void traverse(POIXMLDocument doc) throws IOException{
  84. HashMap<String,POIXMLDocumentPart> context = new HashMap<>();
  85. for (RelationPart p : doc.getRelationParts()){
  86. traverse(p, context);
  87. }
  88. }
  89. /**
  90. * Recursively traverse a OOXML document and assert that same logical parts have the same physical instances
  91. */
  92. private static void traverse(RelationPart rp, HashMap<String,POIXMLDocumentPart> context) throws IOException{
  93. POIXMLDocumentPart dp = rp.getDocumentPart();
  94. assertEquals(rp.getRelationship().getTargetURI().toString(), dp.getPackagePart().getPartName().getName());
  95. context.put(dp.getPackagePart().getPartName().getName(), dp);
  96. for(RelationPart p : dp.getRelationParts()){
  97. assertNotNull(p.getRelationship().toString());
  98. String uri = p.getDocumentPart().getPackagePart().getPartName().getURI().toString();
  99. assertEquals(uri, p.getRelationship().getTargetURI().toString());
  100. if (!context.containsKey(uri)) {
  101. traverse(p, context);
  102. } else {
  103. POIXMLDocumentPart prev = context.get(uri);
  104. assertSame("Duplicate POIXMLDocumentPart instance for targetURI=" + uri, prev, p.getDocumentPart());
  105. }
  106. }
  107. }
  108. public void assertReadWrite(OPCPackage pkg1) throws Exception {
  109. OPCParser doc = new OPCParser(pkg1);
  110. doc.parse(new TestFactory());
  111. traverse(doc);
  112. File tmp = TempFile.createTempFile("poi-ooxml", ".tmp");
  113. FileOutputStream out = new FileOutputStream(tmp);
  114. doc.write(out);
  115. out.close();
  116. // Should not be able to write to an output stream that has been closed
  117. try {
  118. doc.write(out);
  119. fail("Should not be able to write to an output stream that has been closed.");
  120. } catch (final OpenXML4JRuntimeException e) {
  121. // FIXME: A better exception class (IOException?) and message should be raised
  122. // indicating that the document could not be written because the output stream is closed.
  123. // see {@link org.apache.poi.openxml4j.opc.ZipPackage#saveImpl(java.io.OutputStream)}
  124. if (e.getMessage().matches("Fail to save: an error occurs while saving the package : The part .+ failed to be saved in the stream with marshaller .+")) {
  125. // expected
  126. } else {
  127. throw e;
  128. }
  129. }
  130. // Should not be able to write a document that has been closed
  131. doc.close();
  132. try {
  133. doc.write(new NullOutputStream());
  134. fail("Should not be able to write a document that has been closed.");
  135. } catch (final IOException e) {
  136. if (e.getMessage().equals("Cannot write data, document seems to have been closed already")) {
  137. // expected
  138. } else {
  139. throw e;
  140. }
  141. }
  142. // Should be able to close a document multiple times, though subsequent closes will have no effect.
  143. doc.close();
  144. @SuppressWarnings("resource")
  145. OPCPackage pkg2 = OPCPackage.open(tmp.getAbsolutePath());
  146. doc = new OPCParser(pkg1);
  147. try {
  148. doc.parse(new TestFactory());
  149. traverse(doc);
  150. assertEquals(pkg1.getRelationships().size(), pkg2.getRelationships().size());
  151. ArrayList<PackagePart> l1 = pkg1.getParts();
  152. ArrayList<PackagePart> l2 = pkg2.getParts();
  153. assertEquals(l1.size(), l2.size());
  154. for (int i=0; i < l1.size(); i++){
  155. PackagePart p1 = l1.get(i);
  156. PackagePart p2 = l2.get(i);
  157. assertEquals(p1.getContentType(), p2.getContentType());
  158. assertEquals(p1.hasRelationships(), p2.hasRelationships());
  159. if(p1.hasRelationships()){
  160. assertEquals(p1.getRelationships().size(), p2.getRelationships().size());
  161. }
  162. assertEquals(p1.getPartName(), p2.getPartName());
  163. }
  164. } finally {
  165. doc.close();
  166. pkg1.close();
  167. pkg2.close();
  168. }
  169. }
  170. @Test
  171. public void testPPTX() throws Exception {
  172. POIDataSamples pds = POIDataSamples.getSlideShowInstance();
  173. assertReadWrite(PackageHelper.open(pds.openResourceAsStream("PPTWithAttachments.pptm")));
  174. }
  175. @Test
  176. public void testXLSX() throws Exception {
  177. POIDataSamples pds = POIDataSamples.getSpreadSheetInstance();
  178. assertReadWrite(PackageHelper.open(pds.openResourceAsStream("ExcelWithAttachments.xlsm")));
  179. }
  180. @Test
  181. public void testDOCX() throws Exception {
  182. POIDataSamples pds = POIDataSamples.getDocumentInstance();
  183. assertReadWrite(PackageHelper.open(pds.openResourceAsStream("WordWithAttachments.docx")));
  184. }
  185. @Test
  186. public void testRelationOrder() throws Exception {
  187. POIDataSamples pds = POIDataSamples.getDocumentInstance();
  188. @SuppressWarnings("resource")
  189. OPCPackage pkg = PackageHelper.open(pds.openResourceAsStream("WordWithAttachments.docx"));
  190. OPCParser doc = new OPCParser(pkg);
  191. try {
  192. doc.parse(new TestFactory());
  193. for(POIXMLDocumentPart rel : doc.getRelations()){
  194. //TODO finish me
  195. assertNotNull(rel);
  196. }
  197. } finally {
  198. doc.close();
  199. }
  200. }
  201. @Test
  202. public void testGetNextPartNumber() throws Exception {
  203. POIDataSamples pds = POIDataSamples.getDocumentInstance();
  204. @SuppressWarnings("resource")
  205. OPCPackage pkg = PackageHelper.open(pds.openResourceAsStream("WordWithAttachments.docx"));
  206. OPCParser doc = new OPCParser(pkg);
  207. try {
  208. doc.parse(new TestFactory());
  209. // Non-indexed parts: Word is taken, Excel is not
  210. assertEquals(-1, doc.getNextPartNumber(XWPFRelation.DOCUMENT, 0));
  211. assertEquals(-1, doc.getNextPartNumber(XWPFRelation.DOCUMENT, -1));
  212. assertEquals(-1, doc.getNextPartNumber(XWPFRelation.DOCUMENT, 99));
  213. assertEquals(0, doc.getNextPartNumber(XSSFRelation.WORKBOOK, 0));
  214. assertEquals(0, doc.getNextPartNumber(XSSFRelation.WORKBOOK, -1));
  215. assertEquals(0, doc.getNextPartNumber(XSSFRelation.WORKBOOK, 99));
  216. // Indexed parts:
  217. // Has 2 headers
  218. assertEquals(0, doc.getNextPartNumber(XWPFRelation.HEADER, 0));
  219. assertEquals(3, doc.getNextPartNumber(XWPFRelation.HEADER, -1));
  220. assertEquals(3, doc.getNextPartNumber(XWPFRelation.HEADER, 1));
  221. assertEquals(8, doc.getNextPartNumber(XWPFRelation.HEADER, 8));
  222. // Has no Excel Sheets
  223. assertEquals(0, doc.getNextPartNumber(XSSFRelation.WORKSHEET, 0));
  224. assertEquals(1, doc.getNextPartNumber(XSSFRelation.WORKSHEET, -1));
  225. assertEquals(1, doc.getNextPartNumber(XSSFRelation.WORKSHEET, 1));
  226. } finally {
  227. doc.close();
  228. }
  229. }
  230. @Test
  231. public void testCommitNullPart() throws IOException, InvalidFormatException {
  232. POIXMLDocumentPart part = new POIXMLDocumentPart();
  233. part.prepareForCommit();
  234. part.commit();
  235. part.onSave(new HashSet<>());
  236. assertNull(part.getRelationById(null));
  237. assertNull(part.getRelationId(null));
  238. assertFalse(part.removeRelation(null, true));
  239. part.removeRelation((POIXMLDocumentPart)null);
  240. assertEquals("",part.toString());
  241. part.onDocumentCreate();
  242. //part.getTargetPart(null);
  243. }
  244. @Test
  245. public void testVSDX() throws Exception {
  246. POIDataSamples pds = POIDataSamples.getDiagramInstance();
  247. @SuppressWarnings("resource")
  248. OPCPackage open = PackageHelper.open(pds.openResourceAsStream("test.vsdx"));
  249. POIXMLDocument part = new OPCParser(open, PackageRelationshipTypes.VISIO_CORE_DOCUMENT);
  250. assertNotNull(part);
  251. assertEquals(0, part.getRelationCounter());
  252. part.close();
  253. }
  254. @Test
  255. public void testVSDXPart() throws IOException {
  256. POIDataSamples pds = POIDataSamples.getDiagramInstance();
  257. OPCPackage open = PackageHelper.open(pds.openResourceAsStream("test.vsdx"));
  258. POIXMLDocumentPart part = new POIXMLDocumentPart(open, PackageRelationshipTypes.VISIO_CORE_DOCUMENT);
  259. assertNotNull(part);
  260. assertEquals(0, part.getRelationCounter());
  261. open.close();
  262. }
  263. @Test(expected=POIXMLException.class)
  264. public void testInvalidCoreRel() throws IOException {
  265. POIDataSamples pds = POIDataSamples.getDiagramInstance();
  266. OPCPackage open = PackageHelper.open(pds.openResourceAsStream("test.vsdx"));
  267. try {
  268. new POIXMLDocumentPart(open, "somethingillegal");
  269. } finally {
  270. open.close();
  271. }
  272. }
  273. @Test
  274. public void testOSGIClassLoading() {
  275. // the schema type loader is cached per thread in POIXMLTypeLoader.
  276. // So create a new Thread and change the context class loader (which would normally be used)
  277. // to not contain the OOXML classes
  278. Runnable run = new Runnable() {
  279. public void run() {
  280. InputStream is = POIDataSamples.getSlideShowInstance().openResourceAsStream("table_test.pptx");
  281. XMLSlideShow ppt = null;
  282. try {
  283. ppt = new XMLSlideShow(is);
  284. ppt.getSlides().get(0).getShapes();
  285. } catch (IOException e) {
  286. fail("failed to load XMLSlideShow");
  287. } finally {
  288. IOUtils.closeQuietly(ppt);
  289. IOUtils.closeQuietly(is);
  290. }
  291. }
  292. };
  293. Thread thread = Thread.currentThread();
  294. ClassLoader cl = thread.getContextClassLoader();
  295. UncaughtHandler uh = new UncaughtHandler();
  296. // check schema type loading and check if we could run in an OOM
  297. Thread ta[] = new Thread[30];
  298. for (int j=0; j<10; j++) {
  299. for (int i=0; i<ta.length; i++) {
  300. ta[i] = new Thread(run);
  301. ta[i].setContextClassLoader(cl.getParent());
  302. ta[i].setUncaughtExceptionHandler(uh);
  303. ta[i].start();
  304. }
  305. for (int i=0; i<ta.length; i++) {
  306. try {
  307. ta[i].join();
  308. } catch (InterruptedException e) {
  309. fail("failed to join thread");
  310. }
  311. }
  312. }
  313. assertFalse(uh.hasException());
  314. }
  315. private static class UncaughtHandler implements UncaughtExceptionHandler {
  316. Throwable e;
  317. public synchronized void uncaughtException(Thread t, Throwable e) {
  318. this.e = e;
  319. }
  320. public synchronized boolean hasException() {
  321. return e != null;
  322. }
  323. }
  324. }