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
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
20
package org.apache.xmlgraphics.xmp;
23
import java.util.Calendar;
24
import java.util.Date;
25
import java.util.TimeZone;
27
import org.junit.Test;
29
import static org.junit.Assert.assertEquals;
30
import static org.junit.Assert.assertNotNull;
31
import static org.junit.Assert.assertNull;
33
import org.apache.xmlgraphics.xmp.schemas.DublinCoreAdapter;
34
import org.apache.xmlgraphics.xmp.schemas.DublinCoreSchema;
35
import org.apache.xmlgraphics.xmp.schemas.XMPBasicAdapter;
36
import org.apache.xmlgraphics.xmp.schemas.XMPBasicSchema;
37
import org.apache.xmlgraphics.xmp.schemas.pdf.AdobePDFAdapter;
38
import org.apache.xmlgraphics.xmp.schemas.pdf.AdobePDFSchema;
41
* Tests for the XMP parser.
43
public class XMPParserTestCase {
46
public void testParseBasics() throws Exception {
47
URL url = getClass().getResource("test-basics.xmp");
48
Metadata meta = XMPParser.parseXMP(url);
50
DublinCoreAdapter dcAdapter = DublinCoreSchema.getAdapter(meta);
51
XMPBasicAdapter basicAdapter = XMPBasicSchema.getAdapter(meta);
52
AdobePDFAdapter pdfAdapter = AdobePDFSchema.getAdapter(meta);
55
prop = meta.getProperty(XMPConstants.DUBLIN_CORE_NAMESPACE, "creator");
57
array = prop.getArrayValue();
58
assertEquals(1, array.getSize());
59
assertEquals("John Doe", array.getValue(0).toString());
60
assertEquals("John Doe", dcAdapter.getCreators()[0]);
62
prop = meta.getProperty(XMPConstants.DUBLIN_CORE_NAMESPACE, "title");
63
assertEquals("Example document", prop.getValue().toString());
64
assertEquals("Example document", dcAdapter.getTitle());
65
prop = meta.getProperty(XMPConstants.XMP_BASIC_NAMESPACE, "CreateDate");
66
//System.out.println("Creation Date: " + prop.getValue() + " " + prop.getClass().getName());
67
prop = meta.getProperty(XMPConstants.XMP_BASIC_NAMESPACE, "CreatorTool");
68
assertEquals("An XML editor", prop.getValue().toString());
69
assertEquals("An XML editor", basicAdapter.getCreatorTool());
70
prop = meta.getProperty(XMPConstants.ADOBE_PDF_NAMESPACE, "Producer");
71
assertEquals("Apache FOP Version SVN trunk", prop.getValue().toString());
72
assertEquals("Apache FOP Version SVN trunk", pdfAdapter.getProducer());
73
prop = meta.getProperty(XMPConstants.ADOBE_PDF_NAMESPACE, "PDFVersion");
74
assertEquals("1.4", prop.getValue().toString());
75
assertEquals("1.4", pdfAdapter.getPDFVersion());
79
public void testParse1() throws Exception {
80
URL url = getClass().getResource("unknown-schema.xmp");
81
Metadata meta = XMPParser.parseXMP(url);
83
DublinCoreAdapter dcAdapter = DublinCoreSchema.getAdapter(meta);
86
//Access through the known schema as reference
87
prop = meta.getProperty(XMPConstants.DUBLIN_CORE_NAMESPACE, "title");
88
assertEquals("Unknown Schema", prop.getValue().toString());
89
assertEquals("Unknown Schema", dcAdapter.getTitle());
91
//Access through a schema unknown to the XMP framework
92
prop = meta.getProperty("http://unknown.org/something", "dummy");
93
assertEquals("Dummy!", prop.getValue().toString());
97
public void testParseStructures() throws Exception {
98
URL url = getClass().getResource("test-structures.xmp");
99
Metadata meta = XMPParser.parseXMP(url);
103
String testns = "http://foo.bar/test/";
104
prop = meta.getProperty(testns, "something");
105
assertEquals("blablah", prop.getValue().toString());
107
prop = meta.getProperty(testns, "ingredients");
108
XMPArray array = prop.getArrayValue();
109
assertEquals(3, array.getSize());
110
XMPStructure struct = array.getStructure(0);
111
assertEquals(2, struct.getPropertyCount());
112
prop = struct.getValueProperty();
113
assertEquals("Apples", prop.getValue());
114
prop = struct.getProperty(testns, "amount");
115
assertEquals("4", prop.getValue());
117
prop = meta.getProperty(testns, "villain");
119
prop1 = prop.getStructureValue().getProperty(testns, "name");
120
assertEquals("Darth Sidious", prop1.getValue());
121
prop1 = prop.getStructureValue().getProperty(testns, "other-name");
122
assertEquals("Palpatine", prop1.getValue());
124
//Test shorthand form
125
prop = meta.getProperty(testns, "project");
126
prop1 = prop.getStructureValue().getProperty(testns, "name");
127
assertEquals("Apache XML Graphics", prop1.getValue());
128
prop1 = prop.getStructureValue().getProperty(testns, "url");
129
assertEquals("http://xmlgraphics.apache.org/", prop1.getValue());
134
public void testAttributeValues() throws Exception {
135
URL url = getClass().getResource("test-attribute-values.xmp");
136
Metadata meta = XMPParser.parseXMP(url);
138
DublinCoreAdapter dcAdapter = DublinCoreSchema.getAdapter(meta);
139
assertEquals("Ender's Game", dcAdapter.getTitle());
140
assertEquals("Orson Scott Card", dcAdapter.getCreators()[0]);
144
public void testParseDates() throws Exception {
145
URL url = getClass().getResource("test-dates.xmp");
146
Metadata meta = XMPParser.parseXMP(url);
149
DublinCoreAdapter dcAdapter = DublinCoreSchema.getAdapter(meta);
151
//Simple adapter access
152
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+2:00"));
153
cal.set(2006, Calendar.JUNE, 2, 10, 36, 40);
154
cal.set(Calendar.MILLISECOND, 0);
155
assertEquals(cal.getTime(), dcAdapter.getDate());
156
Date[] dates = dcAdapter.getDates();
157
assertEquals(2, dates.length);
159
//The second is the most recent and should match the simple value
160
assertEquals(dates[1], dcAdapter.getDate());
162
prop = meta.getProperty(XMPConstants.DUBLIN_CORE_NAMESPACE, "date");
163
assertNotNull(prop.getArrayValue());
164
assertEquals(2, prop.getArrayValue().getSize());
166
//Now add a new date and check if the adapter's getDate() method returns the new date.
167
cal.set(2008, Calendar.NOVEMBER, 1, 10, 10, 0);
168
dcAdapter.addDate(cal.getTime());
169
assertEquals(3, dcAdapter.getDates().length);
170
prop = meta.getProperty(XMPConstants.DUBLIN_CORE_NAMESPACE, "date");
171
assertNotNull(prop.getArrayValue());
172
assertEquals(3, prop.getArrayValue().getSize());
173
assertEquals(cal.getTime(), dcAdapter.getDate());
177
public void testParseEmptyValues() throws Exception {
178
URL url = getClass().getResource("empty-values.xmp");
179
Metadata meta = XMPParser.parseXMP(url);
181
DublinCoreAdapter dc = DublinCoreSchema.getAdapter(meta);
182
String title = dc.getTitle();
183
assertEquals("empty", title);
185
title = dc.getTitle("fr"); //Does not exist
188
title = dc.getTitle("de");
189
assertNull(title); //Empty value treated same as not existant