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.
18
/* $Id: XMPParserTest.java 889803 2009-12-11 20:36:36Z jeremias $ */
20
package org.apache.xmlgraphics.xmp;
23
import java.util.Calendar;
24
import java.util.Date;
25
import java.util.TimeZone;
27
import junit.framework.TestCase;
29
import org.apache.xmlgraphics.xmp.schemas.DublinCoreAdapter;
30
import org.apache.xmlgraphics.xmp.schemas.DublinCoreSchema;
31
import org.apache.xmlgraphics.xmp.schemas.XMPBasicAdapter;
32
import org.apache.xmlgraphics.xmp.schemas.XMPBasicSchema;
33
import org.apache.xmlgraphics.xmp.schemas.pdf.AdobePDFAdapter;
34
import org.apache.xmlgraphics.xmp.schemas.pdf.AdobePDFSchema;
37
* Tests for the XMP parser.
39
public class XMPParserTest extends TestCase {
41
public void testParseBasics() throws Exception {
42
URL url = getClass().getResource("test-basics.xmp");
43
Metadata meta = XMPParser.parseXMP(url);
45
DublinCoreAdapter dcAdapter = DublinCoreSchema.getAdapter(meta);
46
XMPBasicAdapter basicAdapter = XMPBasicSchema.getAdapter(meta);
47
AdobePDFAdapter pdfAdapter = AdobePDFSchema.getAdapter(meta);
50
prop = meta.getProperty(XMPConstants.DUBLIN_CORE_NAMESPACE, "creator");
52
array = prop.getArrayValue();
53
assertEquals(1, array.getSize());
54
assertEquals("John Doe", array.getValue(0).toString());
55
assertEquals("John Doe", dcAdapter.getCreators()[0]);
57
prop = meta.getProperty(XMPConstants.DUBLIN_CORE_NAMESPACE, "title");
58
assertEquals("Example document", prop.getValue().toString());
59
assertEquals("Example document", dcAdapter.getTitle());
60
prop = meta.getProperty(XMPConstants.XMP_BASIC_NAMESPACE, "CreateDate");
61
//System.out.println("Creation Date: " + prop.getValue() + " " + prop.getClass().getName());
62
prop = meta.getProperty(XMPConstants.XMP_BASIC_NAMESPACE, "CreatorTool");
63
assertEquals("An XML editor", prop.getValue().toString());
64
assertEquals("An XML editor", basicAdapter.getCreatorTool());
65
prop = meta.getProperty(XMPConstants.ADOBE_PDF_NAMESPACE, "Producer");
66
assertEquals("Apache FOP Version SVN trunk", prop.getValue().toString());
67
assertEquals("Apache FOP Version SVN trunk", pdfAdapter.getProducer());
68
prop = meta.getProperty(XMPConstants.ADOBE_PDF_NAMESPACE, "PDFVersion");
69
assertEquals("1.4", prop.getValue().toString());
70
assertEquals("1.4", pdfAdapter.getPDFVersion());
73
public void testParse1() throws Exception {
74
URL url = getClass().getResource("unknown-schema.xmp");
75
Metadata meta = XMPParser.parseXMP(url);
77
DublinCoreAdapter dcAdapter = DublinCoreSchema.getAdapter(meta);
80
//Access through the known schema as reference
81
prop = meta.getProperty(XMPConstants.DUBLIN_CORE_NAMESPACE, "title");
82
assertEquals("Unknown Schema", prop.getValue().toString());
83
assertEquals("Unknown Schema", dcAdapter.getTitle());
85
//Access through a schema unknown to the XMP framework
86
prop = meta.getProperty("http://unknown.org/something", "dummy");
87
assertEquals("Dummy!", prop.getValue().toString());
90
public void testParseStructures() throws Exception {
91
URL url = getClass().getResource("test-structures.xmp");
92
Metadata meta = XMPParser.parseXMP(url);
96
String testns = "http://foo.bar/test/";
97
prop = meta.getProperty(testns, "something");
98
assertEquals("blablah", prop.getValue().toString());
100
prop = meta.getProperty(testns, "ingredients");
101
XMPArray array = prop.getArrayValue();
102
assertEquals(3, array.getSize());
103
XMPStructure struct = array.getStructure(0);
104
assertEquals(2, struct.getPropertyCount());
105
prop = struct.getValueProperty();
106
assertEquals("Apples", prop.getValue());
107
prop = struct.getProperty(testns, "amount");
108
assertEquals("4", prop.getValue());
110
prop = meta.getProperty(testns, "villain");
112
prop1 = prop.getStructureValue().getProperty(testns, "name");
113
assertEquals("Darth Sidious", prop1.getValue());
114
prop1 = prop.getStructureValue().getProperty(testns, "other-name");
115
assertEquals("Palpatine", prop1.getValue());
117
//Test shorthand form
118
prop = meta.getProperty(testns, "project");
119
prop1 = prop.getStructureValue().getProperty(testns, "name");
120
assertEquals("Apache XML Graphics", prop1.getValue());
121
prop1 = prop.getStructureValue().getProperty(testns, "url");
122
assertEquals("http://xmlgraphics.apache.org/", prop1.getValue());
126
public void testAttributeValues() throws Exception {
127
URL url = getClass().getResource("test-attribute-values.xmp");
128
Metadata meta = XMPParser.parseXMP(url);
130
DublinCoreAdapter dcAdapter = DublinCoreSchema.getAdapter(meta);
131
assertEquals("Ender's Game", dcAdapter.getTitle());
132
assertEquals("Orson Scott Card", dcAdapter.getCreators()[0]);
135
public void testParseDates() throws Exception {
136
URL url = getClass().getResource("test-dates.xmp");
137
Metadata meta = XMPParser.parseXMP(url);
140
DublinCoreAdapter dcAdapter = DublinCoreSchema.getAdapter(meta);
142
//Simple adapter access
143
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+2:00"));
144
cal.set(2006, Calendar.JUNE, 2, 10, 36, 40);
145
cal.set(Calendar.MILLISECOND, 0);
146
assertEquals(cal.getTime(), dcAdapter.getDate());
147
Date[] dates = dcAdapter.getDates();
148
assertEquals(2, dates.length);
150
//The second is the most recent and should match the simple value
151
assertEquals(dates[1], dcAdapter.getDate());
153
prop = meta.getProperty(XMPConstants.DUBLIN_CORE_NAMESPACE, "date");
154
assertNotNull(prop.getArrayValue());
155
assertEquals(2, prop.getArrayValue().getSize());
157
//Now add a new date and check if the adapter's getDate() method returns the new date.
158
cal.set(2008, Calendar.NOVEMBER, 1, 10, 10, 0);
159
dcAdapter.addDate(cal.getTime());
160
assertEquals(3, dcAdapter.getDates().length);
161
prop = meta.getProperty(XMPConstants.DUBLIN_CORE_NAMESPACE, "date");
162
assertNotNull(prop.getArrayValue());
163
assertEquals(3, prop.getArrayValue().getSize());
164
assertEquals(cal.getTime(), dcAdapter.getDate());
167
public void testParseEmptyValues() throws Exception {
168
URL url = getClass().getResource("empty-values.xmp");
169
Metadata meta = XMPParser.parseXMP(url);
171
DublinCoreAdapter dc = DublinCoreSchema.getAdapter(meta);
172
String title = dc.getTitle();
173
assertEquals("empty", title);
175
title = dc.getTitle("fr"); //Does not exist
178
title = dc.getTitle("de");
179
assertNull(title); //Empty value treated same as not existant