1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: DocumentProperties.java,v $
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 ************************************************************************/
30 package complex
.docinfo
;
32 import com
.sun
.star
.beans
.PropertyValue
;
33 import com
.sun
.star
.beans
.XPropertyContainer
;
34 import com
.sun
.star
.beans
.XPropertySet
;
35 import com
.sun
.star
.document
.XDocumentInfo
;
36 import com
.sun
.star
.document
.XDocumentInfoSupplier
;
37 import com
.sun
.star
.frame
.XComponentLoader
;
38 import com
.sun
.star
.frame
.XStorable
;
39 import com
.sun
.star
.lang
.XComponent
;
40 import com
.sun
.star
.lang
.XMultiServiceFactory
;
41 import com
.sun
.star
.text
.XTextDocument
;
42 import com
.sun
.star
.uno
.UnoRuntime
;
43 import com
.sun
.star
.uno
.XInterface
;
44 import com
.sun
.star
.util
.Date
;
46 import complexlib
.ComplexTestCase
;
48 import util
.DesktopTools
;
49 import util
.WriterTools
;
52 public class DocumentProperties
extends ComplexTestCase
{
53 XMultiServiceFactory m_xMSF
= null;
54 XTextDocument xTextDoc
= null;
56 public String
[] getTestMethodNames() {
57 return new String
[] {"checkDocInfo", "cleanup"};
60 public void checkDocInfo() {
61 m_xMSF
= (XMultiServiceFactory
) param
.getMSF();
64 "check wether there is a valid MultiServiceFactory");
67 assure("## Couldn't get MultiServiceFactory make sure your Office is started",
71 log
.println("... done");
73 log
.println("Opening a Writer document");
74 xTextDoc
= WriterTools
.createTextDoc(m_xMSF
);
75 log
.println("... done");
77 XDocumentInfoSupplier xDocInfoSup
=
78 (XDocumentInfoSupplier
) UnoRuntime
.queryInterface(XDocumentInfoSupplier
.class,
80 XDocumentInfo xDocInfo
= xDocInfoSup
.getDocumentInfo();
81 XPropertyContainer xPropContainer
=
82 (XPropertyContainer
) UnoRuntime
.queryInterface(XPropertyContainer
.class,
85 log
.println("Trying to add a existing property");
88 addProperty(xPropContainer
, "Author", (short) 0, "");
89 assure("Could set an existing property", !worked
);
90 log
.println("...done");
92 log
.println("Trying to add a integer property");
94 addProperty(xPropContainer
, "intValue", com
.sun
.star
.beans
.PropertyAttribute
.READONLY
,
96 assure("Couldn't set an integer property", worked
);
97 log
.println("...done");
99 log
.println("Trying to add a double property");
101 addProperty(xPropContainer
, "doubleValue", com
.sun
.star
.beans
.PropertyAttribute
.REMOVEABLE
,
103 assure("Couldn't set an double property", worked
);
104 log
.println("...done");
106 log
.println("Trying to add a boolean property");
108 addProperty(xPropContainer
, "booleanValue", com
.sun
.star
.beans
.PropertyAttribute
.REMOVEABLE
,
110 assure("Couldn't set an boolean property", worked
);
111 log
.println("...done");
113 log
.println("Trying to add a date property");
115 addProperty(xPropContainer
, "dateValue", com
.sun
.star
.beans
.PropertyAttribute
.REMOVEABLE
,
117 assure("Couldn't set an date property", worked
);
118 log
.println("...done");
120 log
.println("trying to remove a read only Property");
122 xPropContainer
.removeProperty ("intValue");
123 assure("Could remove read only property", false);
124 } catch (Exception e
) {
125 log
.println("\tException was thrown "+e
);
126 log
.println("\t...OK");
128 log
.println("...done");
131 String tempdir
= System
.getProperty("java.io.tmpdir");
132 String fs
= System
.getProperty("file.separator");
134 if (!tempdir
.endsWith(fs
)) {
138 tempdir
= util
.utils
.getFullURL(tempdir
);
140 log
.println("Storing the document");
144 (XStorable
) UnoRuntime
.queryInterface(XStorable
.class,
146 store
.storeToURL(tempdir
+ "DocInfo.oot",
147 new PropertyValue
[] {});
148 DesktopTools
.closeDoc(xTextDoc
);
149 } catch (Exception e
) {
150 assure("Couldn't store document", false);
153 log
.println("...done");
155 log
.println("loading the document");
158 XComponentLoader xCL
=
159 (XComponentLoader
) UnoRuntime
.queryInterface(XComponentLoader
.class,
160 m_xMSF
.createInstance(
161 "com.sun.star.frame.Desktop"));
163 xCL
.loadComponentFromURL(tempdir
+ "DocInfo.oot",
164 "_blank", 0, new PropertyValue
[] {});
166 (XTextDocument
) UnoRuntime
.queryInterface(XTextDocument
.class,
168 } catch (Exception e
) {
169 assure("Couldn't load document", false);
172 log
.println("...done");
175 (XDocumentInfoSupplier
) UnoRuntime
.queryInterface(XDocumentInfoSupplier
.class,
177 xDocInfo
= xDocInfoSup
.getDocumentInfo();
179 XPropertySet xProps
=
180 (XPropertySet
) UnoRuntime
.queryInterface(XPropertySet
.class,
183 assure("Integer doesn't work",
184 checkType(xProps
, "intValue", "java.lang.Integer"));
185 assure("Double doesn't work",
186 checkType(xProps
, "doubleValue", "java.lang.Double"));
187 assure("Boolean doesn't work",
188 checkType(xProps
, "booleanValue", "java.lang.Boolean"));
189 assure("Date doesn't work",
190 checkType(xProps
, "dateValue",
191 "com.sun.star.util.DateTime"));
194 (XPropertyContainer
) UnoRuntime
.queryInterface(XPropertyContainer
.class,
197 log
.println("trying to remove a not user defined Property");
199 xPropContainer
.removeProperty ("Author");
200 assure("Could remove non user defined property", false);
201 } catch (Exception e
) {
202 log
.println("\tException was thrown "+e
);
203 log
.println("\t...OK");
205 log
.println("...done");
207 log
.println("Trying to remove a user defined property");
209 xPropContainer
.removeProperty ("dateValue");
210 log
.println("\t...OK");
211 } catch (Exception e
) {
212 log
.println("\tException was thrown "+e
);
213 log
.println("\t...FAILED");
214 assure("Could not remove user defined property", false);
216 log
.println("...done");
220 public void cleanup() {
221 DesktopTools
.closeDoc(xTextDoc
);
224 private boolean checkType(XPropertySet xProps
, String aName
,
227 log
.println("Checking " + expected
);
230 getPropertyByName(xProps
, aName
).getClass().getName();
232 if (!getting
.equals(expected
)) {
233 log
.println("\t Expected: " + expected
);
234 log
.println("\t Detting: " + getting
);
239 log
.println("...OK");
245 private Object
getPropertyByName(XPropertySet xProps
, String aName
) {
249 ret
= xProps
.getPropertyValue(aName
);
250 } catch (Exception e
) {
251 log
.println("\tCouldn't get Property " + aName
);
252 log
.println("\tMessage " + e
);
258 private boolean addProperty(XPropertyContainer xPropContainer
,
259 String aName
, short attr
, Object defaults
) {
263 xPropContainer
.addProperty(aName
, attr
, defaults
);
264 } catch (Exception e
) {
266 log
.println("\tCouldn't get Property " + aName
);
267 log
.println("\tMessage " + e
);