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

« back to all changes in this revision

Viewing changes to test/java/org/apache/xmlgraphics/xmp/XMPParserTestCase.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$ */
 
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 org.junit.Test;
 
28
 
 
29
import static org.junit.Assert.assertEquals;
 
30
import static org.junit.Assert.assertNotNull;
 
31
import static org.junit.Assert.assertNull;
 
32
 
 
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;
 
39
 
 
40
/**
 
41
 * Tests for the XMP parser.
 
42
 */
 
43
public class XMPParserTestCase {
 
44
 
 
45
    @Test
 
46
    public void testParseBasics() throws Exception {
 
47
        URL url = getClass().getResource("test-basics.xmp");
 
48
        Metadata meta = XMPParser.parseXMP(url);
 
49
 
 
50
        DublinCoreAdapter dcAdapter = DublinCoreSchema.getAdapter(meta);
 
51
        XMPBasicAdapter basicAdapter = XMPBasicSchema.getAdapter(meta);
 
52
        AdobePDFAdapter pdfAdapter = AdobePDFSchema.getAdapter(meta);
 
53
 
 
54
        XMPProperty prop;
 
55
        prop = meta.getProperty(XMPConstants.DUBLIN_CORE_NAMESPACE, "creator");
 
56
        XMPArray array;
 
57
        array = prop.getArrayValue();
 
58
        assertEquals(1, array.getSize());
 
59
        assertEquals("John Doe", array.getValue(0).toString());
 
60
        assertEquals("John Doe", dcAdapter.getCreators()[0]);
 
61
 
 
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());
 
76
    }
 
77
 
 
78
    @Test
 
79
    public void testParse1() throws Exception {
 
80
        URL url = getClass().getResource("unknown-schema.xmp");
 
81
        Metadata meta = XMPParser.parseXMP(url);
 
82
 
 
83
        DublinCoreAdapter dcAdapter = DublinCoreSchema.getAdapter(meta);
 
84
 
 
85
        XMPProperty prop;
 
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());
 
90
 
 
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());
 
94
    }
 
95
 
 
96
    @Test
 
97
    public void testParseStructures() throws Exception {
 
98
        URL url = getClass().getResource("test-structures.xmp");
 
99
        Metadata meta = XMPParser.parseXMP(url);
 
100
 
 
101
        XMPProperty prop;
 
102
 
 
103
        String testns = "http://foo.bar/test/";
 
104
        prop = meta.getProperty(testns, "something");
 
105
        assertEquals("blablah", prop.getValue().toString());
 
106
 
 
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());
 
116
 
 
117
        prop = meta.getProperty(testns, "villain");
 
118
        XMPProperty prop1;
 
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());
 
123
 
 
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());
 
130
 
 
131
    }
 
132
 
 
133
    @Test
 
134
    public void testAttributeValues() throws Exception {
 
135
        URL url = getClass().getResource("test-attribute-values.xmp");
 
136
        Metadata meta = XMPParser.parseXMP(url);
 
137
 
 
138
        DublinCoreAdapter dcAdapter = DublinCoreSchema.getAdapter(meta);
 
139
        assertEquals("Ender's Game", dcAdapter.getTitle());
 
140
        assertEquals("Orson Scott Card", dcAdapter.getCreators()[0]);
 
141
    }
 
142
 
 
143
    @Test
 
144
    public void testParseDates() throws Exception {
 
145
        URL url = getClass().getResource("test-dates.xmp");
 
146
        Metadata meta = XMPParser.parseXMP(url);
 
147
        XMPProperty prop;
 
148
 
 
149
        DublinCoreAdapter dcAdapter = DublinCoreSchema.getAdapter(meta);
 
150
 
 
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);
 
158
 
 
159
        //The second is the most recent and should match the simple value
 
160
        assertEquals(dates[1], dcAdapter.getDate());
 
161
 
 
162
        prop = meta.getProperty(XMPConstants.DUBLIN_CORE_NAMESPACE, "date");
 
163
        assertNotNull(prop.getArrayValue());
 
164
        assertEquals(2, prop.getArrayValue().getSize());
 
165
 
 
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());
 
174
    }
 
175
 
 
176
    @Test
 
177
    public void testParseEmptyValues() throws Exception {
 
178
        URL url = getClass().getResource("empty-values.xmp");
 
179
        Metadata meta = XMPParser.parseXMP(url);
 
180
 
 
181
        DublinCoreAdapter dc = DublinCoreSchema.getAdapter(meta);
 
182
        String title = dc.getTitle();
 
183
        assertEquals("empty", title);
 
184
 
 
185
        title = dc.getTitle("fr"); //Does not exist
 
186
        assertNull(title);
 
187
 
 
188
        title = dc.getTitle("de");
 
189
        assertNull(title); //Empty value treated same as not existant
 
190
    }
 
191
 
 
192
}