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