Skip to content

Commit aa4c13a

Browse files
committed
PDFBOX-3882: add test
git-svn-id: https://svn.apache.org/repos/asf/pdfbox/trunk@1930337 13f79535-47bb-0310-9956-ffa450edef68
1 parent e9efafb commit aa4c13a

File tree

1 file changed

+134
-28
lines changed

1 file changed

+134
-28
lines changed

xmpbox/src/test/java/org/apache/xmpbox/xml/DomXmpParserTest.java

Lines changed: 134 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@
2424
import java.io.IOException;
2525
import java.io.InputStream;
2626
import java.nio.charset.StandardCharsets;
27+
import java.util.Calendar;
2728
import java.util.List;
2829

2930
import org.apache.xmpbox.XMPMetadata;
31+
import org.apache.xmpbox.schema.PhotoshopSchema;
3032
import org.apache.xmpbox.schema.XMPMediaManagementSchema;
3133
import org.apache.xmpbox.schema.XMPPageTextSchema;
3234
import org.apache.xmpbox.schema.XMPSchema;
@@ -37,8 +39,11 @@
3739
import org.apache.xmpbox.type.PDFASchemaType;
3840
import org.apache.xmpbox.type.ResourceEventType;
3941
import org.apache.xmpbox.type.ResourceRefType;
42+
import org.apache.xmpbox.type.TextType;
4043

41-
import org.junit.jupiter.api.Assertions;
44+
import static org.junit.jupiter.api.Assertions.assertEquals;
45+
import static org.junit.jupiter.api.Assertions.assertNotNull;
46+
import static org.junit.jupiter.api.Assertions.assertThrows;
4247
import org.junit.jupiter.api.Test;
4348

4449
/**
@@ -54,7 +59,7 @@ void testPDFBox5649() throws IOException, XmpParsingException
5459
{
5560
DomXmpParser dxp = new DomXmpParser();
5661
XMPMetadata xmp = dxp.parse(fis);
57-
Assertions.assertNotNull(xmp);
62+
assertNotNull(xmp);
5863
}
5964
}
6065

@@ -65,8 +70,8 @@ void testPDFBox5835() throws IOException, XmpParsingException
6570
{
6671
DomXmpParser dxp = new DomXmpParser();
6772
XMPMetadata xmp = dxp.parse(fis);
68-
Assertions.assertEquals("A", xmp.getPDFAIdentificationSchema().getConformance());
69-
Assertions.assertEquals((Integer) 3, xmp.getPDFAIdentificationSchema().getPart());
73+
assertEquals("A", xmp.getPDFAIdentificationSchema().getConformance());
74+
assertEquals((Integer) 3, xmp.getPDFAIdentificationSchema().getPart());
7075
}
7176
}
7277

@@ -85,8 +90,8 @@ void testPDFBox5976() throws XmpParsingException
8590
"<?xpacket end=\"r\"?>";
8691
DomXmpParser xmpParser = new DomXmpParser();
8792
XMPMetadata xmp = xmpParser.parse(s.getBytes(StandardCharsets.UTF_8));
88-
Assertions.assertEquals("B", xmp.getPDFAIdentificationSchema().getConformance());
89-
Assertions.assertEquals((Integer) 3, xmp.getPDFAIdentificationSchema().getPart());
93+
assertEquals("B", xmp.getPDFAIdentificationSchema().getConformance());
94+
assertEquals((Integer) 3, xmp.getPDFAIdentificationSchema().getPart());
9095
}
9196

9297
/**
@@ -116,10 +121,10 @@ void testPDFBox6106() throws XmpParsingException
116121
" xap:MetadataDate='2004-01-30T17:21:50Z'/>\n" +
117122
"</rdf:RDF><?xpacket end='r'?>";
118123
DomXmpParser xmpParser = new DomXmpParser();
119-
XmpParsingException ex = Assertions.assertThrows(
124+
XmpParsingException ex = assertThrows(
120125
XmpParsingException.class,
121126
() -> xmpParser.parse(s.getBytes(StandardCharsets.UTF_8)));
122-
Assertions.assertEquals("No type defined for {http://ns.adobe.com/pdf/1.3/}CreationDate", ex.getMessage());
127+
assertEquals("No type defined for {http://ns.adobe.com/pdf/1.3/}CreationDate", ex.getMessage());
123128
}
124129

125130
/**
@@ -154,11 +159,11 @@ void testPDFBox5288() throws XmpParsingException
154159
DomXmpParser xmpParser = new DomXmpParser();
155160
XMPMetadata xmp = xmpParser.parse(s.getBytes(StandardCharsets.UTF_8));
156161
XMPMediaManagementSchema xmpMediaManagementSchema = xmp.getXMPMediaManagementSchema();
157-
Assertions.assertEquals("uidd:1f0e03977b90b6365a376454ffdf34a7", xmpMediaManagementSchema.getDocumentID());
162+
assertEquals("uidd:1f0e03977b90b6365a376454ffdf34a7", xmpMediaManagementSchema.getDocumentID());
158163
ArrayProperty historyProperty = xmpMediaManagementSchema.getHistoryProperty();
159164
ResourceEventType firstHistoryEntry = (ResourceEventType) historyProperty.getAllProperties().iterator().next();
160-
Assertions.assertEquals("created", firstHistoryEntry.getAction());
161-
Assertions.assertEquals("iDRS PDF output engine 7", firstHistoryEntry.getParameters());
165+
assertEquals("created", firstHistoryEntry.getAction());
166+
assertEquals("iDRS PDF output engine 7", firstHistoryEntry.getParameters());
162167
}
163168

164169
/**
@@ -200,14 +205,14 @@ void testPageTextSchema() throws XmpParsingException
200205
XMPMetadata xmp = xmpParser.parse(s.getBytes(StandardCharsets.UTF_8));
201206
XMPPageTextSchema pageTextSchema = xmp.getPageTextSchema();
202207
DimensionsType dim = (DimensionsType) pageTextSchema.getProperty(XMPPageTextSchema.MAX_PAGE_SIZE);
203-
Assertions.assertEquals("DimensionsType{4.0 x 3.0 inch}", dim.toString());
204-
Assertions.assertEquals("[NPages=IntegerType:7]", pageTextSchema.getProperty(XMPPageTextSchema.N_PAGES).toString());
208+
assertEquals("DimensionsType{4.0 x 3.0 inch}", dim.toString());
209+
assertEquals("[NPages=IntegerType:7]", pageTextSchema.getProperty(XMPPageTextSchema.N_PAGES).toString());
205210
XMPMediaManagementSchema xmpMediaManagementSchema = xmp.getXMPMediaManagementSchema();
206211
ResourceRefType derivedFromProperty = xmpMediaManagementSchema.getDerivedFromProperty();
207-
Assertions.assertEquals("uuid:b429d411-e628-45ca-b932-d2c77fbe6cd3", xmpMediaManagementSchema.getInstanceID());
208-
Assertions.assertEquals("proof:pdf", xmpMediaManagementSchema.getRenditionClass());
209-
Assertions.assertEquals("adobe:docid:indd:db084a4d-dbb2-11dc-ac34-beb3cc4028ec", xmpMediaManagementSchema.getDocumentID());
210-
Assertions.assertEquals("adobe:docid:indd:fa7c6589-9f4a-11dc-9641-af983df728d7", derivedFromProperty.getDocumentID());
212+
assertEquals("uuid:b429d411-e628-45ca-b932-d2c77fbe6cd3", xmpMediaManagementSchema.getInstanceID());
213+
assertEquals("proof:pdf", xmpMediaManagementSchema.getRenditionClass());
214+
assertEquals("adobe:docid:indd:db084a4d-dbb2-11dc-ac34-beb3cc4028ec", xmpMediaManagementSchema.getDocumentID());
215+
assertEquals("adobe:docid:indd:fa7c6589-9f4a-11dc-9641-af983df728d7", derivedFromProperty.getDocumentID());
211216
}
212217

213218
/**
@@ -238,8 +243,8 @@ void testPageTextSchema2() throws XmpParsingException
238243
XMPMetadata xmp = xmpParser.parse(s.getBytes(StandardCharsets.UTF_8));
239244
XMPPageTextSchema pageTextSchema = xmp.getPageTextSchema();
240245
DimensionsType dim = (DimensionsType) pageTextSchema.getProperty(XMPPageTextSchema.MAX_PAGE_SIZE);
241-
Assertions.assertEquals("DimensionsType{4.0 x 3.0 inch}", dim.toString());
242-
Assertions.assertEquals("[NPages=IntegerType:7]", pageTextSchema.getProperty(XMPPageTextSchema.N_PAGES).toString());
246+
assertEquals("DimensionsType{4.0 x 3.0 inch}", dim.toString());
247+
assertEquals("[NPages=IntegerType:7]", pageTextSchema.getProperty(XMPPageTextSchema.N_PAGES).toString());
243248
}
244249

245250
/**
@@ -268,8 +273,8 @@ void testPageTextSchema3() throws XmpParsingException
268273
XMPMetadata xmp = xmpParser.parse(s.getBytes(StandardCharsets.UTF_8));
269274
XMPPageTextSchema pageTextSchema = xmp.getPageTextSchema();
270275
DimensionsType dim = (DimensionsType) pageTextSchema.getProperty(XMPPageTextSchema.MAX_PAGE_SIZE);
271-
Assertions.assertEquals("DimensionsType{4.0 x 3.0 inch}", dim.toString());
272-
Assertions.assertEquals("[NPages=IntegerType:7]", pageTextSchema.getProperty(XMPPageTextSchema.N_PAGES).toString());
276+
assertEquals("DimensionsType{4.0 x 3.0 inch}", dim.toString());
277+
assertEquals("[NPages=IntegerType:7]", pageTextSchema.getProperty(XMPPageTextSchema.N_PAGES).toString());
273278
}
274279

275280
/**
@@ -287,17 +292,118 @@ void testPDFBox3882() throws IOException, XmpParsingException
287292
DomXmpParser dxp = new DomXmpParser();
288293
XMPMetadata xmp = dxp.parse(is);
289294
List<AbstractField> allProperties = xmp.getPDFExtensionSchema().getSchemasProperty().getAllProperties();
290-
Assertions.assertEquals(1, allProperties.size());
295+
assertEquals(1, allProperties.size());
291296
PDFASchemaType pdfExtensionSchema = (PDFASchemaType) allProperties.get(0);
292-
Assertions.assertEquals("http://www.sagemcom.com/documents/xmlns/dematbox", pdfExtensionSchema.getNamespaceURI());
293-
Assertions.assertEquals("dematbox", pdfExtensionSchema.getPrefixValue());
297+
assertEquals("http://www.sagemcom.com/documents/xmlns/dematbox", pdfExtensionSchema.getNamespaceURI());
298+
assertEquals("dematbox", pdfExtensionSchema.getPrefixValue());
294299
XMPSchema extensionSchema = xmp.getSchema(pdfExtensionSchema.getNamespaceURI());
295-
Assertions.assertEquals(pdfExtensionSchema.getNamespaceURI(), extensionSchema.getNamespace());
296-
Assertions.assertEquals(pdfExtensionSchema.getPrefixValue(), extensionSchema.getPrefix());
300+
assertEquals(pdfExtensionSchema.getNamespaceURI(), extensionSchema.getNamespace());
301+
assertEquals(pdfExtensionSchema.getPrefixValue(), extensionSchema.getPrefix());
297302
ArrayProperty pageInfoProp = (ArrayProperty) extensionSchema.getProperty("PageInfo");
298303
DefinedStructuredType dst = (DefinedStructuredType) pageInfoProp.getAllProperties().get(0);
299-
Assertions.assertEquals("[number=IntegerType:1]", dst.getProperty("number").toString());
300-
Assertions.assertEquals("[origNumber=IntegerType:1]", dst.getProperty("origNumber").toString());
304+
assertEquals("[number=IntegerType:1]", dst.getProperty("number").toString());
305+
assertEquals("[origNumber=IntegerType:1]", dst.getProperty("origNumber").toString());
301306
}
302307
}
308+
309+
/**
310+
* PDFBOX-3882: Test ResourceEventType properties as attributes instead of properties (call of
311+
* tryParseAttributesAsProperties() the the end of parseLiElement())
312+
*
313+
* @throws XmpParsingException
314+
*/
315+
@Test
316+
void testPDFBox3882_2() throws XmpParsingException
317+
{
318+
319+
// data modified from XMP data in the JPEG file in Apache Tika JpegParserTest.testJPEGXMPMM()
320+
String s = "<?xpacket begin=\"\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n" +
321+
"<x:xmpmeta xmlns:x=\"adobe:ns:meta/\"\n" +
322+
" x:xmptk=\"Adobe XMP Core 5.0-c060 61.134777, 2010/02/12-17:32:00 \">\n" +
323+
" <rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" +
324+
" <rdf:Description rdf:about=\"\"\n" +
325+
" xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\"\n" +
326+
" xmlns:dc=\"http://purl.org/dc/elements/1.1/\"\n" +
327+
" xmlns:xmpMM=\"http://ns.adobe.com/xap/1.0/mm/\"\n" +
328+
" xmlns:stEvt=\"http://ns.adobe.com/xap/1.0/sType/ResourceEvent#\"\n" +
329+
" xmlns:stRef=\"http://ns.adobe.com/xap/1.0/sType/ResourceRef#\"\n" +
330+
" xmlns:photoshop=\"http://ns.adobe.com/photoshop/1.0/\"\n" +
331+
" xmp:CreatorTool=\"Adobe Photoshop CS5 Macintosh\"\n" +
332+
" xmp:CreateDate=\"2012-04-30T12:52:07-04:00\"\n" +
333+
" xmp:MetadataDate=\"2012-05-03T13:36:11-04:00\"\n" +
334+
" xmp:ModifyDate=\"2012-05-03T13:36:11-04:00\"\n" +
335+
" dc:format=\"image/jpeg\"\n" +
336+
" xmpMM:InstanceID=\"xmp.iid:49E997338D4911E1AB62EBF9B374B234\"\n" +
337+
" xmpMM:DocumentID=\"xmp.did:49E997348D4911E1AB62EBF9B374B234\"\n" +
338+
" xmpMM:OriginalDocumentID=\"xmp.did:01801174072068118A6D9A879C818256\"\n" +
339+
" photoshop:History=\"2012-05-03T09:34:50-04:00&#x9;File i1222b.jpg opened&#xA;\">\n" +
340+
" <xmpMM:History>\n" +
341+
" <rdf:Seq>\n" +
342+
" <rdf:li stEvt:action=\"created\"\n" +
343+
" stEvt:instanceID=\"xmp.iid:01801174072068118A6D9A879C818256\"\n" +
344+
" stEvt:when=\"2012-04-30T12:52:07-04:00\"\n" +
345+
" stEvt:softwareAgent=\"Adobe Photoshop CS5 Macintosh\"/>\n" +
346+
" <rdf:li stEvt:action=\"saved\"\n" +
347+
" stEvt:instanceID=\"xmp.iid:02801174072068118A6D9A879C818256\"\n" +
348+
" stEvt:when=\"2012-04-30T12:54:04-04:00\"\n" +
349+
" stEvt:softwareAgent=\"Adobe Photoshop CS5 Macintosh\"\n" +
350+
" stEvt:changed=\"/\"/>\n" +
351+
" <rdf:li stEvt:action=\"saved\"\n" +
352+
" stEvt:instanceID=\"xmp.iid:03801174072068118A6D9A879C818256\"\n" +
353+
" stEvt:when=\"2012-04-30T12:54:48-04:00\"\n" +
354+
" stEvt:softwareAgent=\"Adobe Photoshop CS5 Macintosh\"\n" +
355+
" stEvt:changed=\"/\"/>\n" +
356+
" </rdf:Seq>\n" +
357+
" </xmpMM:History>\n" +
358+
" <xmpMM:DerivedFrom stRef:instanceID=\"xmp.iid:21F0677BA22168118A6D9A879C818256\"\n" +
359+
" stRef:documentID=\"xmp.did:01801174072068118A6D9A879C818256\"\n" +
360+
" stRef:originalDocumentID=\"xmp.did:01801174072068118A6D9A879C818256\"/>\n" +
361+
" <photoshop:DocumentAncestors>\n" +
362+
" <rdf:Bag>\n" +
363+
" <rdf:li>adobe:docid:photoshop:11d3ec5a-c131-11d8-9274-ec65c7d7e0c6</rdf:li>\n" +
364+
" <rdf:li>adobe:docid:photoshop:aadc7027-309c-11d8-9596-9cf45d2f630b</rdf:li>\n" +
365+
" <rdf:li>adobe:docid:photoshop:c7961c59-6e0f-11d8-87b7-d67539df12d8</rdf:li>\n" +
366+
" </rdf:Bag>\n" +
367+
" </photoshop:DocumentAncestors>\n" +
368+
" </rdf:Description>\n" +
369+
" </rdf:RDF>\n" +
370+
"</x:xmpmeta>\n" +
371+
"<?xpacket end=\"w\"?>";
372+
DomXmpParser xmpParser = new DomXmpParser();
373+
XMPMetadata xmp = xmpParser.parse(s.getBytes(StandardCharsets.UTF_8));
374+
XMPMediaManagementSchema xmpMediaManagementSchema = xmp.getXMPMediaManagementSchema();
375+
ArrayProperty historyProperty = xmpMediaManagementSchema.getHistoryProperty();
376+
List<AbstractField> historyProperties = historyProperty.getAllProperties();
377+
assertEquals(3, historyProperties.size());
378+
ResourceEventType ret0 = (ResourceEventType) historyProperties.get(0);
379+
ResourceEventType ret1 = (ResourceEventType) historyProperties.get(1);
380+
ResourceEventType ret2 = (ResourceEventType) historyProperties.get(2);
381+
assertEquals("created", ret0.getAction());
382+
assertEquals("xmp.iid:01801174072068118A6D9A879C818256", ret0.getInstanceID());
383+
assertEquals(2012, ret0.getWhen().get(Calendar.YEAR));
384+
assertEquals(52, ret0.getWhen().get(Calendar.MINUTE));
385+
assertEquals("Adobe Photoshop CS5 Macintosh", ret0.getSoftwareAgent());
386+
assertEquals("xmp.iid:02801174072068118A6D9A879C818256", ret1.getInstanceID());
387+
assertEquals("xmp.iid:03801174072068118A6D9A879C818256", ret2.getInstanceID());
388+
assertEquals(2012, ret1.getWhen().get(Calendar.YEAR));
389+
assertEquals(54, ret1.getWhen().get(Calendar.MINUTE));
390+
assertEquals(4, ret1.getWhen().get(Calendar.SECOND));
391+
assertEquals(2012, ret2.getWhen().get(Calendar.YEAR));
392+
assertEquals(2012, ret2.getWhen().get(Calendar.YEAR));
393+
assertEquals(54, ret2.getWhen().get(Calendar.MINUTE));
394+
assertEquals(48, ret2.getWhen().get(Calendar.SECOND));
395+
assertEquals("xmp.iid:49E997338D4911E1AB62EBF9B374B234", xmpMediaManagementSchema.getInstanceID());
396+
assertEquals("xmp.did:49E997348D4911E1AB62EBF9B374B234", xmpMediaManagementSchema.getDocumentID());
397+
assertEquals("xmp.did:01801174072068118A6D9A879C818256", xmpMediaManagementSchema.getOriginalDocumentID());
398+
System.out.println("MediaManagementSchema all: " + xmpMediaManagementSchema.getAllProperties());
399+
PhotoshopSchema photoshopSchema = xmp.getPhotoshopSchema();
400+
assertEquals("2012-05-03T09:34:50-04:00\tFile i1222b.jpg opened\n", photoshopSchema.getHistory());
401+
ArrayProperty ancestorsProperty = (ArrayProperty) photoshopSchema.getProperty("DocumentAncestors");
402+
List<AbstractField> ancestors = ancestorsProperty.getAllProperties();
403+
assertEquals(3, ancestors.size());
404+
assertEquals("adobe:docid:photoshop:11d3ec5a-c131-11d8-9274-ec65c7d7e0c6", ((TextType) ancestors.get(0)).getStringValue());
405+
assertEquals("adobe:docid:photoshop:aadc7027-309c-11d8-9596-9cf45d2f630b", ((TextType) ancestors.get(1)).getStringValue());
406+
assertEquals("adobe:docid:photoshop:c7961c59-6e0f-11d8-87b7-d67539df12d8", ((TextType) ancestors.get(2)).getStringValue());
407+
ResourceRefType derivedFromProperty = xmpMediaManagementSchema.getDerivedFromProperty(); //TODO doesn't work yet?!
408+
}
303409
}

0 commit comments

Comments
 (0)