merge the formfield patch from ooo-build
[ooovba.git] / sfx2 / qa / complex / DocumentMetaData.java
blob8a4c1a91bd8093f620d0957baf1def64ef545275
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DocumentMetaData.java,v $
10 * $Revision: 1.3.170.1 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package complex.framework;
33 import complexlib.ComplexTestCase;
34 import helper.StreamSimulator;
36 import com.sun.star.uno.UnoRuntime;
37 import com.sun.star.uno.XComponentContext;
38 import com.sun.star.uno.Any;
39 import com.sun.star.lang.XInitialization;
40 import com.sun.star.lang.XSingleServiceFactory;
41 import com.sun.star.lang.XMultiServiceFactory;
42 import com.sun.star.lang.Locale;
43 import com.sun.star.lang.EventObject;
44 import com.sun.star.util.Time;
45 import com.sun.star.util.DateTime;
46 import com.sun.star.util.XModifyListener;
47 import com.sun.star.util.XModifyBroadcaster;
48 import com.sun.star.beans.XPropertyContainer;
49 import com.sun.star.beans.XPropertySet;
50 import com.sun.star.beans.PropertyValue;
51 import com.sun.star.beans.NamedValue;
52 import com.sun.star.beans.PropertyAttribute;
53 import com.sun.star.beans.UnknownPropertyException;
54 import com.sun.star.beans.IllegalTypeException;
55 import com.sun.star.embed.XStorage;
56 import com.sun.star.io.XInputStream;
57 import com.sun.star.document.XDocumentProperties;
60 /**
61 * Test case for the service com.sun.star.document.DocumentProperties.
62 * Currently, this service is implemented in
63 * sfx2/source/doc/SfxDocumentMetaData.cxx.
65 * @author mst
67 public class DocumentMetaData extends ComplexTestCase
69 public String[] getTestMethodNames () {
70 return new String[] { "check", "cleanup" };
73 public void cleanup() {
74 // nothing to do
77 // for testing modifications
78 class Listener implements XModifyListener {
79 private boolean m_Called;
81 public Listener() {
82 m_Called = false;
85 public boolean reset() {
86 boolean oldCalled = m_Called;
87 m_Called = false;
88 return oldCalled;
91 public void modified(EventObject e) {
92 m_Called = true;
95 public void disposing(EventObject e) {
99 public void check() {
100 try {
101 XMultiServiceFactory xMSF = (XMultiServiceFactory) param.getMSF();
102 assure("could not create MultiServiceFactory.", xMSF != null);
103 XPropertySet xPropertySet = (XPropertySet)
104 UnoRuntime.queryInterface(XPropertySet.class, xMSF);
105 Object defaultCtx = xPropertySet.getPropertyValue("DefaultContext");
106 XComponentContext xContext = (XComponentContext)
107 UnoRuntime.queryInterface(XComponentContext.class, defaultCtx);
108 assure("could not get component context.", xContext != null);
110 String temp = util.utils.getOfficeTemp/*Dir*/(xMSF);
111 log.println("tempdir: " + temp);
113 PropertyValue[] noArgs = { };
114 PropertyValue mimetype = new PropertyValue();
115 mimetype.Name = "MediaType";
116 mimetype.Value = "application/vnd.oasis.opendocument.text";
117 PropertyValue[] mimeArgs = { mimetype };
118 // new Any("application/vnd.oasis.opendocument.text")) };
119 PropertyValue cfile = new PropertyValue();
120 cfile.Name = "URL";
121 cfile.Value = temp + "EMPTY.odt";
122 PropertyValue[] mimeEmptyArgs = { mimetype, cfile };
124 log.println("Creating service DocumentProperties...");
126 Object oDP =
127 // xMSF.createInstanceWithContext(
128 // "com.sun.star.document.DocumentProperties", xContext);
129 xMSF.createInstance("com.sun.star.document.DocumentProperties");
130 XDocumentProperties xDP = (XDocumentProperties)
131 UnoRuntime.queryInterface(XDocumentProperties.class, oDP);
133 log.println("...done");
136 log.println("Checking initialize ...");
138 XDocumentProperties xDP2 = (XDocumentProperties)
139 UnoRuntime.queryInterface(XDocumentProperties.class,
140 xMSF.createInstance(
141 "com.sun.star.document.DocumentProperties"));
142 XInitialization xInit = (XInitialization)
143 UnoRuntime.queryInterface(XInitialization.class, xDP2);
144 xInit.initialize(new Object[] { });
146 log.println("...done");
148 log.println("Checking storing default-initialized meta data ...");
150 // xDP2.storeToMedium(temp + "EMPTY.odt", mimeArgs);
151 xDP2.storeToMedium("", mimeEmptyArgs);
153 log.println("...done");
155 log.println("Checking loading default-initialized meta data ...");
157 // xDP2.loadFromMedium(temp + "EMPTY.odt", noArgs);
158 xDP2.loadFromMedium("", mimeEmptyArgs);
159 assure ("Author", "".equals(xDP2.getAuthor()));
161 log.println("...done");
163 log.println("(Not) Checking preservation of custom meta data ...");
165 xDP2.loadFromMedium(util.utils.getFullTestURL("CUSTOM.odt"),
166 noArgs);
167 assure ("Author", "".equals(xDP2.getAuthor()));
168 xDP2.storeToMedium(temp + "CUSTOM.odt", mimeArgs);
170 //FIXME: now what? comparing for binary equality seems useless
171 // we could unzip the written file and grep for the custom stuff
172 // but would that work on windows...
174 log.println("...done");
176 log.println("Checking loading from test document...");
178 String file = util.utils.getFullTestURL("TEST.odt");
179 xDP.loadFromMedium(file, noArgs);
180 /* XInputStream xStream =
181 new StreamSimulator("./testdocuments/TEST.odt", true, param);
182 Object oSF =
183 xMSF.createInstance("com.sun.star.embed.StorageFactory");
184 XSingleServiceFactory xSF = (XSingleServiceFactory)
185 UnoRuntime.queryInterface(XSingleServiceFactory.class, oSF);
186 Object oStor = xSF.createInstanceWithArguments(
187 new Object[] { xStream });
188 XStorage xStor = (XStorage) UnoRuntime.queryInterface(
189 XStorage.class, oStor);
190 xDP.loadFromStorage(xStor);*/
192 log.println("...done");
194 log.println("Checking meta-data import...");
196 assure ("Author", "Karl-Heinz Mustermann".equals(xDP.getAuthor()));
197 assure ("Generator",
198 "StarOffice/8$Solaris_x86 OpenOffice.org_project/680m232$Build-9227"
199 .equals(xDP.getGenerator()));
200 assure ("CreationDate", 2007 == xDP.getCreationDate().Year);
201 assure ("Title", "Urgent Memo".equals(xDP.getTitle()));
202 assure ("Subject", "Wichtige Mitteilung".equals(xDP.getSubject()));
203 assure ("Description",
204 "Modern internal company memorandum in full-blocked style"
205 .equals(xDP.getDescription()));
206 // assure ("Language", "".equals(xDP.getLanguage()));
207 assure ("ModifiedBy",
208 "Karl-Heinz Mustermann".equals(xDP.getModifiedBy()));
209 assure ("ModificationDate", 10 == xDP.getModificationDate().Month);
210 assure ("PrintedBy",
211 "Karl-Heinz Mustermann".equals(xDP.getPrintedBy()));
212 assure ("PrintDate", 29 == xDP.getPrintDate().Day);
213 assure ("TemplateName",
214 "Modern Memo".equals(xDP.getTemplateName()));
215 assure ("TemplateURL",
216 xDP.getTemplateURL().endsWith("memmodern.ott"));
217 assure ("TemplateDate", 17 == xDP.getTemplateDate().Hours);
218 assure ("AutoloadURL", "../TEST.odt".equals(xDP.getAutoloadURL()));
219 assure ("AutoloadSecs", 0 == xDP.getAutoloadSecs());
220 assure ("DefaultTarget", "_blank".equals(xDP.getDefaultTarget()));
221 assure ("EditingCycles", 3 == xDP.getEditingCycles());
222 assure ("EditingDuration", 320 == xDP.getEditingDuration());
224 String[] kws = xDP.getKeywords();
225 assure ("Keywords", fromArray(kws).containsAll(
226 fromArray(new Object[] { "Asien", "Memo", "Reis" })));
228 NamedValue[] ds = xDP.getDocumentStatistics();
229 /* for (int i = 0; i < ds.length; ++i) {
230 log.println("nv: " + ds[i].Name + " " + ds[i].Value);
232 NamedValue nv1 = new NamedValue("WordCount", new Integer(23));
233 NamedValue nv2 = new NamedValue("WordCount", new Integer(23));
234 log.println("eq: " + nv1.equals(nv2)); // grrr, this is false...
236 assure ("DocumentStatistics:WordCount", containsNV(ds,
237 new NamedValue("WordCount", new Integer(23))));
238 assure ("DocumentStatistics:PageCount", containsNV(ds,
239 new NamedValue("PageCount", new Integer(1))));
241 XPropertyContainer udpc = xDP.getUserDefinedProperties();
242 XPropertySet udps = (XPropertySet) UnoRuntime.queryInterface(
243 XPropertySet.class, udpc);
244 assure ("UserDefined 1", "Dies ist ein wichtiger Hinweis"
245 .equals(udps.getPropertyValue("Hinweis")));
246 assure ("UserDefined 2", ("Kann Spuren von N"
247 + new String(new byte[] { (byte) 0xc3, (byte) 0xbc }, "UTF-8")
248 + "ssen enthalten")
249 .equals(udps.getPropertyValue("Warnung")));
251 log.println("...done");
253 log.println("Checking meta-data updates...");
255 String str;
256 DateTime dt = new DateTime();
257 Locale l = new Locale();
258 int i;
260 str = "me";
261 xDP.setAuthor(str);
262 assure ("setAuthor", str.equals(xDP.getAuthor()));
263 str = "the computa";
264 xDP.setGenerator(str);
265 assure ("setGenerator", str.equals(xDP.getGenerator()));
266 dt.Year = 2038;
267 dt.Month = 1;
268 dt.Day = 1;
269 xDP.setCreationDate(dt);
270 assure ("setCreationDate", dt.Year == xDP.getCreationDate().Year);
271 str = "El t'itulo";
272 xDP.setTitle(str);
273 assure ("setTitle", str.equals(xDP.getTitle()));
274 str = "Ein verkommenes Subjekt";
275 xDP.setSubject(str);
276 assure ("setSubject", str.equals(xDP.getSubject()));
277 str = "Este descripci'on no es importante";
278 xDP.setDescription(str);
279 assure ("setDescription", str.equals(xDP.getDescription()));
280 l.Language = "en";
281 l.Country = "GB";
282 xDP.setLanguage(l);
283 Locale l2 = xDP.getLanguage();
284 assure ("setLanguage Lang", l.Language.equals(l2.Language));
285 assure ("setLanguage Cty", l.Country.equals(l2.Country));
286 str = "myself";
287 xDP.setModifiedBy(str);
288 assure ("setModifiedBy", str.equals(xDP.getModifiedBy()));
289 dt.Year = 2042;
290 xDP.setModificationDate(dt);
291 assure ("setModificationDate",
292 dt.Year == xDP.getModificationDate().Year);
293 str = "i didnt do it";
294 xDP.setPrintedBy(str);
295 assure ("setPrintedBy", str.equals(xDP.getPrintedBy()));
296 dt.Year = 2024;
297 xDP.setPrintDate(dt);
298 assure ("setPrintDate", dt.Year == xDP.getPrintDate().Year);
299 str = "blah";
300 xDP.setTemplateName(str);
301 assure ("setTemplateName", str.equals(xDP.getTemplateName()));
302 str = "gopher://some-hole-in-the-ground/";
303 xDP.setTemplateURL(str);
304 assure ("setTemplateURL", str.equals(xDP.getTemplateURL()));
305 dt.Year = 2043;
306 xDP.setTemplateDate(dt);
307 assure ("setTemplateDate", dt.Year == xDP.getTemplateDate().Year);
308 str = "http://nowhere/";
309 xDP.setAutoloadURL(str);
310 assure ("setAutoloadURL", str.equals(xDP.getAutoloadURL()));
311 i = 3661; // this might not work (due to conversion via double...)
312 xDP.setAutoloadSecs(i);
313 // log.println("set: " + i + " get: " + xDP.getAutoloadSecs());
314 assure ("setAutoloadSecs", i == xDP.getAutoloadSecs());
315 str = "_blank";
316 xDP.setDefaultTarget(str);
317 assure ("setDefaultTarget", str.equals(xDP.getDefaultTarget()));
318 i = 42;
319 xDP.setEditingCycles((short) i);
320 assure ("setEditingCycles", i == xDP.getEditingCycles());
321 i = 84;
322 xDP.setEditingDuration(i);
323 assure ("setEditingDuration", i == xDP.getEditingDuration());
324 str = "";
326 String[] kws2 = new String[] {
327 "keywordly", "keywordlike", "keywordalicious" };
328 xDP.setKeywords(kws2);
329 kws = xDP.getKeywords();
330 assure ("setKeywords", fromArray(kws).containsAll(fromArray(kws2)));
332 NamedValue[] ds2 = new NamedValue[] {
333 new NamedValue("SyllableCount", new Integer(9)),
334 new NamedValue("FrameCount", new Integer(2)),
335 new NamedValue("SentenceCount", new Integer(7)) };
336 xDP.setDocumentStatistics(ds2);
337 ds = xDP.getDocumentStatistics();
338 assure ("setDocumentStatistics:SyllableCount", containsNV(ds,
339 new NamedValue("SyllableCount", new Integer(9))));
340 assure ("setDocumentStatistics:FrameCount", containsNV(ds,
341 new NamedValue("FrameCount", new Integer(2))));
342 assure ("setDocumentStatistics:SentenceCount", containsNV(ds,
343 new NamedValue("SentenceCount", new Integer(7))));
345 log.println("...done");
347 log.println("Checking user-defined meta-data updates...");
349 // actually, this tests the PropertyBag service
350 // but maybe the DocumentProperties service will be implemented
351 // differently some day...
352 boolean b = true;
353 double d = 3.1415;
354 Time t = new Time();
355 t.Hours = 1;
356 t.Minutes = 16;
357 dt.Year = 2065;
359 udpc.addProperty("Frobnicate", PropertyAttribute.REMOVEABLE,
360 new Boolean(b));
361 udpc.addProperty("FrobDuration", PropertyAttribute.REMOVEABLE, t);
362 udpc.addProperty("FrobStartTime", PropertyAttribute.REMOVEABLE, dt);
363 udpc.addProperty("Pi", PropertyAttribute.REMOVEABLE, new Double(d));
364 udpc.addProperty("Foo", PropertyAttribute.REMOVEABLE, "bar");
365 udpc.addProperty("Removed", PropertyAttribute.REMOVEABLE, "bar");
366 // #i94175#: empty property name is valid ODF 1.1
367 udpc.addProperty("", PropertyAttribute.REMOVEABLE, "eeeeek");
368 try {
369 udpc.removeProperty("Info 1");
370 udpc.removeProperty("Removed");
371 } catch (UnknownPropertyException e) {
372 assure("removeProperty failed", false);
375 try {
376 udpc.addProperty("Forbidden", PropertyAttribute.REMOVEABLE,
377 new String[] { "foo", "bar" });
378 assure("inserting value of non-supported type did not fail",
379 false);
380 } catch (IllegalTypeException e) {
381 // ignore
384 assure ("UserDefined bool", new Boolean(b).equals(
385 udps.getPropertyValue("Frobnicate")));
386 assure ("UserDefined time", eqTime(t, (Time)
387 udps.getPropertyValue("FrobDuration")));
388 assure ("UserDefined date", eqDateTime(dt, (DateTime)
389 udps.getPropertyValue("FrobStartTime")));
390 assure ("UserDefined float", new Double(d).equals(
391 udps.getPropertyValue("Pi")));
392 assure ("UserDefined string", "bar".equals(
393 udps.getPropertyValue("Foo")));
394 assure ("UserDefined empty name", "eeeeek".equals(
395 udps.getPropertyValue("")));
397 try {
398 udps.getPropertyValue("Removed");
399 assure("UserDefined remove didn't", false);
400 } catch (UnknownPropertyException e) {
401 // ok
404 log.println("...done");
406 log.println("Checking storing meta-data to file...");
408 xDP.storeToMedium(temp + "TEST.odt", mimeArgs);
410 log.println("...done");
412 log.println("Checking loading meta-data from stored file...");
414 xDP.loadFromMedium(temp + "TEST.odt", noArgs);
416 log.println("...done");
418 log.println("Checking user-defined meta-data from stored file...");
420 udpc = xDP.getUserDefinedProperties();
421 udps = (XPropertySet) UnoRuntime.queryInterface(
422 XPropertySet.class, udpc);
424 assure ("UserDefined bool", new Boolean(b).equals(
425 udps.getPropertyValue("Frobnicate")));
426 assure ("UserDefined time", eqTime(t, (Time)
427 udps.getPropertyValue("FrobDuration")));
428 assure ("UserDefined date", eqDateTime(dt, (DateTime)
429 udps.getPropertyValue("FrobStartTime")));
430 assure ("UserDefined float", new Double(d).equals(
431 udps.getPropertyValue("Pi")));
432 assure ("UserDefined string", "bar".equals(
433 udps.getPropertyValue("Foo")));
435 try {
436 udps.getPropertyValue("Removed");
437 assure("UserDefined remove didn't", false);
438 } catch (UnknownPropertyException e) {
439 // ok
442 log.println("...done");
444 log.println("Checking notification listener interface...");
446 Listener listener = new Listener();
447 XModifyBroadcaster xMB = (XModifyBroadcaster)
448 UnoRuntime.queryInterface(XModifyBroadcaster.class, xDP);
449 xMB.addModifyListener(listener);
450 xDP.setAuthor("not me");
451 assure ("Listener Author", listener.reset());
452 udpc.addProperty("Listener", PropertyAttribute.REMOVEABLE, "foo");
453 assure ("Listener UserDefined Add", listener.reset());
454 udps.setPropertyValue("Listener", "bar");
455 assure ("Listener UserDefined Set", listener.reset());
456 udpc.removeProperty("Listener");
457 assure ("Listener UserDefined Remove", listener.reset());
458 xMB.removeModifyListener(listener);
459 udpc.addProperty("Listener2", PropertyAttribute.REMOVEABLE, "foo");
460 assure ("Removed Listener UserDefined Add", !listener.reset());
462 log.println("...done");
464 } catch (Exception e) {
465 report(e);
469 // grrr...
470 boolean eqDateTime(DateTime a, DateTime b) {
471 return a.Year == b.Year && a.Month == b.Month && a.Day == b.Day
472 && a.Hours == b.Hours && a.Minutes == b.Minutes
473 && a.Seconds == b.Seconds
474 && a.HundredthSeconds == b.HundredthSeconds;
477 boolean eqTime(Time a, Time b) {
478 return a.Hours == b.Hours && a.Minutes == b.Minutes
479 && a.Seconds == b.Seconds
480 && a.HundredthSeconds == b.HundredthSeconds;
483 java.util.Collection fromArray(Object[] os) {
484 java.util.Collection ret = new java.util.HashSet();
485 for (int i = 0; i < os.length; ++i) {
486 ret.add(os[i]);
488 return ret;
491 // bah, structs do not have proper equals(), and uno.Type is not comparable
492 public static boolean containsNV (NamedValue[] nvs, NamedValue nv) {
493 for (int i = 0; i < nvs.length; ++i) {
494 if (nvs[i].Name.equals(nv.Name) && nvs[i].Value.equals(nv.Value)) {
495 return true;
498 return false;
501 public void report(Exception e) {
502 log.println("Exception occurred:");
503 e.printStackTrace((java.io.PrintWriter) log);
504 failed();