~hjd/ubuntu/wily/xmlgraphics-commons/debian-merged

« back to all changes in this revision

Viewing changes to test/java/org/apache/xmlgraphics/xmp/XMPParserTest.java

  • Committer: Hans Joachim Desserud
  • Date: 2015-11-11 18:22:53 UTC
  • mfrom: (9.1.5 sid)
  • Revision ID: hans_joachim_desserud-20151111182253-zwi0frfm97j0wddn
  * Merge from Debian unstable.  Remaining changes:
    - d/control: Drop dependencies required for unit testing as they
      include libmockito-java which would pull maven into main, disable unit
      test execution.

Show diffs side-by-side

added added

removed removed

Lines of Context:
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
 
 *
9
 
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 
 *
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.
16
 
 */
17
 
 
18
 
/* $Id: XMPParserTest.java 889803 2009-12-11 20:36:36Z jeremias $ */
19
 
 
20
 
package org.apache.xmlgraphics.xmp;
21
 
 
22
 
import java.net.URL;
23
 
import java.util.Calendar;
24
 
import java.util.Date;
25
 
import java.util.TimeZone;
26
 
 
27
 
import junit.framework.TestCase;
28
 
 
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;
35
 
 
36
 
/**
37
 
 * Tests for the XMP parser.
38
 
 */
39
 
public class XMPParserTest extends TestCase {
40
 
 
41
 
    public void testParseBasics() throws Exception {
42
 
        URL url = getClass().getResource("test-basics.xmp");
43
 
        Metadata meta = XMPParser.parseXMP(url);
44
 
 
45
 
        DublinCoreAdapter dcAdapter = DublinCoreSchema.getAdapter(meta);
46
 
        XMPBasicAdapter basicAdapter = XMPBasicSchema.getAdapter(meta);
47
 
        AdobePDFAdapter pdfAdapter = AdobePDFSchema.getAdapter(meta);
48
 
 
49
 
        XMPProperty prop;
50
 
        prop = meta.getProperty(XMPConstants.DUBLIN_CORE_NAMESPACE, "creator");
51
 
        XMPArray array;
52
 
        array = prop.getArrayValue();
53
 
        assertEquals(1, array.getSize());
54
 
        assertEquals("John Doe", array.getValue(0).toString());
55
 
        assertEquals("John Doe", dcAdapter.getCreators()[0]);
56
 
 
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());
71
 
    }
72
 
 
73
 
    public void testParse1() throws Exception {
74
 
        URL url = getClass().getResource("unknown-schema.xmp");
75
 
        Metadata meta = XMPParser.parseXMP(url);
76
 
 
77
 
        DublinCoreAdapter dcAdapter = DublinCoreSchema.getAdapter(meta);
78
 
 
79
 
        XMPProperty prop;
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());
84
 
 
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());
88
 
    }
89
 
 
90
 
    public void testParseStructures() throws Exception {
91
 
        URL url = getClass().getResource("test-structures.xmp");
92
 
        Metadata meta = XMPParser.parseXMP(url);
93
 
 
94
 
        XMPProperty prop;
95
 
 
96
 
        String testns = "http://foo.bar/test/";
97
 
        prop = meta.getProperty(testns, "something");
98
 
        assertEquals("blablah", prop.getValue().toString());
99
 
 
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());
109
 
 
110
 
        prop = meta.getProperty(testns, "villain");
111
 
        XMPProperty prop1;
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());
116
 
 
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());
123
 
 
124
 
    }
125
 
 
126
 
    public void testAttributeValues() throws Exception {
127
 
        URL url = getClass().getResource("test-attribute-values.xmp");
128
 
        Metadata meta = XMPParser.parseXMP(url);
129
 
 
130
 
        DublinCoreAdapter dcAdapter = DublinCoreSchema.getAdapter(meta);
131
 
        assertEquals("Ender's Game", dcAdapter.getTitle());
132
 
        assertEquals("Orson Scott Card", dcAdapter.getCreators()[0]);
133
 
    }
134
 
 
135
 
    public void testParseDates() throws Exception {
136
 
        URL url = getClass().getResource("test-dates.xmp");
137
 
        Metadata meta = XMPParser.parseXMP(url);
138
 
        XMPProperty prop;
139
 
 
140
 
        DublinCoreAdapter dcAdapter = DublinCoreSchema.getAdapter(meta);
141
 
 
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);
149
 
 
150
 
        //The second is the most recent and should match the simple value
151
 
        assertEquals(dates[1], dcAdapter.getDate());
152
 
 
153
 
        prop = meta.getProperty(XMPConstants.DUBLIN_CORE_NAMESPACE, "date");
154
 
        assertNotNull(prop.getArrayValue());
155
 
        assertEquals(2, prop.getArrayValue().getSize());
156
 
 
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());
165
 
    }
166
 
 
167
 
    public void testParseEmptyValues() throws Exception {
168
 
        URL url = getClass().getResource("empty-values.xmp");
169
 
        Metadata meta = XMPParser.parseXMP(url);
170
 
 
171
 
        DublinCoreAdapter dc = DublinCoreSchema.getAdapter(meta);
172
 
        String title = dc.getTitle();
173
 
        assertEquals("empty", title);
174
 
 
175
 
        title = dc.getTitle("fr"); //Does not exist
176
 
        assertNull(title);
177
 
 
178
 
        title = dc.getTitle("de");
179
 
        assertNull(title); //Empty value treated same as not existant
180
 
    }
181
 
 
182
 
}