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

« back to all changes in this revision

Viewing changes to test/java/org/apache/xmlgraphics/xmp/XMPPropertyTestCase.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.io.StringWriter;
 
23
import java.util.Arrays;
 
24
import java.util.Calendar;
 
25
import java.util.Date;
 
26
import java.util.Locale;
 
27
import java.util.Set;
 
28
import java.util.TimeZone;
 
29
 
 
30
import javax.xml.transform.stream.StreamResult;
 
31
import javax.xml.transform.stream.StreamSource;
 
32
 
 
33
import org.junit.Test;
 
34
 
 
35
import static org.junit.Assert.assertEquals;
 
36
import static org.junit.Assert.assertFalse;
 
37
import static org.junit.Assert.assertNotNull;
 
38
import static org.junit.Assert.assertNull;
 
39
import static org.junit.Assert.assertTrue;
 
40
 
 
41
import org.apache.xmlgraphics.util.QName;
 
42
import org.apache.xmlgraphics.xmp.schemas.DublinCoreAdapter;
 
43
import org.apache.xmlgraphics.xmp.schemas.DublinCoreSchema;
 
44
import org.apache.xmlgraphics.xmp.schemas.XMPBasicAdapter;
 
45
import org.apache.xmlgraphics.xmp.schemas.XMPBasicSchema;
 
46
 
 
47
/**
 
48
 * Tests property access methods.
 
49
 */
 
50
public class XMPPropertyTestCase {
 
51
 
 
52
    @Test
 
53
    public void testPropertyAccess() throws Exception {
 
54
        Metadata xmp = new Metadata();
 
55
        DublinCoreAdapter dc = DublinCoreSchema.getAdapter(xmp);
 
56
        assertNull(dc.getContributors());
 
57
 
 
58
        dc.addContributor("Contributor1");
 
59
        assertEquals(1, dc.getContributors().length);
 
60
        assertEquals("Contributor1", dc.getContributors()[0]);
 
61
        dc.removeContributor("Contributor1");
 
62
        assertNull(dc.getContributors());
 
63
 
 
64
        dc.addContributor("Contributor1");
 
65
        assertEquals(1, dc.getContributors().length);
 
66
        dc.addContributor("Contributor2");
 
67
        assertEquals(2, dc.getContributors().length);
 
68
        assertFalse(dc.removeContributor("DoesNotExist"));
 
69
        assertTrue(dc.removeContributor("Contributor1"));
 
70
        assertEquals(1, dc.getContributors().length);
 
71
        assertTrue(dc.removeContributor("Contributor2"));
 
72
        assertFalse(dc.removeContributor("Contributor2"));
 
73
        assertNull(dc.getContributors());
 
74
    }
 
75
 
 
76
    @Test
 
77
    public void testPropertyRemovalLangAlt() throws Exception {
 
78
        Metadata xmp = new Metadata();
 
79
        DublinCoreAdapter dc = DublinCoreSchema.getAdapter(xmp);
 
80
 
 
81
        //dc:title is a "Lang Alt"
 
82
        dc.setTitle("en", "The title");
 
83
        String title = dc.removeTitle("en");
 
84
        assertEquals("The title", title);
 
85
        dc.setTitle("en", "The title");
 
86
        dc.setTitle("de", "Der Titel");
 
87
        title = dc.removeTitle("en");
 
88
        assertEquals("The title", title);
 
89
        title = dc.removeTitle("en");
 
90
        assertNull(title);
 
91
 
 
92
        title = dc.removeTitle("de");
 
93
        assertEquals("Der Titel", title);
 
94
        title = dc.removeTitle("de");
 
95
        assertNull(title);
 
96
    }
 
97
 
 
98
    @Test
 
99
    public void testReplaceLangAlt() throws Exception {
 
100
        Metadata xmp = new Metadata();
 
101
        DublinCoreAdapter dc = DublinCoreSchema.getAdapter(xmp);
 
102
        dc.setTitle("Default title");
 
103
        StringWriter writer = new StringWriter();
 
104
        XMPSerializer.writeXML(xmp, new StreamResult(writer));
 
105
        String xmpString = writer.toString();
 
106
        xmp = XMPParser.parseXMP(new StreamSource(new java.io.StringReader(xmpString)));
 
107
        dc = DublinCoreSchema.getAdapter(xmp);
 
108
        assertEquals("Default title", dc.getTitle());
 
109
        dc.setTitle("Updated title");
 
110
        XMPProperty prop = xmp.getProperty(new QName(DublinCoreSchema.NAMESPACE, "title"));
 
111
        XMPArray array = prop.getArrayValue();
 
112
        assertNotNull(array);
 
113
        //Check that only one title is present. There used to be a bug that didn't set the
 
114
        //non-qualified value equal to the value qualified with "x-default".
 
115
        assertEquals(1, array.getSize());
 
116
        assertEquals("Updated title", array.getValue(0));
 
117
    }
 
118
 
 
119
    @Test
 
120
    public void testPropertyValues() throws Exception {
 
121
        Metadata xmp = new Metadata();
 
122
        DublinCoreAdapter dc = DublinCoreSchema.getAdapter(xmp);
 
123
 
 
124
        String format = dc.getFormat();
 
125
        assertNull(format);
 
126
 
 
127
        dc.setFormat("application/pdf");
 
128
        format = dc.getFormat();
 
129
        assertEquals("application/pdf", format);
 
130
 
 
131
        dc.setFormat("image/jpeg");
 
132
        format = dc.getFormat();
 
133
        assertEquals("image/jpeg", format);
 
134
 
 
135
        dc.setFormat(null);
 
136
        format = dc.getFormat();
 
137
        assertNull(format);
 
138
 
 
139
        dc.setFormat(""); //Empty string same as null value
 
140
        format = dc.getFormat();
 
141
        assertNull(format);
 
142
 
 
143
        dc.setTitle("title");
 
144
        String title = dc.getTitle();
 
145
        assertEquals("title", title);
 
146
 
 
147
        dc.setTitle("Titel");
 
148
        title = dc.getTitle();
 
149
        assertEquals("Titel", title);
 
150
 
 
151
        dc.setTitle(null);
 
152
        title = dc.getTitle();
 
153
        assertNull(title);
 
154
 
 
155
        dc.setTitle("");
 
156
        title = dc.getTitle();
 
157
        assertNull(title);
 
158
    }
 
159
 
 
160
    @Test
 
161
    public void testDates() throws Exception {
 
162
        Metadata xmp = new Metadata();
 
163
        XMPBasicAdapter basic = XMPBasicSchema.getAdapter(xmp);
 
164
 
 
165
        Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.ENGLISH);
 
166
        cal.set(2008, Calendar.FEBRUARY, 07, 15, 11, 07);
 
167
        cal.set(Calendar.MILLISECOND, 0);
 
168
        Date dt = cal.getTime();
 
169
 
 
170
        assertNull(basic.getCreateDate());
 
171
        basic.setCreateDate(dt);
 
172
        Date dt2 = basic.getCreateDate();
 
173
        assertEquals(dt2, dt);
 
174
    }
 
175
 
 
176
    @Test
 
177
    public void testQualifiers() throws Exception {
 
178
        Metadata xmp = new Metadata();
 
179
        XMPBasicAdapter basic = XMPBasicSchema.getAdapter(xmp);
 
180
 
 
181
        basic.addIdentifier("x123");
 
182
        basic.setIdentifier("id1", "system1");
 
183
        basic.setIdentifier("12345", "system2");
 
184
 
 
185
        String[] ids = basic.getIdentifiers();
 
186
        assertEquals(3, ids.length);
 
187
        Set<String> set = new java.util.HashSet<String>(Arrays.asList(ids));
 
188
        assertTrue(set.contains("x123"));
 
189
        assertTrue(set.contains("id1"));
 
190
        assertTrue(set.contains("12345"));
 
191
 
 
192
        assertEquals("id1", basic.getIdentifier("system1"));
 
193
        basic.setIdentifier("id2", "system1");
 
194
        assertEquals("id2", basic.getIdentifier("system1"));
 
195
        assertEquals(3, basic.getIdentifiers().length);
 
196
    }
 
197
 
 
198
}